|
1 |
| -// Headernav Component: |
| 1 | +// Headernav Component // |
2 | 2 |
|
| 3 | +const headerNav = document.querySelector('.js-headernav') |
| 4 | +const subNavs = headerNav.querySelectorAll(':scope > ul > li:has(> ul)') |
3 | 5 | const headerNavMediaQuery = window.matchMedia('(min-width: 760px)')
|
| 6 | +let counter = 1 |
4 | 7 |
|
5 |
| -function clickLink (event) { |
6 |
| - if (this.parentElement.classList.contains('open') === false) { |
7 |
| - this.parentElement.classList.add('open') |
8 |
| - this.setAttribute('aria-expanded', 'true') |
9 |
| - } else { |
10 |
| - this.parentElement.classList.remove('open') |
11 |
| - this.setAttribute('aria-expanded', 'false') |
12 |
| - } |
13 |
| - event.preventDefault() |
14 |
| -} |
| 8 | +if (document.querySelector('.c-headernav')) { |
| 9 | + for (const subNav of subNavs) { |
| 10 | + const subNavSiblingLink = subNav.querySelector('a') |
| 11 | + 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) |
| 20 | + |
| 21 | + const headerNavToggles = mq => { |
| 22 | + const expandedState = () => { |
| 23 | + if (subNavPopover.matches(':popover-open')) { |
| 24 | + subNavSiblingLink.setAttribute('aria-expanded', 'true') |
| 25 | + } else { |
| 26 | + subNavSiblingLink.setAttribute('aria-expanded', 'false') |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + 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 |
15 | 33 |
|
16 |
| -const headerNavToggles = e => { |
17 |
| - if (document.querySelector('.c-headernav') && e.matches) { |
18 |
| - const allMenuItems = document.querySelectorAll('.c-headernav > ul > li'); |
19 |
| - |
20 |
| - [].forEach.call(allMenuItems, function (el) { |
21 |
| - document.querySelector('.c-headernav').classList.remove('c-headernav-no-js') |
22 |
| - document.querySelector('.c-headernav').classList.add('c-headernav-js') |
23 |
| - el.querySelector('a').setAttribute('aria-haspopup', 'true') |
24 |
| - el.querySelector('a').setAttribute('aria-expanded', 'false') |
25 |
| - |
26 |
| - el.addEventListener('mouseover', function (event) { |
27 |
| - this.classList.add('open') |
28 |
| - this.querySelector('a').setAttribute('aria-expanded', 'true') |
29 |
| - }) |
30 |
| - |
31 |
| - el.addEventListener('mouseout', function (event) { |
32 |
| - this.classList.remove('open') |
33 |
| - this.querySelector('a').setAttribute('aria-expanded', 'false') |
34 |
| - }) |
35 |
| - |
36 |
| - el.querySelector('a').addEventListener('click', clickLink) |
37 |
| - }); |
38 |
| - |
39 |
| - [].forEach.call(allMenuItems, function (el) { |
40 |
| - el.querySelector('a').addEventListener('focus', function (event) { |
41 |
| - [].forEach.call( |
42 |
| - allMenuItems, |
43 |
| - function (el) { |
44 |
| - if (el !== this.parentElement) { |
45 |
| - el.classList.remove('open') |
46 |
| - el.querySelector('a').setAttribute('aria-expanded', 'false') |
47 |
| - } |
48 |
| - }, this |
49 |
| - ) |
50 |
| - }) |
51 |
| - }) |
52 |
| - } else { |
53 |
| - const allMenuItems = document.querySelectorAll('.c-headernav > ul > li'); |
54 |
| - |
55 |
| - [].forEach.call(allMenuItems, function (el) { |
56 |
| - el.querySelector('a').removeEventListener('click', clickLink) |
57 |
| - }) |
| 34 | + // Show/hide subnav on mouse pointer over and out: |
| 35 | + subNav.addEventListener('mouseover', () => { |
| 36 | + subNavPopover.showPopover() |
| 37 | + expandedState() |
| 38 | + }) |
| 39 | + |
| 40 | + subNav.addEventListener('mouseout', () => { |
| 41 | + subNavPopover.hidePopover() |
| 42 | + expandedState() |
| 43 | + }) |
| 44 | + |
| 45 | + // Toggle subnav if sibling link is clicked by keyboard return key: |
| 46 | + subNavSiblingLink.addEventListener('click', (e) => { |
| 47 | + subNavPopover.togglePopover() |
| 48 | + e.preventDefault() // disable link from going to its URL when clicked |
| 49 | + expandedState() |
| 50 | + }) |
| 51 | + |
| 52 | + // Hide subnav when keyboard focus leaves its popover control: |
| 53 | + subNav.addEventListener('focusout', (e) => { |
| 54 | + if (!e.currentTarget.contains(e.relatedTarget)) { |
| 55 | + subNavPopover.hidePopover() |
| 56 | + expandedState() |
| 57 | + } |
| 58 | + }) |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + headerNavMediaQuery.addEventListener('change', headerNavToggles) |
| 63 | + headerNavToggles(headerNavMediaQuery) |
58 | 64 | }
|
59 | 65 | }
|
60 |
| - |
61 |
| -headerNavMediaQuery.addEventListener('change', headerNavToggles) |
62 |
| -headerNavToggles(headerNavMediaQuery) |
|
0 commit comments