Files
breadcrumb-the-shire/web/js/components/app-contrast-toggle.js
fs c7b8fd516a feat: extend module platform with UI slots, runtime components, CLI tooling and {{userId}} search support
Completes the generic module platform that enables modules to contribute
UI elements, runtime JS components, and search resources without any
core hardcoding.

New generic UI slot types:
- topbar.right_item: module-contributed topbar buttons
- layout.body_end_template: module-contributed dialog/overlay templates
- layout.head_style: module-contributed global CSS
- runtime.component: declarative JS component registration with phase ordering

New infrastructure:
- ModuleAutoloader: PSR-4 autoloading for module-local PHP classes
- ModuleRuntimePageBuilder: symlinks module pages into runtime directory
- ModuleRuntimeAssetPublisher: publishes module CSS/JS to web/modules/
- ModulePermissionSynchronizer: syncs module permissions to DB
- CLI scripts: module-runtime-sync, module-build, module-migrate,
  module-permissions-sync, module-assets-sync
- {{userId}} placeholder in SearchDataService for user-scoped search queries
- Component runtime with phased initialization (early/default/late)
- AppContainer.protectExistingBindings() to prevent module→core overwrites
- Architecture tests: ModuleStructureContractTest, CoreTemplateIsolationTest,
  FrontendComponentRuntimeContractTest, AppContainerIsolationContractTest

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 22:19:56 +01:00

72 lines
2.3 KiB
JavaScript

/**
* High-contrast mode toggle with localStorage persistence.
*/
import { warnOnce, resolveHost } from '../core/app-dom.js';
import { createUiStorage } from '../core/app-ui-storage.js';
const STORAGE_KEY = 'contrast-mode';
const setContrast = (contrast) => {
const root = document.documentElement;
root.dataset.contrast = contrast === 'high' ? 'high' : 'normal';
};
const setIcon = (button, contrast) => {
const icon = button.querySelector('i');
if (!icon) {
return;
}
icon.classList.remove('bi-circle-half', 'bi-highlights');
icon.classList.add(contrast === 'high' ? 'bi-highlights' : 'bi-circle-half');
};
const setPressedState = (button, contrast) => {
button.setAttribute('aria-pressed', contrast === 'high' ? 'true' : 'false');
};
export function initContrastToggle(root = document, options = {}) {
const selector = String(options.selector || '[data-contrast-toggle]').trim() || '[data-contrast-toggle]';
const storage = createUiStorage({
namespace: options.storageNamespace || 'app-ui',
version: options.storageVersion || 'v1',
scope: options.storageScope || 'contrast',
});
const scopedStorageKey = storage.buildKey('state', options.storageKey || STORAGE_KEY);
const host = resolveHost(root);
const button = host.matches?.(selector) ? host : host.querySelector(selector);
if (!(button instanceof HTMLElement)) {
warnOnce('UI_EL_MISSING', `Missing contrast toggle: ${selector}`, { module: 'contrast-toggle' });
return { destroy: () => {} };
}
if (button.dataset.contrastToggleBound === '1') {
return {
destroy: () => {
delete button.dataset.contrastToggleBound;
},
};
}
button.dataset.contrastToggleBound = '1';
const getCurrent = () => (document.documentElement.dataset.contrast === 'high' ? 'high' : 'normal');
const current = getCurrent();
setIcon(button, current);
setPressedState(button, current);
const onClick = () => {
const next = getCurrent() === 'high' ? 'normal' : 'high';
setContrast(next);
setIcon(button, next);
setPressedState(button, next);
storage.setItem(scopedStorageKey, next);
};
button.addEventListener('click', onClick);
const destroy = () => {
button.removeEventListener('click', onClick);
delete button.dataset.contrastToggleBound;
};
return { destroy };
}