Files
chrnxandClaude Opus 5 a142ece51d 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>
2026-08-13 19:41:24 -05:00

196 lines
5.9 KiB
JavaScript

( function () {
'use strict';
const STORAGE_KEY = 'chrnx-theme';
let cleanupCurrent = null;
function normalizePreference( value ) {
return typeof value === 'string' && ( value === 'light' || value === 'dark' ) ? value : null;
}
function resolveTheme( explicitPreference, systemPrefersDark ) {
const preference = normalizePreference( explicitPreference );
if ( preference ) {
return preference;
}
if ( typeof systemPrefersDark === 'boolean' ) {
return systemPrefersDark ? 'dark' : 'light';
}
return 'dark';
}
function readPreference( storage ) {
try {
return normalizePreference( storage && storage.getItem( STORAGE_KEY ) );
} catch {
return null;
}
}
function writePreference( storage, preference ) {
try {
if ( ! storage ) {
return;
}
if ( preference === 'light' || preference === 'dark' ) {
storage.setItem( STORAGE_KEY, preference );
} else {
storage.removeItem( STORAGE_KEY );
}
} catch {
// Privacy modes may deny storage; the in-memory choice still applies.
}
}
function applyTheme( doc, theme ) {
const resolved = resolveTheme( theme );
doc.documentElement.dataset.theme = resolved;
doc.documentElement.style.colorScheme = resolved;
}
function initialize( options = {} ) {
if ( cleanupCurrent ) {
cleanupCurrent();
}
const doc = options.document || ( typeof document === 'undefined' ? null : document );
if ( ! doc ) {
cleanupCurrent = null;
return false;
}
let storage = options.storage;
if ( storage === undefined ) {
try {
storage = window.localStorage;
} catch {
storage = null;
}
}
let media = options.media;
if ( media === undefined ) {
try {
media = window.matchMedia( '(prefers-color-scheme: dark)' );
} catch {
media = null;
}
}
const controls = [
...doc.querySelectorAll( '[data-chrnx-theme-toggle] [data-chrnx-theme-value]' ),
];
const groups = [ ...doc.querySelectorAll( '[data-chrnx-theme-toggle]' ) ];
let explicitPreference = readPreference( storage );
let listening = false;
const systemPrefersDark = () =>
media && typeof media.matches === 'boolean' ? media.matches : undefined;
const update = () => {
const resolved = resolveTheme( explicitPreference, systemPrefersDark() );
applyTheme( doc, resolved );
const selected = explicitPreference || 'system';
controls.forEach( ( control ) => {
const pressed = control.dataset.chrnxThemeValue === selected;
control.setAttribute( 'aria-pressed', String( pressed ) );
} );
groups.forEach( ( group ) => {
const status = group.querySelector( '[data-chrnx-theme-status]' );
if ( status ) {
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;
}
} );
};
const onMediaChange = () => {
if ( ! explicitPreference ) {
update();
}
};
const setMediaListening = ( shouldListen ) => {
if (
! media ||
typeof media.addEventListener !== 'function' ||
typeof media.removeEventListener !== 'function' ||
listening === shouldListen
) {
return;
}
media[ shouldListen ? 'addEventListener' : 'removeEventListener' ]( 'change', onMediaChange );
listening = shouldListen;
};
const onChange = ( event ) => {
const value = event.currentTarget.dataset.chrnxThemeValue;
explicitPreference = normalizePreference( value );
writePreference( storage, value );
setMediaListening( ! explicitPreference );
update();
};
// 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 );
update();
cleanupCurrent = () => {
controls.forEach( ( control ) => {
control.removeEventListener( 'click', onChange );
control.removeEventListener( 'keydown', onKeydown );
} );
setMediaListening( false );
cleanupCurrent = null;
};
return true;
}
const helpers = {
STORAGE_KEY,
normalizePreference,
resolveTheme,
readPreference,
writePreference,
applyTheme,
initialize,
};
if ( typeof module !== 'undefined' && module.exports ) {
module.exports = helpers;
} else if ( typeof document !== 'undefined' ) {
if ( document.readyState === 'loading' ) {
document.addEventListener( 'DOMContentLoaded', () => initialize(), { once: true } );
} else {
initialize();
}
}
} )();