|
1 | 1 | <template>
|
2 |
| - <div :class="classObject" |
3 |
| - :aria-expanded="show" |
4 |
| - :style="styleObject" |
5 |
| - > |
6 |
| - <slot></slot> |
7 |
| - </div> |
| 2 | + <transition name="collapse"> |
| 3 | + <div :class="classObject" v-show="show"> |
| 4 | + <slot></slot> |
| 5 | + </div> |
| 6 | + </transition> |
8 | 7 | </template>
|
9 | 8 |
|
10 |
| -<script> |
11 |
| -import {TRANSITION_DURATION} from '../utils/helpers'; |
| 9 | +<style scoped> |
| 10 | + .collapse-enter-active, .collapse-leave-active { |
| 11 | + transition: all .35s ease; |
| 12 | + overflow: hidden; |
| 13 | + max-height: 100vh; |
| 14 | + } |
12 | 15 |
|
13 |
| -// export component object |
14 |
| -export default { |
| 16 | + .collapse-enter, .collapse-leave-to { |
| 17 | + max-height: 0; |
| 18 | + } |
| 19 | +</style> |
15 | 20 |
|
16 |
| - data() { |
17 |
| - return { |
18 |
| - collapsing: false, |
19 |
| - show: false, |
20 |
| - height: 0 |
21 |
| - }; |
22 |
| - }, |
| 21 | +<script> |
| 22 | + export default { |
23 | 23 |
|
24 |
| - computed: { |
25 |
| - classObject() { |
| 24 | + data() { |
26 | 25 | return {
|
27 |
| - 'navbar-collapse': this.isNav, |
28 |
| - collapsing: this.collapsing, |
29 |
| - collapse: !this.collapsing, |
30 |
| - show: this.show |
| 26 | + show: false, |
31 | 27 | };
|
32 | 28 | },
|
33 | 29 |
|
34 |
| - styleObject() { |
35 |
| - const obj = {}; |
36 |
| - if (this.collapsing && this.height) { |
37 |
| - obj.height = this.height + 'px'; |
38 |
| - } |
39 |
| - return obj; |
40 |
| - }, |
41 |
| -
|
42 |
| - scrollHeight() { |
43 |
| - return 100;// this.$el.scrollHeight; |
44 |
| - } |
45 |
| - }, |
46 |
| -
|
47 |
| - props: { |
48 |
| - isNav: { |
49 |
| - type: Boolean, |
50 |
| - default: false |
| 30 | + computed: { |
| 31 | + classObject() { |
| 32 | + return { |
| 33 | + 'navbar-collapse': this.isNav, |
| 34 | + show: this.show |
| 35 | + }; |
| 36 | + }, |
51 | 37 | },
|
52 |
| - id: { |
53 |
| - type: String, |
54 |
| - required: true |
55 |
| - } |
56 |
| - }, |
57 | 38 |
|
58 |
| - methods: { |
59 |
| - toggle() { |
60 |
| - if (this.collapsing) { |
61 |
| - return; |
| 39 | + props: { |
| 40 | + isNav: { |
| 41 | + type: Boolean, |
| 42 | + default: false |
| 43 | + }, |
| 44 | + id: { |
| 45 | + type: String, |
| 46 | + required: true |
62 | 47 | }
|
| 48 | + }, |
63 | 49 |
|
64 |
| - this.collapsing = true; |
65 |
| - this.height = (this.show ? 0 : this.scrollHeight); |
66 |
| -
|
67 |
| - this._collapseAnimation = setTimeout(() => { |
68 |
| - this.collapsing = false; |
| 50 | + methods: { |
| 51 | + toggle() { |
69 | 52 | this.show = !this.show;
|
70 |
| - }, TRANSITION_DURATION); |
71 |
| - } |
72 |
| - }, |
73 |
| -
|
74 |
| - created() { |
75 |
| - this.$root.$on('collapse::toggle', target => { |
76 |
| - if (target !== this.id) { |
77 |
| - return; |
78 | 53 | }
|
79 |
| - this.toggle(); |
80 |
| - }); |
81 |
| - }, |
| 54 | + }, |
82 | 55 |
|
83 |
| - destroyed() { |
84 |
| - clearTimeout(this._collapseAnimation); |
85 |
| - } |
86 |
| -}; |
| 56 | + created() { |
| 57 | + this.$root.$on('collapse::toggle', target => { |
| 58 | + if (target !== this.id) { |
| 59 | + return; |
| 60 | + } |
| 61 | + this.toggle(); |
| 62 | + }); |
| 63 | + }, |
| 64 | + }; |
87 | 65 |
|
88 | 66 | </script>
|
0 commit comments