Skip to content

Commit 7f391d9

Browse files
committed
Accordion sections prop fix
1 parent 0fdbc99 commit 7f391d9

File tree

1 file changed

+43
-39
lines changed

1 file changed

+43
-39
lines changed

src/lib/components/ui/Accordion.svelte

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
<script lang="ts">
2-
import type { Snippet } from 'svelte';
2+
import type { Snippet } from "svelte";
33
4-
import { onMount } from 'svelte';
5-
import { SvelteSet } from 'svelte/reactivity';
4+
import { onMount } from "svelte";
5+
import { SvelteSet } from "svelte/reactivity";
66
77
let {
88
sections = [],
9-
hideAllSections = 'Hide all sections',
10-
hideSection = 'Hide',
11-
hideSectionAriaLabel = 'Hide this section',
12-
showAllSections = 'Show all sections',
13-
showSection = 'Show',
14-
showSectionAriaLabel = 'Show this section',
9+
hideAllSections = "Hide all sections",
10+
hideSection = "Hide",
11+
hideSectionAriaLabel = "Hide this section",
12+
showAllSections = "Show all sections",
13+
showSection = "Show",
14+
showSectionAriaLabel = "Show this section",
1515
allSectionToggle = true,
1616
minSectionsAllSectionToggle = 2,
1717
rememberIsExpandedState = true,
18-
headingLevel = 'h4',
18+
headingLevel = "h4",
1919
}: {
2020
sections: {
2121
heading: string;
@@ -37,26 +37,30 @@
3737
} = $props();
3838
3939
//Attempt to ensure that ids are unique by attaching extra characters
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-
});
40+
let uniqueSections = $derived(
41+
sections.map((section) => {
42+
return {
43+
...section,
44+
uniqueid:
45+
section.id +
46+
section.heading.slice(1, 3) +
47+
(section?.summary?.slice(0, 2) ?? ""),
48+
};
49+
}),
50+
);
4951
50-
let expandedSections = new SvelteSet(
51-
uniqueSections
52-
.filter((section) => section.expanded)
53-
.map((section) => section.uniqueid)
52+
let expandedSections = $derived(
53+
new SvelteSet(
54+
uniqueSections
55+
.filter((section) => section.expanded)
56+
.map((section) => section.uniqueid),
57+
),
5458
);
5559
5660
let allExpanded = $derived(expandedSections.size === sections.length);
5761
58-
let ariaLiveValue: 'polite' | 'off' | 'assertive' | null | undefined =
59-
$state('polite');
62+
let ariaLiveValue: "polite" | "off" | "assertive" | null | undefined =
63+
$state("polite");
6064
6165
// Event handlers
6266
@@ -67,19 +71,19 @@
6771
expandedSections.add(uniqueid);
6872
}
6973
//Announce the contents change when an accoridion section is expanded
70-
ariaLiveValue = 'polite';
74+
ariaLiveValue = "polite";
7175
}
7276
7377
function toggleAll() {
7478
if (!allExpanded) {
7579
uniqueSections.forEach((section) =>
76-
expandedSections.add(section.uniqueid)
80+
expandedSections.add(section.uniqueid),
7781
);
7882
} else {
7983
expandedSections.clear();
8084
}
8185
//Don't announce all of the changes when we open all sections - this gets noisy and the content isn't associated with the label
82-
ariaLiveValue = 'off';
86+
ariaLiveValue = "off";
8387
}
8488
8589
// Only use session storage logic if rememberIsExpandedState is true
@@ -92,7 +96,7 @@
9296
expandedSections.add(section.uniqueid);
9397
} else {
9498
const stored = sessionStorage.getItem(section.uniqueid);
95-
if (stored === 'true') {
99+
if (stored === "true") {
96100
expandedSections.add(section.uniqueid);
97101
}
98102
}
@@ -113,7 +117,7 @@
113117
uniqueSections.forEach((section) => {
114118
sessionStorage.setItem(
115119
section.uniqueid,
116-
expandedSections.has(section.uniqueid).toString()
120+
expandedSections.has(section.uniqueid).toString(),
117121
);
118122
});
119123
}
@@ -197,23 +201,23 @@
197201
class:govuk-accordion__section--expanded={isExpanded}
198202
>
199203
<div class="govuk-accordion__section-header">
200-
{#if headingLevel.toLowerCase() == 'h2'}
204+
{#if headingLevel.toLowerCase() == "h2"}
201205
<h2 class="govuk-accordion__section-heading">
202206
{@render headerContent(section, isExpanded)}
203207
</h2>
204-
{:else if headingLevel.toLowerCase() == 'h3'}
208+
{:else if headingLevel.toLowerCase() == "h3"}
205209
<h3 class="govuk-accordion__section-heading">
206210
{@render headerContent(section, isExpanded)}
207211
</h3>
208-
{:else if headingLevel.toLowerCase() == 'h4'}
212+
{:else if headingLevel.toLowerCase() == "h4"}
209213
<h4 class="govuk-accordion__section-heading">
210214
{@render headerContent(section, isExpanded)}
211215
</h4>
212-
{:else if headingLevel.toLowerCase() == 'h5'}
216+
{:else if headingLevel.toLowerCase() == "h5"}
213217
<h5 class="govuk-accordion__section-heading">
214218
{@render headerContent(section, isExpanded)}
215219
</h5>
216-
{:else if headingLevel.toLowerCase() == 'h6'}
220+
{:else if headingLevel.toLowerCase() == "h6"}
217221
<h6 class="govuk-accordion__section-heading">
218222
{@render headerContent(section, isExpanded)}
219223
</h6>
@@ -224,12 +228,12 @@
224228
class="govuk-accordion__section-content"
225229
aria-live={ariaLiveValue}
226230
hidden={!isExpanded}
227-
role={uniqueSections.length < 6 ? 'region' : ''}
231+
role={uniqueSections.length < 6 ? "region" : ""}
228232
aria-labelledby={uniqueSections.length < 6
229-
? section.uniqueid + '-button'
230-
: ''}
233+
? section.uniqueid + "-button"
234+
: ""}
231235
>
232-
{#if typeof section.content === 'string'}
236+
{#if typeof section.content === "string"}
233237
<p class="govuk-body">{section.content}</p>
234238
{:else}
235239
{@render section.content()}

0 commit comments

Comments
 (0)