fix: make CHRNX header fully progressive

This commit is contained in:
chrnx committed 2026-08-11 17:26:48 -05:00
1 parent e1579657d6
commit f983cec4f9
13 files changed
+414 -36

No files matched your search

+20
View File
@@ -75,4 +75,24 @@ describe( 'progressive mobile navigation', () => {
expect( document.querySelector( '[data-chrnx-menu-toggle]' ).hidden ).toBe( true );
expect( document.documentElement.classList ).toContain( 'chrnx-navigation-enhanced' );
} );
test( 'resize moves focus out of a navigation that becomes hidden', () => {
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();
media.change( true );
expect( nav.hidden ).toBe( true );
expect( button.hidden ).toBe( false );
expect( document.activeElement ).toBe( button );
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' ) );
} );
} );
+62
View File
@@ -0,0 +1,62 @@
const modulePath = '../../assets/js/search.js';
function markup() {
document.body.innerHTML = `
<form data-chrnx-search action="/" role="search">
<label for="chrnx-search-input">Search</label>
<input id="chrnx-search-input" name="s" type="search">
<button type="submit" data-chrnx-search-button data-label-open="Open search"
data-label-submit="Search">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 event = new Event( 'submit', { bubbles: true, cancelable: true } );
form.dispatchEvent( event );
expect( event.defaultPrevented ).toBe( true );
expect( form.dataset.expanded ).toBe( 'true' );
expect( input.hidden ).toBe( false );
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' );
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( input.hidden ).toBe( true );
expect( document.activeElement ).toBe( button );
expect( button.getAttribute( 'aria-label' ) ).toBe( 'Open search' );
} );
} );