From 320da3ef2746d885871eb47074b18f0b40147f65 Mon Sep 17 00:00:00 2001 From: Diego Resendez Date: Wed, 12 Aug 2026 07:38:20 -0500 Subject: [PATCH] fix: harden CHRNX header state transitions --- assets/js/navigation.js | 33 +++++++---- tests/js/navigation.test.js | 112 +++++++++++++++++++++++++++++++++--- 2 files changed, 126 insertions(+), 19 deletions(-) diff --git a/assets/js/navigation.js b/assets/js/navigation.js index e0e276d..67b2bc4 100644 --- a/assets/js/navigation.js +++ b/assets/js/navigation.js @@ -50,7 +50,7 @@ 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 themeRadio = themePanel && themePanel.querySelector( 'input[type="radio"]' ); const required = [ menuButton, nav, @@ -61,7 +61,7 @@ theme, themeButton, themePanel, - checkedRadio, + themeRadio, brand, navLink, shell, @@ -106,6 +106,8 @@ let mobile = media.matches; let activePanel = null; + const currentThemeRadio = () => + themePanel.querySelector( 'input[type="radio"]:checked' ) || themeRadio; const restoreOriginalOrder = () => { elements.forEach( ( element ) => { const anchor = anchors.get( element ); @@ -168,7 +170,7 @@ } else if ( ! mobile && focusWasMenuButton ) { navLink.focus(); } else if ( ! mobile && focusWasThemeButton ) { - checkedRadio.focus(); + currentThemeRadio().focus(); } else if ( ! mobile && ( focusWasNav || focusWasThemePanel ) ) { focused.focus(); } else if ( mobile && focusWasNav ) { @@ -177,11 +179,6 @@ themeButton.focus(); } }; - const closeForOutsideTarget = ( target ) => { - if ( activePanel === PANEL.theme && ! theme.contains( target ) ) { - setActivePanel( null ); - } - }; const focusSafely = ( element ) => { try { element.focus( { preventScroll: true } ); @@ -189,6 +186,20 @@ element.focus(); } }; + const closeForOutsideTarget = ( target ) => { + if ( activePanel !== PANEL.theme || theme.contains( target ) ) { + return; + } + const focused = doc.activeElement; + const targetOwnsFocus = + focused !== doc.body && + focused !== doc.documentElement && + ( focused === target || focused.contains( target ) || target.contains( focused ) ); + if ( ! targetOwnsFocus ) { + focusSafely( themeButton ); + } + setActivePanel( null ); + }; const onMenuClick = () => { focusSafely( menuButton ); setActivePanel( activePanel === PANEL.menu ? null : PANEL.menu ); @@ -198,7 +209,7 @@ setActivePanel( activePanel === PANEL.theme ? null : PANEL.theme ); }; const onSearchSubmit = ( event ) => { - if ( searchInput.value.trim() ) { + if ( activePanel === PANEL.search && searchInput.value.trim() ) { return; } event.preventDefault(); @@ -223,8 +234,8 @@ themeButton.addEventListener( 'click', onThemeClick ); search.addEventListener( 'submit', onSearchSubmit ); doc.addEventListener( 'keydown', onKeydown ); - doc.addEventListener( 'pointerdown', onOutside ); doc.addEventListener( 'focusin', onOutside ); + doc.addEventListener( 'click', onOutside ); media.addEventListener( 'change', onMediaChange ); doc.documentElement.classList.add( 'chrnx-navigation-enhanced' ); applyViewport( media.matches ); @@ -234,8 +245,8 @@ themeButton.removeEventListener( 'click', onThemeClick ); search.removeEventListener( 'submit', onSearchSubmit ); doc.removeEventListener( 'keydown', onKeydown ); - doc.removeEventListener( 'pointerdown', onOutside ); doc.removeEventListener( 'focusin', onOutside ); + doc.removeEventListener( 'click', onOutside ); media.removeEventListener( 'change', onMediaChange ); restoreOriginalOrder(); anchors.forEach( ( anchor ) => anchor.remove() ); diff --git a/tests/js/navigation.test.js b/tests/js/navigation.test.js index 8361e6c..74cd3fb 100644 --- a/tests/js/navigation.test.js +++ b/tests/js/navigation.test.js @@ -54,6 +54,7 @@ function shellMarkup() { +
Outside content
`; } @@ -74,6 +75,7 @@ function controls() { checkedRadio: document.querySelector( '[data-chrnx-theme-panel] input:checked' ), firstRadio: document.querySelector( '[data-chrnx-theme-panel] input' ), outside: document.querySelector( '[data-outside]' ), + outsideContent: document.querySelector( '[data-outside-content]' ), }; } @@ -271,6 +273,27 @@ describe( 'coordinated CHRNX header shell', () => { expect( submit( control.search ).defaultPrevented ).toBe( false ); } ); + test( 'retained query reopens closed Search before a second native submit', () => { + const { initialize } = require( modulePath ); + initialize( { document, media: createMedia( true ) } ); + const control = controls(); + submit( control.search ); + control.searchInput.value = 'retained cats'; + press( control.searchInput, 'Escape' ); + expect( control.searchPanel.hidden ).toBe( true ); + + const reopenEvent = submit( control.search ); + expect( reopenEvent.defaultPrevented ).toBe( true ); + expect( control.searchInput.value ).toBe( 'retained cats' ); + expect( control.searchPanel.hidden ).toBe( false ); + expect( control.searchButton.getAttribute( 'aria-expanded' ) ).toBe( 'true' ); + expect( document.activeElement ).toBe( control.searchInput ); + + const nativeEvent = submit( control.search ); + expect( nativeEvent.defaultPrevented ).toBe( false ); + expect( control.searchInput.value ).toBe( 'retained cats' ); + } ); + test( 'Escape closes Search and restores its trigger', () => { const { initialize } = require( modulePath ); initialize( { document, media: createMedia( true ) } ); @@ -314,24 +337,66 @@ describe( 'coordinated CHRNX header shell', () => { expect( document.activeElement ).toBe( control[ buttonName ] ); } ); - test( 'outside pointer and focus close Theme without stealing destination focus', () => { + test( 'real outside focusable click order closes Theme after focus transfers', () => { const { initialize } = require( modulePath ); initialize( { document, media: createMedia( true ) } ); const control = controls(); - control.themeButton.click(); - control.outside.focus(); + + let hidden = control.themePanel.hidden; + let focusAtHide = null; + Object.defineProperty( control.themePanel, 'hidden', { + configurable: true, + get: () => hidden, + set: ( value ) => { + if ( value ) { + focusAtHide = document.activeElement; + } + hidden = value; + }, + } ); + control.outside.dispatchEvent( new Event( 'pointerdown', { bubbles: true } ) ); - expect( control.themePanel.hidden ).toBe( true ); - expect( document.activeElement ).toBe( control.outside ); - - control.themeButton.click(); + expect( control.themePanel.hidden ).toBe( false ); control.outside.focus(); control.outside.dispatchEvent( new Event( 'focusin', { bubbles: true } ) ); + control.outside.dispatchEvent( new Event( 'click', { bubbles: true } ) ); expect( control.themePanel.hidden ).toBe( true ); + expect( focusAtHide ).toBe( control.outside ); expect( document.activeElement ).toBe( control.outside ); } ); + test( 'outside nonfocusable click closes Theme without hiding focused content', () => { + const { initialize } = require( modulePath ); + initialize( { document, media: createMedia( true ) } ); + const control = controls(); + control.themeButton.click(); + control.checkedRadio.focus(); + + let hidden = control.themePanel.hidden; + let focusAtHide = null; + Object.defineProperty( control.themePanel, 'hidden', { + configurable: true, + get: () => hidden, + set: ( value ) => { + if ( value ) { + focusAtHide = document.activeElement; + } + hidden = value; + }, + } ); + + control.outsideContent.dispatchEvent( new Event( 'pointerdown', { bubbles: true } ) ); + expect( control.themePanel.hidden ).toBe( false ); + control.checkedRadio.blur(); + expect( document.activeElement ).toBe( document.body ); + control.outsideContent.dispatchEvent( new Event( 'click', { bubbles: true } ) ); + expect( control.themePanel.hidden ).toBe( true ); + expect( focusAtHide ).toBe( control.themeButton ); + expect( document.activeElement ).toBe( control.themeButton ); + expect( document.activeElement.closest( '[hidden]' ) ).toBeNull(); + } ); + test( 'Tab is never prevented', () => { const { initialize } = require( modulePath ); initialize( { document, media: createMedia( true ) } ); @@ -363,6 +428,37 @@ describe( 'coordinated CHRNX header shell', () => { expect( document.activeElement ).toBe( control.checkedRadio ); } ); + test.each( [ 'light', 'dark' ] )( + '599 to 600 focuses the current %s radio selected after initialization', + ( value ) => { + const { initialize } = require( modulePath ); + const media = createMedia( true ); + initialize( { document, media } ); + const control = controls(); + const currentRadio = control.themePanel.querySelector( `input[value="${ value }"]` ); + control.checkedRadio.checked = false; + currentRadio.checked = true; + control.themeButton.focus(); + + media.change( false ); + expect( document.activeElement ).toBe( currentRadio ); + }, + ); + + test( '599 to 600 safely falls back to the first radio when none is checked', () => { + const { initialize } = require( modulePath ); + const media = createMedia( true ); + initialize( { document, media } ); + const control = controls(); + control.themePanel.querySelectorAll( 'input[type="radio"]' ).forEach( ( radio ) => { + radio.checked = false; + } ); + control.themeButton.focus(); + + media.change( false ); + expect( document.activeElement ).toBe( control.firstRadio ); + } ); + test( '599 to 600 preserves focus on a navigation link or visible radio', () => { const { initialize } = require( modulePath ); const media = createMedia( true ); @@ -458,7 +554,7 @@ describe( 'coordinated CHRNX header shell', () => { test.each( [ [ 'brand', '.chrnx-site-brand' ], [ 'navigation link', '[data-part="nav"] a' ], - [ 'checked theme radio', '[data-chrnx-theme-panel] input:checked' ], + [ 'theme radio', '[data-chrnx-theme-panel] input' ], ] )( 'fails closed without the required %s', ( _name, selector ) => { const { initialize } = require( modulePath ); document.querySelectorAll( selector ).forEach( ( element ) => element.remove() );