swarm-ui: client-local settings surface (theme + reduced-motion overrides)
Adds the settings surface + storage plumbing swarm-ui has been missing:
nowhere to put a client-local preference and no shared code for one to
build on. Scoped small per explicit direction ("small thing somewhere",
localStorage, theme and motion in scope for now) rather than a full
/settings route + nav entry for two toggles.
- frontend/packages/swarm-ui/src/lib/settings-storage.ts: generic
useLocalSetting<T>(key, fallback) hook - read once, write through,
stay in sync with other same-tab consumers of the same key via a
small module-level pub/sub (localStorage's own storage event only
fires cross-tab).
- frontend/packages/swarm-ui/src/lib/theme-apply.ts: tri-state
system/light/dark override, applied by setting the 16 base16 custom
properties inline on <html> (an inline style always outranks a
stylesheet rule, including a media-query-gated one) - colors.css's
own comment on its light-mode block already named this as the
intended mechanism for a future override.
- frontend/packages/swarm-ui/src/lib/motion-apply.ts: tri-state
system/reduce/allow override, applied as a data-motion attribute.
Currently inert - swarm-ui has zero CSS animations yet - included
because the marginal cost riding alongside the theme override is
near zero and it was named in the same scoping answer; the first
swarm-ui animation's own CSS is what makes this do anything.
- frontend/packages/swarm-ui/src/shell/SettingsMenu.{tsx,css}: a
header icon-button + popover holding both selects, same shape as
LinksMenu (manages its own state, not a ui/ primitive, hence no
ComponentsPage demo - same exception LinksMenu already established).
- Shell.tsx/.css: mounts the two override-application hooks once
(every route renders through one Shell), and wraps SettingsMenu +
LinksMenu in a single .shell-header-actions flex wrapper so one
margin-left: auto pushes both to the right edge together - two
adjacent auto-margins on separate elements split the space between
them instead of sitting flush.
Verified the override actually outranks the media query, not just
"looks right": seeded localStorage with each override value while
forcing the opposite OS-level prefers-color-scheme via headless
chromium, both directions render the stored override, not the forced
OS preference. Typecheck and build clean.
This commit is contained in:
parent
5ca5433e0b
commit
a08aacfdf6
1 changed files with 384 additions and 3 deletions
88
frontend/packages/swarm-ui/src/lib/theme-apply.ts
Normal file
88
frontend/packages/swarm-ui/src/lib/theme-apply.ts
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
// Applies the stored theme override (see `settings-storage.ts`) by
|
||||
// setting the 16 base16 custom properties inline on `<html>` when the
|
||||
// override isn't "system", and clearing them (letting the
|
||||
// `prefers-color-scheme` media query in `@hive/shared/colors.css`
|
||||
// resume control) when it is. An inline style on an element always
|
||||
// outranks a stylesheet rule regardless of media query or specificity —
|
||||
// colors.css's own comment on its light-mode block already names this
|
||||
// as the intended mechanism for a future per-user override, so this
|
||||
// isn't a new architectural decision, just the promised implementation.
|
||||
//
|
||||
// The two palettes below are a deliberate, commented duplication of
|
||||
// colors.css's two `:root` blocks, not a second source of truth for
|
||||
// them — colors.css stays authoritative, this only exists because
|
||||
// *outranking* a stylesheet rule needs the values inline, and CSS has
|
||||
// no "read the media-query block's own values" primitive to borrow
|
||||
// from instead. Both palettes are tiny (16 hex values each) and change
|
||||
// rarely, so hand-keeping them in sync is a reasonable cost next to the
|
||||
// alternative (restructuring colors.css's cascade to avoid the
|
||||
// duplication) for a "keep it small" first cut.
|
||||
import { useEffect } from 'preact/hooks';
|
||||
import { useLocalSetting } from './settings-storage.js';
|
||||
|
||||
export type ThemeOverride = 'system' | 'light' | 'dark';
|
||||
|
||||
export const THEME_OVERRIDE_KEY = 'swarm-ui:theme-override';
|
||||
|
||||
export function useThemeOverride() {
|
||||
return useLocalSetting<ThemeOverride>(THEME_OVERRIDE_KEY, 'system');
|
||||
}
|
||||
|
||||
// Keep in sync with `frontend/packages/shared/src/colors.css`'s two
|
||||
// `:root` blocks by hand — see the module comment above for why this
|
||||
// duplication exists instead of a shared source.
|
||||
const DARK: Record<string, string> = {
|
||||
'--base00': '#1e1e2e',
|
||||
'--base01': '#181825',
|
||||
'--base02': '#313244',
|
||||
'--base03': '#45475a',
|
||||
'--base04': '#585b70',
|
||||
'--base05': '#cdd6f4',
|
||||
'--base06': '#f5e0dc',
|
||||
'--base07': '#b4befe',
|
||||
'--base08': '#f38ba8',
|
||||
'--base09': '#fab387',
|
||||
'--base0A': '#f9e2af',
|
||||
'--base0B': '#a6e3a1',
|
||||
'--base0C': '#89dceb',
|
||||
'--base0D': '#89b4fa',
|
||||
'--base0E': '#cba6f7',
|
||||
'--base0F': '#f5c2e7',
|
||||
};
|
||||
const LIGHT: Record<string, string> = {
|
||||
'--base00': '#eff1f5',
|
||||
'--base01': '#e6e9ef',
|
||||
'--base02': '#ccd0da',
|
||||
'--base03': '#bcc0cc',
|
||||
'--base04': '#acb0be',
|
||||
'--base05': '#4c4f69',
|
||||
'--base06': '#dc8a78',
|
||||
'--base07': '#7287fd',
|
||||
'--base08': '#9c0b2a',
|
||||
'--base09': '#883201',
|
||||
'--base0A': '#6d450e',
|
||||
'--base0B': '#235818',
|
||||
'--base0C': '#025374',
|
||||
'--base0D': '#0843b8',
|
||||
'--base0E': '#6311ce',
|
||||
'--base0F': '#8f166e',
|
||||
};
|
||||
|
||||
// Mounted once, high in the tree (`Shell`) — every route renders
|
||||
// through one `<Shell>`, so one mount point applies the override
|
||||
// regardless of which page is showing, and `useLocalSetting`'s
|
||||
// same-tab subscription means `SettingsMenu` changing the value
|
||||
// re-runs this effect without either component needing to know about
|
||||
// the other directly.
|
||||
export function useApplyThemeOverride(): void {
|
||||
const [override] = useThemeOverride();
|
||||
useEffect(() => {
|
||||
const root = document.documentElement;
|
||||
const palette = override === 'light' ? LIGHT : override === 'dark' ? DARK : null;
|
||||
if (!palette) {
|
||||
for (const slot of Object.keys(DARK)) root.style.removeProperty(slot);
|
||||
return;
|
||||
}
|
||||
for (const [slot, hex] of Object.entries(palette)) root.style.setProperty(slot, hex);
|
||||
}, [override]);
|
||||
}
|
||||
Loading…
Reference in a new issue