fix: make CHRNX header fully progressive
This commit is contained in:
13 files changed
+414
-36
No files matched your search
+45
-9
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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' );
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
} )();
|
||||
@@ -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"
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
/**
|
||||
* Render translated primary navigation and progressive site search.
|
||||
*
|
||||
* @package CHRNX
|
||||
*/
|
||||
|
||||
$chrnx_header_links = array(
|
||||
array(
|
||||
'label' => __( '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-' );
|
||||
?>
|
||||
<nav id="chrnx-primary-navigation" class="chrnx-primary-navigation" aria-label="<?php echo esc_attr__( 'Primary navigation', 'chrnx' ); ?>">
|
||||
<ul>
|
||||
<?php foreach ( $chrnx_header_links as $chrnx_link ) : ?>
|
||||
<li><a href="<?php echo esc_url( $chrnx_link['url'] ); ?>"><?php echo esc_html( $chrnx_link['label'] ); ?></a></li>
|
||||
<?php endforeach; ?>
|
||||
</ul>
|
||||
</nav>
|
||||
<form class="chrnx-header-search" role="search" method="get" action="<?php echo esc_url( home_url( '/' ) ); ?>" data-chrnx-search data-expanded="false">
|
||||
<label class="chrnx-screen-reader-text" for="<?php echo esc_attr( $chrnx_search_id ); ?>"><?php echo esc_html( $chrnx_search_label ); ?></label>
|
||||
<input id="<?php echo esc_attr( $chrnx_search_id ); ?>" type="search" name="s" autocomplete="off">
|
||||
<button
|
||||
type="submit"
|
||||
data-chrnx-search-button
|
||||
data-label-open="<?php echo esc_attr__( 'Open search', 'chrnx' ); ?>"
|
||||
data-label-submit="<?php echo esc_attr( $chrnx_search_label ); ?>"
|
||||
>
|
||||
<span aria-hidden="true">⌕</span>
|
||||
<span class="chrnx-screen-reader-text"><?php echo esc_html( $chrnx_search_label ); ?></span>
|
||||
</button>
|
||||
</form>
|
||||
+1
-1
@@ -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 ) ) {
|
||||
|
||||
@@ -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(
|
||||
|
||||
+1
-13
@@ -4,19 +4,7 @@
|
||||
|
||||
<!-- wp:chrnx/mobile-menu-toggle /-->
|
||||
|
||||
<!-- wp:group {"anchor":"chrnx-primary-navigation","className":"chrnx-primary-navigation-shell","layout":{"type":"default"}} -->
|
||||
<div id="chrnx-primary-navigation" class="wp-block-group chrnx-primary-navigation-shell">
|
||||
<!-- wp:navigation {"overlayMenu":"never","ariaLabel":"Primary navigation","className":"chrnx-primary-navigation","layout":{"type":"flex","justifyContent":"center"}} -->
|
||||
<!-- wp:navigation-link {"label":"Journal","url":"/journal/","kind":"custom"} /-->
|
||||
<!-- wp:navigation-link {"label":"Projects","url":"/projects/","kind":"custom"} /-->
|
||||
<!-- wp:navigation-link {"label":"Music","url":"/music/","kind":"custom"} /-->
|
||||
<!-- wp:navigation-link {"label":"Podcasts","url":"/podcasts/","kind":"custom"} /-->
|
||||
<!-- wp:navigation-link {"label":"About","url":"/about/","kind":"custom"} /-->
|
||||
<!-- /wp:navigation -->
|
||||
</div>
|
||||
<!-- /wp:group -->
|
||||
|
||||
<!-- wp:search {"label":"Search","showLabel":false,"buttonText":"Search","buttonPosition":"button-only","buttonUseIcon":true,"className":"chrnx-header-search"} /-->
|
||||
<!-- wp:chrnx/header-tools /-->
|
||||
|
||||
<!-- wp:chrnx/theme-toggle /-->
|
||||
</div>
|
||||
|
||||
@@ -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(?:&|$)` ) );
|
||||
} );
|
||||
} );
|
||||
} );
|
||||
@@ -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' ) );
|
||||
} );
|
||||
} );
|
||||
@@ -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' );
|
||||
} );
|
||||
} );
|
||||
@@ -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, '<nav ' ) );
|
||||
$this->assertStringContainsString( 'aria-label="Navegación principal"', $output );
|
||||
$this->assertSame( 1, substr_count( $output, 'aria-label="Navegación principal"' ) );
|
||||
$this->assertStringContainsString( '>Diario</a>', $output );
|
||||
foreach ( array( 'journal', 'projects', 'music', 'podcasts', 'about' ) as $path ) {
|
||||
$this->assertStringContainsString( 'href="https://example.test/subdirectory/' . $path . '/"', $output );
|
||||
}
|
||||
$this->assertStringContainsString( 'role="search"', $output );
|
||||
$this->assertStringContainsString( 'action="https://example.test/subdirectory/"', $output );
|
||||
$this->assertStringContainsString( 'name="s"', $output );
|
||||
$this->assertStringContainsString( 'chrnx-screen-reader-text">Buscar</span>', $output );
|
||||
$this->assertStringContainsString( 'data-label-open="Abrir búsqueda"', $output );
|
||||
}
|
||||
|
||||
/** Mobile menu control is localized, initially hidden, and targets the one primary navigation. */
|
||||
public function test_mobile_menu_toggle_renders_progressive_control() {
|
||||
$output = render_block(
|
||||
|
||||
@@ -43,3 +43,15 @@ test('footer user-facing copy lives in an extractable PHP render block', async (
|
||||
assert.ok(render.includes(`'${text}', 'chrnx'`), `${text} must use the chrnx text domain`);
|
||||
}
|
||||
});
|
||||
|
||||
test('header navigation and search copy lives in one extractable PHP render block', async () => {
|
||||
const part = await readProjectFile('parts/header.html');
|
||||
const render = await readProjectFile('blocks/header-tools/render.php');
|
||||
assert.match(part, /<!-- wp:chrnx\/header-tools \/-->/);
|
||||
assert.doesNotMatch(part, /wp:navigation|wp:navigation-link|wp:search|Journal|Primary navigation/);
|
||||
for (const text of ['Primary navigation', 'Journal', 'Projects', 'Music', 'Podcasts', 'About', 'Search', 'Open search']) {
|
||||
assert.ok(render.includes(`'${text}', 'chrnx'`), `${text} must use the chrnx text domain`);
|
||||
}
|
||||
assert.match(render, /home_url\( '\/journal\/' \)/);
|
||||
assert.match(render, /home_url\( '\/' \)/);
|
||||
});
|
||||
Reference in new issue
Block a user