feat: add CHRNX theme preference and marks
This commit is contained in:
11 files changed
+469
-3
No files matched your search
@@ -0,0 +1,160 @@
|
||||
( 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] input[type="radio"]' ) ];
|
||||
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 ) => {
|
||||
control.checked = control.value === selected;
|
||||
} );
|
||||
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 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.value;
|
||||
explicitPreference = normalizePreference( value );
|
||||
writePreference( storage, value );
|
||||
setMediaListening( ! explicitPreference );
|
||||
update();
|
||||
};
|
||||
|
||||
controls.forEach( ( control ) => control.addEventListener( 'change', onChange ) );
|
||||
setMediaListening( ! explicitPreference );
|
||||
update();
|
||||
|
||||
cleanupCurrent = () => {
|
||||
controls.forEach( ( control ) => control.removeEventListener( 'change', onChange ) );
|
||||
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();
|
||||
}
|
||||
}
|
||||
} )();
|
||||
Reference in new issue
Block a user