Skip to content

Commit 03beeb0

Browse files
committed
Move $summary to AccordionSection
Removes the need for a class
1 parent 56ba966 commit 03beeb0

File tree

2 files changed

+44
-48
lines changed

2 files changed

+44
-48
lines changed

packages/govuk-frontend/src/govuk/components/accordion/accordion-section.mjs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ export class AccordionSection extends Component {
5353
'.govuk-accordion__section-heading',
5454
'Section heading (`.govuk-accordion__section-heading`)'
5555
)
56+
57+
const $summary = this.$root.querySelector(
58+
`.govuk-accordion__section-summary`
59+
)
60+
if ($summary) {
61+
this.$summary = createSummarySpan($summary)
62+
63+
$summary.remove()
64+
}
5665
}
5766

5867
/**
@@ -79,6 +88,37 @@ export class AccordionSection extends Component {
7988
}
8089
}
8190

91+
/**
92+
* Creates the `<span>` element with the summary for the section
93+
*
94+
* This is necessary because the summary line text is now inside
95+
* a button element, which can only contain phrasing content, and
96+
* not a `<div>` element
97+
*
98+
* @param {Element} $summary - The original `<div>` containing the summary
99+
* @returns {HTMLSpanElement} - The `<span>` element containing the summary
100+
*/
101+
function createSummarySpan($summary) {
102+
// Create an inner summary container to limit the width of the summary
103+
// focus state
104+
const $summarySpanFocus = createElement(
105+
'span',
106+
{
107+
class: 'govuk-accordion__section-summary-focus'
108+
},
109+
Array.from($summary.childNodes)
110+
)
111+
112+
const $summarySpan = createElement('span', {}, [$summarySpanFocus])
113+
114+
// Get original attributes, and pass them to the replacement
115+
for (const attr of Array.from($summary.attributes)) {
116+
$summarySpan.setAttribute(attr.name, attr.value)
117+
}
118+
119+
return $summarySpan
120+
}
121+
82122
/**
83123
* Creates the `<span>` containing the text of the section's heading
84124
*

packages/govuk-frontend/src/govuk/components/accordion/accordion.mjs

Lines changed: 4 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ const iconClass = 'govuk-accordion-nav__chevron'
2020
/** @private */
2121
const iconOpenModifier = 'govuk-accordion-nav__chevron--down'
2222

23-
/** @private */
24-
const sectionSummaryClass = 'govuk-accordion__section-summary'
25-
2623
/**
2724
* Accordion component
2825
*
@@ -172,12 +169,6 @@ export class Accordion extends ConfigurableComponent {
172169
* @param {number} index - Section index
173170
*/
174171
constructHeaderMarkup(section, index) {
175-
// TEMPORARY: This helps keeping current code intact
176-
// before it gets moved to the `AccordionSection` class
177-
const $header = section.$header
178-
179-
const $summary = $header.querySelector(`.${sectionSummaryClass}`)
180-
181172
// Create a button element that will replace the
182173
// '.govuk-accordion__section-button' span
183174
const $button = createElement('button', {
@@ -202,11 +193,8 @@ export class Accordion extends ConfigurableComponent {
202193
$button.appendChild(this.getButtonPunctuationEl())
203194

204195
// If summary content exists add to DOM in correct order
205-
if ($summary) {
206-
// Replace the original summary `div` with the new summary `span`
207-
$summary.remove()
208-
209-
$button.appendChild(this.createSummarySpan($summary))
196+
if (section.$summary) {
197+
$button.appendChild(section.$summary)
210198
$button.appendChild(this.getButtonPunctuationEl())
211199
}
212200

@@ -216,37 +204,6 @@ export class Accordion extends ConfigurableComponent {
216204
section.$heading.appendChild($button)
217205
}
218206

219-
/**
220-
* Creates the `<span>` element with the summary for the section
221-
*
222-
* This is necessary because the summary line text is now inside
223-
* a button element, which can only contain phrasing content, and
224-
* not a `<div>` element
225-
*
226-
* @param {Element} $summary - The original `<div>` containing the summary
227-
* @returns {HTMLSpanElement} - The `<span>` element containing the summary
228-
*/
229-
createSummarySpan($summary) {
230-
// Create an inner summary container to limit the width of the summary
231-
// focus state
232-
const $summarySpanFocus = createElement(
233-
'span',
234-
{
235-
class: 'govuk-accordion__section-summary-focus'
236-
},
237-
Array.from($summary.childNodes)
238-
)
239-
240-
const $summarySpan = createElement('span', {}, [$summarySpanFocus])
241-
242-
// Get original attributes, and pass them to the replacement
243-
for (const attr of Array.from($summary.attributes)) {
244-
$summarySpan.setAttribute(attr.name, attr.value)
245-
}
246-
247-
return $summarySpan
248-
}
249-
250207
/**
251208
* When a section is opened by the user agent via the 'beforematch' event
252209
*
@@ -335,9 +292,8 @@ export class Accordion extends ConfigurableComponent {
335292

336293
ariaLabelParts.push(`${section.$headingText.textContent}`.trim())
337294

338-
const $summary = $section.querySelector(`.${sectionSummaryClass}`)
339-
if ($summary) {
340-
ariaLabelParts.push(`${$summary.textContent}`.trim())
295+
if (section.$summary) {
296+
ariaLabelParts.push(`${section.$summary.textContent}`.trim())
341297
}
342298

343299
const ariaLabelMessage = expanded

0 commit comments

Comments
 (0)