fix: preserve progressive CHRNX search

This commit is contained in:
chrnx committed 2026-08-11 19:45:14 -05:00
1 parent 55166c6d2a
commit e7956a1496
7 files changed
+64 -29

No files matched your search

+4 -2
View File
@@ -18,9 +18,10 @@
.map( ( form ) => ( { .map( ( form ) => ( {
form, form,
input: form.querySelector( 'input[name="s"]' ), input: form.querySelector( 'input[name="s"]' ),
panel: form.querySelector( '[data-chrnx-search-panel]' ),
button: form.querySelector( '[data-chrnx-search-button]' ), button: form.querySelector( '[data-chrnx-search-button]' ),
} ) ) } ) )
.filter( ( control ) => control.input && control.button ); .filter( ( control ) => control.input && control.panel && control.button );
if ( controls.length === 0 ) { if ( controls.length === 0 ) {
cleanupCurrent = null; cleanupCurrent = null;
return false; return false;
@@ -28,7 +29,8 @@
const setExpanded = ( control, expanded, restoreFocus = false ) => { const setExpanded = ( control, expanded, restoreFocus = false ) => {
control.form.dataset.expanded = String( expanded ); control.form.dataset.expanded = String( expanded );
control.input.hidden = ! expanded; control.panel.hidden = ! expanded;
control.button.setAttribute( 'aria-expanded', String( expanded ) );
control.button.setAttribute( control.button.setAttribute(
'aria-label', 'aria-label',
expanded ? control.button.dataset.labelSubmit : control.button.dataset.labelOpen, expanded ? control.button.dataset.labelSubmit : control.button.dataset.labelOpen,
+1
View File
@@ -8,5 +8,6 @@
"description": "Display translated primary navigation and progressive site search.", "description": "Display translated primary navigation and progressive site search.",
"textdomain": "chrnx", "textdomain": "chrnx",
"supports": { "html": false }, "supports": { "html": false },
"viewScript": "file:../../assets/js/search.js",
"render": "file:./render.php" "render": "file:./render.php"
} }
+2 -2
View File
@@ -38,7 +38,7 @@ $chrnx_search_panel_id = wp_unique_id( 'chrnx-search-panel-' );
<?php endforeach; ?> <?php endforeach; ?>
</ul> </ul>
</nav> </nav>
<form class="chrnx-header-search" role="search" method="get" action="<?php echo esc_url( home_url( '/' ) ); ?>" data-chrnx-search data-expanded="false"> <form class="chrnx-header-search" role="search" method="get" action="<?php echo esc_url( home_url( '/' ) ); ?>" data-chrnx-search data-expanded="true">
<div id="<?php echo esc_attr( $chrnx_search_panel_id ); ?>" class="chrnx-header-search-row" data-chrnx-search-panel> <div id="<?php echo esc_attr( $chrnx_search_panel_id ); ?>" class="chrnx-header-search-row" data-chrnx-search-panel>
<label class="chrnx-screen-reader-text" for="<?php echo esc_attr( $chrnx_search_id ); ?>"><?php echo esc_html( $chrnx_search_label ); ?></label> <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"> <input id="<?php echo esc_attr( $chrnx_search_id ); ?>" type="search" name="s" autocomplete="off">
@@ -47,7 +47,7 @@ $chrnx_search_panel_id = wp_unique_id( 'chrnx-search-panel-' );
type="submit" type="submit"
data-chrnx-search-button data-chrnx-search-button
aria-controls="<?php echo esc_attr( $chrnx_search_panel_id ); ?>" aria-controls="<?php echo esc_attr( $chrnx_search_panel_id ); ?>"
aria-expanded="false" aria-expanded="true"
data-label-open="<?php echo esc_attr__( 'Open search', 'chrnx' ); ?>" data-label-open="<?php echo esc_attr__( 'Open search', 'chrnx' ); ?>"
data-label-submit="<?php echo esc_attr( $chrnx_search_label ); ?>" data-label-submit="<?php echo esc_attr( $chrnx_search_label ); ?>"
> >
@@ -26,7 +26,8 @@ properties, Jest/jsdom, PHPUnit, Playwright, axe-core, wp-env on Colima.
radio panel with stable IDs and accessible state. radio panel with stable IDs and accessible state.
- `blocks/header-tools/render.php`: expose stable search panel IDs; keep native - `blocks/header-tools/render.php`: expose stable search panel IDs; keep native
GET submission. GET submission.
- `blocks/header-tools/block.json`: stop loading the retired search controller. - `blocks/header-tools/block.json`: keep the search controller through Task 1,
then transfer ownership to the navigation controller in Task 2.
- `assets/js/navigation.js`: own mobile ordering and all three panel states. - `assets/js/navigation.js`: own mobile ordering and all three panel states.
- `assets/js/search.js`: remove after its behavior moves into the controller. - `assets/js/search.js`: remove after its behavior moves into the controller.
- `assets/js/theme-toggle.js`: retain preference resolution and storage only. - `assets/js/theme-toggle.js`: retain preference resolution and storage only.
@@ -81,16 +82,18 @@ git commit -m "docs: plan compact CHRNX mobile header"
- Modify: `blocks/theme-toggle/render.php` - Modify: `blocks/theme-toggle/render.php`
- Modify: `blocks/header-tools/render.php` - Modify: `blocks/header-tools/render.php`
- Modify: `blocks/header-tools/block.json` - Modify: `assets/js/search.js`
- Modify: `tests/php/BlockRenderingTest.php` - Modify: `tests/php/BlockRenderingTest.php`
- Modify: `tests/js/search.test.js`
- Modify: `tests/smoke/theme-preference-contract.test.mjs` - Modify: `tests/smoke/theme-preference-contract.test.mjs`
- [ ] **Step 1: Write failing PHP and smoke tests** - [ ] **Step 1: Write failing PHP and smoke tests**
Require one theme button with a stable `aria-controls` target, the target Require one theme button with a stable `aria-controls` target, the target
fieldset, a search button with matching `aria-controls` and fieldset, a search button with matching `aria-controls` and
`aria-expanded="false"`, a controlled search-row ID, and only `navigation.js` server-rendered `aria-expanded="true"`, and a visible controlled search-row ID.
as the shell controller. Preserve native radio and GET form semantics. Keep `search.js` owned by the header-tools block so current enhanced behavior
continues until Task 2. Preserve native radio and GET form semantics.
```php ```php
$this->assertSame( 1, substr_count( $html, 'data-chrnx-theme-button' ) ); $this->assertSame( 1, substr_count( $html, 'data-chrnx-theme-button' ) );
@@ -103,17 +106,29 @@ $this->assertStringContainsString( 'role="search"', $search_html );
$this->assertStringContainsString( 'method="get"', $search_html ); $this->assertStringContainsString( 'method="get"', $search_html );
$this->assertMatchesRegularExpression( $this->assertMatchesRegularExpression(
'/data-chrnx-search-button[^>]+aria-controls="([^"]+)"' . '/data-chrnx-search-button[^>]+aria-controls="([^"]+)"' .
'[^>]+aria-expanded="false"/', '[^>]+aria-expanded="true"/',
$search_html $search_html
); );
$this->assertStringContainsString( 'data-chrnx-search-panel', $search_html ); $this->assertStringContainsString( 'data-chrnx-search-panel', $search_html );
``` ```
```js ```js
assert.doesNotMatch(headerToolsMetadata, /search\.js/); assert.match(headerToolsMetadata, /search\.js/);
assert.match(mobileMenuMetadata, /navigation\.js/); assert.match(mobileMenuMetadata, /navigation\.js/);
``` ```
Require the existing search controller to synchronize the enhanced state:
```js
initialize({ document });
expect(searchPanel.hidden).toBe(true);
expect(searchButton.getAttribute('aria-expanded')).toBe('false');
openEmptySearch();
expect(searchButton.getAttribute('aria-expanded')).toBe('true');
pressEscape();
expect(searchButton.getAttribute('aria-expanded')).toBe('false');
```
- [ ] **Step 2: Run the focused tests and verify RED** - [ ] **Step 2: Run the focused tests and verify RED**
Run: Run:
@@ -122,10 +137,11 @@ Run:
PATH="/opt/homebrew/opt/docker/bin:$PATH" \ PATH="/opt/homebrew/opt/docker/bin:$PATH" \
npm run test:php -- --filter BlockRenderingTest npm run test:php -- --filter BlockRenderingTest
node --test tests/smoke/theme-preference-contract.test.mjs node --test tests/smoke/theme-preference-contract.test.mjs
npm run test:js -- --runInBand tests/js/search.test.js
``` ```
Expected: FAIL because the theme trigger and panel IDs do not exist and the Expected: FAIL because the theme trigger and panel IDs do not exist and the
header-tools block still owns `search.js`. search button does not yet expose its visible server-rendered state.
- [ ] **Step 3: Implement minimal progressive markup** - [ ] **Step 3: Implement minimal progressive markup**
@@ -142,9 +158,10 @@ Render a wrapper containing:
Use `wp_unique_id()`, translation functions, context-appropriate escaping, and Use `wp_unique_id()`, translation functions, context-appropriate escaping, and
the existing status strings. Wrap the search input in a controlled search-row the existing status strings. Wrap the search input in a controlled search-row
element with a stable ID. Point the Search button's `aria-controls` to that ID element with a stable ID. Point the Search button's `aria-controls` to that ID
and initialize `aria-expanded="false"`. Keep the native GET form. Remove and render `aria-expanded="true"` while the no-JavaScript row is visible. Keep
`search.js` from `blocks/header-tools/block.json`; `navigation.js` remains the native GET form and the header-tools block's existing `search.js` ownership.
loaded by the mobile menu block. On initialization, `search.js` hides the controlled row and synchronizes the
button to `aria-expanded="false"`; open and Escape synchronize both states.
- [ ] **Step 4: Run the focused tests and verify GREEN** - [ ] **Step 4: Run the focused tests and verify GREEN**
@@ -156,7 +173,8 @@ Expected: PASS with no PHP warnings or Node test failures.
```bash ```bash
git add blocks/theme-toggle/render.php blocks/header-tools/render.php \ git add blocks/theme-toggle/render.php blocks/header-tools/render.php \
blocks/header-tools/block.json tests/php/BlockRenderingTest.php \ assets/js/search.js tests/php/BlockRenderingTest.php \
tests/js/search.test.js \
tests/smoke/theme-preference-contract.test.mjs tests/smoke/theme-preference-contract.test.mjs
git diff --cached --check git diff --cached --check
git commit -m "feat: define compact CHRNX header controls" git commit -m "feat: define compact CHRNX header controls"
@@ -166,6 +184,7 @@ git commit -m "feat: define compact CHRNX header controls"
**Files:** **Files:**
- Modify: `blocks/header-tools/block.json`
- Modify: `assets/js/navigation.js` - Modify: `assets/js/navigation.js`
- Delete: `assets/js/search.js` - Delete: `assets/js/search.js`
- Modify: `tests/js/navigation.test.js` - Modify: `tests/js/navigation.test.js`
@@ -247,7 +266,8 @@ Rules:
- Never intercept a non-empty native search submit or Tab. - Never intercept a non-empty native search submit or Tab.
- Fail closed when any required element or media API is absent. - Fail closed when any required element or media API is absent.
Remove `search.js` and its standalone test only after the merged tests cover all Remove the header-tools block's `search.js` metadata ownership, `search.js`
itself, and its standalone test atomically only after the merged tests cover all
three original search behaviors. three original search behaviors.
- [ ] **Step 4: Run Jest and verify GREEN** - [ ] **Step 4: Run Jest and verify GREEN**
@@ -264,8 +284,8 @@ Expected: PASS; no duplicate listeners or remote calls.
- [ ] **Step 5: Commit controller behavior** - [ ] **Step 5: Commit controller behavior**
```bash ```bash
git add assets/js/navigation.js assets/js/search.js \ git add blocks/header-tools/block.json assets/js/navigation.js \
tests/js/navigation.test.js tests/js/search.test.js assets/js/search.js tests/js/navigation.test.js tests/js/search.test.js
git diff --cached --check git diff --cached --check
git commit -m "feat: coordinate CHRNX mobile header panels" git commit -m "feat: coordinate CHRNX mobile header panels"
``` ```
+13 -4
View File
@@ -2,11 +2,14 @@ const modulePath = '../../assets/js/search.js';
function markup() { function markup() {
document.body.innerHTML = ` document.body.innerHTML = `
<form data-chrnx-search action="/" role="search"> <form data-chrnx-search action="/" role="search" data-expanded="true">
<div id="chrnx-search-panel" data-chrnx-search-panel>
<label for="chrnx-search-input">Search</label> <label for="chrnx-search-input">Search</label>
<input id="chrnx-search-input" name="s" type="search"> <input id="chrnx-search-input" name="s" type="search">
</div>
<button type="submit" data-chrnx-search-button data-label-open="Open search" <button type="submit" data-chrnx-search-button data-label-open="Open search"
data-label-submit="Search">Search</button> data-label-submit="Search" aria-controls="chrnx-search-panel"
aria-expanded="true">Search</button>
</form> </form>
`; `;
} }
@@ -24,11 +27,15 @@ describe( 'progressive header search', () => {
const form = document.querySelector( 'form' ); const form = document.querySelector( 'form' );
const input = document.querySelector( 'input' ); const input = document.querySelector( 'input' );
const button = document.querySelector( 'button' ); const button = document.querySelector( 'button' );
const panel = document.querySelector( '[data-chrnx-search-panel]' );
expect( panel.hidden ).toBe( true );
expect( button.getAttribute( 'aria-expanded' ) ).toBe( 'false' );
const event = new Event( 'submit', { bubbles: true, cancelable: true } ); const event = new Event( 'submit', { bubbles: true, cancelable: true } );
form.dispatchEvent( event ); form.dispatchEvent( event );
expect( event.defaultPrevented ).toBe( true ); expect( event.defaultPrevented ).toBe( true );
expect( form.dataset.expanded ).toBe( 'true' ); expect( form.dataset.expanded ).toBe( 'true' );
expect( input.hidden ).toBe( false ); expect( panel.hidden ).toBe( false );
expect( button.getAttribute( 'aria-expanded' ) ).toBe( 'true' );
expect( document.activeElement ).toBe( input ); expect( document.activeElement ).toBe( input );
expect( button.getAttribute( 'aria-label' ) ).toBe( 'Search' ); expect( button.getAttribute( 'aria-label' ) ).toBe( 'Search' );
} ); } );
@@ -52,10 +59,12 @@ describe( 'progressive header search', () => {
const form = document.querySelector( 'form' ); const form = document.querySelector( 'form' );
const input = document.querySelector( 'input' ); const input = document.querySelector( 'input' );
const button = document.querySelector( 'button' ); const button = document.querySelector( 'button' );
const panel = document.querySelector( '[data-chrnx-search-panel]' );
form.dispatchEvent( new Event( 'submit', { bubbles: true, cancelable: true } ) ); form.dispatchEvent( new Event( 'submit', { bubbles: true, cancelable: true } ) );
input.dispatchEvent( new window.KeyboardEvent( 'keydown', { key: 'Escape', bubbles: true } ) ); input.dispatchEvent( new window.KeyboardEvent( 'keydown', { key: 'Escape', bubbles: true } ) );
expect( form.dataset.expanded ).toBe( 'false' ); expect( form.dataset.expanded ).toBe( 'false' );
expect( input.hidden ).toBe( true ); expect( panel.hidden ).toBe( true );
expect( button.getAttribute( 'aria-expanded' ) ).toBe( 'false' );
expect( document.activeElement ).toBe( button ); expect( document.activeElement ).toBe( button );
expect( button.getAttribute( 'aria-label' ) ).toBe( 'Open search' ); expect( button.getAttribute( 'aria-label' ) ).toBe( 'Open search' );
} ); } );
+7 -4
View File
@@ -72,16 +72,19 @@ class BlockRenderingTest extends WP_UnitTestCase {
$this->assertStringContainsString( 'chrnx-screen-reader-text">Buscar</span>', $output ); $this->assertStringContainsString( 'chrnx-screen-reader-text">Buscar</span>', $output );
$this->assertStringContainsString( 'data-label-open="Abrir búsqueda"', $output ); $this->assertStringContainsString( 'data-label-open="Abrir búsqueda"', $output );
$this->assertMatchesRegularExpression( $this->assertMatchesRegularExpression(
'/data-chrnx-search-button[^>]+aria-controls="([^"]+)"[^>]+aria-expanded="false"/', '/data-chrnx-search-button[^>]+aria-controls="([^"]+)"[^>]+aria-expanded="true"/',
$output $output
); );
preg_match( '/data-chrnx-search-button[^>]+aria-controls="([^"]+)"/', $output, $search_control ); preg_match( '/data-chrnx-search-button[^>]+aria-controls="([^"]+)"/', $output, $search_control );
$this->assertNotEmpty( $search_control[1] ); $this->assertNotEmpty( $search_control[1] );
$this->assertSame( 1, substr_count( $output, 'id="' . $search_control[1] . '"' ) ); $this->assertSame( 1, substr_count( $output, 'id="' . $search_control[1] . '"' ) );
$this->assertMatchesRegularExpression( preg_match(
'/<[^>]+id="' . preg_quote( $search_control[1], '/' ) . '"[^>]+data-chrnx-search-panel/', '/<[^>]+id="' . preg_quote( $search_control[1], '/' ) . '"[^>]+data-chrnx-search-panel[^>]*>/',
$output $output,
$search_panel
); );
$this->assertNotEmpty( $search_panel[0] );
$this->assertStringNotContainsString( ' hidden', $search_panel[0] );
} }
/** Theme preference keeps native controls behind one translated mobile trigger. */ /** Theme preference keeps native controls behind one translated mobile trigger. */
@@ -14,17 +14,17 @@ test('theme toggle metadata is dynamic, localized, and frontend-only', async ()
assert.equal(metadata.script, undefined); assert.equal(metadata.script, undefined);
}); });
test('header shell has one controller owner and progressive panel markup', async () => { test('header controls retain progressive script ownership and panel markup', async () => {
const headerToolsMetadata = JSON.parse(await readProjectFile('blocks/header-tools/block.json')); const headerToolsMetadata = JSON.parse(await readProjectFile('blocks/header-tools/block.json'));
const mobileMenuMetadata = JSON.parse(await readProjectFile('blocks/mobile-menu-toggle/block.json')); const mobileMenuMetadata = JSON.parse(await readProjectFile('blocks/mobile-menu-toggle/block.json'));
const headerToolsRender = await readProjectFile('blocks/header-tools/render.php'); const headerToolsRender = await readProjectFile('blocks/header-tools/render.php');
const themeToggleRender = await readProjectFile('blocks/theme-toggle/render.php'); const themeToggleRender = await readProjectFile('blocks/theme-toggle/render.php');
assert.equal(headerToolsMetadata.viewScript, undefined); assert.equal(headerToolsMetadata.viewScript, 'file:../../assets/js/search.js');
assert.equal(mobileMenuMetadata.viewScript, 'file:../../assets/js/navigation.js'); assert.equal(mobileMenuMetadata.viewScript, 'file:../../assets/js/navigation.js');
assert.match(headerToolsRender, /data-chrnx-search-panel/); assert.match(headerToolsRender, /data-chrnx-search-panel/);
assert.match(headerToolsRender, /data-chrnx-search-button[\s\S]+aria-controls=/); assert.match(headerToolsRender, /data-chrnx-search-button[\s\S]+aria-controls=/);
assert.match(headerToolsRender, /aria-expanded="false"/); assert.match(headerToolsRender, /data-chrnx-search-button[\s\S]+aria-expanded="true"/);
assert.match(themeToggleRender, /data-chrnx-theme-button/); assert.match(themeToggleRender, /data-chrnx-theme-button/);
assert.match(themeToggleRender, /data-chrnx-theme-panel/); assert.match(themeToggleRender, /data-chrnx-theme-panel/);
assert.match(themeToggleRender, /'Choose color theme', 'chrnx'/); assert.match(themeToggleRender, /'Choose color theme', 'chrnx'/);