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

Rework accordion anchor link nav to allow for colon anchors #1974

Merged
merged 1 commit into from
Mar 16, 2021
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
## Unreleased

* Summary list changes ([PR #1971](https://github.com/alphagov/govuk_publishing_components/pull/1971))
* Rework accordion anchor link nav to allow for colon anchors ([PR #1974](https://github.com/alphagov/govuk_publishing_components/pull/1974))

## 24.5.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,10 @@ window.GOVUK.Modules = window.GOVUK.Modules || {};

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

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

Expand All @@ -305,20 +307,20 @@ window.GOVUK.Modules = window.GOVUK.Modules || {};

links.forEach(function (link) {
if (link.pathname === window.location.pathname) {
link.addEventListener('click', this.openForAnchor.bind(this, link.hash))
link.addEventListener('click', this.openForAnchor.bind(this, link.hash.split('#')[1]))
}
}.bind(this))
}

// Find the parent accordion section for the given id and open it
GemAccordion.prototype.openForAnchor = function (hash) {
var target = document.querySelector(hash)
var target = document.getElementById(hash)
var section = this.getContainingSection(target)

this.setExpanded(true, section)
}

// Loop through the given ids ancestors until the parent section class is found
// Loop through the given id's ancestors until the parent section class is found
GemAccordion.prototype.getContainingSection = function (target) {
while (!target.classList.contains(this.sectionClass)) {
target = target.parentElement
Expand Down