feat: add CHRNX shell and theme preference

This commit is contained in:
chrnx committed 2026-08-11 16:54:17 -05:00
1 parent aad237b211
commit f371db4cb5
18 files changed
+633 -16

No files matched your search

+226
View File
@@ -0,0 +1,226 @@
/* CHRNX shell: stable, compact, and intentionally free of scroll animation. */
html[data-theme="light"] body {
background: var(--wp--preset--color--paper);
color: var(--wp--preset--color--ink);
}
html[data-theme="light"] h1,
html[data-theme="light"] h2,
html[data-theme="light"] h3,
html[data-theme="light"] h4,
html[data-theme="light"] h5,
html[data-theme="light"] h6 {
color: var(--wp--preset--color--ink);
}
header.wp-block-template-part {
position: sticky;
z-index: 100;
top: 0;
background: color-mix(in srgb, var(--wp--preset--color--night) 94%, transparent);
border-bottom: 1px solid color-mix(in srgb, var(--wp--preset--color--muted) 40%, transparent);
backdrop-filter: blur(12px);
}
html[data-theme="light"] header.wp-block-template-part {
background: color-mix(in srgb, var(--wp--preset--color--paper) 94%, transparent);
border-bottom-color: var(--wp--preset--color--border);
}
.chrnx-site-header__inner,
.chrnx-site-footer__inner {
gap: var(--wp--preset--spacing--20) var(--wp--preset--spacing--30);
max-width: var(--wp--style--global--wide-size);
margin-inline: auto;
padding: var(--wp--preset--spacing--20) var(--wp--preset--spacing--30);
}
.chrnx-skip-link {
position: fixed;
z-index: 1000;
top: var(--wp--preset--spacing--20);
left: var(--wp--preset--spacing--20);
padding: 0.75rem 1rem;
transform: translateY(-200%);
background: var(--wp--preset--color--green);
color: var(--wp--preset--color--night);
font-weight: 700;
}
.chrnx-skip-link:focus {
transform: translateY(0);
}
.chrnx-site-brand {
display: flex;
align-items: center;
min-width: 45px;
min-height: 45px;
}
.chrnx-site-brand img {
width: clamp(8.5rem, 12vw, 10.5rem);
height: auto;
}
.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 {
min-width: 45px;
min-height: 45px;
}
.chrnx-primary-navigation a,
.chrnx-footer-navigation a {
display: inline-flex;
align-items: center;
padding-inline: 0.55rem;
}
.chrnx-primary-navigation .wp-block-navigation__container {
gap: 0.25rem;
}
.chrnx-header-search {
width: min(10rem, 100%);
}
.chrnx-header-search .wp-block-search__inside-wrapper {
padding: 0;
}
.chrnx-theme-toggle {
display: none;
align-items: center;
gap: 0.25rem;
min-width: 0;
margin: 0;
padding: 0;
border: 0;
}
.chrnx-theme-enhanced .chrnx-theme-toggle {
display: flex;
}
.chrnx-theme-toggle legend {
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 label {
display: inline-flex;
align-items: center;
justify-content: center;
min-width: 45px;
min-height: 45px;
padding: 0 0.45rem;
border: 1px solid transparent;
border-radius: 0.35rem;
cursor: pointer;
}
.chrnx-theme-toggle label:has(input:checked) {
border-color: currentcolor;
background: var(--wp--preset--color--panel);
}
html[data-theme="light"] .chrnx-theme-toggle label:has(input:checked) {
background: var(--wp--preset--color--surface);
}
.chrnx-theme-toggle input {
accent-color: var(--wp--preset--color--green);
}
.chrnx-theme-toggle output {
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-noscript {
margin: 0;
font-size: var(--wp--preset--font-size--small);
}
main#chrnx-main {
min-height: 50vh;
padding-inline: var(--wp--preset--spacing--30);
}
footer.wp-block-template-part {
margin-top: var(--wp--preset--spacing--60);
border-top: 1px solid color-mix(in srgb, var(--wp--preset--color--muted) 40%, transparent);
}
html[data-theme="light"] footer.wp-block-template-part {
border-top-color: var(--wp--preset--color--border);
}
.chrnx-site-footer__line,
.chrnx-site-footer__inner .wp-block-site-title {
margin: 0;
font-size: var(--wp--preset--font-size--small);
}
@media (max-width: 780px) {
.chrnx-site-header__inner {
justify-content: flex-start;
}
.chrnx-primary-navigation {
margin-left: auto;
}
.chrnx-header-search {
order: 3;
flex: 1 1 10rem;
}
.chrnx-theme-toggle {
order: 4;
flex: 1 1 100%;
}
}
@media (max-width: 599px) {
html:not(.chrnx-theme-enhanced) .chrnx-primary-navigation .wp-block-navigation__responsive-container {
display: block;
position: static;
width: 100%;
padding: 0;
}
html:not(.chrnx-theme-enhanced) .chrnx-primary-navigation .wp-block-navigation__responsive-container-open,
html:not(.chrnx-theme-enhanced) .chrnx-primary-navigation .wp-block-navigation__responsive-container-close {
display: none;
}
}
@media (prefers-reduced-motion: reduce) {
.chrnx-skip-link {
transition: none;
}
}
+4 -5
View File
@@ -94,11 +94,10 @@
groups.forEach( ( group ) => {
const status = group.querySelector( '[data-chrnx-theme-status]' );
if ( status ) {
const label =
group.dataset[ `status${ resolved[ 0 ].toUpperCase() }${ resolved.slice( 1 ) }` ] ||
`${ resolved === 'dark' ? 'Dark' : 'Light' } theme active`;
const systemSuffix = group.dataset.statusSystem || 'following system';
status.textContent = explicitPreference ? label : `${ label }, ${ systemSuffix }`;
const resolvedName = resolved[ 0 ].toUpperCase() + resolved.slice( 1 );
const explicitStatus = group.dataset[ `status${ resolvedName }` ];
const systemStatus = group.dataset[ `statusSystem${ resolvedName }` ];
status.textContent = explicitPreference ? explicitStatus : systemStatus;
}
} );
};
+12
View File
@@ -0,0 +1,12 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 3,
"name": "chrnx/site-brand",
"version": "0.1.0",
"title": "CHRNX Site Brand",
"category": "theme",
"description": "Display the local CHRNX wordmark linked to the site home.",
"textdomain": "chrnx",
"supports": { "html": false },
"render": "file:./render.php"
}
+22
View File
@@ -0,0 +1,22 @@
<?php
/**
* Render the portable CHRNX site brand and localized skip link.
*
* @package CHRNX
*/
$chrnx_home_url = home_url( '/' );
$chrnx_wordmark_url = get_theme_file_uri( 'assets/icons/chrnx-wordmark.svg' );
?>
<a class="chrnx-skip-link" href="#chrnx-main"><?php esc_html_e( 'Skip to content', 'chrnx' ); ?></a>
<a
<?php
// WordPress escapes each generated wrapper attribute.
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo get_block_wrapper_attributes( array( 'class' => 'chrnx-site-brand' ) );
?>
href="<?php echo esc_url( $chrnx_home_url ); ?>"
aria-label="<?php esc_attr_e( 'CHRNX home', 'chrnx' ); ?>"
>
<img src="<?php echo esc_url( $chrnx_wordmark_url ); ?>" alt="<?php esc_attr_e( 'CHRNX', 'chrnx' ); ?>" width="250" height="64">
</a>
+3 -1
View File
@@ -20,7 +20,8 @@ $chrnx_control_name = wp_unique_id( 'chrnx-theme-' );
?>
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="<?php esc_attr_e( 'following system', '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' ); ?>"
>
<legend><?php esc_html_e( 'Color theme', 'chrnx' ); ?></legend>
<label><input type="radio" name="<?php echo esc_attr( $chrnx_control_name ); ?>" value="light"> <?php esc_html_e( 'Light', 'chrnx' ); ?></label>
@@ -28,3 +29,4 @@ $chrnx_control_name = wp_unique_id( 'chrnx-theme-' );
<label><input type="radio" name="<?php echo esc_attr( $chrnx_control_name ); ?>" value="system" checked> <?php esc_html_e( 'System', 'chrnx' ); ?></label>
<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>
+6
View File
@@ -15,5 +15,11 @@ function chrnx_enqueue_assets() {
array(),
CHRNX_VERSION
);
wp_enqueue_style(
'chrnx-layout',
get_theme_file_uri( 'assets/css/layout.css' ),
array( 'chrnx-base' ),
CHRNX_VERSION
);
}
add_action( 'wp_enqueue_scripts', 'chrnx_enqueue_assets' );
+1 -1
View File
@@ -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' );
$blocks = array( 'featured-stories', 'journal-stream', 'latest-podcast', 'podcast-meta', 'topic-hero', 'related-posts', 'subscribe-cta', 'theme-toggle', 'site-brand' );
foreach ( $blocks as $block ) {
$name = 'chrnx/' . $block;
if ( WP_Block_Type_Registry::get_instance()->is_registered( $name ) ) {
+2 -1
View File
@@ -13,7 +13,8 @@ function chrnx_setup() {
add_theme_support( 'post-thumbnails' );
add_theme_support( 'responsive-embeds' );
add_theme_support( 'editor-styles' );
add_editor_style( 'assets/css/base.css' );
add_editor_style( array( 'assets/css/base.css', 'assets/css/layout.css' ) );
remove_action( 'wp_footer', 'the_block_template_skip_link' );
add_image_size( 'chrnx-card', 720, 480, true );
}
+7 -3
View File
@@ -7,8 +7,12 @@
/** Print the no-flash theme resolution before stylesheet links. */
function chrnx_print_theme_preference_bootstrap() {
?>
<script id="chrnx-theme-preference-bootstrap">(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;}());</script>
<?php
$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');}());";
// 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(
$script,
array( 'id' => 'chrnx-theme-preference-bootstrap' )
);
}
add_action( 'wp_head', 'chrnx_print_theme_preference_bootstrap', 0 );
+17
View File
@@ -0,0 +1,17 @@
<!-- wp:group {"className":"chrnx-site-footer__inner","layout":{"type":"flex","flexWrap":"wrap","justifyContent":"space-between"}} -->
<div class="wp-block-group chrnx-site-footer__inner">
<!-- wp:site-title {"level":0,"isLink":true} /-->
<!-- wp:navigation {"overlayMenu":"never","ariaLabel":"Footer navigation","className":"chrnx-footer-navigation","layout":{"type":"flex","flexWrap":"wrap"}} -->
<!-- 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 -->
<!-- wp:paragraph {"className":"chrnx-site-footer__line"} -->
<p class="chrnx-site-footer__line">Made in Sinaloa, shared from home.</p>
<!-- /wp:paragraph -->
</div>
<!-- /wp:group -->
+17
View File
@@ -0,0 +1,17 @@
<!-- wp:group {"className":"chrnx-site-header__inner","layout":{"type":"flex","flexWrap":"wrap","justifyContent":"space-between"}} -->
<div class="wp-block-group chrnx-site-header__inner">
<!-- wp:chrnx/site-brand /-->
<!-- wp:navigation {"overlayMenu":"mobile","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 -->
<!-- wp:search {"label":"Search","showLabel":false,"buttonText":"Search","buttonUseIcon":true,"className":"chrnx-header-search"} /-->
<!-- wp:chrnx/theme-toggle /-->
</div>
<!-- /wp:group -->
+2 -2
View File
@@ -1,7 +1,7 @@
<!-- wp:template-part {"slug":"header","tagName":"header"} /-->
<!-- wp:group {"tagName":"main","layout":{"type":"constrained"}} -->
<main class="wp-block-group">
<!-- wp:group {"tagName":"main","anchor":"chrnx-main","layout":{"type":"constrained"}} -->
<main id="chrnx-main" class="wp-block-group" tabindex="-1">
<!-- wp:query {"queryId":0,"query":{"inherit":true},"layout":{"type":"default"}} -->
<div class="wp-block-query">
<!-- wp:post-template -->
+109
View File
@@ -0,0 +1,109 @@
import { expect, test } from '@playwright/test';
const destinations = [
[ 'Journal', '/journal/' ],
[ 'Projects', '/projects/' ],
[ 'Music', '/music/' ],
[ 'Podcasts', '/podcasts/' ],
[ 'About', '/about/' ],
];
async function expectTargetSize( locator ) {
const box = await locator.boundingBox();
expect( box.width ).toBeGreaterThanOrEqual( 44 );
expect( box.height ).toBeGreaterThanOrEqual( 44 );
}
test.describe( 'site navigation shell', () => {
test( 'desktop has a stable accessible header, approved local brand, and compact footer', async ( {
page,
}, testInfo ) => {
await page.setViewportSize( { width: 1280, height: 800 } );
await page.goto( '/' );
const skip = page.getByRole( 'link', { name: 'Skip to content' } );
await page.keyboard.press( testInfo.project.name === 'webkit' ? 'Alt+Tab' : 'Tab' );
await expect( skip ).toBeFocused();
await expect( skip ).toHaveAttribute( 'href', '#chrnx-main' );
await skip.press( 'Enter' );
await expect( page.locator( '#chrnx-main' ) ).toBeFocused();
const header = page.getByRole( 'banner' );
const nav = header.getByRole( 'navigation', { name: 'Primary navigation' } );
await expect( header ).toBeVisible();
await expect( nav ).toBeVisible();
await expect( header.getByRole( 'search' ) ).toBeVisible();
await expect( header.locator( '[data-chrnx-theme-toggle]' ) ).toBeVisible();
const brand = header.getByRole( 'img', { name: 'CHRNX' } );
await expect( brand ).toBeVisible();
const brandSource = await brand.getAttribute( 'src' );
expect( new URL( brandSource, page.url() ).origin ).toBe( new URL( page.url() ).origin );
expect( new URL( brandSource, page.url() ).pathname ).toMatch(
/\/wp-content\/themes\/chrnx\/assets\/icons\/chrnx-wordmark\.svg$/,
);
for ( const [ name, path ] of destinations ) {
const link = nav.getByRole( 'link', { name, exact: true } );
await expect( link ).toHaveAttribute( 'href', path );
}
await expect( header.getByRole( 'button', { name: /menu/i } ) ).toBeHidden();
const before = await header.boundingBox();
await page.evaluate( () => window.scrollTo( 0, document.body.scrollHeight ) );
const after = await header.boundingBox();
expect( after.y ).toBe( 0 );
expect( after.height ).toBe( before.height );
const footer = page.getByRole( 'contentinfo' );
await expect( footer ).toBeVisible();
await expect( footer.getByRole( 'navigation', { name: 'Footer navigation' } ) ).toBeVisible();
await expect( footer.getByText( 'Made in Sinaloa, shared from home.' ) ).toBeVisible();
await expect(
footer.locator( 'form, a[href*="facebook"], a[href*="spotify"], a[href*="youtube"]' ),
).toHaveCount( 0 );
} );
test( 'visible keyboard focus and minimum target sizes survive layout', async ( { page } ) => {
await page.setViewportSize( { width: 1280, height: 800 } );
await page.goto( '/' );
const journal = page
.getByRole( 'banner' )
.getByRole( 'link', { name: 'Journal', exact: true } );
await journal.focus();
expect(
await journal.evaluate( ( element ) => window.getComputedStyle( element ).outlineStyle ),
).not.toBe( 'none' );
await expectTargetSize( journal );
await expectTargetSize( page.getByRole( 'searchbox' ) );
} );
test( 'mobile core navigation opens, closes with Escape, and restores focus without trapping Tab', async ( {
page,
} ) => {
await page.setViewportSize( { width: 390, height: 844 } );
await page.goto( '/' );
const header = page.getByRole( 'banner' );
const open = header.getByRole( 'button', { name: /open menu/i } );
await expect( open ).toBeVisible();
await expectTargetSize( open );
await open.click();
const nav = header.getByRole( 'navigation', { name: 'Primary navigation' } );
await expect( nav ).toBeVisible();
for ( const [ name, path ] of destinations ) {
await expect( nav.getByRole( 'link', { name, exact: true } ) ).toHaveAttribute(
'href',
path,
);
}
await page.keyboard.press( 'Escape' );
await expect( open ).toBeFocused();
await expect( nav.getByRole( 'link', { name: 'Journal', exact: true } ) ).toBeHidden();
await open.click();
const close = header.getByRole( 'button', { name: /close menu/i } );
await expectTargetSize( close );
await close.focus();
await page.keyboard.press( 'Tab' );
await expect( close ).not.toBeFocused();
await close.click();
await expect( open ).toBeFocused();
} );
} );
+147
View File
@@ -0,0 +1,147 @@
import { expect, test } from '@playwright/test';
const preferenceKey = 'chrnx-theme';
async function observeFirstPaintTheme( page ) {
await page.addInitScript( () => {
window.__chrnxThemeFrames = [];
const sample = () => {
window.__chrnxThemeFrames.push( document.documentElement?.dataset.theme || '' );
if ( document.readyState === 'loading' ) {
window.requestAnimationFrame( sample );
}
};
window.requestAnimationFrame( sample );
} );
}
test.describe( 'real theme preference shell', () => {
test.beforeEach( async ( { page } ) => {
await page.goto( '/' );
await page.evaluate( ( key ) => localStorage.removeItem( key ), preferenceKey );
} );
test( 'inline bootstrap precedes styles and the first observable frame', async ( { page } ) => {
await observeFirstPaintTheme( page );
await page.emulateMedia( { colorScheme: 'dark' } );
const response = await page.goto( '/' );
const html = await response.text();
expect( html.indexOf( 'chrnx-theme-preference-bootstrap' ) ).toBeGreaterThan( -1 );
expect( html.indexOf( 'chrnx-theme-preference-bootstrap' ) ).toBeLessThan(
html.indexOf( 'chrnx-base-css' ),
);
await page.waitForLoadState( 'domcontentloaded' );
const result = await page.evaluate( () => ( {
frames: window.__chrnxThemeFrames,
theme: document.documentElement.dataset.theme,
} ) );
expect( result.theme ).toBe( 'dark' );
expect( result.frames.length ).toBeGreaterThan( 0 );
expect( result.frames.every( ( value ) => value === 'dark' ) ).toBe( true );
} );
test( 'saved choice wins, persists across reload, and synchronizes controls', async ( {
page,
} ) => {
await page.emulateMedia( { colorScheme: 'dark' } );
await page.goto( '/' );
const groups = page.locator( '[data-chrnx-theme-toggle]' );
await expect( groups.first() ).toHaveAccessibleName( 'Color theme' );
await groups.first().getByRole( 'radio', { name: 'Light', exact: true } ).check();
await expect( page.locator( 'html' ) ).toHaveAttribute( 'data-theme', 'light' );
await expect
.poll( () => page.evaluate( ( key ) => localStorage.getItem( key ), preferenceKey ) )
.toBe( 'light' );
await page.reload();
await expect( page.locator( 'html' ) ).toHaveAttribute( 'data-theme', 'light' );
await expect( page.getByRole( 'radio', { name: 'Light', exact: true } ) ).toBeChecked();
await page.getByRole( 'radio', { name: 'Dark', exact: true } ).check();
await page.reload();
await expect( page.locator( 'html' ) ).toHaveAttribute( 'data-theme', 'dark' );
await expect( page.getByRole( 'radio', { name: 'Dark', exact: true } ) ).toBeChecked();
const instances = await page.locator( '[data-chrnx-theme-toggle]' ).count();
if ( instances > 1 ) {
await expect( page.getByRole( 'radio', { name: 'Dark', exact: true } ) ).toHaveCount(
instances,
);
for ( const radio of await page.getByRole( 'radio', { name: 'Dark', exact: true } ).all() ) {
await expect( radio ).toBeChecked();
}
}
} );
test( 'system removes storage and follows emulated operating-system changes', async ( {
page,
} ) => {
await page.evaluate( ( key ) => localStorage.setItem( key, 'dark' ), preferenceKey );
await page.emulateMedia( { colorScheme: 'light' } );
await page.reload();
await expect( page.locator( 'html' ) ).toHaveAttribute( 'data-theme', 'dark' );
await page.getByRole( 'radio', { name: 'System', exact: true } ).check();
await expect
.poll( () => page.evaluate( ( key ) => localStorage.getItem( key ), preferenceKey ) )
.toBeNull();
await expect( page.locator( 'html' ) ).toHaveAttribute( 'data-theme', 'light' );
await page.emulateMedia( { colorScheme: 'dark' } );
await expect( page.locator( 'html' ) ).toHaveAttribute( 'data-theme', 'dark' );
} );
test( 'invalid storage and storage denial fall back safely', async ( { page } ) => {
await page.evaluate( ( key ) => localStorage.setItem( key, 'invalid' ), preferenceKey );
await page.emulateMedia( { colorScheme: 'light' } );
await page.reload();
await expect( page.locator( 'html' ) ).toHaveAttribute( 'data-theme', 'light' );
await page.addInitScript( () => {
Object.defineProperty( window, 'localStorage', {
configurable: true,
get() {
throw new DOMException( 'Denied', 'SecurityError' );
},
} );
} );
await page.addInitScript( () => {
window.matchMedia = () => {
throw new Error( 'Unavailable' );
};
} );
await expect( page.goto( '/' ) ).resolves.toBeTruthy();
await expect( page.locator( 'html' ) ).toHaveAttribute( 'data-theme', 'dark' );
} );
test( 'native radios expose state, keyboard interaction, status, and 44px targets', async ( {
page,
} ) => {
await page.goto( '/' );
const light = page.getByRole( 'radio', { name: 'Light', exact: true } );
await light.focus();
await page.keyboard.press( 'ArrowRight' );
await expect( page.getByRole( 'radio', { name: 'Dark', exact: true } ) ).toBeChecked();
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() ) {
const box = await label.boundingBox();
expect( box.width ).toBeGreaterThanOrEqual( 44 );
expect( box.height ).toBeGreaterThanOrEqual( 44 );
}
} );
test( 'without JavaScript the inert control is hidden and a clear explanation remains', async ( {
browser,
baseURL,
} ) => {
const context = await browser.newContext( {
baseURL,
javaScriptEnabled: false,
} );
const page = await context.newPage();
await page.goto( '/' );
await expect( page.locator( '[data-chrnx-theme-toggle]' ) ).toBeHidden();
await expect( page.locator( '.chrnx-theme-noscript' ) ).toHaveText(
'Enable JavaScript to choose a color theme.',
);
await expect(
page.getByRole( 'banner' ).getByRole( 'link', { name: 'Journal', exact: true } ),
).toBeVisible();
await context.close();
} );
} );
+8 -1
View File
@@ -16,7 +16,11 @@ function createMedia( matches = false ) {
function toggleMarkup() {
document.body.innerHTML = `
<fieldset class="chrnx-theme-toggle" data-chrnx-theme-toggle>
<fieldset class="chrnx-theme-toggle" data-chrnx-theme-toggle
data-status-light="Light theme active"
data-status-dark="Dark theme active"
data-status-system-light="Light theme active, following system preference"
data-status-system-dark="Dark theme active, following system preference">
<legend>Color theme</legend>
<label><input type="radio" name="theme-a" value="light">Light</label>
<label><input type="radio" name="theme-a" value="dark">Dark</label>
@@ -124,6 +128,9 @@ describe( 'theme preference helpers', () => {
status.textContent.includes( 'Light' ),
),
).toBe( true );
expect( document.querySelector( '[data-chrnx-theme-status]' ).textContent ).toBe(
'Light theme active',
);
expect( document.querySelector( 'fieldset' ).textContent ).toContain( 'Color theme' );
} );
+20 -2
View File
@@ -15,9 +15,9 @@ class BlockRenderingTest extends WP_UnitTestCase {
delete_option( 'chrnx_subscribe_page_id' );
}
/** All eight block types register from metadata. */
/** All nine 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' );
$names = array( 'featured-stories', 'journal-stream', 'latest-podcast', 'podcast-meta', 'topic-hero', 'related-posts', 'subscribe-cta', 'theme-toggle', 'site-brand' );
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,24 @@ class BlockRenderingTest extends WP_UnitTestCase {
}
}
/** The approved local wordmark links to the portable site home URL. */
public function test_site_brand_renders_escaped_local_home_and_wordmark_urls() {
$output = render_block(
array(
'blockName' => 'chrnx/site-brand',
'attrs' => array(),
'innerBlocks' => array(),
'innerHTML' => '',
'innerContent' => array(),
)
);
$this->assertStringContainsString( 'href="' . esc_url( home_url( '/' ) ) . '"', $output );
$this->assertStringContainsString( 'src="' . esc_url( get_theme_file_uri( 'assets/icons/chrnx-wordmark.svg' ) ) . '"', $output );
$this->assertStringContainsString( 'alt="CHRNX"', $output );
$this->assertStringContainsString( 'href="#chrnx-main"', $output );
}
/** Topic art is an exact fixed mapping and rejects arbitrary paths. */
public function test_topic_art_uses_only_the_fixed_mapping() {
$expected = array(
+24
View File
@@ -27,6 +27,27 @@ class ThemePreferenceTest extends WP_UnitTestCase {
$this->assertStringNotContainsString( 'fetch(', $output );
}
/** Standard inline-script filters can add CSP attributes without replacing the bootstrap. */
public function test_bootstrap_uses_filterable_wordpress_inline_script_tag() {
$filter = static function ( $attributes ) {
$attributes['nonce'] = 'test-nonce';
$attributes['data-test-policy'] = 'allowed';
return $attributes;
};
add_filter( 'wp_inline_script_attributes', $filter );
try {
ob_start();
chrnx_print_theme_preference_bootstrap();
$output = ob_get_clean();
} finally {
remove_filter( 'wp_inline_script_attributes', $filter );
}
$this->assertStringContainsString( 'nonce="test-nonce"', $output );
$this->assertStringContainsString( 'data-test-policy="allowed"', $output );
$this->assertStringContainsString( 'document.documentElement.dataset.theme=t', $output );
}
/** Toggle uses a labeled native radio group with a live status. */
public function test_toggle_renders_accessible_native_controls() {
do_action( 'init' );
@@ -47,5 +68,8 @@ class ThemePreferenceTest extends WP_UnitTestCase {
$this->assertStringContainsString( 'value="dark"', $output );
$this->assertStringContainsString( 'value="system"', $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-dark="Dark theme active, following system preference"', $output );
$this->assertStringContainsString( '<noscript>', $output );
}
}
+6
View File
@@ -33,6 +33,7 @@ class ThemeSetupTest extends WP_UnitTestCase {
global $editor_styles;
$this->assertContains( 'assets/css/base.css', $editor_styles );
$this->assertContains( 'assets/css/layout.css', $editor_styles );
}
/**
@@ -54,9 +55,14 @@ class ThemeSetupTest extends WP_UnitTestCase {
$this->assertSame( get_theme_file_uri( 'assets/css/base.css' ), $base->src );
$this->assertSame( CHRNX_VERSION, $base->ver );
$this->assertTrue( wp_style_is( 'chrnx-layout', 'enqueued' ) );
$this->assertSame( array( 'chrnx-base' ), $styles->registered['chrnx-layout']->deps );
$this->assertSame( get_theme_file_uri( 'assets/css/layout.css' ), $styles->registered['chrnx-layout']->src );
} finally {
wp_dequeue_style( 'chrnx-base' );
wp_deregister_style( 'chrnx-base' );
wp_dequeue_style( 'chrnx-layout' );
wp_deregister_style( 'chrnx-layout' );
}
}
}