@@ -7,8 +7,6 @@ Dynamic CSS height transition from _any to auto_ and vice versa. Accordion ready
77
88[ Examples and Demo] ( https://vue-collapsed.netlify.com ) - [ Stackblitz] ( https://stackblitz.com/edit/vue-dmjqey?file=src/App.vue )
99
10- > :bulb : Requires Vue v3.0.0 or above.
11-
1210<br />
1311
1412Check out my other packages for Vue 3:
@@ -37,15 +35,20 @@ npm i -S vue-collapsed
3735
3836## Props
3937
40- | name | description | type | required |
41- | ------------- | ----------------------------------------- | ----------------------------- | ------------------ |
42- | ` when ` | Value to control collapse | boolean | :white_check_mark : |
43- | ` baseHeight ` | Collapsed height in px, defaults to ` 0 ` . | number | :x : |
44- | ` as ` | Tag to use instead of ` div ` | _ keyof_ HTMLElementTagNameMap | :x : |
45- | ` onExpand ` | Callback on expand transition start | () => void | :x : |
46- | ` onExpanded ` | Callback on expand transition completed | () => void | :x : |
47- | ` onCollapse ` | Callback on collapse transition start | () => void | :x : |
48- | ` onCollapsed ` | Callback on collapse transition completed | () => void | :x : |
38+ | Name | Description | Type | Required |
39+ | ------------ | ---------------------------------------- | ----------------------------- | ------------------ |
40+ | ` when ` | Value to control collapse | boolean | :white_check_mark : |
41+ | ` baseHeight ` | Collapsed height in px, defaults to ` 0 ` . | number | :x : |
42+ | ` as ` | Tag to use instead of ` div ` | _ keyof_ HTMLElementTagNameMap | :x : |
43+
44+ ## Emits
45+
46+ | Name | Description | Type |
47+ | ------------ | ----------------------------- | ---------- |
48+ | ` @expand ` | Expand transition start | () => void |
49+ | ` @expanded ` | Expand transition completed | () => void |
50+ | ` @collapse ` | Collapse transition start | () => void |
51+ | ` @collapsed ` | Collapse transition completed | () => void |
4952
5053## Usage
5154
@@ -58,36 +61,43 @@ const isExpanded = ref(false)
5861</script>
5962
6063<template>
61- <button @click="isExpanded = !isExpanded">This a panel.</button>
62- <Collapse :when="isExpanded" class="v-collapse">
64+ <button @click="isExpanded = !isExpanded">Trigger</button>
65+
66+ <Collapse :when="isExpanded">
6367 <p>This is a paragraph.</p>
6468 </Collapse>
6569</template>
66-
67- <style>
68- .v-collapse {
69- transition: height 300ms cubic-bezier(0.33, 1, 0.68, 1);
70- }
71- </style>
7270```
7371
74- ## Auto Duration
72+ ## Automatic Transition / Duration
73+
74+ By default, as long as no class is added to the ` Collapse ` element, ` vue-collapsed ` will add this transition for you:
75+
76+ ` height var(--vc-auto-duration) cubic-bezier(0.33, 1, 0.68, 1) `
7577
76- Vue Collapsed automatically calculates the optimal duration according to the content height. Use it by referencing the variable ` --vc-auto-duration ` :
78+ ` var(--vc-auto-duration) ` corresponds to the optimal transition duration which is automatically calculated according to the content height.
79+
80+ If you prefer to use a custom duration or easing, add a class to Collapse that transitions the ` height ` property:
81+
82+ ``` vue
83+ <Collapse :when="isExpanded" class="v-collapse">
84+ <p>This is a paragraph.</p>
85+ </Collapse>
86+ ```
7787
7888``` css
7989.v-collapse {
80- transition : height var ( --vc-auto-duration ) ease-out ;
90+ transition : height 300 ms ease-out ;
8191}
8292```
8393
8494:bulb : Use [ calc()] ( https://developer.mozilla.org/en-US/docs/Web/CSS/calc ) to control the speed, _ e.g. ` calc(var(--vc-auto-duration) * 0.75) ` _ .
8595
8696:bulb : Find a full list of easings at [ easings.net] ( https://easings.net ) .
8797
88- ## Additional transitions/styles
98+ ## Multiple transitions
8999
90- To transition other properties or add granular styles use the attribute ` data-collapse ` :
100+ To transition other properties use the attribute ` data-collapse ` :
91101
92102| Transition | From | Enter | Leave |
93103| ---------- | ----------- | ------------ | ----------- |
@@ -115,7 +125,7 @@ Above values can also be accessed using `v-slot`:
115125
116126``` vue
117127<Collapse :when="isExpanded" class="v-collapse" v-slot="{ state }">
118- {{ state === 'collapsing' ? 'Collapsing...' : null }}
128+ {{ state === 'collapsing' ? 'Collapsing the content ...' : null }}
119129</Collapse>
120130```
121131
@@ -164,19 +174,13 @@ function handleAccordion(selectedIndex) {
164174 <button @click="handleAccordion(index)">
165175 {{ question.title }}
166176 </button>
167- <Collapse :when="questions[index].isExpanded" class="v-collapse" >
177+ <Collapse :when="questions[index].isExpanded">
168178 <p>
169179 {{ question.answer }}
170180 </p>
171181 </Collapse>
172182 </div>
173183</template>
174-
175- <style>
176- .v-collapse {
177- transition: height var(--vc-auto-duration) cubic-bezier(0.33, 1, 0.68, 1);
178- }
179- </style>
180184```
181185
182186## Example - Callbacks
@@ -197,23 +201,13 @@ function scrollIntoView(index) {
197201 <button @click="handleAccordion(index)">
198202 {{ question.title }}
199203 </button>
200- <Collapse
201- :when="questions[index].isExpanded"
202- :onExpanded="() => scrollIntoView(index)"
203- class="v-collapse"
204- >
204+ <Collapse :when="questions[index].isExpanded" @expanded="() => scrollIntoView(index)">
205205 <p>
206206 {{ question.answer }}
207207 </p>
208208 </Collapse>
209209 </div>
210210</template>
211-
212- <style>
213- .v-collapse {
214- transition: height var(--vc-auto-duration) cubic-bezier(0.33, 1, 0.68, 1);
215- }
216- </style>
217211```
218212
219213## Make it accessible
@@ -245,17 +239,11 @@ function handleCollapse() {
245239<template>
246240 <div>
247241 <button v-bind="toggleAttrs" @click="handleCollapse">This a panel.</button>
248- <Collapse v-bind="collapseAttrs" :when="isExpanded" class="v-collapse" >
242+ <Collapse v-bind="collapseAttrs" :when="isExpanded">
249243 <p>This is a paragraph.</p>
250244 </Collapse>
251245 </div>
252246</template>
253-
254- <style>
255- .v-collapse {
256- transition: height var(--vc-auto-duration) cubic-bezier(0.33, 1, 0.68, 1);
257- }
258- </style>
259247```
260248
261249## License
0 commit comments