diff --git a/assets/css/layout.css b/assets/css/layout.css index 5c47fef..9403443 100644 --- a/assets/css/layout.css +++ b/assets/css/layout.css @@ -119,10 +119,8 @@ html[data-theme="light"] header.wp-block-template-part { .chrnx-primary-navigation a, .chrnx-footer-navigation a, -.wp-block-navigation__responsive-container-open, -.wp-block-navigation__responsive-container-close, -.chrnx-header-search .wp-block-search__button, -.chrnx-header-search .wp-block-search__input { +.chrnx-header-search button, +.chrnx-header-search input { min-width: 45px; min-height: 45px; } @@ -134,16 +132,50 @@ html[data-theme="light"] header.wp-block-template-part { padding-inline: 0.55rem; } -.chrnx-primary-navigation .wp-block-navigation__container { +.chrnx-primary-navigation ul { + display: flex; + align-items: center; gap: 0.25rem; + margin: 0; + padding: 0; + list-style: none; } .chrnx-header-search { + display: flex; + align-items: center; + gap: 0.25rem; width: auto; } -.chrnx-header-search .wp-block-search__inside-wrapper { +.chrnx-header-search input { + box-sizing: border-box; + width: min(15rem, 45vw); + padding-inline: 0.75rem; +} + +.chrnx-header-search button { + display: inline-flex; + align-items: center; + justify-content: center; + padding: 0 0.75rem; + border: 1px solid currentcolor; + background: transparent; + color: inherit; + font: inherit; + cursor: pointer; +} + +.chrnx-screen-reader-text { + position: absolute; + width: 1px; + height: 1px; padding: 0; + margin: -1px; + overflow: hidden; + clip: rect(0, 0, 0, 0); + white-space: nowrap; + border: 0; } .chrnx-theme-toggle { @@ -197,6 +229,10 @@ html[data-theme="light"] .chrnx-theme-toggle label:has(input:checked) { accent-color: var(--wp--preset--color--green); } +.js .chrnx-header-search[data-expanded="false"] input { + display: none; +} + .chrnx-theme-toggle output { position: absolute; width: 1px; @@ -257,7 +293,7 @@ html[data-theme="light"] footer.wp-block-template-part { justify-content: flex-start; } - .chrnx-primary-navigation-shell { + .chrnx-primary-navigation { margin-left: auto; } @@ -279,13 +315,13 @@ html[data-theme="light"] footer.wp-block-template-part { margin-left: auto; } - .chrnx-primary-navigation-shell { + .chrnx-primary-navigation { order: 3; width: 100%; margin-left: 0; } - .chrnx-primary-navigation .wp-block-navigation__container { + .chrnx-primary-navigation ul { flex-wrap: wrap; } diff --git a/assets/js/navigation.js b/assets/js/navigation.js index 66e3bea..5ef6982 100644 --- a/assets/js/navigation.js +++ b/assets/js/navigation.js @@ -48,7 +48,15 @@ pair.button.focus(); } }; - const reset = () => pairs.forEach( ( pair ) => setOpen( pair, false ) ); + 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' ); diff --git a/assets/js/search.js b/assets/js/search.js new file mode 100644 index 0000000..32d5d06 --- /dev/null +++ b/assets/js/search.js @@ -0,0 +1,87 @@ +( 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"]' ), + button: form.querySelector( '[data-chrnx-search-button]' ), + } ) ) + .filter( ( control ) => control.input && control.button ); + if ( controls.length === 0 ) { + cleanupCurrent = null; + return false; + } + + const setExpanded = ( control, expanded, restoreFocus = false ) => { + control.form.dataset.expanded = String( expanded ); + control.input.hidden = ! 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(); + } + } +} )(); diff --git a/blocks/header-tools/block.json b/blocks/header-tools/block.json new file mode 100644 index 0000000..17021a8 --- /dev/null +++ b/blocks/header-tools/block.json @@ -0,0 +1,13 @@ +{ + "$schema": "https://schemas.wp.org/trunk/block.json", + "apiVersion": 3, + "name": "chrnx/header-tools", + "version": "0.1.0", + "title": "CHRNX Header Tools", + "category": "theme", + "description": "Display translated primary navigation and progressive site search.", + "textdomain": "chrnx", + "supports": { "html": false }, + "viewScript": "file:../../assets/js/search.js", + "render": "file:./render.php" +} diff --git a/blocks/header-tools/render.php b/blocks/header-tools/render.php new file mode 100644 index 0000000..bb3be49 --- /dev/null +++ b/blocks/header-tools/render.php @@ -0,0 +1,52 @@ + __( 'Journal', 'chrnx' ), + 'url' => home_url( '/journal/' ), + ), + array( + 'label' => __( 'Projects', 'chrnx' ), + 'url' => home_url( '/projects/' ), + ), + array( + 'label' => __( 'Music', 'chrnx' ), + 'url' => home_url( '/music/' ), + ), + array( + 'label' => __( 'Podcasts', 'chrnx' ), + 'url' => home_url( '/podcasts/' ), + ), + array( + 'label' => __( 'About', 'chrnx' ), + 'url' => home_url( '/about/' ), + ), +); +$chrnx_search_label = __( 'Search', 'chrnx' ); +$chrnx_search_id = wp_unique_id( 'chrnx-search-' ); +?> + +
diff --git a/inc/blocks.php b/inc/blocks.php index 947d632..a23705a 100644 --- a/inc/blocks.php +++ b/inc/blocks.php @@ -74,7 +74,7 @@ function chrnx_get_editorial_card_markup( $post_id, $heading_level = 3, $class_n /** Register all block metadata directories that exist. */ function chrnx_register_editorial_blocks() { - $blocks = array( 'featured-stories', 'journal-stream', 'latest-podcast', 'podcast-meta', 'topic-hero', 'related-posts', 'subscribe-cta', 'theme-toggle', 'site-brand', 'mobile-menu-toggle', 'footer-content' ); + $blocks = array( 'featured-stories', 'journal-stream', 'latest-podcast', 'podcast-meta', 'topic-hero', 'related-posts', 'subscribe-cta', 'theme-toggle', 'site-brand', 'mobile-menu-toggle', 'footer-content', 'header-tools' ); foreach ( $blocks as $block ) { $name = 'chrnx/' . $block; if ( WP_Block_Type_Registry::get_instance()->is_registered( $name ) ) { diff --git a/inc/theme-preference.php b/inc/theme-preference.php index c3f4821..566fa0b 100644 --- a/inc/theme-preference.php +++ b/inc/theme-preference.php @@ -7,7 +7,7 @@ /** Print the no-flash theme resolution before stylesheet links. */ function chrnx_print_theme_preference_bootstrap() { - $script = "(function(){var t=null,m;try{var s=localStorage.getItem('chrnx-theme');if(s==='light'||s==='dark'){t=s;}}catch(e){}if(!t){try{m=matchMedia('(prefers-color-scheme: dark)').matches;t=m?'dark':'light';}catch(e){t='dark';}}document.documentElement.dataset.theme=t;document.documentElement.style.colorScheme=t;document.documentElement.classList.add('chrnx-theme-enhanced');}());"; + $script = "(function(){var t=null,m;try{var s=localStorage.getItem('chrnx-theme');if(s==='light'||s==='dark'){t=s;}}catch(e){}if(!t){try{m=matchMedia('(prefers-color-scheme: dark)').matches;t=m?'dark':'light';}catch(e){t='dark';}}document.documentElement.dataset.theme=t;document.documentElement.style.colorScheme=t;document.documentElement.classList.add('js','chrnx-theme-enhanced');}());"; // wp_get_inline_script_tag applies WordPress's standard CSP attribute filter. // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Core escapes tag attributes and script is a fixed literal. echo wp_get_inline_script_tag( diff --git a/parts/header.html b/parts/header.html index 2a58df1..2a203ff 100644 --- a/parts/header.html +++ b/parts/header.html @@ -4,19 +4,7 @@ - - - - - + diff --git a/tests/e2e/navigation.spec.mjs b/tests/e2e/navigation.spec.mjs index acf502f..6e4bfb2 100644 --- a/tests/e2e/navigation.spec.mjs +++ b/tests/e2e/navigation.spec.mjs @@ -14,6 +14,10 @@ async function expectTargetSize( locator ) { expect( box.height ).toBeGreaterThanOrEqual( 44 ); } +async function getHomeURL( page ) { + return page.getByRole( 'banner' ).getByRole( 'link', { name: 'CHRNX' } ).getAttribute( 'href' ); +} + test.describe( 'site navigation shell', () => { test( 'desktop has a stable accessible header, approved local brand, and compact footer', async ( { page, @@ -41,19 +45,29 @@ test.describe( 'site navigation shell', () => { /\/wp-content\/themes\/chrnx\/assets\/icons\/chrnx-wordmark\.svg$/, ); + expect( await header.getByRole( 'navigation' ).count() ).toBe( 1 ); + const homeURL = await getHomeURL( page ); for ( const [ name, path ] of destinations ) { const link = nav.getByRole( 'link', { name, exact: true } ); - await expect( link ).toHaveAttribute( 'href', path ); + await expect( link ).toHaveAttribute( 'href', new URL( path.slice( 1 ), homeURL ).href ); } await expect( header.locator( '[data-chrnx-menu-toggle]' ) ).toBeHidden(); - const compactSearch = header.locator( '.wp-block-search__button' ); + const compactSearch = header.locator( '[data-chrnx-search-button]' ); await expect( compactSearch ).toBeVisible(); - await expect( compactSearch ).toHaveAccessibleName( 'Expand search field' ); + await expect( compactSearch ).toHaveAccessibleName( 'Open search' ); await expect( header.getByRole( 'searchbox' ) ).toBeHidden(); await expectTargetSize( compactSearch ); await compactSearch.click(); await expect( header.getByRole( 'searchbox' ) ).toBeVisible(); - await expect( compactSearch ).toHaveAccessibleName( 'Submit Search' ); + await expect( header.getByRole( 'searchbox' ) ).toBeFocused(); + await expect( compactSearch ).toHaveAccessibleName( 'Search' ); + await page.keyboard.press( 'Escape' ); + await expect( compactSearch ).toBeFocused(); + await expect( header.getByRole( 'searchbox' ) ).toBeHidden(); + await compactSearch.click(); + await header.getByRole( 'searchbox' ).fill( 'portable cats' ); + await compactSearch.click(); + await expect( page ).toHaveURL( new RegExp( `[?&]s=portable(?:\\+|%20)cats(?:&|$)` ) ); const before = await header.boundingBox(); await page.evaluate( () => window.scrollTo( 0, document.body.scrollHeight ) ); const after = await header.boundingBox(); @@ -86,7 +100,7 @@ test.describe( 'site navigation shell', () => { ).not.toBe( 'none' ); await expectTargetSize( journal ); await expectTargetSize( - page.getByRole( 'banner' ).getByRole( 'button', { name: 'Expand search field' } ), + page.getByRole( 'banner' ).getByRole( 'button', { name: 'Open search' } ), ); } ); @@ -104,10 +118,11 @@ test.describe( 'site navigation shell', () => { await expect( open ).toHaveAccessibleName( 'Close primary navigation' ); const nav = header.getByRole( 'navigation', { name: 'Primary navigation' } ); await expect( nav ).toBeVisible(); + const homeURL = await getHomeURL( page ); for ( const [ name, path ] of destinations ) { await expect( nav.getByRole( 'link', { name, exact: true } ) ).toHaveAttribute( 'href', - path, + new URL( path.slice( 1 ), homeURL ).href, ); } await open.focus(); @@ -117,7 +132,7 @@ test.describe( 'site navigation shell', () => { await expect( nav.getByRole( 'link', { name, exact: true } ) ).toBeFocused(); } await page.keyboard.press( focusKey ); - await expect( header.getByRole( 'button', { name: 'Expand search field' } ) ).toBeFocused(); + await expect( header.getByRole( 'button', { name: 'Open search' } ) ).toBeFocused(); await page.keyboard.press( focusKey ); await expect( header.getByRole( 'radio', { name: 'System', exact: true } ) ).toBeFocused(); await nav.getByRole( 'link', { name: 'Music', exact: true } ).focus(); @@ -125,8 +140,8 @@ test.describe( 'site navigation shell', () => { await expect( open ).toBeFocused(); await expect( open ).toHaveAccessibleName( 'Open primary navigation' ); await expect( nav ).toBeHidden(); - const compactSearch = header.locator( '.wp-block-search__button' ); - await expect( compactSearch ).toHaveAccessibleName( 'Expand search field' ); + const compactSearch = header.locator( '[data-chrnx-search-button]' ); + await expect( compactSearch ).toHaveAccessibleName( 'Open search' ); await expectTargetSize( compactSearch ); await compactSearch.click(); await expect( header.getByRole( 'searchbox' ) ).toBeVisible(); @@ -135,4 +150,42 @@ test.describe( 'site navigation shell', () => { await expect( nav ).toBeVisible(); await expect( open ).toHaveAttribute( 'aria-expanded', 'false' ); } ); + + test( 'resize never leaves focus inside hidden navigation', async ( { page } ) => { + await page.setViewportSize( { width: 1280, height: 800 } ); + await page.goto( '/' ); + const header = page.getByRole( 'banner' ); + const nav = header.getByRole( 'navigation', { name: 'Primary navigation' } ); + const journal = nav.getByRole( 'link', { name: 'Journal', exact: true } ); + const toggle = header.locator( '[data-chrnx-menu-toggle]' ); + await journal.focus(); + await page.setViewportSize( { width: 390, height: 844 } ); + await expect( nav ).toBeHidden(); + await expect( toggle ).toBeFocused(); + await toggle.click(); + await journal.focus(); + await page.setViewportSize( { width: 1280, height: 800 } ); + await expect( nav ).toBeVisible(); + await expect( journal ).toBeFocused(); + } ); + + test.describe( 'without JavaScript', () => { + test.use( { javaScriptEnabled: false } ); + + test( 'navigation and named search remain fully usable', async ( { page } ) => { + await page.setViewportSize( { width: 390, height: 844 } ); + await page.goto( '/' ); + const header = page.getByRole( 'banner' ); + await expect( + header.getByRole( 'navigation', { name: 'Primary navigation' } ), + ).toBeVisible(); + const search = header.getByRole( 'searchbox', { name: 'Search' } ); + const submit = header.getByRole( 'button', { name: 'Search', exact: true } ); + await expect( search ).toBeVisible(); + await expect( submit ).toBeVisible(); + await search.fill( 'no js cats' ); + await submit.click(); + await expect( page ).toHaveURL( new RegExp( `[?&]s=no(?:\\+|%20)js(?:\\+|%20)cats(?:&|$)` ) ); + } ); + } ); } ); diff --git a/tests/js/navigation.test.js b/tests/js/navigation.test.js index 48817f6..89189b4 100644 --- a/tests/js/navigation.test.js +++ b/tests/js/navigation.test.js @@ -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' ) ); + } ); } ); diff --git a/tests/js/search.test.js b/tests/js/search.test.js new file mode 100644 index 0000000..7cc3e1e --- /dev/null +++ b/tests/js/search.test.js @@ -0,0 +1,62 @@ +const modulePath = '../../assets/js/search.js'; + +function markup() { + document.body.innerHTML = ` + + `; +} + +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' ); + } ); +} ); diff --git a/tests/php/BlockRenderingTest.php b/tests/php/BlockRenderingTest.php index d95c074..e8bc7d2 100644 --- a/tests/php/BlockRenderingTest.php +++ b/tests/php/BlockRenderingTest.php @@ -15,9 +15,9 @@ class BlockRenderingTest extends WP_UnitTestCase { delete_option( 'chrnx_subscribe_page_id' ); } - /** All eleven block types register from metadata. */ + /** All twelve block types register from metadata. */ public function test_all_editorial_blocks_are_registered() { - $names = array( 'featured-stories', 'journal-stream', 'latest-podcast', 'podcast-meta', 'topic-hero', 'related-posts', 'subscribe-cta', 'theme-toggle', 'site-brand', 'mobile-menu-toggle', 'footer-content' ); + $names = array( 'featured-stories', 'journal-stream', 'latest-podcast', 'podcast-meta', 'topic-hero', 'related-posts', 'subscribe-cta', 'theme-toggle', 'site-brand', 'mobile-menu-toggle', 'footer-content', 'header-tools' ); foreach ( $names as $name ) { $type = WP_Block_Type_Registry::get_instance()->get_registered( 'chrnx/' . $name ); $this->assertInstanceOf( WP_Block_Type::class, $type, $name ); @@ -25,6 +25,53 @@ class BlockRenderingTest extends WP_UnitTestCase { } } + /** Header navigation and search use translated labels and portable home URLs. */ + public function test_header_tools_render_localized_portable_navigation_and_search() { + $translate = static function ( $translated, $text, $domain ) { + if ( 'chrnx' !== $domain ) { + return $translated; + } + return array( + 'Primary navigation' => 'Navegación principal', + 'Journal' => 'Diario', + 'Search' => 'Buscar', + 'Open search' => 'Abrir búsqueda', + )[ $text ] ?? $translated; + }; + $portable_home = static function ( $url, $path ) { + return 'https://example.test/subdirectory/' . ltrim( (string) $path, '/' ); + }; + add_filter( 'gettext', $translate, 10, 3 ); + add_filter( 'home_url', $portable_home, 10, 2 ); + try { + $output = render_block( + array( + 'blockName' => 'chrnx/header-tools', + 'attrs' => array(), + 'innerBlocks' => array(), + 'innerHTML' => '', + 'innerContent' => array(), + ) + ); + } finally { + remove_filter( 'home_url', $portable_home, 10 ); + remove_filter( 'gettext', $translate, 10 ); + } + + $this->assertSame( 1, substr_count( $output, '