feat: coordinate CHRNX mobile header panels
This commit is contained in:
6 files changed
+536
-262
No files matched your search
+203
-47
@@ -1,6 +1,11 @@
|
||||
( function () {
|
||||
'use strict';
|
||||
|
||||
const PANEL = {
|
||||
search: 'search',
|
||||
theme: 'theme',
|
||||
menu: 'menu',
|
||||
};
|
||||
let cleanupCurrent = null;
|
||||
|
||||
function initialize( options = {} ) {
|
||||
@@ -21,71 +26,222 @@
|
||||
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 ) {
|
||||
if (
|
||||
! media ||
|
||||
typeof media.matches !== 'boolean' ||
|
||||
typeof media.addEventListener !== 'function' ||
|
||||
typeof media.removeEventListener !== 'function'
|
||||
) {
|
||||
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(
|
||||
const menuButton = doc.querySelector( '[data-chrnx-menu-toggle]' );
|
||||
const search = doc.querySelector( '[data-chrnx-search]' );
|
||||
const themeButton = doc.querySelector( '[data-chrnx-theme-button]' );
|
||||
const searchInput = search && search.querySelector( 'input[name="s"]' );
|
||||
const searchButton = search && search.querySelector( '[data-chrnx-search-button]' );
|
||||
const searchPanel =
|
||||
searchButton && doc.getElementById( searchButton.getAttribute( 'aria-controls' ) );
|
||||
const nav = menuButton && doc.getElementById( menuButton.getAttribute( 'aria-controls' ) );
|
||||
const themePanel =
|
||||
themeButton && doc.getElementById( themeButton.getAttribute( 'aria-controls' ) );
|
||||
const theme = themeButton && themeButton.parentElement;
|
||||
const shell = menuButton && menuButton.parentElement;
|
||||
const brand = shell && shell.querySelector( '.chrnx-site-brand' );
|
||||
const navLink = nav && nav.querySelector( 'a[href]' );
|
||||
const checkedRadio = themePanel && themePanel.querySelector( 'input[type="radio"]:checked' );
|
||||
const required = [
|
||||
menuButton,
|
||||
nav,
|
||||
search,
|
||||
searchInput,
|
||||
searchPanel,
|
||||
searchButton,
|
||||
theme,
|
||||
themeButton,
|
||||
themePanel,
|
||||
checkedRadio,
|
||||
brand,
|
||||
navLink,
|
||||
shell,
|
||||
];
|
||||
if (
|
||||
required.some( ( element ) => ! element ) ||
|
||||
searchPanel.parentElement !== search ||
|
||||
! [ brand, nav, search, theme ].every( ( element ) => element.parentElement === shell )
|
||||
) {
|
||||
cleanupCurrent = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
const elements = [ menuButton, nav, search, theme ];
|
||||
const anchors = new Map();
|
||||
const original = new Map();
|
||||
const enhancedClassWasPresent = doc.documentElement.classList.contains(
|
||||
'chrnx-navigation-enhanced',
|
||||
);
|
||||
const remember = ( element, attributes ) => {
|
||||
const state = { hidden: element.hidden, attributes: {} };
|
||||
attributes.forEach( ( attribute ) => {
|
||||
state.attributes[ attribute ] = element.hasAttribute( attribute )
|
||||
? element.getAttribute( attribute )
|
||||
: null;
|
||||
} );
|
||||
original.set( element, state );
|
||||
};
|
||||
remember( menuButton, [ 'aria-expanded', 'aria-label' ] );
|
||||
remember( nav, [] );
|
||||
remember( search, [ 'data-expanded' ] );
|
||||
remember( searchPanel, [] );
|
||||
remember( searchButton, [ 'aria-expanded', 'aria-label' ] );
|
||||
remember( themeButton, [ 'aria-expanded' ] );
|
||||
remember( themePanel, [] );
|
||||
elements.forEach( ( element ) => {
|
||||
const anchor = doc.createComment( 'chrnx-original-position' );
|
||||
element.parentNode.insertBefore( anchor, element );
|
||||
anchors.set( element, anchor );
|
||||
} );
|
||||
|
||||
let mobile = media.matches;
|
||||
let activePanel = null;
|
||||
|
||||
const restoreOriginalOrder = () => {
|
||||
elements.forEach( ( element ) => {
|
||||
const anchor = anchors.get( element );
|
||||
anchor.parentNode.insertBefore( element, anchor.nextSibling );
|
||||
} );
|
||||
};
|
||||
const setSearchState = ( open ) => {
|
||||
search.dataset.expanded = String( open );
|
||||
searchPanel.hidden = ! open;
|
||||
searchButton.setAttribute( 'aria-expanded', String( open ) );
|
||||
searchButton.setAttribute(
|
||||
'aria-label',
|
||||
expanded ? pair.button.dataset.labelClose : pair.button.dataset.labelOpen,
|
||||
open ? searchButton.dataset.labelSubmit : searchButton.dataset.labelOpen,
|
||||
);
|
||||
if ( restoreFocus ) {
|
||||
pair.button.focus();
|
||||
};
|
||||
const synchronize = () => {
|
||||
const searchOpen = activePanel === PANEL.search;
|
||||
const themeOpen = mobile && activePanel === PANEL.theme;
|
||||
const menuOpen = mobile && activePanel === PANEL.menu;
|
||||
setSearchState( searchOpen );
|
||||
themeButton.hidden = ! mobile;
|
||||
themeButton.setAttribute( 'aria-expanded', String( themeOpen ) );
|
||||
themePanel.hidden = mobile && ! themeOpen;
|
||||
menuButton.hidden = ! mobile;
|
||||
menuButton.setAttribute( 'aria-expanded', String( menuOpen ) );
|
||||
menuButton.setAttribute(
|
||||
'aria-label',
|
||||
menuOpen ? menuButton.dataset.labelClose : menuButton.dataset.labelOpen,
|
||||
);
|
||||
nav.hidden = mobile && ! menuOpen;
|
||||
};
|
||||
const setActivePanel = ( name, transitionOptions = {} ) => {
|
||||
activePanel = name;
|
||||
synchronize();
|
||||
if ( name === PANEL.search ) {
|
||||
searchInput.focus();
|
||||
} else if ( transitionOptions.restoreFocus && name === null ) {
|
||||
transitionOptions.trigger.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 applyViewport = ( matches ) => {
|
||||
const focused = doc.activeElement;
|
||||
const focusWasSearch = searchPanel.contains( focused );
|
||||
const focusWasMenuButton = focused === menuButton;
|
||||
const focusWasThemeButton = focused === themeButton;
|
||||
const focusWasNav = nav.contains( focused );
|
||||
const focusWasThemePanel = themePanel.contains( focused );
|
||||
|
||||
mobile = matches;
|
||||
activePanel = null;
|
||||
if ( mobile ) {
|
||||
shell.append( search, theme, menuButton, nav );
|
||||
} else {
|
||||
restoreOriginalOrder();
|
||||
}
|
||||
synchronize();
|
||||
|
||||
if ( ! mobile && focusWasSearch ) {
|
||||
searchButton.focus();
|
||||
} else if ( ! mobile && focusWasMenuButton ) {
|
||||
navLink.focus();
|
||||
} else if ( ! mobile && focusWasThemeButton ) {
|
||||
checkedRadio.focus();
|
||||
} else if ( ! mobile && ( focusWasNav || focusWasThemePanel ) ) {
|
||||
focused.focus();
|
||||
} else if ( mobile && focusWasNav ) {
|
||||
menuButton.focus();
|
||||
} else if ( mobile && focusWasThemePanel ) {
|
||||
themeButton.focus();
|
||||
}
|
||||
};
|
||||
const onKeydown = ( event ) => {
|
||||
if ( event.key !== 'Escape' ) {
|
||||
const closeForOutsideTarget = ( target ) => {
|
||||
if ( activePanel === PANEL.theme && ! theme.contains( target ) ) {
|
||||
setActivePanel( null );
|
||||
}
|
||||
};
|
||||
const onMenuClick = () => {
|
||||
setActivePanel( activePanel === PANEL.menu ? null : PANEL.menu );
|
||||
};
|
||||
const onThemeClick = () => {
|
||||
setActivePanel( activePanel === PANEL.theme ? null : PANEL.theme );
|
||||
};
|
||||
const onSearchSubmit = ( event ) => {
|
||||
if ( searchInput.value.trim() ) {
|
||||
return;
|
||||
}
|
||||
const pair = pairs.find(
|
||||
( candidate ) => candidate.button.getAttribute( 'aria-expanded' ) === 'true',
|
||||
);
|
||||
if ( pair ) {
|
||||
setOpen( pair, false, true );
|
||||
}
|
||||
event.preventDefault();
|
||||
setActivePanel( PANEL.search );
|
||||
};
|
||||
const onKeydown = ( event ) => {
|
||||
if ( event.key !== 'Escape' || ! activePanel ) {
|
||||
return;
|
||||
}
|
||||
event.preventDefault();
|
||||
const trigger = {
|
||||
[ PANEL.search ]: searchButton,
|
||||
[ PANEL.theme ]: themeButton,
|
||||
[ PANEL.menu ]: menuButton,
|
||||
}[ activePanel ];
|
||||
setActivePanel( null, { restoreFocus: true, trigger } );
|
||||
};
|
||||
const onOutside = ( event ) => closeForOutsideTarget( event.target );
|
||||
const onMediaChange = ( event ) => applyViewport( event.matches );
|
||||
|
||||
buttons.forEach( ( button ) => button.addEventListener( 'click', onClick ) );
|
||||
menuButton.addEventListener( 'click', onMenuClick );
|
||||
themeButton.addEventListener( 'click', onThemeClick );
|
||||
search.addEventListener( 'submit', onSearchSubmit );
|
||||
doc.addEventListener( 'keydown', onKeydown );
|
||||
if ( media && typeof media.addEventListener === 'function' ) {
|
||||
media.addEventListener( 'change', reset );
|
||||
}
|
||||
doc.addEventListener( 'pointerdown', onOutside );
|
||||
doc.addEventListener( 'focusin', onOutside );
|
||||
media.addEventListener( 'change', onMediaChange );
|
||||
doc.documentElement.classList.add( 'chrnx-navigation-enhanced' );
|
||||
reset();
|
||||
applyViewport( media.matches );
|
||||
|
||||
cleanupCurrent = () => {
|
||||
buttons.forEach( ( button ) => button.removeEventListener( 'click', onClick ) );
|
||||
menuButton.removeEventListener( 'click', onMenuClick );
|
||||
themeButton.removeEventListener( 'click', onThemeClick );
|
||||
search.removeEventListener( 'submit', onSearchSubmit );
|
||||
doc.removeEventListener( 'keydown', onKeydown );
|
||||
if ( media && typeof media.removeEventListener === 'function' ) {
|
||||
media.removeEventListener( 'change', reset );
|
||||
doc.removeEventListener( 'pointerdown', onOutside );
|
||||
doc.removeEventListener( 'focusin', onOutside );
|
||||
media.removeEventListener( 'change', onMediaChange );
|
||||
restoreOriginalOrder();
|
||||
anchors.forEach( ( anchor ) => anchor.remove() );
|
||||
original.forEach( ( state, element ) => {
|
||||
element.hidden = state.hidden;
|
||||
Object.entries( state.attributes ).forEach( ( [ attribute, value ] ) => {
|
||||
if ( value === null ) {
|
||||
element.removeAttribute( attribute );
|
||||
} else {
|
||||
element.setAttribute( attribute, value );
|
||||
}
|
||||
} );
|
||||
} );
|
||||
if ( ! enhancedClassWasPresent ) {
|
||||
doc.documentElement.classList.remove( 'chrnx-navigation-enhanced' );
|
||||
}
|
||||
cleanupCurrent = null;
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user