fix: restore CHRNX search focus on resize

This commit is contained in:
chrnx committed 2026-08-12 08:24:26 -05:00
1 parent 6cafab7967
commit 6df38eb4c8
3 files changed
+45 -2

No files matched your search

+2
View File
@@ -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 ) {
+35 -2
View File
@@ -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 } ) => {
+8
View File
@@ -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 );