Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug when getting target element from URL hash #2985

Merged
merged 1 commit into from
Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
useful summary for people upgrading their application, not a replication
of the commit log.

## Unreleased

* Fix bug when getting target element from URL hash in accordion component ([PR #2985](https://github.com/alphagov/govuk_publishing_components/pull/2985))

## 30.7.3

* Lint ga4-core ([PR #2982](https://github.com/alphagov/govuk_publishing_components/pull/2982))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,9 @@ window.GOVUK.Modules.GovukAccordion = window.GOVUKFrontend.Accordion;

// Navigate to and open accordions with anchored content on page load if a hash is present
GemAccordion.prototype.openByAnchorOnLoad = function () {
if (!window.location.hash) return
var splitHash = window.location.hash.split('#')[1]

if (window.location.hash && document.getElementById(splitHash)) {
this.openForAnchor(splitHash)
}
this.openForAnchor(splitHash)
}

// Add event listeners for links to open accordion sections when navigated to using said anchor links on the page
Expand All @@ -80,7 +78,9 @@ window.GOVUK.Modules.GovukAccordion = window.GOVUKFrontend.Accordion;

// Find the parent accordion section for the given id and open it
GemAccordion.prototype.openForAnchor = function (hash) {
var target = document.getElementById(hash)
hash = hash.replace(':', '\\:')
var target = this.$module.querySelector('#' + hash)
if (!target) return
var $section = this.getContainingSection(target)
var $header = $section.querySelector(this.sectionHeader)
var $expanded = this.getContainingSection($section)
Expand Down
1 change: 1 addition & 0 deletions spec/javascripts/components/accordion-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ describe('Accordion component', function () {
'</button>' +
'</h2>' +
'</div>' +
'</div>' +
'<div class="govuk-accordion__section">' +
'<div class="govuk-accordion__section-header">' +
'<h2 class="govuk-accordion__section-heading">' +
Expand Down