Skip to content

Commit 9820505

Browse files
committed
1.2.5
* improve logic to set default transition and auto duration * add reduced motion support * split collapseStyles computed * add nested example * update readme
1 parent 3d775b0 commit 9820505

File tree

11 files changed

+257
-133
lines changed

11 files changed

+257
-133
lines changed

README.md

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,21 +69,17 @@ const isExpanded = ref(false)
6969
</template>
7070
```
7171

72-
## Automatic transition
72+
## Automatic transition (default behavior)
7373

74-
By default, as long as **no class** is added to the `Collapse` element, this transition is automatically added to the element:
74+
By default, if no height transition is specified the following one is automatically added to the Collapse element:
7575

7676
`height var(--vc-auto-duration) cubic-bezier(0.33, 1, 0.68, 1)`
7777

78-
`var(--vc-auto-duration)` corresponds to the optimal transition duration which is automatically calculated according to the content height.
78+
`--vc-auto-duration` is calculated in background and corresponds to an optimal transition duration based on your content height.
7979

80-
<br />
81-
82-
This is the usage I recommend: keep Collapse clean without any class and forget about it. Instead focus on styling its content and wrapper.
80+
This is the recommended way to use this package unless you want to customize the transition.
8381

84-
> :warning: If you really want to add classes to Collapse, you will have to add the height transition manually as displayed below.
85-
86-
## Custom Transition / Duration
82+
## Custom transition
8783

8884
If you prefer to use a custom duration or easing, add a class to Collapse that transitions the `height` property:
8985

@@ -104,7 +100,7 @@ If you prefer to use a custom duration or easing, add a class to Collapse that t
104100

105101
:bulb: Find a full list of easings at [easings.net](https://easings.net).
106102

107-
## Multiple transitions
103+
### Multiple transitions
108104

109105
To transition other properties use the attribute `data-collapse`:
110106

@@ -219,7 +215,11 @@ function scrollIntoView(index) {
219215
</template>
220216
```
221217

222-
## Make it accessible
218+
## Accessibility
219+
220+
`vue-collapsed` automatically detects if users prefer reduced motion and will disable transitions accordingly while keeping the same API behavior (emitting events and post-transition styles).
221+
222+
You should only add `aria` attributes to the Collapse element according to your use case.
223223

224224
```vue
225225
<script setup>
@@ -255,6 +255,22 @@ function handleCollapse() {
255255
</template>
256256
```
257257

258+
## Manually disable transitions
259+
260+
```vue
261+
<template>
262+
<Collapse :when="isExpanded" class="instant-collapse">
263+
<p>This is a paragraph.</p>
264+
</Collapse>
265+
</template>
266+
267+
<style>
268+
.instant-collapse {
269+
transition: none;
270+
}
271+
</style>
272+
```
273+
258274
## License
259275

260276
MIT

demo/Accordion.vue

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,7 @@ function handleAccordion(selectedIndex: number) {
2727
<ExampleHeader title="Accordion" href="Accordion.vue" hint="Using reactive()" />
2828
<div v-for="(question, index) in questions" :key="question.title" class="Section">
2929
<button
30-
:class="[
31-
'Panel',
32-
{
33-
Active: question.isExpanded,
34-
},
35-
]"
30+
:class="['Panel', { Active: question.isExpanded }]"
3631
@click="handleAccordion(index)"
3732
>
3833
{{ question.title }}

demo/App.vue

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Accordion from './Accordion.vue'
44
import IndividualControl from './IndividualControl.vue'
55
import WithCallbacks from './WithCallbacks.vue'
66
import AdvancedControl from './AdvancedControl.vue'
7+
import NestedCollapse from './NestedCollapse.vue'
78
</script>
89

910
<template>
@@ -31,9 +32,10 @@ import AdvancedControl from './AdvancedControl.vue'
3132
<main>
3233
<SingleCollapse />
3334
<Accordion />
35+
<IndividualControl />
3436
<WithCallbacks />
3537
<AdvancedControl />
36-
<IndividualControl />
38+
<NestedCollapse />
3739
</main>
3840

3941
<footer>
@@ -55,6 +57,7 @@ import AdvancedControl from './AdvancedControl.vue'
5557
--ForegroundColorLight: #929292;
5658
--BackgroundColor: #1a1a1a;
5759
--BackgroundAlternateColor: #242424;
60+
--ArticleBorder: 1px solid var(--ForegroundColorLight);
5861
}
5962
* {
6063
box-sizing: border-box;
@@ -178,12 +181,12 @@ footer {
178181
background: var(--BackgroundAlternateColor);
179182
width: 100%;
180183
max-width: 600px;
181-
border-top: 1px solid var(--ForegroundColorLight);
184+
border-top: var(--ArticleBorder);
182185
margin: 0;
183186
}
184187
185188
.Section:last-of-type {
186-
border-bottom: 1px solid var(--ForegroundColorLight);
189+
border-bottom: var(--ArticleBorder);
187190
}
188191
189192
.Section button {
@@ -194,6 +197,12 @@ footer {
194197
cursor: pointer;
195198
}
196199
200+
.NestedCollapse {
201+
margin: 0 20px 20px;
202+
border-top: var(--ArticleBorder);
203+
border-bottom: var(--ArticleBorder);
204+
}
205+
197206
.CollapseContent {
198207
padding: 0 10px 10px;
199208
margin: 0;

demo/IndividualControl.vue

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,7 @@ const collapseAttrs = computed(() =>
5151
<button
5252
v-bind="toggleAttrs[index]"
5353
@click="handleIndividual(index)"
54-
:class="[
55-
'Panel',
56-
{
57-
Active: question.isExpanded,
58-
},
59-
]"
54+
:class="['Panel', { Active: question.isExpanded }]"
6055
>
6156
{{ question.title }}
6257
</button>

demo/NestedCollapse.vue

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<script lang="ts" setup>
2+
import { reactive } from 'vue'
3+
4+
import ExampleHeader from './ExampleHeader.vue'
5+
6+
const nested = reactive({
7+
first: true, // Initial value
8+
second: false,
9+
third: false,
10+
fourth: false,
11+
})
12+
13+
const innerText = `
14+
As an interesting side note, as a head without a body, I envy the dead. Kids don't
15+
turn rotten just from watching TV. Bender, I didn't know you liked cooking. That's so
16+
cute. That's right, baby. I ain't your loverboy Flexo, the guy you love so much. You
17+
even love anyone pretending to be him! I'll tell them you went down prying the
18+
wedding ring off his cold, dead finger.
19+
`
20+
</script>
21+
22+
<template>
23+
<article class="Wrapper">
24+
<ExampleHeader
25+
title="Nested Collapse"
26+
href="NestedCollapse.vue"
27+
hint="Multiple collapse nested"
28+
/>
29+
30+
<div class="Section">
31+
<button @click="nested.first = !nested.first" :class="['Panel', { Active: nested.first }]">
32+
1. Hello buddy, how are you today?
33+
</button>
34+
35+
<Collapse :when="nested.first">
36+
<p class="CollapseContent">{{ innerText }}</p>
37+
38+
<div class="NestedCollapse">
39+
<button
40+
@click="nested.second = !nested.second"
41+
:class="['Panel', { Active: nested.second }]"
42+
>
43+
2. Hello buddy, how are you today?
44+
</button>
45+
46+
<Collapse :when="nested.second">
47+
<p class="CollapseContent">{{ innerText }}</p>
48+
49+
<div class="NestedCollapse">
50+
<button
51+
@click="nested.third = !nested.third"
52+
:class="['Panel', { Active: nested.third }]"
53+
>
54+
3. Hello buddy, how are you today?
55+
</button>
56+
57+
<Collapse :when="nested.third">
58+
<p class="CollapseContent">
59+
{{ innerText }}
60+
</p>
61+
</Collapse>
62+
</div>
63+
</Collapse>
64+
</div>
65+
</Collapse>
66+
</div>
67+
</article>
68+
</template>
69+
70+
<!-- Check styles in App.vue -->

demo/SingleCollapse.vue

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<script lang="ts" setup>
22
import { computed, ref } from 'vue'
3+
34
import ExampleHeader from './ExampleHeader.vue'
45
56
const isExpanded = ref(false)
@@ -38,12 +39,7 @@ const collapseAttrs = {
3839
<button
3940
v-bind="toggleAttrs"
4041
@click="handleCollapse"
41-
:class="[
42-
'Panel',
43-
{
44-
Active: isExpanded,
45-
},
46-
]"
42+
:class="['Panel', { Active: isExpanded }]"
4743
>
4844
Hello buddy, how are you today?
4945
</button>

demo/WithCallbacks.vue

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import ExampleHeader from './ExampleHeader.vue'
66
import fakeData from './fakeData.json'
77
88
const collapseRefs = ref<Element[]>([])
9-
const indexToScroll = ref(-1)
109
11-
let isBusy = false
10+
let indexToScroll = -1
11+
let isCollapsing = false
1212
1313
const questions = reactive(
1414
fakeData.map(({ title, answer }) => ({ title, answer, isExpanded: false }))
@@ -27,20 +27,20 @@ function scrollIntoView(index: number) {
2727
}
2828
2929
function onExpanded(index: number) {
30-
indexToScroll.value = index
31-
if (!isBusy) {
30+
indexToScroll = index
31+
if (!isCollapsing) {
3232
scrollIntoView(index)
3333
}
3434
}
3535
3636
function onCollapse() {
37-
isBusy = true
37+
isCollapsing = true
3838
}
3939
4040
function onCollapsed() {
41-
if (isBusy && indexToScroll.value !== -1) {
42-
scrollIntoView(indexToScroll.value)
43-
isBusy = false
41+
if (isCollapsing && indexToScroll !== -1) {
42+
scrollIntoView(indexToScroll)
43+
isCollapsing = false
4444
}
4545
}
4646
</script>
@@ -61,22 +61,17 @@ function onCollapsed() {
6161
ref="collapseRefs"
6262
>
6363
<button
64-
:class="[
65-
'Panel',
66-
{
67-
Active: question.isExpanded,
68-
},
69-
]"
64+
:class="['Panel', { Active: question.isExpanded }]"
7065
@click="handleAccordion(index)"
7166
>
7267
{{ question.title }}
7368
</button>
7469
<Collapse
7570
as="section"
7671
:when="question.isExpanded"
77-
:onExpanded="() => onExpanded(index)"
78-
:onCollapse="onCollapse"
79-
:onCollapsed="onCollapsed"
72+
@expanded="() => onExpanded(index)"
73+
@collapse="onCollapse"
74+
@collapsed="onCollapsed"
8075
>
8176
<p class="CollapseContent">
8277
{{ question.answer }}

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-collapsed",
3-
"version": "1.2.0",
3+
"version": "1.2.5",
44
"private": false,
55
"description": "Dynamic CSS height transition from any to auto and vice versa for Vue 3. Accordion ready.",
66
"keywords": [
@@ -49,7 +49,7 @@
4949
"devDependencies": {
5050
"@babel/types": "^7.22.5",
5151
"@rollup/plugin-terser": "^0.4.3",
52-
"@types/node": "^18.17.0",
52+
"@types/node": "^18.17.1",
5353
"@vitejs/plugin-vue": "^4.2.3",
5454
"cypress": "^12.17.2",
5555
"cypress-wait-frames": "^0.9.4",
@@ -62,6 +62,6 @@
6262
"vite": "^4.4.7",
6363
"vite-plugin-dts": "^1.7.3",
6464
"vue": "^3.3.4",
65-
"vue-tsc": "^1.8.6"
65+
"vue-tsc": "^1.8.8"
6666
}
6767
}

0 commit comments

Comments
 (0)