Theme

stable 1.2 kB
<HkThemeService>

Reads, writes and persists the token layer at runtime. One class on the root element retunes every component, with no rebuild and no per-component theme prop.

#theming#service

Live demo

Running in this page — interact with it.

Inputs

NameTypeDefaultDescription
themeSignal<'light' | 'dark'>'dark'Current theme, readable as a signal.
storageKeystring'hk-theme'Where the choice is remembered.
accentsHkAccent[]16 named presetsThe built-in palette, from Crimson through Slate. A shortcut, not the API — any CSS colour works.

Methods

  • toggle()() => void

    Flips between light and dark.

  • set(theme)(theme: 'light' | 'dark') => void

    Applies a theme and persists the choice.

  • setToken(name, value)(name: string, value: string) => void

    Overrides one token at runtime.

  • setAccent(colour)(colour: string) => void

    Writes --hk-accent. Every shade is derived from it with color-mix, so an accent is one value rather than a five-step ramp.

Theming

Component tokens, each falling back to a global one:

PropertyDefaultControls
--hk-accent#dc2626Primary accent every component derives from.
--hk-radius12pxCorner radius scale.
--hk-density1Padding multiplier.

Accessibility

  • Sets color-scheme alongside the class, so browser-drawn controls — dropdown popups, scrollbars, date pickers — are painted the right way round.
  • The stored choice wins over the OS preference; the OS only decides for a first-time visitor.
  • The class is applied before first paint by an inline script, so a dark-mode visitor never sees a white flash.

More in Utility