Dark Mode
Theme switching with the data-theme attribute, persistence, and flash prevention.
Dark mode is the default. @diffgazer/ui uses the [data-theme] attribute for theme switching.
How it works
All @diffgazer/ui components use semantic CSS variables (var(--background), var(--foreground), etc.) which reference the primitives. Changing the data-theme attribute switches every component instantly.
Subtree theming
data-theme is not limited to <html>. Because the Tailwind bridge maps tokens with @theme inline, every token utility resolves its semantic variable at the styled element — so setting data-theme on any element re-themes that subtree only:
This is how a theme preview can show light and dark side by side without re-declaring the token table.
Switching themes
Set the data-theme attribute on the root element:
Persisting theme preference
Store the user's choice in localStorage:
Respecting system preference
Tip: @diffgazer/ui defaults to dark mode. If no data-theme attribute is set, the :root selector applies dark mode variables.
Flash prevention
To prevent a flash of wrong theme on page load, set the attribute before React hydrates: