Skip to content
Alea0.1

Extras

ThemeProvider and toggle

Dark, light or system, remembered, with a toggle in two looks.

import { ThemeProvider, ThemeToggle, useTheme } from '@alea/ui';

Examples

Theme toggle

An icon button that flips dark and light, or a segmented control with system.

Picked: dark · showing: dark

Provider

Wrap the app once. scope="element" themes one area instead of the page.

Always light

This area has its own provider.

Props

ThemeProvider props

Puts the Alea tokens in dark, light or system mode and shares the choice.

PropTypeDefaultDescription
brandBrandTheme-Brand changes: accent color, radius, density, fonts.
classNamestring-Class names for the wrapper (scope="element").
defaultTheme"dark" | "light" | "system"'dark'Theme to start with when nothing is stored. Defaults to dark.
navigate((path: string, options?: any) => void)-Your router's navigate function (React Router's useNavigate(), Next's router.push): links in the kit then navigate without a reload.
onThemeChange((theme: ThemeName) => void)-Called with the new theme when it changes.
scope"document" | "element"'document'Where data-theme goes: the document root (default) or a wrapper div.
storageKeystring | null'alea-theme'localStorage key to remember the choice; pass null to not store it.
theme"dark" | "light" | "system"-Controlled theme.
useHref((href: string) => string)-Your router's useHref, for routers with a base path.

ThemeToggle props

Switches the theme (needs ThemeProvider for system and storage).

PropTypeDefaultDescription
appearance"icon" | "segmented"'icon'icon: one button cycling dark/light; segmented: dark, light, system.
classNamestring-Extra class names.

Keyboard

KeyDoes
Enter or SpaceSwitches the theme

Do and don't

Do

  • Put ThemeProvider once near the root.
  • Respect system as the default.

Don't

  • Don't hard-code colors: use the tokens so both themes work.
  • Don't hide the toggle on mobile.
Tested with axe, keyboard and screen reader roles