Skip to content

Commit df0fb95

Browse files
fix: Fix accordion animation using --collapse and --collapse-max-height CSS vars
1 parent 084e4ff commit df0fb95

File tree

1 file changed

+42
-7
lines changed

1 file changed

+42
-7
lines changed

src/components/DsfrAccordion/DsfrAccordion.vue

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,42 @@ export default defineComponent({
2424
default: 'Sans intitulé',
2525
},
2626
},
27+
2728
emits: ['expand'],
2829
30+
data () {
31+
return {
32+
collapsing: false,
33+
}
34+
},
35+
2936
computed: {
3037
expanded () {
3138
return this.expandedId === this.id
3239
},
3340
},
3441
42+
watch: {
43+
/*
44+
* @see https://github.com/GouvernementFR/dsfr/blob/main/src/core/script/collapse/collapse.js
45+
*/
46+
expanded (newValue, oldValue) {
47+
if (newValue !== oldValue) {
48+
if (newValue === true) {
49+
this.$refs.collapse.style.setProperty('--collapse-max-height', 'none')
50+
}
51+
this.collapsing = true
52+
this.adjust()
53+
setTimeout(() => {
54+
this.collapsing = false
55+
if (newValue === false) {
56+
this.$refs.collapse.style.removeProperty('--collapse-max-height')
57+
}
58+
}, 300)
59+
}
60+
},
61+
},
62+
3563
methods: {
3664
toggleExpanded () {
3765
/*
@@ -43,6 +71,15 @@ export default defineComponent({
4371
this.$emit('expand', this.id)
4472
}
4573
},
74+
/*
75+
* @see https://github.com/GouvernementFR/dsfr/blob/main/src/core/script/collapse/collapse.js#L61
76+
*/
77+
adjust () {
78+
this.$refs.collapse.style.setProperty('--collapser', 'none')
79+
const height = this.$refs.collapse.offsetHeight
80+
this.$refs.collapse.style.setProperty('--collaspe', -height + 'px')
81+
this.$refs.collapse.style.setProperty('--collapser', '')
82+
},
4683
},
4784
4885
})
@@ -66,8 +103,12 @@ export default defineComponent({
66103
</h3>
67104
<div
68105
:id="id"
106+
ref="collapse"
69107
class="fr-collapse"
70-
:class="{ 'fr-collapse--expanded': expanded }"
108+
:class="{
109+
'fr-collapse--expanded': expanded,
110+
'fr-collapsing': collapsing,
111+
}"
71112
>
72113
<!-- @slot Slot par défaut pour le contenu de l’accordéon: sera dans `<div class="fr-collapse">` -->
73114
<slot />
@@ -76,9 +117,3 @@ export default defineComponent({
76117
</template>
77118

78119
<style src="@gouvfr/dsfr/dist/component/accordion/accordion.main.min.css" />
79-
80-
<style scoped>
81-
.fr-collapse--expanded {
82-
max-height: none !important;
83-
}
84-
</style>

0 commit comments

Comments
 (0)