Skip to content

Commit b602819

Browse files
committed
Move heading text to AccordionSection
This allows to remove the variable storing the CSS class, as well as drop an `if` that was only to satisfy TypeScript being unaware that the element existed for sure as we had created it ourselves after we used `querySelector`
1 parent a44d63a commit b602819

File tree

2 files changed

+36
-40
lines changed

2 files changed

+36
-40
lines changed

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ export class AccordionSection extends Component {
3636
})
3737
}
3838

39+
this.$headingText = createHeadingText(this.$buttonPlaceholder)
40+
3941
// Technically slightly different from the current implementation
4042
// However, I'm not sure we originally intended to check whether the content element
4143
// is present every time we toggle a section.
@@ -63,6 +65,38 @@ export class AccordionSection extends Component {
6365
}
6466
}
6567

68+
/**
69+
* Creates the `<span>` containing the text of the section's heading
70+
*
71+
* @param {Element} $buttonPlaceholder - The heading of the span
72+
* @returns {HTMLSpanElement} - The `<span>` containing the text of the section's heading
73+
*/
74+
function createHeadingText($buttonPlaceholder) {
75+
// Create an inner heading text container to limit the width of the focus
76+
// state
77+
const $headingTextFocus = createElement(
78+
'span',
79+
{
80+
class: 'govuk-accordion__section-heading-text-focus'
81+
},
82+
// span could contain HTML elements which need moving to the new span
83+
// (see https://www.w3.org/TR/2011/WD-html5-20110525/content-models.html#phrasing-content)
84+
Array.from($buttonPlaceholder.childNodes)
85+
)
86+
87+
// Create container for heading text so it can be styled
88+
const $headingText = createElement(
89+
'span',
90+
{
91+
class: 'govuk-accordion__section-heading-text',
92+
id: $buttonPlaceholder.id
93+
},
94+
[$headingTextFocus]
95+
)
96+
97+
return $headingText
98+
}
99+
66100
/**
67101
* Creates a `<span>` rendering the 'Show'/'Hide' toggle
68102
*

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

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ const sectionButtonClass = 'govuk-accordion__section-button'
1717
/** @private */
1818
const sectionHeadingClass = 'govuk-accordion__section-heading'
1919

20-
/** @private */
21-
const sectionHeadingTextClass = 'govuk-accordion__section-heading-text'
22-
2320
/** @private */
2421
const iconClass = 'govuk-accordion-nav__chevron'
2522

@@ -212,7 +209,7 @@ export class Accordion extends ConfigurableComponent {
212209
// 2. Punctuation
213210
// 3. (Optional: Summary line followed by punctuation)
214211
// 4. Show / hide toggle
215-
$button.appendChild(this.createHeadingText(section.$buttonPlaceholder))
212+
$button.appendChild(section.$headingText)
216213
$button.appendChild(this.getButtonPunctuationEl())
217214

218215
// If summary content exists add to DOM in correct order
@@ -230,38 +227,6 @@ export class Accordion extends ConfigurableComponent {
230227
$heading.appendChild($button)
231228
}
232229

233-
/**
234-
* Creates the `<span>` containing the text of the section's heading
235-
*
236-
* @param {Element} $buttonPlaceholder - The heading of the span
237-
* @returns {HTMLSpanElement} - The `<span>` containing the text of the section's heading
238-
*/
239-
createHeadingText($buttonPlaceholder) {
240-
// Create an inner heading text container to limit the width of the focus
241-
// state
242-
const $headingTextFocus = createElement(
243-
'span',
244-
{
245-
class: 'govuk-accordion__section-heading-text-focus'
246-
},
247-
// span could contain HTML elements which need moving to the new span
248-
// (see https://www.w3.org/TR/2011/WD-html5-20110525/content-models.html#phrasing-content)
249-
Array.from($buttonPlaceholder.childNodes)
250-
)
251-
252-
// Create container for heading text so it can be styled
253-
const $headingText = createElement(
254-
'span',
255-
{
256-
class: sectionHeadingTextClass,
257-
id: $buttonPlaceholder.id
258-
},
259-
[$headingTextFocus]
260-
)
261-
262-
return $headingText
263-
}
264-
265230
/**
266231
* Creates the `<span>` element with the summary for the section
267232
*
@@ -379,10 +344,7 @@ export class Accordion extends ConfigurableComponent {
379344
// Update aria-label combining
380345
const ariaLabelParts = []
381346

382-
const $headingText = $section.querySelector(`.${sectionHeadingTextClass}`)
383-
if ($headingText) {
384-
ariaLabelParts.push(`${$headingText.textContent}`.trim())
385-
}
347+
ariaLabelParts.push(`${section.$headingText.textContent}`.trim())
386348

387349
const $summary = $section.querySelector(`.${sectionSummaryClass}`)
388350
if ($summary) {

0 commit comments

Comments
 (0)