feat: rebuild the header controls as icon buttons

The theme switch was three labelled radios that wrapped the header onto
a second line with long translations. It is three SVG buttons now, with
search and menu sharing the same language: no resting outline, a filled
chip for the active choice.

Radios gave arrow-key roving for free, so the group reimplements it
rather than losing it. The first chip colour put accent on accent and
failed WCAG AA; the label carries full contrast instead. Mobile targets
sit at 45px because a floor of exactly 44 rounds to 43.99997 in Firefox.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
chrnxandClaude Opus 5 committed 2026-08-13 19:41:24 -05:00
1 parent f55d9d9b45
commit a142ece51d
16 files changed
+316 -156

No files matched your search

+84 -40
View File
@@ -101,26 +101,64 @@ html[data-theme="light"] header.wp-block-template-part {
height: auto; height: auto;
} }
.chrnx-mobile-menu-toggle { /* One icon language in the header: no resting outline, chip on hover/focus. */
.chrnx-mobile-menu-toggle,
.chrnx-header-search button,
.chrnx-theme-control > [data-chrnx-theme-button] {
display: inline-flex; display: inline-flex;
align-items: center; align-items: center;
justify-content: center; justify-content: center;
min-width: 45px; min-width: 45px;
min-height: 45px; min-height: 45px;
padding: 0; padding: 0;
border: 1px solid currentcolor; border: 0;
border-radius: 0.35rem;
background: transparent; background: transparent;
color: inherit; color: inherit;
font-size: 1.4rem; font: inherit;
cursor: pointer;
opacity: 0.65;
} }
.chrnx-mobile-menu-toggle[hidden] { .chrnx-mobile-menu-toggle:hover,
.chrnx-mobile-menu-toggle[aria-expanded="true"],
.chrnx-header-search button:hover,
.chrnx-theme-control > [data-chrnx-theme-button]:hover,
.chrnx-theme-control > [data-chrnx-theme-button][aria-expanded="true"] {
background: color-mix(in srgb, var(--wp--preset--color--green) 24%, transparent);
color: var(--wp--preset--color--text);
opacity: 1;
}
html[data-theme="light"] .chrnx-mobile-menu-toggle:hover,
html[data-theme="light"] .chrnx-mobile-menu-toggle[aria-expanded="true"],
html[data-theme="light"] .chrnx-header-search button:hover,
html[data-theme="light"] .chrnx-theme-control > [data-chrnx-theme-button]:hover,
html[data-theme="light"] .chrnx-theme-control > [data-chrnx-theme-button][aria-expanded="true"] {
background: color-mix(in srgb, var(--wp--preset--color--light-green) 20%, transparent);
color: var(--wp--preset--color--ink);
}
@media (prefers-color-scheme: light) {
:root:not([data-theme]) .chrnx-mobile-menu-toggle:hover,
:root:not([data-theme]) .chrnx-mobile-menu-toggle[aria-expanded="true"],
:root:not([data-theme]) .chrnx-header-search button:hover,
:root:not([data-theme]) .chrnx-theme-control > [data-chrnx-theme-button]:hover,
:root:not([data-theme]) .chrnx-theme-control > [data-chrnx-theme-button][aria-expanded="true"] {
background: color-mix(in srgb, var(--wp--preset--color--light-green) 20%, transparent);
color: var(--wp--preset--color--ink);
}
}
/* The shared rule above sets display, so hidden must win back explicitly. */
.chrnx-mobile-menu-toggle[hidden],
.chrnx-theme-control > [data-chrnx-theme-button][hidden] {
display: none; display: none;
} }
.chrnx-primary-navigation a, .chrnx-primary-navigation a,
.chrnx-footer-navigation a, .chrnx-footer-navigation a,
.chrnx-header-search button,
.chrnx-header-search input { .chrnx-header-search input {
min-width: 45px; min-width: 45px;
min-height: 45px; min-height: 45px;
@@ -155,18 +193,6 @@ html[data-theme="light"] header.wp-block-template-part {
padding-inline: 0.75rem; 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 { .chrnx-screen-reader-text {
position: absolute; position: absolute;
width: 1px; width: 1px;
@@ -193,7 +219,7 @@ html[data-theme="light"] header.wp-block-template-part {
display: flex; display: flex;
} }
.chrnx-theme-toggle legend { .chrnx-theme-toggle__legend {
position: absolute; position: absolute;
width: 1px; width: 1px;
height: 1px; height: 1px;
@@ -205,29 +231,50 @@ html[data-theme="light"] header.wp-block-template-part {
border: 0; border: 0;
} }
.chrnx-theme-toggle label { /* No resting outline: the active choice reads as a filled chip instead. */
.chrnx-theme-toggle [data-chrnx-theme-value] {
display: inline-flex; display: inline-flex;
align-items: center; align-items: center;
justify-content: center; justify-content: center;
min-width: 45px; min-width: 45px;
min-height: 45px; min-height: 45px;
padding: 0 0.45rem; padding: 0;
border: 1px solid transparent; border: 0;
border-radius: 0.35rem; border-radius: 0.35rem;
background: transparent;
color: inherit;
font: inherit;
cursor: pointer; cursor: pointer;
opacity: 0.65;
} }
.chrnx-theme-toggle label:has(input:checked) { .chrnx-theme-toggle [data-chrnx-theme-value] svg {
border-color: currentcolor; width: 20px;
background: var(--wp--preset--color--panel); height: 20px;
} }
html[data-theme="light"] .chrnx-theme-toggle label:has(input:checked) { .chrnx-theme-toggle [data-chrnx-theme-value]:hover {
background: var(--wp--preset--color--surface); opacity: 1;
} }
.chrnx-theme-toggle input { /* Quiet chip, full-contrast label: accent-on-accent fails WCAG AA. */
accent-color: var(--wp--preset--color--green); .chrnx-theme-toggle [data-chrnx-theme-value][aria-pressed="true"] {
background: color-mix(in srgb, var(--wp--preset--color--green) 24%, transparent);
color: var(--wp--preset--color--text);
opacity: 1;
}
html[data-theme="light"] .chrnx-theme-toggle [data-chrnx-theme-value][aria-pressed="true"] {
background: color-mix(in srgb, var(--wp--preset--color--light-green) 20%, transparent);
color: var(--wp--preset--color--ink);
}
@media (prefers-color-scheme: light) {
:root:not([data-theme]) .chrnx-theme-toggle [data-chrnx-theme-value][aria-pressed="true"] {
background: color-mix(in srgb, var(--wp--preset--color--light-green) 20%, transparent);
color: var(--wp--preset--color--ink);
}
} }
.js .chrnx-header-search[data-expanded="false"] input { .js .chrnx-header-search[data-expanded="false"] input {
@@ -407,16 +454,8 @@ html[data-theme="light"] footer.wp-block-template-part {
} }
.chrnx-theme-control > [data-chrnx-theme-button] { .chrnx-theme-control > [data-chrnx-theme-button] {
display: inline-flex;
align-items: center;
justify-content: center;
width: 45px; width: 45px;
height: 45px; height: 45px;
padding: 0;
border: 1px solid currentcolor;
background: transparent;
color: inherit;
cursor: pointer;
} }
.chrnx-navigation-enhanced .chrnx-theme-control > [data-chrnx-theme-button] { .chrnx-navigation-enhanced .chrnx-theme-control > [data-chrnx-theme-button] {
@@ -441,20 +480,25 @@ html[data-theme="light"] footer.wp-block-template-part {
min-width: 0; min-width: 0;
height: auto; height: auto;
padding: 0.5rem; padding: 0.5rem;
border: 1px solid currentcolor; border: 1px solid color-mix(in srgb, currentcolor 18%, transparent);
border-radius: 0.35rem; border-radius: 0.35rem;
background: var(--wp--preset--color--panel); background: var(--wp--preset--color--panel);
color: inherit; color: inherit;
box-shadow: 0 0.75rem 2rem rgb(0 0 0 / 28%);
} }
html[data-theme="light"].chrnx-navigation-enhanced .chrnx-theme-toggle { html[data-theme="light"].chrnx-navigation-enhanced .chrnx-theme-toggle {
background: var(--wp--preset--color--surface); background: var(--wp--preset--color--surface);
} }
.chrnx-navigation-enhanced .chrnx-theme-toggle label { /*
* 45px, not 44: a floor set exactly at the WCAG minimum rounds to
* 43.99997px in Firefox and fails the target-size check.
*/
.chrnx-navigation-enhanced .chrnx-theme-toggle [data-chrnx-theme-value] {
flex: 1 1 auto; flex: 1 1 auto;
min-width: 44px; min-width: 45px;
min-height: 44px; min-height: 45px;
white-space: normal; white-space: normal;
} }
} }
+2 -2
View File
@@ -50,7 +50,7 @@
const shell = menuButton && menuButton.parentElement; const shell = menuButton && menuButton.parentElement;
const brand = shell && shell.querySelector( '.chrnx-site-brand' ); const brand = shell && shell.querySelector( '.chrnx-site-brand' );
const navLink = nav && nav.querySelector( 'a[href]' ); const navLink = nav && nav.querySelector( 'a[href]' );
const themeRadio = themePanel && themePanel.querySelector( 'input[type="radio"]' ); const themeRadio = themePanel && themePanel.querySelector( '[data-chrnx-theme-value]' );
const required = [ const required = [
menuButton, menuButton,
nav, nav,
@@ -107,7 +107,7 @@
let activePanel = null; let activePanel = null;
const currentThemeRadio = () => const currentThemeRadio = () =>
themePanel.querySelector( 'input[type="radio"]:checked' ) || themeRadio; themePanel.querySelector( '[data-chrnx-theme-value][aria-pressed="true"]' ) || themeRadio;
const restoreOriginalOrder = () => { const restoreOriginalOrder = () => {
elements.forEach( ( element ) => { elements.forEach( ( element ) => {
const anchor = anchors.get( element ); const anchor = anchors.get( element );
+41 -5
View File
@@ -77,7 +77,9 @@
} }
} }
const controls = [ ...doc.querySelectorAll( '[data-chrnx-theme-toggle] input[type="radio"]' ) ]; const controls = [
...doc.querySelectorAll( '[data-chrnx-theme-toggle] [data-chrnx-theme-value]' ),
];
const groups = [ ...doc.querySelectorAll( '[data-chrnx-theme-toggle]' ) ]; const groups = [ ...doc.querySelectorAll( '[data-chrnx-theme-toggle]' ) ];
let explicitPreference = readPreference( storage ); let explicitPreference = readPreference( storage );
let listening = false; let listening = false;
@@ -89,7 +91,8 @@
applyTheme( doc, resolved ); applyTheme( doc, resolved );
const selected = explicitPreference || 'system'; const selected = explicitPreference || 'system';
controls.forEach( ( control ) => { controls.forEach( ( control ) => {
control.checked = control.value === selected; const pressed = control.dataset.chrnxThemeValue === selected;
control.setAttribute( 'aria-pressed', String( pressed ) );
} ); } );
groups.forEach( ( group ) => { groups.forEach( ( group ) => {
const status = group.querySelector( '[data-chrnx-theme-status]' ); const status = group.querySelector( '[data-chrnx-theme-status]' );
@@ -119,19 +122,52 @@
listening = shouldListen; listening = shouldListen;
}; };
const onChange = ( event ) => { const onChange = ( event ) => {
const value = event.currentTarget.value; const value = event.currentTarget.dataset.chrnxThemeValue;
explicitPreference = normalizePreference( value ); explicitPreference = normalizePreference( value );
writePreference( storage, value ); writePreference( storage, value );
setMediaListening( ! explicitPreference ); setMediaListening( ! explicitPreference );
update(); update();
}; };
controls.forEach( ( control ) => control.addEventListener( 'change', onChange ) ); // Buttons do not get the arrow-key roving a native radio group provides,
// so the group reimplements it: move focus and select, like radios did.
const STEPS = { ArrowLeft: -1, ArrowUp: -1, ArrowRight: 1, ArrowDown: 1 };
const onKeydown = ( event ) => {
const step = STEPS[ event.key ];
const toStart = event.key === 'Home';
const toEnd = event.key === 'End';
if ( step === undefined && ! toStart && ! toEnd ) {
return;
}
const group = event.currentTarget.closest( '[data-chrnx-theme-toggle]' );
const siblings = group ? [ ...group.querySelectorAll( '[data-chrnx-theme-value]' ) ] : [];
const index = siblings.indexOf( event.currentTarget );
if ( index < 0 ) {
return;
}
event.preventDefault();
let next = 0;
if ( toEnd ) {
next = siblings.length - 1;
} else if ( ! toStart ) {
next = ( index + step + siblings.length ) % siblings.length;
}
siblings[ next ].focus();
siblings[ next ].click();
};
controls.forEach( ( control ) => {
control.addEventListener( 'click', onChange );
control.addEventListener( 'keydown', onKeydown );
} );
setMediaListening( ! explicitPreference ); setMediaListening( ! explicitPreference );
update(); update();
cleanupCurrent = () => { cleanupCurrent = () => {
controls.forEach( ( control ) => control.removeEventListener( 'change', onChange ) ); controls.forEach( ( control ) => {
control.removeEventListener( 'click', onChange );
control.removeEventListener( 'keydown', onKeydown );
} );
setMediaListening( false ); setMediaListening( false );
cleanupCurrent = null; cleanupCurrent = null;
}; };
+1 -1
View File
@@ -2,7 +2,7 @@
"$schema": "https://schemas.wp.org/trunk/block.json", "$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 3, "apiVersion": 3,
"name": "chrnx/header-tools", "name": "chrnx/header-tools",
"version": "0.1.0", "version": "0.2.0",
"title": "CHRNX Header Tools", "title": "CHRNX Header Tools",
"category": "theme", "category": "theme",
"description": "Display translated primary navigation and progressive site search.", "description": "Display translated primary navigation and progressive site search.",
+2 -23
View File
@@ -5,28 +5,7 @@
* @package CHRNX * @package CHRNX
*/ */
$chrnx_header_links = array( $chrnx_header_links = chrnx_get_navigation_links( 'chrnx-primary' );
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_label = __( 'Search', 'chrnx' );
$chrnx_search_id = wp_unique_id( 'chrnx-search-' ); $chrnx_search_id = wp_unique_id( 'chrnx-search-' );
$chrnx_search_panel_id = wp_unique_id( 'chrnx-search-panel-' ); $chrnx_search_panel_id = wp_unique_id( 'chrnx-search-panel-' );
@@ -52,7 +31,7 @@ $chrnx_current_url = chrnx_get_current_navigation_destination();
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 ); ?>"
> >
<span aria-hidden="true">⌕</span> <?php echo chrnx_get_icon_markup( 'search' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Helper returns a fixed literal SVG. ?>
<span class="chrnx-screen-reader-text"><?php echo esc_html( $chrnx_search_label ); ?></span> <span class="chrnx-screen-reader-text"><?php echo esc_html( $chrnx_search_label ); ?></span>
</button> </button>
</form> </form>
+1 -1
View File
@@ -2,7 +2,7 @@
"$schema": "https://schemas.wp.org/trunk/block.json", "$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 3, "apiVersion": 3,
"name": "chrnx/mobile-menu-toggle", "name": "chrnx/mobile-menu-toggle",
"version": "0.1.0", "version": "0.2.0",
"title": "CHRNX Mobile Menu Toggle", "title": "CHRNX Mobile Menu Toggle",
"category": "theme", "category": "theme",
"description": "Progressively reveal the primary navigation on small screens.", "description": "Progressively reveal the primary navigation on small screens.",
+1 -1
View File
@@ -27,5 +27,5 @@ $chrnx_close_label = __( 'Close primary navigation', 'chrnx' );
data-label-open="<?php echo esc_attr( $chrnx_open_label ); ?>" data-label-open="<?php echo esc_attr( $chrnx_open_label ); ?>"
data-label-close="<?php echo esc_attr( $chrnx_close_label ); ?>" data-label-close="<?php echo esc_attr( $chrnx_close_label ); ?>"
> >
<span aria-hidden="true">☰</span> <?php echo chrnx_get_icon_markup( 'menu' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Helper returns a fixed literal SVG. ?>
</button> </button>
+1 -1
View File
@@ -2,7 +2,7 @@
"$schema": "https://schemas.wp.org/trunk/block.json", "$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 3, "apiVersion": 3,
"name": "chrnx/theme-toggle", "name": "chrnx/theme-toggle",
"version": "0.1.0", "version": "0.2.0",
"title": "Theme Toggle", "title": "Theme Toggle",
"category": "theme", "category": "theme",
"description": "Choose a light, dark, or system color theme.", "description": "Choose a light, dark, or system color theme.",
+33 -8
View File
@@ -5,9 +5,23 @@
* @package CHRNX * @package CHRNX
*/ */
$chrnx_control_name = wp_unique_id( 'chrnx-theme-' );
$chrnx_panel_id = wp_unique_id( 'chrnx-theme-panel-' ); $chrnx_panel_id = wp_unique_id( 'chrnx-theme-panel-' );
$chrnx_legend_id = wp_unique_id( 'chrnx-theme-legend-' );
$chrnx_button_label = __( 'Choose color theme', 'chrnx' ); $chrnx_button_label = __( 'Choose color theme', 'chrnx' );
$chrnx_choices = array(
'light' => array(
'label' => __( 'Light', 'chrnx' ),
'icon' => '<circle cx="12" cy="12" r="4"/><path d="M12 2v2M12 20v2M4.9 4.9l1.4 1.4M17.7 17.7l1.4 1.4M2 12h2M20 12h2M4.9 19.1l1.4-1.4M17.7 6.3l1.4-1.4"/>',
),
'dark' => array(
'label' => __( 'Dark', 'chrnx' ),
'icon' => '<path d="M20 14.5A8.5 8.5 0 0 1 9.5 4a8.5 8.5 0 1 0 10.5 10.5Z"/>',
),
'system' => array(
'label' => __( 'System', 'chrnx' ),
'icon' => '<rect x="3" y="4" width="18" height="12" rx="2"/><path d="M8 20h8M12 16v4"/>',
),
);
?> ?>
<div class="chrnx-theme-control"> <div class="chrnx-theme-control">
<button <button
@@ -18,10 +32,12 @@ $chrnx_button_label = __( 'Choose color theme', 'chrnx' );
aria-expanded="false" aria-expanded="false"
aria-label="<?php echo esc_attr( $chrnx_button_label ); ?>" aria-label="<?php echo esc_attr( $chrnx_button_label ); ?>"
> >
<span aria-hidden="true">◐</span> <?php echo chrnx_get_icon_markup( 'theme' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Helper returns a fixed literal SVG. ?>
</button> </button>
<fieldset <div
id="<?php echo esc_attr( $chrnx_panel_id ); ?>" id="<?php echo esc_attr( $chrnx_panel_id ); ?>"
role="group"
aria-labelledby="<?php echo esc_attr( $chrnx_legend_id ); ?>"
<?php <?php
// WordPress escapes each generated wrapper attribute. // WordPress escapes each generated wrapper attribute.
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
@@ -38,11 +54,20 @@ $chrnx_button_label = __( 'Choose color theme', 'chrnx' );
data-status-system-light="<?php esc_attr_e( 'Light theme active, following system preference', 'chrnx' ); ?>" data-status-system-light="<?php esc_attr_e( 'Light theme active, following system preference', 'chrnx' ); ?>"
data-status-system-dark="<?php esc_attr_e( 'Dark theme active, following system preference', 'chrnx' ); ?>" data-status-system-dark="<?php esc_attr_e( 'Dark theme active, following system preference', 'chrnx' ); ?>"
> >
<legend><?php esc_html_e( 'Color theme', 'chrnx' ); ?></legend> <span class="chrnx-theme-toggle__legend" id="<?php echo esc_attr( $chrnx_legend_id ); ?>"><?php esc_html_e( 'Color theme', 'chrnx' ); ?></span>
<label><input type="radio" name="<?php echo esc_attr( $chrnx_control_name ); ?>" value="light"> <?php esc_html_e( 'Light', 'chrnx' ); ?></label> <?php
<label><input type="radio" name="<?php echo esc_attr( $chrnx_control_name ); ?>" value="dark"> <?php esc_html_e( 'Dark', 'chrnx' ); ?></label> foreach ( $chrnx_choices as $chrnx_value => $chrnx_choice ) {
<label><input type="radio" name="<?php echo esc_attr( $chrnx_control_name ); ?>" value="system" checked> <?php esc_html_e( 'System', 'chrnx' ); ?></label> printf(
// The icon geometry is a fixed literal above, never user input.
'<button type="button" data-chrnx-theme-value="%1$s" aria-pressed="%2$s" title="%3$s"><svg viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">%4$s</svg><span class="chrnx-screen-reader-text">%3$s</span></button>',
esc_attr( $chrnx_value ),
'system' === $chrnx_value ? 'true' : 'false',
esc_html( $chrnx_choice['label'] ),
$chrnx_choice['icon'] // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
);
}
?>
<output data-chrnx-theme-status aria-live="polite"><?php esc_html_e( 'Following system theme', 'chrnx' ); ?></output> <output data-chrnx-theme-status aria-live="polite"><?php esc_html_e( 'Following system theme', 'chrnx' ); ?></output>
</fieldset> </div>
<noscript><p class="chrnx-theme-noscript"><?php esc_html_e( 'Enable JavaScript to choose a color theme.', 'chrnx' ); ?></p></noscript> <noscript><p class="chrnx-theme-noscript"><?php esc_html_e( 'Enable JavaScript to choose a color theme.', 'chrnx' ); ?></p></noscript>
</div> </div>
+1 -1
View File
@@ -3,6 +3,6 @@ Theme Name: CHRNX
Description: A native block theme for chrnx.dev. Description: A native block theme for chrnx.dev.
Requires at least: 6.9 Requires at least: 6.9
Requires PHP: 8.1 Requires PHP: 8.1
Version: 0.1.0 Version: 0.2.0
Text Domain: chrnx Text Domain: chrnx
*/ */
+25 -16
View File
@@ -547,12 +547,12 @@ test.describe( 'site navigation shell', () => {
expect( themeBox.x ).toBeGreaterThanOrEqual( 0 ); expect( themeBox.x ).toBeGreaterThanOrEqual( 0 );
expect( themeBox.x + themeBox.width ).toBeLessThanOrEqual( width ); expect( themeBox.x + themeBox.width ).toBeLessThanOrEqual( width );
for ( const name of [ 'Light', 'Dark', 'System' ] ) { for ( const name of [ 'Light', 'Dark', 'System' ] ) {
const radio = header.getByRole( 'radio', { name, exact: true } ); const radio = header.getByRole( 'button', { name, exact: true } );
await expect( radio ).toBeVisible(); await expect( radio ).toBeVisible();
const radioBox = await getBox( radio ); const radioBox = await getBox( radio );
expect( radioBox.width ).toBeGreaterThan( 0 ); expect( radioBox.width ).toBeGreaterThan( 0 );
expect( radioBox.height ).toBeGreaterThan( 0 ); expect( radioBox.height ).toBeGreaterThan( 0 );
await expectContainedTarget( radio.locator( '..' ), width ); await expectContainedTarget( radio, width );
} }
await expectNoHorizontalOverflow( page, header ); await expectNoHorizontalOverflow( page, header );
} ); } );
@@ -566,17 +566,20 @@ test.describe( 'site navigation shell', () => {
await page.setViewportSize( { width, height: 844 } ); await page.setViewportSize( { width, height: 844 } );
await page.goto( '/' ); await page.goto( '/' );
if ( scenario === 'long translated labels' ) { if ( scenario === 'long translated labels' ) {
await page.locator( '[data-chrnx-theme-panel] label' ).evaluateAll( ( labels ) => { await page
const translations = [ .locator( '[data-chrnx-theme-panel] [data-chrnx-theme-value]' )
'Helles Erscheinungsbild verwenden', .evaluateAll( ( buttons ) => {
'Dunkles Erscheinungsbild verwenden', const translations = [
'Automatisch dem Farbschema des Betriebssystems folgen', 'Helles Erscheinungsbild verwenden',
]; 'Dunkles Erscheinungsbild verwenden',
labels.forEach( ( label, index ) => { 'Automatisch dem Farbschema des Betriebssystems folgen',
const text = Array.from( label.childNodes ).find( ( node ) => node.nodeType === 3 ); ];
text.nodeValue = ` ${ translations[ index ] }`; // The label is the accessible name only; icons carry the layout.
buttons.forEach( ( button, index ) => {
button.querySelector( '.chrnx-screen-reader-text' ).textContent =
translations[ index ];
} );
} ); } );
} );
} else { } else {
await page.evaluate( () => { await page.evaluate( () => {
document.documentElement.style.fontSize = '200%'; document.documentElement.style.fontSize = '200%';
@@ -650,7 +653,7 @@ test.describe( 'site navigation shell', () => {
const themePanel = header.locator( '[data-chrnx-theme-panel]' ); const themePanel = header.locator( '[data-chrnx-theme-panel]' );
await expect( nav ).toBeVisible(); await expect( nav ).toBeVisible();
await expect( themePanel ).toBeVisible(); await expect( themePanel ).toBeVisible();
await expect( header.getByRole( 'radio', { name: 'System', exact: true } ) ).toBeVisible(); await expect( header.getByRole( 'button', { name: 'System', exact: true } ) ).toBeVisible();
await expect( header.locator( '[data-chrnx-menu-toggle]' ) ).toBeHidden(); await expect( header.locator( '[data-chrnx-menu-toggle]' ) ).toBeHidden();
await expect( header.locator( '[data-chrnx-theme-button]' ) ).toBeHidden(); await expect( header.locator( '[data-chrnx-theme-button]' ) ).toBeHidden();
await expect( header.getByRole( 'button', { name: 'Open search' } ) ).toBeVisible(); await expect( header.getByRole( 'button', { name: 'Open search' } ) ).toBeVisible();
@@ -791,7 +794,9 @@ test.describe( 'site navigation shell', () => {
await page.goto( '/' ); await page.goto( '/' );
header = page.getByRole( 'banner' ); header = page.getByRole( 'banner' );
let themeButton = header.locator( '[data-chrnx-theme-button]' ); let themeButton = header.locator( '[data-chrnx-theme-button]' );
let checkedTheme = header.locator( '[data-chrnx-theme-panel] input:checked' ); let checkedTheme = header.locator(
'[data-chrnx-theme-panel] [data-chrnx-theme-value][aria-pressed="true"]',
);
await expect( themeButton ).toBeVisible(); await expect( themeButton ).toBeVisible();
await themeButton.focus(); await themeButton.focus();
await page.setViewportSize( { width: 600, height: 844 } ); await page.setViewportSize( { width: 600, height: 844 } );
@@ -818,7 +823,9 @@ test.describe( 'site navigation shell', () => {
await page.setViewportSize( { width: 600, height: 844 } ); await page.setViewportSize( { width: 600, height: 844 } );
await page.goto( '/' ); await page.goto( '/' );
header = page.getByRole( 'banner' ); header = page.getByRole( 'banner' );
checkedTheme = header.locator( '[data-chrnx-theme-panel] input:checked' ); checkedTheme = header.locator(
'[data-chrnx-theme-panel] [data-chrnx-theme-value][aria-pressed="true"]',
);
themeButton = header.locator( '[data-chrnx-theme-button]' ); themeButton = header.locator( '[data-chrnx-theme-button]' );
await checkedTheme.focus(); await checkedTheme.focus();
await page.setViewportSize( { width: 390, height: 844 } ); await page.setViewportSize( { width: 390, height: 844 } );
@@ -832,7 +839,9 @@ test.describe( 'site navigation shell', () => {
const menuButton = header.locator( '[data-chrnx-menu-toggle]' ); const menuButton = header.locator( '[data-chrnx-menu-toggle]' );
const themeButton = header.locator( '[data-chrnx-theme-button]' ); const themeButton = header.locator( '[data-chrnx-theme-button]' );
const journal = header.getByRole( 'link', { name: 'Journal', exact: true } ); const journal = header.getByRole( 'link', { name: 'Journal', exact: true } );
const checkedTheme = header.locator( '[data-chrnx-theme-panel] input:checked' ); const checkedTheme = header.locator(
'[data-chrnx-theme-panel] [data-chrnx-theme-value][aria-pressed="true"]',
);
await menuButton.click(); await menuButton.click();
await journal.focus(); await journal.focus();
+32 -15
View File
@@ -47,25 +47,31 @@ test.describe( 'real theme preference shell', () => {
await page.goto( '/' ); await page.goto( '/' );
const groups = page.locator( '[data-chrnx-theme-toggle]' ); const groups = page.locator( '[data-chrnx-theme-toggle]' );
await expect( groups.first() ).toHaveAccessibleName( 'Color theme' ); await expect( groups.first() ).toHaveAccessibleName( 'Color theme' );
await groups.first().getByRole( 'radio', { name: 'Light', exact: true } ).check(); await groups.first().getByRole( 'button', { name: 'Light', exact: true } ).click();
await expect( page.locator( 'html' ) ).toHaveAttribute( 'data-theme', 'light' ); await expect( page.locator( 'html' ) ).toHaveAttribute( 'data-theme', 'light' );
await expect await expect
.poll( () => page.evaluate( ( key ) => localStorage.getItem( key ), preferenceKey ) ) .poll( () => page.evaluate( ( key ) => localStorage.getItem( key ), preferenceKey ) )
.toBe( 'light' ); .toBe( 'light' );
await page.reload(); await page.reload();
await expect( page.locator( 'html' ) ).toHaveAttribute( 'data-theme', 'light' ); await expect( page.locator( 'html' ) ).toHaveAttribute( 'data-theme', 'light' );
await expect( page.getByRole( 'radio', { name: 'Light', exact: true } ) ).toBeChecked(); await expect( page.getByRole( 'button', { name: 'Light', exact: true } ) ).toHaveAttribute(
await page.getByRole( 'radio', { name: 'Dark', exact: true } ).check(); 'aria-pressed',
'true',
);
await page.getByRole( 'button', { name: 'Dark', exact: true } ).click();
await page.reload(); await page.reload();
await expect( page.locator( 'html' ) ).toHaveAttribute( 'data-theme', 'dark' ); await expect( page.locator( 'html' ) ).toHaveAttribute( 'data-theme', 'dark' );
await expect( page.getByRole( 'radio', { name: 'Dark', exact: true } ) ).toBeChecked(); await expect( page.getByRole( 'button', { name: 'Dark', exact: true } ) ).toHaveAttribute(
'aria-pressed',
'true',
);
const instances = await page.locator( '[data-chrnx-theme-toggle]' ).count(); const instances = await page.locator( '[data-chrnx-theme-toggle]' ).count();
if ( instances > 1 ) { if ( instances > 1 ) {
await expect( page.getByRole( 'radio', { name: 'Dark', exact: true } ) ).toHaveCount( await expect( page.getByRole( 'button', { name: 'Dark', exact: true } ) ).toHaveCount(
instances, instances,
); );
for ( const radio of await page.getByRole( 'radio', { name: 'Dark', exact: true } ).all() ) { for ( const radio of await page.getByRole( 'button', { name: 'Dark', exact: true } ).all() ) {
await expect( radio ).toBeChecked(); await expect( radio ).toHaveAttribute( 'aria-pressed', 'true' );
} }
} }
} ); } );
@@ -85,7 +91,10 @@ test.describe( 'real theme preference shell', () => {
await page.reload(); await page.reload();
await expect( page.locator( 'html' ) ).toHaveAttribute( 'data-theme', saved ); await expect( page.locator( 'html' ) ).toHaveAttribute( 'data-theme', saved );
const savedName = saved[ 0 ].toUpperCase() + saved.slice( 1 ); const savedName = saved[ 0 ].toUpperCase() + saved.slice( 1 );
await expect( page.getByRole( 'radio', { name: savedName, exact: true } ) ).toBeChecked(); await expect( page.getByRole( 'button', { name: savedName, exact: true } ) ).toHaveAttribute(
'aria-pressed',
'true',
);
await page.emulateMedia( { colorScheme: saved } ); await page.emulateMedia( { colorScheme: saved } );
await page.emulateMedia( { colorScheme: operatingSystem } ); await page.emulateMedia( { colorScheme: operatingSystem } );
await expect( page.locator( 'html' ) ).toHaveAttribute( 'data-theme', saved ); await expect( page.locator( 'html' ) ).toHaveAttribute( 'data-theme', saved );
@@ -102,7 +111,7 @@ test.describe( 'real theme preference shell', () => {
await page.emulateMedia( { colorScheme: 'light' } ); await page.emulateMedia( { colorScheme: 'light' } );
await page.reload(); await page.reload();
await expect( page.locator( 'html' ) ).toHaveAttribute( 'data-theme', 'dark' ); await expect( page.locator( 'html' ) ).toHaveAttribute( 'data-theme', 'dark' );
await page.getByRole( 'radio', { name: 'System', exact: true } ).check(); await page.getByRole( 'button', { name: 'System', exact: true } ).click();
await expect await expect
.poll( () => page.evaluate( ( key ) => localStorage.getItem( key ), preferenceKey ) ) .poll( () => page.evaluate( ( key ) => localStorage.getItem( key ), preferenceKey ) )
.toBeNull(); .toBeNull();
@@ -110,7 +119,10 @@ test.describe( 'real theme preference shell', () => {
await page.emulateMedia( { colorScheme: 'dark' } ); await page.emulateMedia( { colorScheme: 'dark' } );
await expect( page.locator( 'html' ) ).toHaveAttribute( 'data-theme', 'dark' ); await expect( page.locator( 'html' ) ).toHaveAttribute( 'data-theme', 'dark' );
await page.reload(); await page.reload();
await expect( page.getByRole( 'radio', { name: 'System', exact: true } ) ).toBeChecked(); await expect( page.getByRole( 'button', { name: 'System', exact: true } ) ).toHaveAttribute(
'aria-pressed',
'true',
);
await expect( page.locator( 'html' ) ).toHaveAttribute( 'data-theme', 'dark' ); await expect( page.locator( 'html' ) ).toHaveAttribute( 'data-theme', 'dark' );
await expect await expect
.poll( () => page.evaluate( ( key ) => localStorage.getItem( key ), preferenceKey ) ) .poll( () => page.evaluate( ( key ) => localStorage.getItem( key ), preferenceKey ) )
@@ -139,7 +151,7 @@ test.describe( 'real theme preference shell', () => {
await expect( trigger ).toHaveAttribute( 'aria-expanded', 'false' ); await expect( trigger ).toHaveAttribute( 'aria-expanded', 'false' );
await trigger.click(); await trigger.click();
await page.getByRole( 'radio', { name: 'System', exact: true } ).focus(); await page.getByRole( 'button', { name: 'System', exact: true } ).focus();
await page.getByText( 'Made in Sinaloa, shared from home.', { exact: true } ).click(); await page.getByText( 'Made in Sinaloa, shared from home.', { exact: true } ).click();
await expect( panel ).toBeHidden(); await expect( panel ).toBeHidden();
await expect( trigger ).toBeVisible(); await expect( trigger ).toBeVisible();
@@ -176,16 +188,21 @@ test.describe( 'real theme preference shell', () => {
await expect( page.locator( 'html' ) ).toHaveAttribute( 'data-theme', 'dark' ); await expect( page.locator( 'html' ) ).toHaveAttribute( 'data-theme', 'dark' );
} ); } );
test( 'native radios expose state, keyboard interaction, status, and 44px targets', async ( { test( 'native toggle buttons expose state, keyboard interaction, status, and 44px targets', async ( {
page, page,
} ) => { } ) => {
await page.goto( '/' ); await page.goto( '/' );
const light = page.getByRole( 'radio', { name: 'Light', exact: true } ); const light = page.getByRole( 'button', { name: 'Light', exact: true } );
await light.focus(); await light.focus();
await page.keyboard.press( 'ArrowRight' ); await page.keyboard.press( 'ArrowRight' );
await expect( page.getByRole( 'radio', { name: 'Dark', exact: true } ) ).toBeChecked(); await expect( page.getByRole( 'button', { name: 'Dark', exact: true } ) ).toHaveAttribute(
'aria-pressed',
'true',
);
await expect( page.locator( '[data-chrnx-theme-status]' ) ).toHaveText( 'Dark theme active' ); await expect( page.locator( '[data-chrnx-theme-status]' ) ).toHaveText( 'Dark theme active' );
for ( const label of await page.locator( '[data-chrnx-theme-toggle] label' ).all() ) { for ( const label of await page
.locator( '[data-chrnx-theme-toggle] [data-chrnx-theme-value]' )
.all() ) {
const box = await label.boundingBox(); const box = await label.boundingBox();
expect( box.width ).toBeGreaterThanOrEqual( 44 ); expect( box.width ).toBeGreaterThanOrEqual( 44 );
expect( box.height ).toBeGreaterThanOrEqual( 44 ); expect( box.height ).toBeGreaterThanOrEqual( 44 );
+20 -16
View File
@@ -46,11 +46,11 @@ function shellMarkup() {
<div data-part="theme" class="chrnx-theme-control"> <div data-part="theme" class="chrnx-theme-control">
<button hidden type="button" data-chrnx-theme-button <button hidden type="button" data-chrnx-theme-button
aria-controls="chrnx-theme-panel" aria-expanded="false">Theme</button> aria-controls="chrnx-theme-panel" aria-expanded="false">Theme</button>
<fieldset id="chrnx-theme-panel" data-chrnx-theme-panel data-chrnx-theme-toggle> <div id="chrnx-theme-panel" role="group" aria-label="Color theme" data-chrnx-theme-panel data-chrnx-theme-toggle>
<label><input type="radio" name="theme" value="light"> Light</label> <button type="button" data-chrnx-theme-value="light" aria-pressed="false">Light</button>
<label><input type="radio" name="theme" value="dark"> Dark</label> <button type="button" data-chrnx-theme-value="dark" aria-pressed="false">Dark</button>
<label><input type="radio" name="theme" value="system" checked> System</label> <button type="button" data-chrnx-theme-value="system" aria-pressed="true">System</button>
</fieldset> </div>
</div> </div>
</header> </header>
<button data-outside>Outside</button> <button data-outside>Outside</button>
@@ -72,8 +72,10 @@ function controls() {
theme: document.querySelector( '[data-part="theme"]' ), theme: document.querySelector( '[data-part="theme"]' ),
themeButton: document.querySelector( '[data-chrnx-theme-button]' ), themeButton: document.querySelector( '[data-chrnx-theme-button]' ),
themePanel: document.querySelector( '[data-chrnx-theme-panel]' ), themePanel: document.querySelector( '[data-chrnx-theme-panel]' ),
checkedRadio: document.querySelector( '[data-chrnx-theme-panel] input:checked' ), checkedRadio: document.querySelector(
firstRadio: document.querySelector( '[data-chrnx-theme-panel] input' ), '[data-chrnx-theme-panel] [data-chrnx-theme-value][aria-pressed="true"]',
),
firstRadio: document.querySelector( '[data-chrnx-theme-panel] [data-chrnx-theme-value]' ),
outside: document.querySelector( '[data-outside]' ), outside: document.querySelector( '[data-outside]' ),
outsideContent: document.querySelector( '[data-outside-content]' ), outsideContent: document.querySelector( '[data-outside-content]' ),
}; };
@@ -104,10 +106,10 @@ function visibleFocusableElements() {
if ( element.hidden || element.closest( '[hidden]' ) ) { if ( element.hidden || element.closest( '[hidden]' ) ) {
return false; return false;
} }
if ( element.type !== 'radio' || element.checked ) { if ( ! element.dataset.chrnxThemeValue ) {
return true; return true;
} }
return ! document.querySelector( `input[type="radio"][name="${ element.name }"]:checked` ); return element.getAttribute( 'aria-pressed' ) === 'true';
} ); } );
} }
@@ -331,7 +333,7 @@ describe( 'coordinated CHRNX header shell', () => {
initialize( { document, media: createMedia( true ) } ); initialize( { document, media: createMedia( true ) } );
const control = controls(); const control = controls();
control[ buttonName ].click(); control[ buttonName ].click();
control[ panelName ].querySelector( 'input, a' ).focus(); control[ panelName ].querySelector( 'input, a, button' ).focus();
press( control[ panelName ], 'Escape' ); press( control[ panelName ], 'Escape' );
expect( control[ panelName ].hidden ).toBe( true ); expect( control[ panelName ].hidden ).toBe( true );
expect( document.activeElement ).toBe( control[ buttonName ] ); expect( document.activeElement ).toBe( control[ buttonName ] );
@@ -435,9 +437,11 @@ describe( 'coordinated CHRNX header shell', () => {
const media = createMedia( true ); const media = createMedia( true );
initialize( { document, media } ); initialize( { document, media } );
const control = controls(); const control = controls();
const currentRadio = control.themePanel.querySelector( `input[value="${ value }"]` ); const currentRadio = control.themePanel.querySelector(
control.checkedRadio.checked = false; `[data-chrnx-theme-value="${ value }"]`,
currentRadio.checked = true; );
control.checkedRadio.setAttribute( 'aria-pressed', 'false' );
currentRadio.setAttribute( 'aria-pressed', 'true' );
control.themeButton.focus(); control.themeButton.focus();
media.change( false ); media.change( false );
@@ -450,8 +454,8 @@ describe( 'coordinated CHRNX header shell', () => {
const media = createMedia( true ); const media = createMedia( true );
initialize( { document, media } ); initialize( { document, media } );
const control = controls(); const control = controls();
control.themePanel.querySelectorAll( 'input[type="radio"]' ).forEach( ( radio ) => { control.themePanel.querySelectorAll( '[data-chrnx-theme-value]' ).forEach( ( radio ) => {
radio.checked = false; radio.setAttribute( 'aria-pressed', 'false' );
} ); } );
control.themeButton.focus(); control.themeButton.focus();
@@ -562,7 +566,7 @@ describe( 'coordinated CHRNX header shell', () => {
test.each( [ test.each( [
[ 'brand', '.chrnx-site-brand' ], [ 'brand', '.chrnx-site-brand' ],
[ 'navigation link', '[data-part="nav"] a' ], [ 'navigation link', '[data-part="nav"] a' ],
[ 'theme radio', '[data-chrnx-theme-panel] input' ], [ 'theme button', '[data-chrnx-theme-panel] [data-chrnx-theme-value]' ],
] )( 'fails closed without the required %s', ( _name, selector ) => { ] )( 'fails closed without the required %s', ( _name, selector ) => {
const { initialize } = require( modulePath ); const { initialize } = require( modulePath );
document.querySelectorAll( selector ).forEach( ( element ) => element.remove() ); document.querySelectorAll( selector ).forEach( ( element ) => element.remove() );
+15 -12
View File
@@ -16,17 +16,18 @@ function createMedia( matches = false ) {
function toggleMarkup() { function toggleMarkup() {
document.body.innerHTML = ` document.body.innerHTML = `
<fieldset class="chrnx-theme-toggle" data-chrnx-theme-toggle <div class="chrnx-theme-toggle" role="group" aria-label="Color theme" data-chrnx-theme-toggle
data-group="theme-a"
data-status-light="Light theme active" data-status-light="Light theme active"
data-status-dark="Dark theme active" data-status-dark="Dark theme active"
data-status-system-light="Light theme active, following system preference" data-status-system-light="Light theme active, following system preference"
data-status-system-dark="Dark theme active, following system preference"> data-status-system-dark="Dark theme active, following system preference">
<legend>Color theme</legend> <span class="chrnx-theme-toggle__legend">Color theme</span>
<label><input type="radio" name="theme-a" value="light">Light</label> <button type="button" data-chrnx-theme-value="light" aria-pressed="false">Light</button>
<label><input type="radio" name="theme-a" value="dark">Dark</label> <button type="button" data-chrnx-theme-value="dark" aria-pressed="false">Dark</button>
<label><input type="radio" name="theme-a" value="system" checked>System</label> <button type="button" data-chrnx-theme-value="system" aria-pressed="true">System</button>
<output data-chrnx-theme-status aria-live="polite">Following system theme</output> <output data-chrnx-theme-status aria-live="polite">Following system theme</output>
</fieldset>`; </div>`;
} }
describe( 'theme preference helpers', () => { describe( 'theme preference helpers', () => {
@@ -85,7 +86,7 @@ describe( 'theme preference helpers', () => {
const media = createMedia( true ); const media = createMedia( true );
expect( () => initialize( { document, storage: denied, media } ) ).not.toThrow(); expect( () => initialize( { document, storage: denied, media } ) ).not.toThrow();
expect( document.documentElement.dataset.theme ).toBe( 'dark' ); expect( document.documentElement.dataset.theme ).toBe( 'dark' );
const light = document.querySelector( 'input[value="light"]' ); const light = document.querySelector( '[data-chrnx-theme-value="light"]' );
expect( () => light.click() ).not.toThrow(); expect( () => light.click() ).not.toThrow();
expect( document.documentElement.dataset.theme ).toBe( 'light' ); expect( document.documentElement.dataset.theme ).toBe( 'light' );
} ); } );
@@ -99,7 +100,7 @@ describe( 'theme preference helpers', () => {
expect( document.documentElement.dataset.theme ).toBe( 'light' ); expect( document.documentElement.dataset.theme ).toBe( 'light' );
media.change( true ); media.change( true );
expect( document.documentElement.dataset.theme ).toBe( 'dark' ); expect( document.documentElement.dataset.theme ).toBe( 'dark' );
document.querySelector( 'input[value="light"]' ).click(); document.querySelector( '[data-chrnx-theme-value="light"]' ).click();
media.change( false ); media.change( false );
expect( document.documentElement.dataset.theme ).toBe( 'light' ); expect( document.documentElement.dataset.theme ).toBe( 'light' );
} ); } );
@@ -117,10 +118,10 @@ describe( 'theme preference helpers', () => {
storage: { getItem: () => null, setItem: jest.fn(), removeItem: jest.fn() }, storage: { getItem: () => null, setItem: jest.fn(), removeItem: jest.fn() },
media, media,
} ); } );
document.querySelector( 'input[name="theme-a"][value="light"]' ).click(); document.querySelector( '[data-group="theme-a"] [data-chrnx-theme-value="light"]' ).click();
expect( expect(
[ ...document.querySelectorAll( 'input[value="light"]' ) ].every( [ ...document.querySelectorAll( '[data-chrnx-theme-value="light"]' ) ].every(
( input ) => input.checked, ( button ) => button.getAttribute( 'aria-pressed' ) === 'true',
), ),
).toBe( true ); ).toBe( true );
expect( expect(
@@ -131,7 +132,9 @@ describe( 'theme preference helpers', () => {
expect( document.querySelector( '[data-chrnx-theme-status]' ).textContent ).toBe( expect( document.querySelector( '[data-chrnx-theme-status]' ).textContent ).toBe(
'Light theme active', 'Light theme active',
); );
expect( document.querySelector( 'fieldset' ).textContent ).toContain( 'Color theme' ); expect( document.querySelector( '[data-chrnx-theme-toggle]' ).textContent ).toContain(
'Color theme',
);
} ); } );
test( 'applies data-theme and color-scheme together', () => { test( 'applies data-theme and color-scheme together', () => {
+49 -7
View File
@@ -119,13 +119,13 @@ class BlockRenderingTest extends WP_UnitTestCase {
$this->assertNotEmpty( $theme_control[1] ); $this->assertNotEmpty( $theme_control[1] );
$this->assertSame( 1, substr_count( $output, 'id="' . $theme_control[1] . '"' ) ); $this->assertSame( 1, substr_count( $output, 'id="' . $theme_control[1] . '"' ) );
$this->assertMatchesRegularExpression( $this->assertMatchesRegularExpression(
'/<fieldset[^>]+id="' . preg_quote( $theme_control[1], '/' ) . '"[^>]+data-chrnx-theme-toggle[^>]+data-chrnx-theme-panel/', '/<div[^>]+id="' . preg_quote( $theme_control[1], '/' ) . '"[^>]+role="group"[^>]+data-chrnx-theme-toggle[^>]+data-chrnx-theme-panel/s',
$output $output
); );
$this->assertSame( 3, substr_count( $output, 'type="radio"' ) ); $this->assertSame( 3, substr_count( $output, 'data-chrnx-theme-value="' ) );
foreach ( array( 'light', 'dark', 'system' ) as $value ) { foreach ( array( 'light', 'dark', 'system' ) as $value ) {
$this->assertMatchesRegularExpression( $this->assertMatchesRegularExpression(
'/<input[^>]+type="radio"[^>]+value="' . $value . '"/', '/<button[^>]+data-chrnx-theme-value="' . $value . '"[^>]+aria-pressed="(?:true|false)"/s',
$output $output
); );
} }
@@ -358,6 +358,41 @@ class BlockRenderingTest extends WP_UnitTestCase {
$this->assertStringContainsString( 'href="#chrnx-main"', $output ); $this->assertStringContainsString( 'href="#chrnx-main"', $output );
} }
/** Navigation falls back to the built-in pages and prefers an assigned menu. */
public function test_navigation_links_prefer_an_assigned_menu_over_the_builtin_fallback() {
$this->assertTrue( function_exists( 'chrnx_get_navigation_links' ) );
$fallback = chrnx_get_navigation_links( 'chrnx-primary' );
$this->assertCount( 5, $fallback );
$this->assertSame( 'Journal', $fallback[0]['label'] );
$this->assertSame( home_url( '/journal/' ), $fallback[0]['url'] );
$menu_id = wp_create_nav_menu( 'Prod nav ' . wp_unique_id() );
$this->assertIsInt( $menu_id );
wp_update_nav_menu_item(
$menu_id,
0,
array(
'menu-item-title' => 'Notas',
'menu-item-url' => home_url( '/notas/' ),
'menu-item-status' => 'publish',
)
);
$locations = get_nav_menu_locations();
$locations['chrnx-primary'] = $menu_id;
set_theme_mod( 'nav_menu_locations', $locations );
$assigned = chrnx_get_navigation_links( 'chrnx-primary' );
$this->assertCount( 1, $assigned );
$this->assertSame( 'Notas', $assigned[0]['label'] );
$this->assertSame( home_url( '/notas/' ), $assigned[0]['url'] );
// An empty location must not leave the header without navigation.
$locations['chrnx-primary'] = 0;
set_theme_mod( 'nav_menu_locations', $locations );
$this->assertCount( 5, chrnx_get_navigation_links( 'chrnx-primary' ) );
}
/** Topic art is an exact fixed mapping and rejects arbitrary paths. */ /** Topic art is an exact fixed mapping and rejects arbitrary paths. */
public function test_topic_art_uses_only_the_fixed_mapping() { public function test_topic_art_uses_only_the_fixed_mapping() {
$expected = array( $expected = array(
@@ -722,7 +757,7 @@ class BlockRenderingTest extends WP_UnitTestCase {
); );
$this->assertStringContainsString( '>Silicon Ranch</h2>', $output ); $this->assertStringContainsString( '>Silicon Ranch</h2>', $output );
$this->assertStringContainsString( 'alter-ego-podcasts.png', $output ); $this->assertStringContainsString( 'alter-ego-podcasts-768.webp', $output );
$this->assertStringNotContainsString( '>Podcasts</h2>', $output ); $this->assertStringNotContainsString( '>Podcasts</h2>', $output );
$this->assertSame( 1, substr_count( $output, 'class="chrnx-character-stage"' ) ); $this->assertSame( 1, substr_count( $output, 'class="chrnx-character-stage"' ) );
} }
@@ -771,7 +806,11 @@ class BlockRenderingTest extends WP_UnitTestCase {
$this->assertSame( 1, substr_count( $output, '<article' ) ); $this->assertSame( 1, substr_count( $output, '<article' ) );
$this->assertStringContainsString( 'Only Story', $output ); $this->assertStringContainsString( 'Only Story', $output );
$this->assertStringContainsString( 'aria-hidden="true"', $output ); $this->assertStringContainsString( 'aria-hidden="true"', $output );
$this->assertStringNotContainsString( 'placeholder', strtolower( $output ) ); // A card without media now shows the shared placeholder, which must stay
// decorative and keep its topic class so the accent still varies.
$this->assertStringContainsString( 'placeholder-post', $output );
$this->assertMatchesRegularExpression( '/<picture[^>]*aria-hidden="true"/', $output );
$this->assertStringContainsString( 'is-topic-code', $output );
} }
/** Primed editorial cards reuse relationship caches instead of querying once per fallback. */ /** Primed editorial cards reuse relationship caches instead of querying once per fallback. */
@@ -871,8 +910,11 @@ class BlockRenderingTest extends WP_UnitTestCase {
$output = $this->render_for_post( 'chrnx/related-posts', $current ); $output = $this->render_for_post( 'chrnx/related-posts', $current );
$this->assertStringContainsString( 'chrnx-related-card__fallback', $output ); $this->assertStringContainsString( 'chrnx-related-card__fallback', $output );
$this->assertMatchesRegularExpression( '/class="[^"]*chrnx-related-card__fallback[^"]*" aria-hidden="true"/', $output ); // The fallback is the shared placeholder image now, still decorative and
$this->assertStringNotContainsString( '<img', $output ); // still carrying its topic class for the accent underneath it.
$this->assertMatchesRegularExpression( '/<picture[^>]*aria-hidden="true"/', $output );
$this->assertMatchesRegularExpression( '/<img[^>]*class="[^"]*chrnx-related-card__fallback[^"]*is-topic-research/', $output );
$this->assertStringContainsString( 'placeholder-post', $output );
} }
/** Provide strict Related Stories limit inputs and their expected card counts. */ /** Provide strict Related Stories limit inputs and their expected card counts. */
+8 -7
View File
@@ -48,7 +48,7 @@ class ThemePreferenceTest extends WP_UnitTestCase {
$this->assertStringContainsString( 'document.documentElement.dataset.theme=t', $output ); $this->assertStringContainsString( 'document.documentElement.dataset.theme=t', $output );
} }
/** Toggle uses a labeled native radio group with a live status. */ /** Toggle uses a labeled group of native toggle buttons with a live status. */
public function test_toggle_renders_accessible_native_controls() { public function test_toggle_renders_accessible_native_controls() {
do_action( 'init' ); do_action( 'init' );
$output = render_block( $output = render_block(
@@ -61,12 +61,13 @@ class ThemePreferenceTest extends WP_UnitTestCase {
) )
); );
$this->assertStringContainsString( '<fieldset', $output ); $this->assertStringContainsString( 'role="group"', $output );
$this->assertStringContainsString( '<legend', $output ); $this->assertStringContainsString( 'aria-labelledby="chrnx-theme-legend-', $output );
$this->assertSame( 3, substr_count( $output, 'type="radio"' ) ); $this->assertSame( 3, substr_count( $output, 'data-chrnx-theme-value="' ) );
$this->assertStringContainsString( 'value="light"', $output ); $this->assertSame( 1, substr_count( $output, 'aria-pressed="true"' ) );
$this->assertStringContainsString( 'value="dark"', $output ); $this->assertStringContainsString( 'data-chrnx-theme-value="light"', $output );
$this->assertStringContainsString( 'value="system"', $output ); $this->assertStringContainsString( 'data-chrnx-theme-value="dark"', $output );
$this->assertStringContainsString( 'data-chrnx-theme-value="system"', $output );
$this->assertStringContainsString( 'aria-live="polite"', $output ); $this->assertStringContainsString( 'aria-live="polite"', $output );
$this->assertStringContainsString( 'data-status-system-light="Light theme active, following system preference"', $output ); $this->assertStringContainsString( 'data-status-system-light="Light theme active, following system preference"', $output );
$this->assertStringContainsString( 'data-status-system-dark="Dark theme active, following system preference"', $output ); $this->assertStringContainsString( 'data-status-system-dark="Dark theme active, following system preference"', $output );