guides
Dark Mode
Add dark mode to Next.js, Vite, React Router, TanStack Start and Astro without a flash of the wrong theme.
cnippet ui switches themes with a dark class on the <html> element. The CSS defines every color token twice, once in :root and once in .dark, so adding or removing the class re-colors every component. There are no dark: color overrides to maintain.
The CLI adds both blocks when you run init. Colors lists every token in both themes.
Next.js
next-themes stores the user's choice, follows the system setting and adds the class before the page paints.
Install next-themes
Create a theme provider
Wrap your root layout
Add suppressHydrationWarning to <html>. next-themes changes its class before React hydrates, and the attribute only silences the warning for that one element.
Add a toggle
Use the mode toggle below. With next-themes, import useTheme from next-themes.
Vite
A Vite app has no server, so add a small script to index.html that sets the class before React loads, plus a provider that saves the user's choice.
Add the script to index.html
Put it in <head>, before any stylesheet or module script.
Create a theme provider
Wrap your app
React Router and TanStack Start
Both render on the server, which can't read localStorage. Use the same script and provider as the Vite setup, but render the script in the root route's <head> and add suppressHydrationWarning to <html>.
In TanStack Start, do the same in the shellComponent of src/routes/__root.tsx: put the script next to <HeadContent /> and wrap {children} in <ThemeProvider>.
Astro
Add the script to your layout's <head> with is:inline so Astro doesn't bundle or defer it:
Astro islands don't share React context, so render the mode toggle with client:load and let it write to localStorage directly instead of using a provider:
Mode toggle
This toggle works with next-themes and with the provider above. It reads the current theme from the <html> class, and both icons are rendered and hidden with CSS, so the server and client output match and there's no hydration warning.
To offer Light, Dark and System, put the options in a Menu with a MenuRadioGroup and call setTheme from onValueChange.
Avoiding a flash of the wrong theme
The page flashes when the browser paints with the light tokens and the dark class is added afterwards by JavaScript. To avoid it:
- Set the class before the first paint. The theme must be applied by a blocking script in
<head>. next-themes injects one for you. For other frameworks, use the inline script above. Don't set the class inuseEffectalone, because effects run after paint. - Set
color-scheme. It makes native scrollbars, form controls and the page background match the theme. next-themes does this withenableColorScheme, and the script above sets it too. - Disable transitions while switching. Components have color transitions, so switching themes can animate every surface at once. next-themes'
disableTransitionOnChangeturns them off for one frame. - Don't render the theme name on the server. The server doesn't know the user's choice. Show icons with
dark:hidden/not-dark:hiddenas in the toggle, or render the theme name only after the component has mounted.
Force a theme on part of the page
Add the dark class to any element to render its subtree with the dark tokens, even when the page is light:
The element needs bg-background and text-foreground because the page background comes from <body>, which is outside the section.
To force light on part of a dark page, set the light values on a class of your own (for example .light) with the same variables as :root.