|
37 | 37 | headingLevel?: string;
|
38 | 38 | } = $props();
|
39 | 39 |
|
| 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 | +
|
40 | 50 | 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) |
42 | 54 | );
|
43 | 55 |
|
44 | 56 | let allExpanded = $derived(expandedSections.size === sections.length);
|
|
48 | 60 |
|
49 | 61 | // Event handlers
|
50 | 62 |
|
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); |
54 | 66 | } else {
|
55 |
| - expandedSections.add(id); |
| 67 | + expandedSections.add(uniqueid); |
56 | 68 | }
|
57 | 69 | //Announce the contents change when an accoridion section is expanded
|
58 | 70 | ariaLiveValue = 'polite';
|
59 | 71 | }
|
60 | 72 |
|
61 | 73 | function toggleAll() {
|
62 | 74 | if (!allExpanded) {
|
63 |
| - sections.forEach((section) => expandedSections.add(section.id)); |
| 75 | + uniqueSections.forEach((section) => |
| 76 | + expandedSections.add(section.uniqueid) |
| 77 | + ); |
64 | 78 | } else {
|
65 | 79 | expandedSections.clear();
|
66 | 80 | }
|
|
71 | 85 | // Only use session storage logic if rememberIsExpandedState is true
|
72 | 86 | onMount(() => {
|
73 | 87 | if (browser && rememberIsExpandedState) {
|
74 |
| - sections.forEach((section) => { |
| 88 | + uniqueSections.forEach((section) => { |
75 | 89 | // If the section is explicitly expanded, respect that.
|
76 | 90 | // Otherwise, try to restore from session storage.
|
77 | 91 | // **** Is this the behaviour we want? Shouldn't it be the other way around?
|
78 | 92 | if (section.expanded) {
|
79 |
| - expandedSections.add(section.id); |
| 93 | + expandedSections.add(section.uniqueid); |
80 | 94 | } else {
|
81 |
| - const stored = sessionStorage.getItem(section.id); |
| 95 | + const stored = sessionStorage.getItem(section.uniqueid); |
82 | 96 | if (stored === 'true') {
|
83 |
| - expandedSections.add(section.id); |
| 97 | + expandedSections.add(section.uniqueid); |
84 | 98 | }
|
85 | 99 | }
|
86 | 100 | });
|
87 | 101 | } else {
|
88 | 102 | // If rememberIsExpandedState is false, just respect the initial `section.expanded` values
|
89 |
| - sections.forEach((section) => { |
| 103 | + uniqueSections.forEach((section) => { |
90 | 104 | if (section.expanded) {
|
91 |
| - expandedSections.add(section.id); |
| 105 | + expandedSections.add(section.uniqueid); |
92 | 106 | }
|
93 | 107 | });
|
94 | 108 | }
|
|
97 | 111 | // Effect to update sessionStorage when sections change
|
98 | 112 | $effect(() => {
|
99 | 113 | if (browser && rememberIsExpandedState) {
|
100 |
| - sections.forEach((section) => { |
| 114 | + uniqueSections.forEach((section) => { |
101 | 115 | sessionStorage.setItem(
|
102 |
| - section.id, |
103 |
| - expandedSections.has(section.id).toString() |
| 116 | + section.uniqueid, |
| 117 | + expandedSections.has(section.uniqueid).toString() |
104 | 118 | );
|
105 | 119 | });
|
106 | 120 | }
|
|
114 | 128 | >
|
115 | 129 | <div
|
116 | 130 | class="govuk-accordion__controls"
|
117 |
| - hidden={!allSectionToggle || sections.length < minSectionsAllSectionToggle} |
| 131 | + hidden={!allSectionToggle || |
| 132 | + uniqueSections.length < minSectionsAllSectionToggle} |
118 | 133 | >
|
119 | 134 | <button
|
120 | 135 | type="button"
|
|
135 | 150 | {#snippet content(section, isExpanded)}
|
136 | 151 | <button
|
137 | 152 | type="button"
|
138 |
| - aria-controls="{section.id}-content" |
| 153 | + aria-controls="{section.uniqueid}-content" |
139 | 154 | class="govuk-accordion__section-button"
|
140 | 155 | aria-expanded={isExpanded}
|
141 |
| - onclick={() => toggleSection(section.id)} |
| 156 | + onclick={() => toggleSection(section.uniqueid)} |
142 | 157 | aria-label="{section.heading}, {section?.summary}, {isExpanded
|
143 | 158 | ? hideSectionAriaLabel
|
144 | 159 | : showSectionAriaLabel}"
|
|
175 | 190 | </button>
|
176 | 191 | {/snippet}
|
177 | 192 |
|
178 |
| - {#each sections as section} |
179 |
| - {@const isExpanded = expandedSections.has(section.id)} |
| 193 | + {#each uniqueSections as section} |
| 194 | + {@const isExpanded = expandedSections.has(section.uniqueid)} |
180 | 195 | <div
|
181 | 196 | class="govuk-accordion__section"
|
182 | 197 | class:govuk-accordion__section--expanded={isExpanded}
|
183 | 198 | >
|
184 | 199 | <div class="govuk-accordion__section-header">
|
185 |
| - <!-- <h2 class="govuk-accordion__section-heading"> --> |
186 | 200 | {#if headingLevel == 'h2'}
|
187 | 201 | <h2 class="govuk-accordion__section-heading">
|
188 | 202 | {@render content(section, isExpanded)}
|
|
206 | 220 | {/if}
|
207 | 221 | </div>
|
208 | 222 | <div
|
209 |
| - id="{section.id}-content" |
| 223 | + id="{section.uniqueid}-content" |
210 | 224 | class="govuk-accordion__section-content"
|
211 |
| - aria-labelledby="{section.id}-heading" |
| 225 | + aria-labelledby="{section.uniqueid}-heading" |
212 | 226 | aria-live={ariaLiveValue}
|
213 | 227 | hidden={!isExpanded}
|
214 | 228 | >
|
|
0 commit comments