Skip to content

Commit

Permalink
Merge pull request #4069 from alphagov/update-accordion-initialisatio…
Browse files Browse the repository at this point in the history
…n-check

Add guard to accordion component init method
  • Loading branch information
MartinJJones authored Jun 20, 2024
2 parents e40e547 + 859c06c commit 33ff5e3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

## Unreleased

* Add guard to accordion component init method ([PR #4069](https://github.com/alphagov/govuk_publishing_components/pull/4069))
* Prevent screen readers from announcing document list child/parts items dashes ([PR #4066](https://github.com/alphagov/govuk_publishing_components/pull/4066))
* Align checkboxes component more toward Design System ([PR #4061](https://github.com/alphagov/govuk_publishing_components/pull/4061))
* Add govuk-frontend checking to the component auditing ([PR #4058](https://github.com/alphagov/govuk_publishing_components/pull/4058))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ window.GOVUK.Modules.GovukAccordion = window.GOVUKFrontend.Accordion;
}

GemAccordion.prototype.init = function () {
// Do not initialise if the accordion component from govuk-frontend has not initialised
if (this.$module.querySelector(this.showAllControls) === null) {
return
}

// Indicate that JavaScript has worked
this.$module.querySelector(this.showAllControls).classList.add('gem-c-accordion__show-all')

Expand Down
10 changes: 9 additions & 1 deletion spec/javascripts/components/accordion-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('Accordion component', function () {
var html =
'<div class="gem-c-accordion" data-show-text="Show" data-hide-text="Hide" data-show-all-text="Show all sections" data-hide-all-text="Hide all sections" data-this-section-visually-hidden=" this section">' +
'<div class="govuk-accordion__controls">' +
'<button type="button" class="govuk-accordion__show-all gem-c-accordion__show-all" aria-expanded="false">' +
'<button type="button" class="govuk-accordion__show-all" aria-expanded="false">' +
'<span class="govuk-accordion__show-all-text">Show all sections</span>' +
'</button>' +
'</div>' +
Expand Down Expand Up @@ -67,6 +67,14 @@ describe('Accordion component', function () {
}
})

it('does not initialise if the accordion from govuk-frontend has not initialised', function () {
var accordionShowAllButton = accordion.querySelector('.govuk-accordion__show-all')
accordionShowAllButton.classList.remove('govuk-accordion__show-all')
startAccordion()

expect(accordion.querySelector('.govuk-accordion__controls button')).not.toHaveClass('gem-c-accordion__show-all')
})

it('applies custom attributes to click event if specified in section', function () {
accordion.setAttribute('data-track-sections', 'true')

Expand Down

0 comments on commit 33ff5e3

Please sign in to comment.