From 6df38eb4c8204660b7e3a36aed39d63850c2966c Mon Sep 17 00:00:00 2001 From: Diego Resendez Date: Wed, 12 Aug 2026 08:24:26 -0500 Subject: [PATCH] fix: restore CHRNX search focus on resize --- assets/js/navigation.js | 2 ++ tests/e2e/navigation.spec.mjs | 37 +++++++++++++++++++++++++++++++++-- tests/js/navigation.test.js | 8 ++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/assets/js/navigation.js b/assets/js/navigation.js index 67b2bc4..17176cd 100644 --- a/assets/js/navigation.js +++ b/assets/js/navigation.js @@ -173,6 +173,8 @@ currentThemeRadio().focus(); } else if ( ! mobile && ( focusWasNav || focusWasThemePanel ) ) { focused.focus(); + } else if ( mobile && focusWasSearch ) { + searchButton.focus(); } else if ( mobile && focusWasNav ) { menuButton.focus(); } else if ( mobile && focusWasThemePanel ) { diff --git a/tests/e2e/navigation.spec.mjs b/tests/e2e/navigation.spec.mjs index 90f337a..9aa11c6 100644 --- a/tests/e2e/navigation.spec.mjs +++ b/tests/e2e/navigation.spec.mjs @@ -528,8 +528,13 @@ test.describe( 'site navigation shell', () => { const searchButton = header.locator( '[data-chrnx-search-button]' ); const themeButton = header.locator( '[data-chrnx-theme-button]' ); const menuButton = header.locator( '[data-chrnx-menu-toggle]' ); - const journal = header.getByRole( 'link', { name: 'Journal', exact: true } ); + const nav = header.getByRole( 'navigation', { name: 'Primary navigation' } ); + const links = destinations.map( ( [ name ] ) => + nav.getByRole( 'link', { name, exact: true } ), + ); + const firstMainLink = page.locator( 'main#chrnx-main a[href]' ).first(); const focusKey = testInfo.project.name === 'webkit' ? 'Alt+Tab' : 'Tab'; + const reverseFocusKey = testInfo.project.name === 'webkit' ? 'Alt+Shift+Tab' : 'Shift+Tab'; await searchButton.focus(); await page.keyboard.press( focusKey ); @@ -537,8 +542,36 @@ test.describe( 'site navigation shell', () => { await page.keyboard.press( focusKey ); await expect( menuButton ).toBeFocused(); await menuButton.press( 'Enter' ); + for ( const link of links ) { + await page.keyboard.press( focusKey ); + await expect( link ).toBeFocused(); + } await page.keyboard.press( focusKey ); - await expect( journal ).toBeFocused(); + await expect( firstMainLink ).toBeFocused(); + + await links[ 0 ].focus(); + await page.keyboard.press( reverseFocusKey ); + await expect( menuButton ).toBeFocused(); + } ); + + test( 'desktop Search focus returns to its visible trigger when mobile closes the panel', async ( { + page, + } ) => { + await page.setViewportSize( { width: 600, height: 844 } ); + await page.goto( '/' ); + const header = page.getByRole( 'banner' ); + const searchButton = header.locator( '[data-chrnx-search-button]' ); + const searchInput = header.getByRole( 'searchbox' ); + const searchPanel = header.locator( '[data-chrnx-search-panel]' ); + + await searchButton.click(); + await expect( searchInput ).toBeFocused(); + await page.setViewportSize( { width: 390, height: 844 } ); + await expect( searchPanel ).toBeHidden(); + await expect( searchInput ).toBeHidden(); + await expect( searchButton ).toBeVisible(); + await expect( searchButton ).toHaveAttribute( 'aria-expanded', 'false' ); + await expect( searchButton ).toBeFocused(); } ); test( 'resize sends focus only from controls that become hidden', async ( { page } ) => { diff --git a/tests/js/navigation.test.js b/tests/js/navigation.test.js index 74cd3fb..8d28c54 100644 --- a/tests/js/navigation.test.js +++ b/tests/js/navigation.test.js @@ -483,6 +483,14 @@ describe( 'coordinated CHRNX header shell', () => { initialize( { document, media } ); const control = controls(); + submit( control.search ); + control.searchInput.focus(); + media.change( true ); + expect( control.searchPanel.hidden ).toBe( true ); + expect( control.searchButton.getAttribute( 'aria-expanded' ) ).toBe( 'false' ); + expect( document.activeElement ).toBe( control.searchButton ); + + media.change( false ); control.firstLink.focus(); media.change( true ); expect( document.activeElement ).toBe( control.menuButton );