-
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 toggle logic with popover api
- Loading branch information
Showing
11 changed files
with
222 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,10 @@ | |
mask: url('data-url:npm:fa-light/angle-right.svg') center / 0.7rem no-repeat; | ||
} | ||
} | ||
|
||
ul { | ||
display: none; | ||
} | ||
} | ||
} | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// ***** Anchor Positioning Polyfill ***** // | ||
|
||
// Required for positioning elements that use the popover API. | ||
|
||
// https://github.com/oddbird/css-anchor-positioning | ||
|
||
const anchorPositioningPolyfill = async () => { | ||
const { default: polyfill } = await import('@oddbird/css-anchor-positioning/dist/css-anchor-positioning-fn.js') | ||
|
||
polyfill({ | ||
elements: undefined, | ||
excludeInlineStyles: false, | ||
useAnimationFrame: false | ||
}) | ||
} | ||
|
||
if (!('anchorName' in document.documentElement.style)) { | ||
anchorPositioningPolyfill() | ||
} |
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) |
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,6 +1,8 @@ | ||
import './anchor-positioning.js' | ||
import './headernav.js' | ||
import './mediaqueries.js' | ||
import './newsreel.js' | ||
import './popover-support.js' | ||
import './sidebarposts.js' | ||
import './slideshow.js' | ||
import './toggles.js' |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// ***** Popover Support for CSS ***** // | ||
|
||
if (window.HTMLElement.prototype.hasOwnProperty('popover')) { // eslint-disable-line no-prototype-builtins | ||
document.querySelector('html').classList.remove('no-popover') | ||
} |
Oops, something went wrong.