Skip to content

Commit 5e21219

Browse files
MikeListerajhillman-ddj
authored andcommitted
update
1 parent 131ef31 commit 5e21219

File tree

1 file changed

+37
-23
lines changed

1 file changed

+37
-23
lines changed

src/lib/components/ui/Accordion.svelte

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,20 @@
3737
headingLevel?: string;
3838
} = $props();
3939
40+
let uniqueSections = sections.map((section, i) => {
41+
return {
42+
...section,
43+
uniqueid:
44+
section.id +
45+
section.heading.slice(1, 3) +
46+
(section?.summary?.slice(0, 2) ?? ''),
47+
};
48+
});
49+
4050
let expandedSections = new SvelteSet(
41-
sections.filter((section) => section.expanded).map((section) => section.id)
51+
uniqueSections
52+
.filter((section) => section.expanded)
53+
.map((section) => section.uniqueid)
4254
);
4355
4456
let allExpanded = $derived(expandedSections.size === sections.length);
@@ -48,19 +60,21 @@
4860
4961
// Event handlers
5062
51-
function toggleSection(id: string) {
52-
if (expandedSections.has(id)) {
53-
expandedSections.delete(id);
63+
function toggleSection(uniqueid: string) {
64+
if (expandedSections.has(uniqueid)) {
65+
expandedSections.delete(uniqueid);
5466
} else {
55-
expandedSections.add(id);
67+
expandedSections.add(uniqueid);
5668
}
5769
//Announce the contents change when an accoridion section is expanded
5870
ariaLiveValue = 'polite';
5971
}
6072
6173
function toggleAll() {
6274
if (!allExpanded) {
63-
sections.forEach((section) => expandedSections.add(section.id));
75+
uniqueSections.forEach((section) =>
76+
expandedSections.add(section.uniqueid)
77+
);
6478
} else {
6579
expandedSections.clear();
6680
}
@@ -71,24 +85,24 @@
7185
// Only use session storage logic if rememberIsExpandedState is true
7286
onMount(() => {
7387
if (browser && rememberIsExpandedState) {
74-
sections.forEach((section) => {
88+
uniqueSections.forEach((section) => {
7589
// If the section is explicitly expanded, respect that.
7690
// Otherwise, try to restore from session storage.
7791
// **** Is this the behaviour we want? Shouldn't it be the other way around?
7892
if (section.expanded) {
79-
expandedSections.add(section.id);
93+
expandedSections.add(section.uniqueid);
8094
} else {
81-
const stored = sessionStorage.getItem(section.id);
95+
const stored = sessionStorage.getItem(section.uniqueid);
8296
if (stored === 'true') {
83-
expandedSections.add(section.id);
97+
expandedSections.add(section.uniqueid);
8498
}
8599
}
86100
});
87101
} else {
88102
// If rememberIsExpandedState is false, just respect the initial `section.expanded` values
89-
sections.forEach((section) => {
103+
uniqueSections.forEach((section) => {
90104
if (section.expanded) {
91-
expandedSections.add(section.id);
105+
expandedSections.add(section.uniqueid);
92106
}
93107
});
94108
}
@@ -97,10 +111,10 @@
97111
// Effect to update sessionStorage when sections change
98112
$effect(() => {
99113
if (browser && rememberIsExpandedState) {
100-
sections.forEach((section) => {
114+
uniqueSections.forEach((section) => {
101115
sessionStorage.setItem(
102-
section.id,
103-
expandedSections.has(section.id).toString()
116+
section.uniqueid,
117+
expandedSections.has(section.uniqueid).toString()
104118
);
105119
});
106120
}
@@ -114,7 +128,8 @@
114128
>
115129
<div
116130
class="govuk-accordion__controls"
117-
hidden={!allSectionToggle || sections.length < minSectionsAllSectionToggle}
131+
hidden={!allSectionToggle ||
132+
uniqueSections.length < minSectionsAllSectionToggle}
118133
>
119134
<button
120135
type="button"
@@ -135,10 +150,10 @@
135150
{#snippet content(section, isExpanded)}
136151
<button
137152
type="button"
138-
aria-controls="{section.id}-content"
153+
aria-controls="{section.uniqueid}-content"
139154
class="govuk-accordion__section-button"
140155
aria-expanded={isExpanded}
141-
onclick={() => toggleSection(section.id)}
156+
onclick={() => toggleSection(section.uniqueid)}
142157
aria-label="{section.heading}, {section?.summary}, {isExpanded
143158
? hideSectionAriaLabel
144159
: showSectionAriaLabel}"
@@ -175,14 +190,13 @@
175190
</button>
176191
{/snippet}
177192

178-
{#each sections as section}
179-
{@const isExpanded = expandedSections.has(section.id)}
193+
{#each uniqueSections as section}
194+
{@const isExpanded = expandedSections.has(section.uniqueid)}
180195
<div
181196
class="govuk-accordion__section"
182197
class:govuk-accordion__section--expanded={isExpanded}
183198
>
184199
<div class="govuk-accordion__section-header">
185-
<!-- <h2 class="govuk-accordion__section-heading"> -->
186200
{#if headingLevel == 'h2'}
187201
<h2 class="govuk-accordion__section-heading">
188202
{@render content(section, isExpanded)}
@@ -206,9 +220,9 @@
206220
{/if}
207221
</div>
208222
<div
209-
id="{section.id}-content"
223+
id="{section.uniqueid}-content"
210224
class="govuk-accordion__section-content"
211-
aria-labelledby="{section.id}-heading"
225+
aria-labelledby="{section.uniqueid}-heading"
212226
aria-live={ariaLiveValue}
213227
hidden={!isExpanded}
214228
>

0 commit comments

Comments
 (0)