-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace header nav logic and styles with popover equivalents
- Loading branch information
Showing
3 changed files
with
102 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,65 @@ | ||
// Headernav Component: | ||
// Headernav Component // | ||
|
||
const headerNav = document.querySelector('.js-headernav') | ||
const subNavs = headerNav.querySelectorAll(':scope > ul > li:has(> ul)') | ||
const headerNavMediaQuery = window.matchMedia('(min-width: 760px)') | ||
let counter = 1 | ||
|
||
function clickLink (event) { | ||
if (this.parentElement.classList.contains('open') === false) { | ||
this.parentElement.classList.add('open') | ||
this.setAttribute('aria-expanded', 'true') | ||
} else { | ||
this.parentElement.classList.remove('open') | ||
this.setAttribute('aria-expanded', 'false') | ||
} | ||
event.preventDefault() | ||
} | ||
if (document.querySelector('.c-headernav')) { | ||
for (const subNav of subNavs) { | ||
const subNavSiblingLink = subNav.querySelector('a') | ||
const subNavPopover = subNav.querySelector('ul') | ||
subNavPopover.popover = '' | ||
|
||
// Anchor each sibling link to its popover using unique anchor names: | ||
const anchorName = '--anchor' + counter++ | ||
|
||
// The properties 'anchor-name' and 'position-anchor' can't be set using style.setProperty in browsers that don't support them. Instead, use setAttribute to force them to appear in so that the anchor positioning polyfill sees them: | ||
subNavSiblingLink.setAttribute('style', 'anchor-name: ' + anchorName) | ||
subNavPopover.setAttribute('style', 'position-anchor: ' + anchorName) | ||
|
||
const headerNavToggles = mq => { | ||
const expandedState = () => { | ||
if (subNavPopover.matches(':popover-open')) { | ||
subNavSiblingLink.setAttribute('aria-expanded', 'true') | ||
} else { | ||
subNavSiblingLink.setAttribute('aria-expanded', 'false') | ||
} | ||
} | ||
|
||
if (mq.matches) { | ||
// Only a <button> as a popover control has built-in accessiblity bindings, so set and toggle sibling link aria expanded state: | ||
subNavSiblingLink.setAttribute('aria-expanded', 'false') // initial state | ||
|
||
const headerNavToggles = e => { | ||
if (document.querySelector('.c-headernav') && e.matches) { | ||
const allMenuItems = document.querySelectorAll('.c-headernav > ul > li'); | ||
|
||
[].forEach.call(allMenuItems, function (el) { | ||
document.querySelector('.c-headernav').classList.remove('c-headernav-no-js') | ||
document.querySelector('.c-headernav').classList.add('c-headernav-js') | ||
el.querySelector('a').setAttribute('aria-haspopup', 'true') | ||
el.querySelector('a').setAttribute('aria-expanded', 'false') | ||
|
||
el.addEventListener('mouseover', function (event) { | ||
this.classList.add('open') | ||
this.querySelector('a').setAttribute('aria-expanded', 'true') | ||
}) | ||
|
||
el.addEventListener('mouseout', function (event) { | ||
this.classList.remove('open') | ||
this.querySelector('a').setAttribute('aria-expanded', 'false') | ||
}) | ||
|
||
el.querySelector('a').addEventListener('click', clickLink) | ||
}); | ||
|
||
[].forEach.call(allMenuItems, function (el) { | ||
el.querySelector('a').addEventListener('focus', function (event) { | ||
[].forEach.call( | ||
allMenuItems, | ||
function (el) { | ||
if (el !== this.parentElement) { | ||
el.classList.remove('open') | ||
el.querySelector('a').setAttribute('aria-expanded', 'false') | ||
} | ||
}, this | ||
) | ||
}) | ||
}) | ||
} else { | ||
const allMenuItems = document.querySelectorAll('.c-headernav > ul > li'); | ||
|
||
[].forEach.call(allMenuItems, function (el) { | ||
el.querySelector('a').removeEventListener('click', clickLink) | ||
}) | ||
// Show/hide subnav on mouse pointer over and out: | ||
subNav.addEventListener('mouseover', () => { | ||
subNavPopover.showPopover() | ||
expandedState() | ||
}) | ||
|
||
subNav.addEventListener('mouseout', () => { | ||
subNavPopover.hidePopover() | ||
expandedState() | ||
}) | ||
|
||
// Toggle subnav if sibling link is clicked by keyboard return key: | ||
subNavSiblingLink.addEventListener('click', (e) => { | ||
subNavPopover.togglePopover() | ||
e.preventDefault() // disable link from going to its URL when clicked | ||
expandedState() | ||
}) | ||
|
||
// Hide subnav when keyboard focus leaves its popover control: | ||
subNav.addEventListener('focusout', (e) => { | ||
if (!e.currentTarget.contains(e.relatedTarget)) { | ||
subNavPopover.hidePopover() | ||
expandedState() | ||
} | ||
}) | ||
} | ||
} | ||
|
||
headerNavMediaQuery.addEventListener('change', headerNavToggles) | ||
headerNavToggles(headerNavMediaQuery) | ||
} | ||
} | ||
|
||
headerNavMediaQuery.addEventListener('change', headerNavToggles) | ||
headerNavToggles(headerNavMediaQuery) |