Skip to content

Commit 4d2686f

Browse files
committed
Extract creation of section's heading text in its own method
1 parent 0c83b08 commit 4d2686f

1 file changed

Lines changed: 31 additions & 19 deletions

File tree

  • packages/govuk-frontend/src/govuk/components/accordion

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

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -200,24 +200,6 @@ export class Accordion extends ConfigurableComponent {
200200
})
201201
}
202202

203-
// Create container for heading text so it can be styled
204-
const $headingText = createElement('span', {
205-
class: sectionHeadingTextClass,
206-
id: $span.id
207-
})
208-
209-
// Create an inner heading text container to limit the width of the focus
210-
// state
211-
const $headingTextFocus = createElement('span', {
212-
class: 'govuk-accordion__section-heading-text-focus'
213-
})
214-
$headingText.appendChild($headingTextFocus)
215-
// span could contain HTML elements
216-
// (see https://www.w3.org/TR/2011/WD-html5-20110525/content-models.html#phrasing-content)
217-
Array.from($span.childNodes).forEach(($child) =>
218-
$headingTextFocus.appendChild($child)
219-
)
220-
221203
// Create a button element that will replace the
222204
// '.govuk-accordion__section-button' span
223205
const $button = createElement('button', {
@@ -238,7 +220,7 @@ export class Accordion extends ConfigurableComponent {
238220
// 2. Punctuation
239221
// 3. (Optional: Summary line followed by punctuation)
240222
// 4. Show / hide toggle
241-
$button.appendChild($headingText)
223+
$button.appendChild(this.createHeadingText($span))
242224
$button.appendChild(this.getButtonPunctuationEl())
243225

244226
// If summary content exists add to DOM in correct order
@@ -291,6 +273,36 @@ export class Accordion extends ConfigurableComponent {
291273
return $showHideToggle
292274
}
293275

276+
/**
277+
* Creates the `<span>` containing the text of the section's heading
278+
*
279+
* @param {Element} $span - The heading of the span
280+
* @returns {HTMLSpanElement} - The `<span>` containing the text of the section's heading
281+
*/
282+
createHeadingText($span) {
283+
// Create an inner heading text container to limit the width of the focus
284+
// state
285+
const $headingTextFocus = createElement('span', {
286+
class: 'govuk-accordion__section-heading-text-focus'
287+
})
288+
289+
// span could contain HTML elements which need moving to the new span
290+
// (see https://www.w3.org/TR/2011/WD-html5-20110525/content-models.html#phrasing-content)
291+
Array.from($span.childNodes).forEach(($child) =>
292+
$headingTextFocus.appendChild($child)
293+
)
294+
295+
// Create container for heading text so it can be styled
296+
const $headingText = createElement('span', {
297+
class: sectionHeadingTextClass,
298+
id: $span.id
299+
})
300+
301+
$headingText.appendChild($headingTextFocus)
302+
303+
return $headingText
304+
}
305+
294306
/**
295307
* Creates the `<span>` element with the summary for the section
296308
*

0 commit comments

Comments
 (0)