useDark
Synchronize a dark-mode preference with a CSS class and the user’s preferred color scheme.
Edit the code and the preview updates as you type.
What it does
A theme toggle should honor the system preference, let the user override it, and keep the document class in sync.
The playground is a small, runnable example. Change the code and try the interaction before copying it into an app.
The code
const isDark = useDark()
const toggleDark = useToggle(isDark)
// Bind isDark to the theme toggle and CSS.Where you'd use it
- A user-controlled site theme
- Keeping theme state synced across tabs
- Applying a preference to the document class
In Nuxt (and other gotchas)
The system preference and persisted override are client state. Avoid rendering theme-dependent markup differently on the server; for flash-free theme setup, initialize the class before hydration or use an SSR-readable cookie strategy.
When not to use it
Use CSS prefers-color-scheme alone when users do not need an override. Check contrast in both themes and do not rely on color alone to communicate state.
Links
- Official useDark docs for every option and type
- Source code on GitHub