Skip to content

Commit eec005a

Browse files
authored
Merge pull request #463 from ambroisemaupate/fix/accordion
fix: Fixed and improved Accordions
2 parents 02e49ac + 858cb41 commit eec005a

File tree

3 files changed

+74
-27
lines changed

3 files changed

+74
-27
lines changed

src/components/DsfrAccordion/DsfrAccordion.stories.js

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -100,24 +100,26 @@ export const AccordeonDansUnAccordeon = (args) => ({
100100
:expanded-id="expandedId"
101101
@expand="expandedId = $event"
102102
>
103-
<li>
104-
<DsfrAccordion
105-
:title="titleSub1"
106-
:expanded-id="subExpandedId"
107-
@expand="subExpandedId = $event"
108-
>
109-
Contenu de l’accordéon dans l’accordéon
110-
</DsfrAccordion>
111-
</li>
112-
<li>
113-
<DsfrAccordion
114-
:title="titleSub2"
115-
:expanded-id="subExpandedId"
116-
@expand="subExpandedId = $event"
117-
>
118-
Contenu de l’accordéon dans l’accordéon
119-
</DsfrAccordion>
120-
</li>
103+
<DsfrAccordionsGroup>
104+
<li>
105+
<DsfrAccordion
106+
:title="titleSub1"
107+
:expanded-id="subExpandedId"
108+
@expand="subExpandedId = $event"
109+
>
110+
Contenu de l’accordéon dans l’accordéon
111+
</DsfrAccordion>
112+
</li>
113+
<li>
114+
<DsfrAccordion
115+
:title="titleSub2"
116+
:expanded-id="subExpandedId"
117+
@expand="subExpandedId = $event"
118+
>
119+
Contenu de l’accordéon dans l’accordéon
120+
</DsfrAccordion>
121+
</li>
122+
</DsfrAccordionsGroup>
121123
</DsfrAccordion>
122124
</li>
123125
</DsfrAccordionsGroup>

src/components/DsfrAccordion/DsfrAccordion.vue

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,61 @@ 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 () {
37-
this.$emit('expand', this.id)
65+
/*
66+
* Close current accordion if expanded
67+
*/
68+
if (this.expanded) {
69+
this.$emit('expand', undefined)
70+
} else {
71+
this.$emit('expand', this.id)
72+
}
73+
},
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('--collapse', -height + 'px')
81+
this.$refs.collapse.style.setProperty('--collapser', '')
3882
},
3983
},
4084
@@ -59,8 +103,12 @@ export default defineComponent({
59103
</h3>
60104
<div
61105
:id="id"
106+
ref="collapse"
62107
class="fr-collapse"
63-
:class="{ 'fr-collapse--expanded': expanded }"
108+
:class="{
109+
'fr-collapse--expanded': expanded,
110+
'fr-collapsing': collapsing,
111+
}"
64112
>
65113
<!-- @slot Slot par défaut pour le contenu de l’accordéon: sera dans `<div class="fr-collapse">` -->
66114
<slot />
@@ -69,9 +117,3 @@ export default defineComponent({
69117
</template>
70118

71119
<style src="@gouvfr/dsfr/dist/component/accordion/accordion.main.min.css" />
72-
73-
<style scoped>
74-
.fr-collapse--expanded {
75-
max-height: none !important;
76-
}
77-
</style>

src/utils/random-utils.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
export const alphanum = 'abcdefghijklmnopqrstuvwyz0123456789'
1+
const alphanumBase = 'abcdefghijklmnopqrstuvwyz0123456789'
2+
// We need to duplicate the base string to have a longer string
3+
// to avoid Math.random to return the same value twice
4+
export const alphanum = alphanumBase.repeat(10)
25

36
export const getRandomAlphaNum = () => {
47
const randomIndex = Math.floor(Math.random() * alphanum.length)

0 commit comments

Comments
 (0)