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;
|
||||
};
|
||||
|
||||
@@ -1,89 +0,0 @@
|
||||
( 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;
|
||||
}
|
||||
|
||||
const forms = [ ...doc.querySelectorAll( '[data-chrnx-search]' ) ];
|
||||
const controls = forms
|
||||
.map( ( form ) => ( {
|
||||
form,
|
||||
input: form.querySelector( 'input[name="s"]' ),
|
||||
panel: form.querySelector( '[data-chrnx-search-panel]' ),
|
||||
button: form.querySelector( '[data-chrnx-search-button]' ),
|
||||
} ) )
|
||||
.filter( ( control ) => control.input && control.panel && control.button );
|
||||
if ( controls.length === 0 ) {
|
||||
cleanupCurrent = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
const setExpanded = ( control, expanded, restoreFocus = false ) => {
|
||||
control.form.dataset.expanded = String( expanded );
|
||||
control.panel.hidden = ! expanded;
|
||||
control.button.setAttribute( 'aria-expanded', String( expanded ) );
|
||||
control.button.setAttribute(
|
||||
'aria-label',
|
||||
expanded ? control.button.dataset.labelSubmit : control.button.dataset.labelOpen,
|
||||
);
|
||||
if ( expanded ) {
|
||||
control.input.focus();
|
||||
} else if ( restoreFocus ) {
|
||||
control.button.focus();
|
||||
}
|
||||
};
|
||||
const onSubmit = ( event ) => {
|
||||
const control = controls.find( ( candidate ) => candidate.form === event.currentTarget );
|
||||
if ( ! control.input.value.trim() ) {
|
||||
event.preventDefault();
|
||||
setExpanded( control, true );
|
||||
}
|
||||
};
|
||||
const onKeydown = ( event ) => {
|
||||
if ( event.key !== 'Escape' ) {
|
||||
return;
|
||||
}
|
||||
const control = controls.find(
|
||||
( candidate ) =>
|
||||
candidate.form.dataset.expanded === 'true' && candidate.form.contains( event.target ),
|
||||
);
|
||||
if ( control ) {
|
||||
event.preventDefault();
|
||||
setExpanded( control, false, true );
|
||||
}
|
||||
};
|
||||
|
||||
controls.forEach( ( control ) => {
|
||||
setExpanded( control, false );
|
||||
control.form.addEventListener( 'submit', onSubmit );
|
||||
} );
|
||||
doc.addEventListener( 'keydown', onKeydown );
|
||||
|
||||
cleanupCurrent = () => {
|
||||
controls.forEach( ( control ) => control.form.removeEventListener( 'submit', onSubmit ) );
|
||||
doc.removeEventListener( 'keydown', onKeydown );
|
||||
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();
|
||||
}
|
||||
}
|
||||
} )();
|
||||
@@ -8,6 +8,5 @@
|
||||
"description": "Display translated primary navigation and progressive site search.",
|
||||
"textdomain": "chrnx",
|
||||
"supports": { "html": false },
|
||||
"viewScript": "file:../../assets/js/search.js",
|
||||
"render": "file:./render.php"
|
||||
}
|
||||
+331
-52
@@ -4,8 +4,16 @@ function createMedia( matches = true ) {
|
||||
const listeners = new Set();
|
||||
return {
|
||||
matches,
|
||||
addEventListener: jest.fn( ( _type, listener ) => listeners.add( listener ) ),
|
||||
removeEventListener: jest.fn( ( _type, listener ) => listeners.delete( listener ) ),
|
||||
addEventListener: jest.fn( ( type, listener ) => {
|
||||
if ( type === 'change' ) {
|
||||
listeners.add( listener );
|
||||
}
|
||||
} ),
|
||||
removeEventListener: jest.fn( ( type, listener ) => {
|
||||
if ( type === 'change' ) {
|
||||
listeners.delete( listener );
|
||||
}
|
||||
} ),
|
||||
change( next ) {
|
||||
this.matches = next;
|
||||
listeners.forEach( ( listener ) => listener( { matches: next } ) );
|
||||
@@ -16,83 +24,354 @@ function createMedia( matches = true ) {
|
||||
|
||||
function shellMarkup() {
|
||||
document.body.innerHTML = `
|
||||
<button hidden data-chrnx-menu-toggle aria-controls="chrnx-primary-navigation"
|
||||
aria-expanded="false" data-label-open="Open primary navigation"
|
||||
data-label-close="Close primary navigation">Open primary navigation</button>
|
||||
<nav id="chrnx-primary-navigation"><a href="/journal/">Journal</a></nav>
|
||||
<input aria-label="Search">
|
||||
<header data-chrnx-header-shell>
|
||||
<a href="/" class="chrnx-site-brand" data-part="brand">CHRNX</a>
|
||||
<button hidden data-part="menu" data-chrnx-menu-toggle
|
||||
aria-controls="chrnx-primary-navigation" aria-expanded="false"
|
||||
data-label-open="Open primary navigation"
|
||||
data-label-close="Close primary navigation">Menu</button>
|
||||
<nav id="chrnx-primary-navigation" data-part="nav">
|
||||
<a href="/journal/">Journal</a><a href="/projects/">Projects</a>
|
||||
</nav>
|
||||
<form data-part="search" data-chrnx-search action="/" method="get"
|
||||
role="search" data-expanded="true">
|
||||
<div id="chrnx-search-panel" data-chrnx-search-panel>
|
||||
<label for="chrnx-search-input">Search</label>
|
||||
<input id="chrnx-search-input" name="s" type="search">
|
||||
</div>
|
||||
<button type="submit" data-chrnx-search-button data-label-open="Open search"
|
||||
data-label-submit="Search" aria-controls="chrnx-search-panel"
|
||||
aria-expanded="true">Search</button>
|
||||
</form>
|
||||
<div data-part="theme" class="chrnx-theme-control">
|
||||
<button hidden type="button" data-chrnx-theme-button
|
||||
aria-controls="chrnx-theme-panel" aria-expanded="false">Theme</button>
|
||||
<fieldset id="chrnx-theme-panel" data-chrnx-theme-panel data-chrnx-theme-toggle>
|
||||
<label><input type="radio" name="theme" value="light"> Light</label>
|
||||
<label><input type="radio" name="theme" value="dark"> Dark</label>
|
||||
<label><input type="radio" name="theme" value="system" checked> System</label>
|
||||
</fieldset>
|
||||
</div>
|
||||
</header>
|
||||
<button data-outside>Outside</button>
|
||||
`;
|
||||
}
|
||||
|
||||
describe( 'progressive mobile navigation', () => {
|
||||
function controls() {
|
||||
return {
|
||||
shell: document.querySelector( '[data-chrnx-header-shell]' ),
|
||||
brand: document.querySelector( '[data-part="brand"]' ),
|
||||
menuButton: document.querySelector( '[data-chrnx-menu-toggle]' ),
|
||||
nav: document.querySelector( '[data-part="nav"]' ),
|
||||
firstLink: document.querySelector( '[data-part="nav"] a' ),
|
||||
search: document.querySelector( '[data-chrnx-search]' ),
|
||||
searchPanel: document.querySelector( '[data-chrnx-search-panel]' ),
|
||||
searchInput: document.querySelector( 'input[name="s"]' ),
|
||||
searchButton: document.querySelector( '[data-chrnx-search-button]' ),
|
||||
theme: document.querySelector( '[data-part="theme"]' ),
|
||||
themeButton: document.querySelector( '[data-chrnx-theme-button]' ),
|
||||
themePanel: document.querySelector( '[data-chrnx-theme-panel]' ),
|
||||
checkedRadio: document.querySelector( '[data-chrnx-theme-panel] input:checked' ),
|
||||
firstRadio: document.querySelector( '[data-chrnx-theme-panel] input' ),
|
||||
outside: document.querySelector( '[data-outside]' ),
|
||||
};
|
||||
}
|
||||
|
||||
function directOrder( shell ) {
|
||||
return [ ...shell.children ].map( ( element ) => element.dataset.part );
|
||||
}
|
||||
|
||||
function submit( form ) {
|
||||
const event = new Event( 'submit', { bubbles: true, cancelable: true } );
|
||||
form.dispatchEvent( event );
|
||||
return event;
|
||||
}
|
||||
|
||||
function press( target, key ) {
|
||||
const event = new window.KeyboardEvent( 'keydown', {
|
||||
key,
|
||||
bubbles: true,
|
||||
cancelable: true,
|
||||
} );
|
||||
target.dispatchEvent( event );
|
||||
return event;
|
||||
}
|
||||
|
||||
function visibleFocusableElements() {
|
||||
return [ ...document.querySelectorAll( 'a[href], button, input' ) ].filter( ( element ) => {
|
||||
if ( element.hidden || element.closest( '[hidden]' ) ) {
|
||||
return false;
|
||||
}
|
||||
if ( element.type !== 'radio' || element.checked ) {
|
||||
return true;
|
||||
}
|
||||
return ! document.querySelector( `input[type="radio"][name="${ element.name }"]:checked` );
|
||||
} );
|
||||
}
|
||||
|
||||
function nextVisibleFocusable( element ) {
|
||||
const focusables = visibleFocusableElements();
|
||||
return focusables[ focusables.indexOf( element ) + 1 ];
|
||||
}
|
||||
|
||||
function activePanelCount( control ) {
|
||||
return [ control.searchButton, control.themeButton, control.menuButton ].filter(
|
||||
( button ) => button.getAttribute( 'aria-expanded' ) === 'true',
|
||||
).length;
|
||||
}
|
||||
|
||||
describe( 'coordinated CHRNX header shell', () => {
|
||||
beforeEach( () => {
|
||||
jest.resetModules();
|
||||
document.documentElement.className = '';
|
||||
shellMarkup();
|
||||
} );
|
||||
|
||||
test( 'mobile toggle opens one target without intercepting Tab', () => {
|
||||
test( 'mobile order is brand, Search, Theme, Menu, then Navigation', () => {
|
||||
const { initialize } = require( modulePath );
|
||||
const media = createMedia( true );
|
||||
expect( initialize( { document, media } ) ).toBe( true );
|
||||
const button = document.querySelector( '[data-chrnx-menu-toggle]' );
|
||||
const nav = document.querySelector( 'nav' );
|
||||
expect( button.hidden ).toBe( false );
|
||||
expect( nav.hidden ).toBe( true );
|
||||
button.click();
|
||||
expect( nav.hidden ).toBe( false );
|
||||
expect( button.getAttribute( 'aria-expanded' ) ).toBe( 'true' );
|
||||
expect( button.getAttribute( 'aria-label' ) ).toBe( 'Close primary navigation' );
|
||||
const tabEvent = new window.KeyboardEvent( 'keydown', {
|
||||
key: 'Tab',
|
||||
bubbles: true,
|
||||
cancelable: true,
|
||||
} );
|
||||
document.dispatchEvent( tabEvent );
|
||||
expect( tabEvent.defaultPrevented ).toBe( false );
|
||||
expect( initialize( { document, media: createMedia( true ) } ) ).toBe( true );
|
||||
const control = controls();
|
||||
expect( directOrder( control.shell ) ).toEqual( [ 'brand', 'search', 'theme', 'menu', 'nav' ] );
|
||||
expect( control.searchPanel.hidden ).toBe( true );
|
||||
expect( control.themePanel.hidden ).toBe( true );
|
||||
expect( control.nav.hidden ).toBe( true );
|
||||
expect( activePanelCount( control ) ).toBe( 0 );
|
||||
} );
|
||||
|
||||
test( 'Escape closes from a descendant and restores trigger focus', () => {
|
||||
test( 'Search, Theme, and Menu are mutually exclusive', () => {
|
||||
const { initialize } = require( modulePath );
|
||||
initialize( { document, media: createMedia( true ) } );
|
||||
const button = document.querySelector( '[data-chrnx-menu-toggle]' );
|
||||
const nav = document.querySelector( 'nav' );
|
||||
button.click();
|
||||
nav.querySelector( 'a' ).focus();
|
||||
nav.dispatchEvent( new window.KeyboardEvent( 'keydown', { key: 'Escape', bubbles: true } ) );
|
||||
expect( nav.hidden ).toBe( true );
|
||||
expect( document.activeElement ).toBe( button );
|
||||
const control = controls();
|
||||
|
||||
submit( control.search );
|
||||
expect( control.searchPanel.hidden ).toBe( false );
|
||||
expect( activePanelCount( control ) ).toBe( 1 );
|
||||
|
||||
control.themeButton.click();
|
||||
expect( control.searchPanel.hidden ).toBe( true );
|
||||
expect( control.themePanel.hidden ).toBe( false );
|
||||
expect( activePanelCount( control ) ).toBe( 1 );
|
||||
|
||||
control.menuButton.click();
|
||||
expect( control.themePanel.hidden ).toBe( true );
|
||||
expect( control.nav.hidden ).toBe( false );
|
||||
expect( activePanelCount( control ) ).toBe( 1 );
|
||||
} );
|
||||
|
||||
test( 'desktop and reinitialization reset state without duplicate listeners', () => {
|
||||
test( 'empty Search opens and focuses while a non-empty GET remains native', () => {
|
||||
const { initialize } = require( modulePath );
|
||||
initialize( { document, media: createMedia( true ) } );
|
||||
const control = controls();
|
||||
expect( control.search.dataset.expanded ).toBe( 'false' );
|
||||
expect( control.searchPanel.hidden ).toBe( true );
|
||||
expect( control.searchButton.getAttribute( 'aria-expanded' ) ).toBe( 'false' );
|
||||
|
||||
const emptyEvent = submit( control.search );
|
||||
expect( emptyEvent.defaultPrevented ).toBe( true );
|
||||
expect( control.search.dataset.expanded ).toBe( 'true' );
|
||||
expect( control.searchPanel.hidden ).toBe( false );
|
||||
expect( control.searchButton.getAttribute( 'aria-expanded' ) ).toBe( 'true' );
|
||||
expect( control.searchButton.getAttribute( 'aria-label' ) ).toBe( 'Search' );
|
||||
expect( document.activeElement ).toBe( control.searchInput );
|
||||
|
||||
control.searchInput.value = 'cat notes';
|
||||
expect( submit( control.search ).defaultPrevented ).toBe( false );
|
||||
} );
|
||||
|
||||
test( 'Escape closes Search and restores its trigger', () => {
|
||||
const { initialize } = require( modulePath );
|
||||
initialize( { document, media: createMedia( true ) } );
|
||||
const control = controls();
|
||||
submit( control.search );
|
||||
press( control.searchInput, 'Escape' );
|
||||
expect( control.search.dataset.expanded ).toBe( 'false' );
|
||||
expect( control.searchPanel.hidden ).toBe( true );
|
||||
expect( control.searchButton.getAttribute( 'aria-expanded' ) ).toBe( 'false' );
|
||||
expect( control.searchButton.getAttribute( 'aria-label' ) ).toBe( 'Open search' );
|
||||
expect( document.activeElement ).toBe( control.searchButton );
|
||||
} );
|
||||
|
||||
test( 'Theme and Menu retain trigger focus and precede their panel contents', () => {
|
||||
const { initialize } = require( modulePath );
|
||||
initialize( { document, media: createMedia( true ) } );
|
||||
const control = controls();
|
||||
|
||||
control.themeButton.focus();
|
||||
control.themeButton.click();
|
||||
expect( document.activeElement ).toBe( control.themeButton );
|
||||
expect( nextVisibleFocusable( control.themeButton ) ).toBe( control.checkedRadio );
|
||||
|
||||
control.menuButton.focus();
|
||||
control.menuButton.click();
|
||||
expect( document.activeElement ).toBe( control.menuButton );
|
||||
expect( nextVisibleFocusable( control.menuButton ) ).toBe( control.firstLink );
|
||||
} );
|
||||
|
||||
test.each( [
|
||||
[ 'Theme', 'themeButton', 'themePanel' ],
|
||||
[ 'Menu', 'menuButton', 'nav' ],
|
||||
] )( 'Escape closes %s and restores its trigger', ( _name, buttonName, panelName ) => {
|
||||
const { initialize } = require( modulePath );
|
||||
initialize( { document, media: createMedia( true ) } );
|
||||
const control = controls();
|
||||
control[ buttonName ].click();
|
||||
control[ panelName ].querySelector( 'input, a' ).focus();
|
||||
press( control[ panelName ], 'Escape' );
|
||||
expect( control[ panelName ].hidden ).toBe( true );
|
||||
expect( document.activeElement ).toBe( control[ buttonName ] );
|
||||
} );
|
||||
|
||||
test( 'outside pointer and focus close Theme without stealing destination focus', () => {
|
||||
const { initialize } = require( modulePath );
|
||||
initialize( { document, media: createMedia( true ) } );
|
||||
const control = controls();
|
||||
|
||||
control.themeButton.click();
|
||||
control.outside.focus();
|
||||
control.outside.dispatchEvent( new Event( 'pointerdown', { bubbles: true } ) );
|
||||
expect( control.themePanel.hidden ).toBe( true );
|
||||
expect( document.activeElement ).toBe( control.outside );
|
||||
|
||||
control.themeButton.click();
|
||||
control.outside.focus();
|
||||
control.outside.dispatchEvent( new Event( 'focusin', { bubbles: true } ) );
|
||||
expect( control.themePanel.hidden ).toBe( true );
|
||||
expect( document.activeElement ).toBe( control.outside );
|
||||
} );
|
||||
|
||||
test( 'Tab is never prevented', () => {
|
||||
const { initialize } = require( modulePath );
|
||||
initialize( { document, media: createMedia( true ) } );
|
||||
const control = controls();
|
||||
control.themeButton.click();
|
||||
expect( press( control.themeButton, 'Tab' ).defaultPrevented ).toBe( false );
|
||||
} );
|
||||
|
||||
test( '599 to 600 moves focus only when the focused control becomes hidden', () => {
|
||||
const { initialize } = require( modulePath );
|
||||
const media = createMedia( true );
|
||||
initialize( { document, media } );
|
||||
initialize( { document, media } );
|
||||
expect( media.listenerCount() ).toBe( 1 );
|
||||
let control = controls();
|
||||
|
||||
submit( control.search );
|
||||
control.searchInput.focus();
|
||||
media.change( false );
|
||||
expect( document.querySelector( 'nav' ).hidden ).toBe( false );
|
||||
expect( document.querySelector( '[data-chrnx-menu-toggle]' ).hidden ).toBe( true );
|
||||
expect( document.documentElement.classList ).toContain( 'chrnx-navigation-enhanced' );
|
||||
expect( document.activeElement ).toBe( control.searchButton );
|
||||
|
||||
media.change( true );
|
||||
control = controls();
|
||||
control.menuButton.focus();
|
||||
media.change( false );
|
||||
expect( document.activeElement ).toBe( control.firstLink );
|
||||
|
||||
media.change( true );
|
||||
control.themeButton.focus();
|
||||
media.change( false );
|
||||
expect( document.activeElement ).toBe( control.checkedRadio );
|
||||
} );
|
||||
|
||||
test( 'resize moves focus out of a navigation that becomes hidden', () => {
|
||||
test( '599 to 600 preserves focus on a navigation link or visible radio', () => {
|
||||
const { initialize } = require( modulePath );
|
||||
const media = createMedia( true );
|
||||
initialize( { document, media } );
|
||||
const control = controls();
|
||||
|
||||
control.menuButton.click();
|
||||
control.firstLink.focus();
|
||||
media.change( false );
|
||||
expect( document.activeElement ).toBe( control.firstLink );
|
||||
|
||||
media.change( true );
|
||||
control.themeButton.click();
|
||||
control.checkedRadio.focus();
|
||||
media.change( false );
|
||||
expect( document.activeElement ).toBe( control.checkedRadio );
|
||||
} );
|
||||
|
||||
test( '600 to 599 moves focus from newly hidden desktop panels to their triggers', () => {
|
||||
const { initialize } = require( modulePath );
|
||||
const media = createMedia( false );
|
||||
initialize( { document, media } );
|
||||
const button = document.querySelector( '[data-chrnx-menu-toggle]' );
|
||||
const nav = document.querySelector( 'nav' );
|
||||
nav.querySelector( 'a' ).focus();
|
||||
const control = controls();
|
||||
|
||||
control.firstLink.focus();
|
||||
media.change( true );
|
||||
expect( nav.hidden ).toBe( true );
|
||||
expect( button.hidden ).toBe( false );
|
||||
expect( document.activeElement ).toBe( button );
|
||||
expect( document.activeElement ).toBe( control.menuButton );
|
||||
|
||||
button.click();
|
||||
nav.querySelector( 'a' ).focus();
|
||||
media.change( false );
|
||||
expect( nav.hidden ).toBe( false );
|
||||
expect( button.hidden ).toBe( true );
|
||||
expect( document.activeElement ).toBe( nav.querySelector( 'a' ) );
|
||||
control.checkedRadio.focus();
|
||||
media.change( true );
|
||||
expect( document.activeElement ).toBe( control.themeButton );
|
||||
expect( document.activeElement.hidden ).toBe( false );
|
||||
expect( document.activeElement.closest( '[hidden]' ) ).toBeNull();
|
||||
} );
|
||||
|
||||
test( 'desktop restores server order and progressive visibility', () => {
|
||||
const { initialize } = require( modulePath );
|
||||
initialize( { document, media: createMedia( false ) } );
|
||||
const control = controls();
|
||||
expect( directOrder( control.shell ) ).toEqual( [ 'brand', 'menu', 'nav', 'search', 'theme' ] );
|
||||
expect( control.nav.hidden ).toBe( false );
|
||||
expect( control.themePanel.hidden ).toBe( false );
|
||||
expect( control.menuButton.hidden ).toBe( true );
|
||||
expect( control.themeButton.hidden ).toBe( true );
|
||||
expect( control.searchPanel.hidden ).toBe( true );
|
||||
} );
|
||||
|
||||
test( 'reinitialization leaves one listener set and cleanup restores original DOM', () => {
|
||||
const { initialize } = require( modulePath );
|
||||
const media = createMedia( true );
|
||||
const originalElements = [ ...controls().shell.children ];
|
||||
initialize( { document, media } );
|
||||
initialize( { document, media } );
|
||||
expect( media.listenerCount() ).toBe( 1 );
|
||||
|
||||
const incompleteDocument = document.implementation.createHTMLDocument( 'incomplete' );
|
||||
expect( initialize( { document: incompleteDocument, media } ) ).toBe( false );
|
||||
const control = controls();
|
||||
expect( [ ...control.shell.children ] ).toEqual( originalElements );
|
||||
expect(
|
||||
[ ...control.shell.childNodes ].filter( ( node ) => node.nodeType === 8 ),
|
||||
).toHaveLength( 0 );
|
||||
expect( media.listenerCount() ).toBe( 0 );
|
||||
expect( document.documentElement.classList ).not.toContain( 'chrnx-navigation-enhanced' );
|
||||
expect( control.nav.hidden ).toBe( false );
|
||||
expect( control.searchPanel.hidden ).toBe( false );
|
||||
expect( control.searchButton.getAttribute( 'aria-expanded' ) ).toBe( 'true' );
|
||||
expect( control.themePanel.hidden ).toBe( false );
|
||||
expect( control.menuButton.hidden ).toBe( true );
|
||||
} );
|
||||
|
||||
test.each( [ null, {}, { matches: true, addEventListener() {} } ] )(
|
||||
'fails closed without a complete media API (%p)',
|
||||
( media ) => {
|
||||
const { initialize } = require( modulePath );
|
||||
const originalOrder = [ ...controls().shell.children ];
|
||||
expect( initialize( { document, media } ) ).toBe( false );
|
||||
expect( [ ...controls().shell.children ] ).toEqual( originalOrder );
|
||||
expect( document.documentElement.className ).toBe( '' );
|
||||
},
|
||||
);
|
||||
|
||||
test( 'fails closed when any required header element is missing', () => {
|
||||
const { initialize } = require( modulePath );
|
||||
controls().themeButton.remove();
|
||||
const originalOrder = [ ...controls().shell.children ];
|
||||
expect( initialize( { document, media: createMedia( true ) } ) ).toBe( false );
|
||||
expect( [ ...controls().shell.children ] ).toEqual( originalOrder );
|
||||
expect( document.documentElement.className ).toBe( '' );
|
||||
} );
|
||||
|
||||
test.each( [
|
||||
[ 'brand', '.chrnx-site-brand' ],
|
||||
[ 'navigation link', '[data-part="nav"] a' ],
|
||||
[ 'checked theme radio', '[data-chrnx-theme-panel] input:checked' ],
|
||||
] )( 'fails closed without the required %s', ( _name, selector ) => {
|
||||
const { initialize } = require( modulePath );
|
||||
document.querySelectorAll( selector ).forEach( ( element ) => element.remove() );
|
||||
const originalOrder = [ ...controls().shell.children ];
|
||||
expect( initialize( { document, media: createMedia( true ) } ) ).toBe( false );
|
||||
expect( [ ...controls().shell.children ] ).toEqual( originalOrder );
|
||||
expect( document.documentElement.className ).toBe( '' );
|
||||
} );
|
||||
} );
|
||||
@@ -1,71 +0,0 @@
|
||||
const modulePath = '../../assets/js/search.js';
|
||||
|
||||
function markup() {
|
||||
document.body.innerHTML = `
|
||||
<form data-chrnx-search action="/" role="search" data-expanded="true">
|
||||
<div id="chrnx-search-panel" data-chrnx-search-panel>
|
||||
<label for="chrnx-search-input">Search</label>
|
||||
<input id="chrnx-search-input" name="s" type="search">
|
||||
</div>
|
||||
<button type="submit" data-chrnx-search-button data-label-open="Open search"
|
||||
data-label-submit="Search" aria-controls="chrnx-search-panel"
|
||||
aria-expanded="true">Search</button>
|
||||
</form>
|
||||
`;
|
||||
}
|
||||
|
||||
describe( 'progressive header search', () => {
|
||||
beforeEach( () => {
|
||||
jest.resetModules();
|
||||
document.documentElement.className = 'js';
|
||||
markup();
|
||||
} );
|
||||
|
||||
test( 'first submit expands and focuses the empty search instead of submitting', () => {
|
||||
const { initialize } = require( modulePath );
|
||||
expect( initialize( { document } ) ).toBe( true );
|
||||
const form = document.querySelector( 'form' );
|
||||
const input = document.querySelector( 'input' );
|
||||
const button = document.querySelector( 'button' );
|
||||
const panel = document.querySelector( '[data-chrnx-search-panel]' );
|
||||
expect( panel.hidden ).toBe( true );
|
||||
expect( button.getAttribute( 'aria-expanded' ) ).toBe( 'false' );
|
||||
const event = new Event( 'submit', { bubbles: true, cancelable: true } );
|
||||
form.dispatchEvent( event );
|
||||
expect( event.defaultPrevented ).toBe( true );
|
||||
expect( form.dataset.expanded ).toBe( 'true' );
|
||||
expect( panel.hidden ).toBe( false );
|
||||
expect( button.getAttribute( 'aria-expanded' ) ).toBe( 'true' );
|
||||
expect( document.activeElement ).toBe( input );
|
||||
expect( button.getAttribute( 'aria-label' ) ).toBe( 'Search' );
|
||||
} );
|
||||
|
||||
test( 'a non-empty query submits natively without interception', () => {
|
||||
const { initialize } = require( modulePath );
|
||||
initialize( { document } );
|
||||
const form = document.querySelector( 'form' );
|
||||
const input = document.querySelector( 'input' );
|
||||
form.dispatchEvent( new Event( 'submit', { bubbles: true, cancelable: true } ) );
|
||||
input.value = 'cat notes';
|
||||
const event = new Event( 'submit', { bubbles: true, cancelable: true } );
|
||||
form.dispatchEvent( event );
|
||||
expect( event.defaultPrevented ).toBe( false );
|
||||
} );
|
||||
|
||||
test( 'Escape collapses and restores focus without duplicate listeners', () => {
|
||||
const { initialize } = require( modulePath );
|
||||
initialize( { document } );
|
||||
initialize( { document } );
|
||||
const form = document.querySelector( 'form' );
|
||||
const input = document.querySelector( 'input' );
|
||||
const button = document.querySelector( 'button' );
|
||||
const panel = document.querySelector( '[data-chrnx-search-panel]' );
|
||||
form.dispatchEvent( new Event( 'submit', { bubbles: true, cancelable: true } ) );
|
||||
input.dispatchEvent( new window.KeyboardEvent( 'keydown', { key: 'Escape', bubbles: true } ) );
|
||||
expect( form.dataset.expanded ).toBe( 'false' );
|
||||
expect( panel.hidden ).toBe( true );
|
||||
expect( button.getAttribute( 'aria-expanded' ) ).toBe( 'false' );
|
||||
expect( document.activeElement ).toBe( button );
|
||||
expect( button.getAttribute( 'aria-label' ) ).toBe( 'Open search' );
|
||||
} );
|
||||
} );
|
||||
@@ -14,13 +14,13 @@ test('theme toggle metadata is dynamic, localized, and frontend-only', async ()
|
||||
assert.equal(metadata.script, undefined);
|
||||
});
|
||||
|
||||
test('header controls retain progressive script ownership and panel markup', async () => {
|
||||
test('header controls use one progressive shell script and retain panel markup', async () => {
|
||||
const headerToolsMetadata = JSON.parse(await readProjectFile('blocks/header-tools/block.json'));
|
||||
const mobileMenuMetadata = JSON.parse(await readProjectFile('blocks/mobile-menu-toggle/block.json'));
|
||||
const headerToolsRender = await readProjectFile('blocks/header-tools/render.php');
|
||||
const themeToggleRender = await readProjectFile('blocks/theme-toggle/render.php');
|
||||
|
||||
assert.equal(headerToolsMetadata.viewScript, 'file:../../assets/js/search.js');
|
||||
assert.equal(headerToolsMetadata.viewScript, undefined);
|
||||
assert.equal(mobileMenuMetadata.viewScript, 'file:../../assets/js/navigation.js');
|
||||
assert.match(headerToolsRender, /data-chrnx-search-panel/);
|
||||
assert.match(headerToolsRender, /data-chrnx-search-button[\s\S]+aria-controls=/);
|
||||
|
||||
Reference in new issue
Block a user