Theming
One typed theme file controls color, type, shape, depth, motion and component defaults. Providers apply it and handle color mode.
Status: stable. Category: Patterns.
import { GlyphProvider, ThemeScope, ColorModeProvider, ColorModeScript } from '@glyphui/ui'
Exports: GlyphProvider, ThemeScope, ColorModeProvider, ColorModeScript, getColorModeScript, useColorMode, useGlyph, useLinkComponent, useIsActive, defineTheme, themeToCss, tokenVar, auditContrast, contrastRatio, presets, glyph, brutalist, editorial, playful.
Notes
- Render GlyphProvider once at the root. Prevent the dark-mode flash with the Vite plugin (@glyphui/ui/vite), <ColorModeScript /> with SSR, or getColorModeScript() in <head>.
- Pass router to connect your router: links navigate client-side and navigation items mark the current page. useIsActive(href) exposes the same matching to your own components.
- theme.components is typed per component: a Button size that does not exist is a compile error.
- Change a solid color like primary without its foreground and defineTheme picks readable text for it. In development, GlyphProvider warns when a theme still fails WCAG contrast; auditContrast(theme) lists every failing pair.
- theme.motion sets durations, easings, the motion level (none, subtle or expressive) and the spring ({ stiffness, damping, mass? }) behind overlays, indicators and reveals. See Motion.
GlyphProvider props
| Prop | Type | Default | Description |
|---|---|---|---|
defaultMode | ColorModePreference | Color mode used until the user picks one. | |
linkComponent | LinkComponent | Your router's link only. Prefer router, which also handles active items. | |
motion | "none" | "subtle" | "expressive" | Overrides the theme's motion level everywhere, e.g. "none" in tests or for a user setting. Visitors who ask for reduced motion never get animation either way. | |
router | GlyphRouter | Your router: its link component, plus an optional useIsActive hook that marks the current page in Sidebar, Navbar and NavigationMenu. React Router: import { reactRouter } from '@glyphui/ui/react-router'. | |
storageKey | string | null | localStorage key for the user's color mode. null disables persistence. | |
theme | Theme | tokenPresets.glyph | The theme for the whole app. Defaults to the glyph preset. |
toaster | boolean | (ToasterProps & RefAttributes<HTMLElement>) | false | Mounts a themed <Toaster /> so toast() works anywhere. true for defaults, or pass Toaster props (e.g. { position: 'top-center' }). Off by default. |
Example: Presets side by side
ThemeScope renders a subtree with a different theme.
import { Badge, Button, presets, ThemeScope, useColorMode } from '@glyphui/ui'
export default function Example() {
const { resolvedMode } = useColorMode()
return (
<div className="grid gap-3 sm:grid-cols-2">
{Object.values(presets).map((theme) => (
<ThemeScope
key={theme.name}
theme={theme}
mode={resolvedMode}
className="grid gap-3 rounded-lg border border-border p-4"
>
<div className="flex items-center justify-between">
<span className="font-display text-h4 capitalize">{theme.name}</span>
<Badge tone="primary">Preset</Badge>
</div>
<div className="flex gap-2">
<Button size="sm">Primary</Button>
<Button size="sm" variant="outline">
Outline
</Button>
</div>
</ThemeScope>
))}
</div>
)
}
Example: Color mode
import { type ColorModePreference, SegmentedControl, Text, useColorMode } from '@glyphui/ui'
export default function Example() {
const { mode, resolvedMode, setMode } = useColorMode()
return (
<div className="grid gap-2">
<SegmentedControl<ColorModePreference>
aria-label="Color mode"
value={mode}
onValueChange={setMode}
options={[
{ value: 'light', label: 'Light' },
{ value: 'dark', label: 'Dark' },
{ value: 'system', label: 'System' },
]}
/>
<Text size="small" tone="muted">
Showing {resolvedMode} mode.
</Text>
</div>
)
}
Example: Extending a preset
import { Button, defineTheme, presets, ThemeScope, useColorMode } from '@glyphui/ui'
const acme = defineTheme({
name: 'acme',
extends: presets.glyph,
colors: {
light: { primary: 'oklch(55% 0.19 45)', ring: 'oklch(55% 0.19 45)' },
// A bright orange needs dark text on top to stay readable.
dark: {
primary: 'oklch(72% 0.17 45)',
'primary-foreground': 'oklch(20% 0.04 45)',
ring: 'oklch(72% 0.17 45)',
},
},
radius: { md: '9999px' },
components: { Button: { defaultVariants: { size: 'lg' } } },
})
export default function Example() {
const { resolvedMode } = useColorMode()
return (
<ThemeScope
theme={acme}
mode={resolvedMode}
className="flex flex-wrap gap-2 rounded-lg border border-border p-4"
>
<Button>Orange, pill, large</Button>
<Button variant="outline">Same theme file</Button>
</ThemeScope>
)
}
Live page: https://glyphui.xyz/components/theme