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
41
frontend/packages/swarm-ui/src/lib/motion-apply.ts
Normal file
41
frontend/packages/swarm-ui/src/lib/motion-apply.ts
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
// Applies the stored reduced-motion override (see `settings-storage.ts`)
|
||||
// as a `data-motion="reduce"|"allow"` attribute on `<html>`, cleared
|
||||
// when the override is "system" (letting `prefers-reduced-motion`
|
||||
// alone decide, same as today).
|
||||
//
|
||||
// Currently inert: swarm-ui has zero CSS animations as of this file's
|
||||
// writing (the design guide's "playful whimsy" motion — the matrix-rain
|
||||
// home background — lives in the dashboard package, not swarm-ui). This
|
||||
// hook exists because the settings surface itself was scoped to cover
|
||||
// theme *and* motion together (both named in the same go-ahead), and the
|
||||
// marginal cost of the storage/attribute plumbing is near zero riding
|
||||
// alongside the theme override's real wiring — but there is genuinely
|
||||
// nothing in swarm-ui for `data-motion` to gate yet. The first swarm-ui
|
||||
// animation's own CSS is what makes this do anything, e.g.:
|
||||
// @media (prefers-reduced-motion: reduce) { ... }
|
||||
// :root[data-motion='reduce'] { ... same rule ... }
|
||||
// :root[data-motion='allow'] { /* opt back in despite OS-level reduce */ }
|
||||
import { useEffect } from 'preact/hooks';
|
||||
import { useLocalSetting } from './settings-storage.js';
|
||||
|
||||
export type MotionOverride = 'system' | 'reduce' | 'allow';
|
||||
|
||||
export const MOTION_OVERRIDE_KEY = 'swarm-ui:motion-override';
|
||||
|
||||
export function useMotionOverride() {
|
||||
return useLocalSetting<MotionOverride>(MOTION_OVERRIDE_KEY, 'system');
|
||||
}
|
||||
|
||||
// Mounted once alongside `useApplyThemeOverride` — see that module's
|
||||
// comment for why `Shell` is the right single mount point.
|
||||
export function useApplyMotionOverride(): void {
|
||||
const [override] = useMotionOverride();
|
||||
useEffect(() => {
|
||||
const root = document.documentElement;
|
||||
if (override === 'system') {
|
||||
delete root.dataset.motion;
|
||||
} else {
|
||||
root.dataset.motion = override;
|
||||
}
|
||||
}, [override]);
|
||||
}
|
||||
Loading…
Reference in a new issue