feat: define compact CHRNX header controls
This commit is contained in:
5 files changed
+100
-6
No files matched your search
@@ -8,6 +8,5 @@
|
||||
"description": "Display translated primary navigation and progressive site search.",
|
||||
"textdomain": "chrnx",
|
||||
"supports": { "html": false },
|
||||
"viewScript": "file:../../assets/js/search.js",
|
||||
"render": "file:./render.php"
|
||||
}
|
||||
@@ -5,7 +5,7 @@
|
||||
* @package CHRNX
|
||||
*/
|
||||
|
||||
$chrnx_header_links = array(
|
||||
$chrnx_header_links = array(
|
||||
array(
|
||||
'label' => __( 'Journal', 'chrnx' ),
|
||||
'url' => home_url( '/journal/' ),
|
||||
@@ -27,8 +27,9 @@ $chrnx_header_links = array(
|
||||
'url' => home_url( '/about/' ),
|
||||
),
|
||||
);
|
||||
$chrnx_search_label = __( 'Search', 'chrnx' );
|
||||
$chrnx_search_id = wp_unique_id( 'chrnx-search-' );
|
||||
$chrnx_search_label = __( 'Search', 'chrnx' );
|
||||
$chrnx_search_id = wp_unique_id( 'chrnx-search-' );
|
||||
$chrnx_search_panel_id = wp_unique_id( 'chrnx-search-panel-' );
|
||||
?>
|
||||
<nav id="chrnx-primary-navigation" class="chrnx-primary-navigation" aria-label="<?php echo esc_attr__( 'Primary navigation', 'chrnx' ); ?>">
|
||||
<ul>
|
||||
@@ -38,11 +39,15 @@ $chrnx_search_id = wp_unique_id( 'chrnx-search-' );
|
||||
</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">
|
||||
<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>
|
||||
<input id="<?php echo esc_attr( $chrnx_search_id ); ?>" type="search" name="s" autocomplete="off">
|
||||
</div>
|
||||
<button
|
||||
type="submit"
|
||||
data-chrnx-search-button
|
||||
aria-controls="<?php echo esc_attr( $chrnx_search_panel_id ); ?>"
|
||||
aria-expanded="false"
|
||||
data-label-open="<?php echo esc_attr__( 'Open search', 'chrnx' ); ?>"
|
||||
data-label-submit="<?php echo esc_attr( $chrnx_search_label ); ?>"
|
||||
>
|
||||
|
||||
@@ -6,8 +6,22 @@
|
||||
*/
|
||||
|
||||
$chrnx_control_name = wp_unique_id( 'chrnx-theme-' );
|
||||
$chrnx_panel_id = wp_unique_id( 'chrnx-theme-panel-' );
|
||||
$chrnx_button_label = __( 'Choose color theme', 'chrnx' );
|
||||
?>
|
||||
<div class="chrnx-theme-control">
|
||||
<button
|
||||
type="button"
|
||||
hidden
|
||||
data-chrnx-theme-button
|
||||
aria-controls="<?php echo esc_attr( $chrnx_panel_id ); ?>"
|
||||
aria-expanded="false"
|
||||
aria-label="<?php echo esc_attr( $chrnx_button_label ); ?>"
|
||||
>
|
||||
<span aria-hidden="true">◐</span>
|
||||
</button>
|
||||
<fieldset
|
||||
id="<?php echo esc_attr( $chrnx_panel_id ); ?>"
|
||||
<?php
|
||||
// WordPress escapes each generated wrapper attribute.
|
||||
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
@@ -18,6 +32,7 @@ $chrnx_control_name = wp_unique_id( 'chrnx-theme-' );
|
||||
)
|
||||
);
|
||||
?>
|
||||
data-chrnx-theme-panel
|
||||
data-status-light="<?php esc_attr_e( 'Light theme active', 'chrnx' ); ?>"
|
||||
data-status-dark="<?php esc_attr_e( 'Dark theme active', 'chrnx' ); ?>"
|
||||
data-status-system-light="<?php esc_attr_e( 'Light theme active, following system preference', 'chrnx' ); ?>"
|
||||
@@ -30,3 +45,4 @@ $chrnx_control_name = wp_unique_id( 'chrnx-theme-' );
|
||||
<output data-chrnx-theme-status aria-live="polite"><?php esc_html_e( 'Following system theme', 'chrnx' ); ?></output>
|
||||
</fieldset>
|
||||
<noscript><p class="chrnx-theme-noscript"><?php esc_html_e( 'Enable JavaScript to choose a color theme.', 'chrnx' ); ?></p></noscript>
|
||||
</div>
|
||||
@@ -70,6 +70,63 @@ class BlockRenderingTest extends WP_UnitTestCase {
|
||||
$this->assertStringContainsString( 'name="s"', $output );
|
||||
$this->assertStringContainsString( 'chrnx-screen-reader-text">Buscar</span>', $output );
|
||||
$this->assertStringContainsString( 'data-label-open="Abrir búsqueda"', $output );
|
||||
$this->assertMatchesRegularExpression(
|
||||
'/data-chrnx-search-button[^>]+aria-controls="([^"]+)"[^>]+aria-expanded="false"/',
|
||||
$output
|
||||
);
|
||||
preg_match( '/data-chrnx-search-button[^>]+aria-controls="([^"]+)"/', $output, $search_control );
|
||||
$this->assertNotEmpty( $search_control[1] );
|
||||
$this->assertSame( 1, substr_count( $output, 'id="' . $search_control[1] . '"' ) );
|
||||
$this->assertMatchesRegularExpression(
|
||||
'/<[^>]+id="' . preg_quote( $search_control[1], '/' ) . '"[^>]+data-chrnx-search-panel/',
|
||||
$output
|
||||
);
|
||||
}
|
||||
|
||||
/** Theme preference keeps native controls behind one translated mobile trigger. */
|
||||
public function test_theme_toggle_renders_accessible_mobile_trigger_and_native_panel() {
|
||||
$translate = static function ( $translated, $text, $domain ) {
|
||||
if ( 'chrnx' === $domain && 'Choose color theme' === $text ) {
|
||||
return 'Elegir tema de color';
|
||||
}
|
||||
return $translated;
|
||||
};
|
||||
add_filter( 'gettext', $translate, 10, 3 );
|
||||
try {
|
||||
$output = render_block(
|
||||
array(
|
||||
'blockName' => 'chrnx/theme-toggle',
|
||||
'attrs' => array(),
|
||||
'innerBlocks' => array(),
|
||||
'innerHTML' => '',
|
||||
'innerContent' => array(),
|
||||
)
|
||||
);
|
||||
} finally {
|
||||
remove_filter( 'gettext', $translate, 10 );
|
||||
}
|
||||
|
||||
$this->assertSame( 1, substr_count( $output, 'data-chrnx-theme-button' ) );
|
||||
$this->assertMatchesRegularExpression(
|
||||
'/data-chrnx-theme-button[^>]+aria-controls="([^"]+)"[^>]+aria-expanded="false"[^>]+aria-label="Elegir tema de color"/',
|
||||
$output
|
||||
);
|
||||
preg_match( '/data-chrnx-theme-button[^>]+aria-controls="([^"]+)"/', $output, $theme_control );
|
||||
$this->assertNotEmpty( $theme_control[1] );
|
||||
$this->assertSame( 1, substr_count( $output, 'id="' . $theme_control[1] . '"' ) );
|
||||
$this->assertMatchesRegularExpression(
|
||||
'/<fieldset[^>]+id="' . preg_quote( $theme_control[1], '/' ) . '"[^>]+data-chrnx-theme-toggle[^>]+data-chrnx-theme-panel/',
|
||||
$output
|
||||
);
|
||||
$this->assertSame( 3, substr_count( $output, 'type="radio"' ) );
|
||||
foreach ( array( 'light', 'dark', 'system' ) as $value ) {
|
||||
$this->assertMatchesRegularExpression(
|
||||
'/<input[^>]+type="radio"[^>]+value="' . $value . '"/',
|
||||
$output
|
||||
);
|
||||
}
|
||||
$this->assertStringContainsString( 'data-chrnx-theme-status', $output );
|
||||
$this->assertStringContainsString( '<noscript>', $output );
|
||||
}
|
||||
|
||||
/** Mobile menu control is localized, initially hidden, and targets the one primary navigation. */
|
||||
|
||||
@@ -14,6 +14,23 @@ test('theme toggle metadata is dynamic, localized, and frontend-only', async ()
|
||||
assert.equal(metadata.script, undefined);
|
||||
});
|
||||
|
||||
test('header shell has one controller owner and progressive panel markup', async () => {
|
||||
const headerToolsMetadata = JSON.parse(await readProjectFile('blocks/header-tools/block.json'));
|
||||
const mobileMenuMetadata = JSON.parse(await readProjectFile('blocks/mobile-menu-toggle/block.json'));
|
||||
const headerToolsRender = await readProjectFile('blocks/header-tools/render.php');
|
||||
const themeToggleRender = await readProjectFile('blocks/theme-toggle/render.php');
|
||||
|
||||
assert.equal(headerToolsMetadata.viewScript, undefined);
|
||||
assert.equal(mobileMenuMetadata.viewScript, 'file:../../assets/js/navigation.js');
|
||||
assert.match(headerToolsRender, /data-chrnx-search-panel/);
|
||||
assert.match(headerToolsRender, /data-chrnx-search-button[\s\S]+aria-controls=/);
|
||||
assert.match(headerToolsRender, /aria-expanded="false"/);
|
||||
assert.match(themeToggleRender, /data-chrnx-theme-button/);
|
||||
assert.match(themeToggleRender, /data-chrnx-theme-panel/);
|
||||
assert.match(themeToggleRender, /'Choose color theme', 'chrnx'/);
|
||||
assert.match(themeToggleRender, /wp_unique_id\( 'chrnx-theme-panel-'/);
|
||||
});
|
||||
|
||||
for (const asset of ['chrnx-cat.svg', 'chrnx-wordmark.svg']) {
|
||||
test(`${asset} is an inert, local, vector-only asset`, async () => {
|
||||
const svg = await readProjectFile(`assets/icons/${asset}`);
|
||||
|
||||
Reference in new issue
Block a user