106 lines
3.2 KiB
JavaScript
106 lines
3.2 KiB
JavaScript
( function () {
|
|
'use strict';
|
|
|
|
let cleanupCurrent = null;
|
|
|
|
function initialize( options = {} ) {
|
|
if ( cleanupCurrent ) {
|
|
cleanupCurrent();
|
|
}
|
|
|
|
const doc = options.document || ( typeof document === 'undefined' ? null : document );
|
|
if ( ! doc ) {
|
|
return false;
|
|
}
|
|
|
|
let media = options.media;
|
|
if ( media === undefined ) {
|
|
try {
|
|
media = window.matchMedia( '(max-width: 599px)' );
|
|
} catch {
|
|
media = null;
|
|
}
|
|
}
|
|
|
|
const buttons = [ ...doc.querySelectorAll( '[data-chrnx-menu-toggle]' ) ];
|
|
const pairs = buttons
|
|
.map( ( button ) => ( {
|
|
button,
|
|
target: doc.getElementById( button.getAttribute( 'aria-controls' ) ),
|
|
} ) )
|
|
.filter( ( pair ) => pair.target );
|
|
if ( pairs.length === 0 ) {
|
|
cleanupCurrent = null;
|
|
return false;
|
|
}
|
|
|
|
const isMobile = () => Boolean( media && media.matches );
|
|
const setOpen = ( pair, open, restoreFocus = false ) => {
|
|
const expanded = isMobile() && open;
|
|
pair.target.hidden = isMobile() && ! expanded;
|
|
pair.button.hidden = ! isMobile();
|
|
pair.button.setAttribute( 'aria-expanded', String( expanded ) );
|
|
pair.button.setAttribute(
|
|
'aria-label',
|
|
expanded ? pair.button.dataset.labelClose : pair.button.dataset.labelOpen,
|
|
);
|
|
if ( restoreFocus ) {
|
|
pair.button.focus();
|
|
}
|
|
};
|
|
const reset = () =>
|
|
pairs.forEach( ( pair ) => {
|
|
const wasInsideTarget = pair.target.contains( doc.activeElement );
|
|
const becomingMobile = isMobile();
|
|
setOpen( pair, false );
|
|
if ( becomingMobile && wasInsideTarget ) {
|
|
pair.button.focus();
|
|
}
|
|
} );
|
|
const onClick = ( event ) => {
|
|
const pair = pairs.find( ( candidate ) => candidate.button === event.currentTarget );
|
|
setOpen( pair, pair.button.getAttribute( 'aria-expanded' ) !== 'true' );
|
|
};
|
|
const onKeydown = ( event ) => {
|
|
if ( event.key !== 'Escape' ) {
|
|
return;
|
|
}
|
|
const pair = pairs.find(
|
|
( candidate ) => candidate.button.getAttribute( 'aria-expanded' ) === 'true',
|
|
);
|
|
if ( pair ) {
|
|
setOpen( pair, false, true );
|
|
}
|
|
};
|
|
|
|
buttons.forEach( ( button ) => button.addEventListener( 'click', onClick ) );
|
|
doc.addEventListener( 'keydown', onKeydown );
|
|
if ( media && typeof media.addEventListener === 'function' ) {
|
|
media.addEventListener( 'change', reset );
|
|
}
|
|
doc.documentElement.classList.add( 'chrnx-navigation-enhanced' );
|
|
reset();
|
|
|
|
cleanupCurrent = () => {
|
|
buttons.forEach( ( button ) => button.removeEventListener( 'click', onClick ) );
|
|
doc.removeEventListener( 'keydown', onKeydown );
|
|
if ( media && typeof media.removeEventListener === 'function' ) {
|
|
media.removeEventListener( 'change', reset );
|
|
}
|
|
cleanupCurrent = null;
|
|
};
|
|
return true;
|
|
}
|
|
|
|
const helpers = { initialize };
|
|
if ( typeof module !== 'undefined' && module.exports ) {
|
|
module.exports = helpers;
|
|
} else if ( typeof document !== 'undefined' ) {
|
|
if ( document.readyState === 'loading' ) {
|
|
document.addEventListener( 'DOMContentLoaded', () => initialize(), { once: true } );
|
|
} else {
|
|
initialize();
|
|
}
|
|
}
|
|
} )();
|