Skip to content

Commit fc5ff7a

Browse files
author
JoelCDL
committed
Update headernav popover js
Import popover support function, don’t set anchor-name and position-anchor attributes (since css now uses anchor-scope), add button role to subnav link for more intuitive screen reader accessibility, rename subhav link variable and clarify code comments.
1 parent 2150f5c commit fc5ff7a

File tree

1 file changed

+16
-20
lines changed

1 file changed

+16
-20
lines changed

src/js/headernav.js

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,51 @@
11
// Headernav Component //
22

3+
import { popoverSupport } from './popover-support'
34
const headerNav = document.querySelector('.js-headernav')
45
const subNavs = headerNav.querySelectorAll(':scope > ul > li:has(> ul)')
56
const headerNavMediaQuery = window.matchMedia('(min-width: 760px)')
6-
let counter = 1
77

8-
if (document.querySelector('.c-headernav')) {
8+
if (headerNav && popoverSupport()) {
99
for (const subNav of subNavs) {
10-
const subNavSiblingLink = subNav.querySelector('a')
10+
const subNavLink = subNav.querySelector('a')
1111
const subNavPopover = subNav.querySelector('ul')
12-
subNavPopover.popover = ''
13-
14-
// Anchor each sibling link to its popover using unique anchor names:
15-
const anchorName = '--anchor' + counter++
16-
17-
// 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:
18-
subNavSiblingLink.setAttribute('style', 'anchor-name: ' + anchorName)
19-
subNavPopover.setAttribute('style', 'position-anchor: ' + anchorName)
12+
subNavPopover.popover = 'auto'
13+
subNavLink.setAttribute('role', 'button')
2014

2115
const headerNavToggles = mq => {
16+
// Only a <button> as a popover invoker has built-in accessiblity bindings, so set and toggle the link's aria expanded state:
2217
const expandedState = () => {
2318
if (subNavPopover.matches(':popover-open')) {
24-
subNavSiblingLink.setAttribute('aria-expanded', 'true')
19+
subNavLink.setAttribute('aria-expanded', 'true')
2520
} else {
26-
subNavSiblingLink.setAttribute('aria-expanded', 'false')
21+
subNavLink.setAttribute('aria-expanded', 'false')
2722
}
2823
}
2924

3025
if (mq.matches) {
31-
// Only a <button> as a popover control has built-in accessiblity bindings, so set and toggle sibling link aria expanded state:
32-
subNavSiblingLink.setAttribute('aria-expanded', 'false') // initial state
26+
// Initial state:
27+
expandedState()
3328

34-
// Show/hide subnav on mouse pointer over and out:
29+
// Show subnav on mouseover:
3530
subNav.addEventListener('mouseover', () => {
3631
subNavPopover.showPopover()
3732
expandedState()
3833
})
3934

35+
// Hide subnav on mouseout:
4036
subNav.addEventListener('mouseout', () => {
4137
subNavPopover.hidePopover()
4238
expandedState()
4339
})
4440

45-
// Toggle subnav if sibling link is clicked by keyboard return key:
46-
subNavSiblingLink.addEventListener('click', (e) => {
41+
// Toggle subnav if link clicked:
42+
subNavLink.addEventListener('click', (e) => {
4743
subNavPopover.togglePopover()
48-
e.preventDefault() // disable link from going to its URL when clicked
4944
expandedState()
45+
e.preventDefault() // disable link from going to its URL when clicked
5046
})
5147

52-
// Hide subnav when keyboard focus leaves its popover control:
48+
// Hide previously opened subnav when keyboard focus leaves it:
5349
subNav.addEventListener('focusout', (e) => {
5450
if (!e.currentTarget.contains(e.relatedTarget)) {
5551
subNavPopover.hidePopover()

0 commit comments

Comments
 (0)