Skip to content

Commit a44d63a

Browse files
committed
Move button placeholder access to AccordionSection
Ultimately, it shouldn't get accessed by the `Accordion`, but while refactoring it'll be publicly accessible.
1 parent dc003e6 commit a44d63a

File tree

3 files changed

+18
-16
lines changed

3 files changed

+18
-16
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ export class AccordionSection extends Component {
2626
})
2727
}
2828

29+
this.$buttonPlaceholder = this.$header.querySelector(
30+
`.govuk-accordion__section-button`
31+
)
32+
if (!this.$buttonPlaceholder) {
33+
throw new ElementError({
34+
component: AccordionSection,
35+
identifier: `Section button placeholder (\`<span class="govuk-accordion__section-button">\`)`
36+
})
37+
}
38+
2939
// Technically slightly different from the current implementation
3040
// However, I'm not sure we originally intended to check whether the content element
3141
// is present every time we toggle a section.

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

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,6 @@ export class Accordion extends ConfigurableComponent {
182182
// before it gets moved to the `AccordionSection` class
183183
const $header = section.$header
184184

185-
const $span = $header.querySelector(`.${sectionButtonClass}`)
186185
const $heading = $header.querySelector(`.${sectionHeadingClass}`)
187186
const $summary = $header.querySelector(`.${sectionSummaryClass}`)
188187

@@ -193,13 +192,6 @@ export class Accordion extends ConfigurableComponent {
193192
})
194193
}
195194

196-
if (!$span) {
197-
throw new ElementError({
198-
component: Accordion,
199-
identifier: `Section button placeholder (\`<span class="${sectionButtonClass}">\`)`
200-
})
201-
}
202-
203195
// Create a button element that will replace the
204196
// '.govuk-accordion__section-button' span
205197
const $button = createElement('button', {
@@ -209,7 +201,7 @@ export class Accordion extends ConfigurableComponent {
209201

210202
// Copy all attributes from $span to $button (except `id`, which gets added
211203
// to the `$headingText` element)
212-
for (const attr of Array.from($span.attributes)) {
204+
for (const attr of Array.from(section.$buttonPlaceholder.attributes)) {
213205
if (attr.name !== 'id') {
214206
$button.setAttribute(attr.name, attr.value)
215207
}
@@ -220,7 +212,7 @@ export class Accordion extends ConfigurableComponent {
220212
// 2. Punctuation
221213
// 3. (Optional: Summary line followed by punctuation)
222214
// 4. Show / hide toggle
223-
$button.appendChild(this.createHeadingText($span))
215+
$button.appendChild(this.createHeadingText(section.$buttonPlaceholder))
224216
$button.appendChild(this.getButtonPunctuationEl())
225217

226218
// If summary content exists add to DOM in correct order
@@ -234,17 +226,17 @@ export class Accordion extends ConfigurableComponent {
234226

235227
$button.appendChild(section.$toggle)
236228

237-
$heading.removeChild($span)
229+
$heading.removeChild(section.$buttonPlaceholder)
238230
$heading.appendChild($button)
239231
}
240232

241233
/**
242234
* Creates the `<span>` containing the text of the section's heading
243235
*
244-
* @param {Element} $span - The heading of the span
236+
* @param {Element} $buttonPlaceholder - The heading of the span
245237
* @returns {HTMLSpanElement} - The `<span>` containing the text of the section's heading
246238
*/
247-
createHeadingText($span) {
239+
createHeadingText($buttonPlaceholder) {
248240
// Create an inner heading text container to limit the width of the focus
249241
// state
250242
const $headingTextFocus = createElement(
@@ -254,15 +246,15 @@ export class Accordion extends ConfigurableComponent {
254246
},
255247
// span could contain HTML elements which need moving to the new span
256248
// (see https://www.w3.org/TR/2011/WD-html5-20110525/content-models.html#phrasing-content)
257-
Array.from($span.childNodes)
249+
Array.from($buttonPlaceholder.childNodes)
258250
)
259251

260252
// Create container for heading text so it can be styled
261253
const $headingText = createElement(
262254
'span',
263255
{
264256
class: sectionHeadingTextClass,
265-
id: $span.id
257+
id: $buttonPlaceholder.id
266258
},
267259
[$headingTextFocus]
268260
)

packages/govuk-frontend/src/govuk/components/accordion/accordion.puppeteer.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,7 @@ describe('/components/accordion', () => {
836836
cause: {
837837
name: 'ElementError',
838838
message:
839-
'govuk-accordion: Section button placeholder (`<span class="govuk-accordion__section-button">`) not found'
839+
'govuk-accordion-section: Section button placeholder (`<span class="govuk-accordion__section-button">`) not found'
840840
}
841841
})
842842
})

0 commit comments

Comments
 (0)