Skip to content

shadcn/ui dark mode not working? Check these seven things

Is the dark class on <html>? If not: no provider, a flash on load, or Astro view transitions. If it is: colours without a dark value or @theme without inline.

Updated 23 November 2026Tested with
  • shadcn 4.23.0
  • Tailwind CSS 4.3.5
  • Vite 8
  • React 19.3
  • Astro 7.4.13
  • next-themes 0.4.6

shadcn/ui components have no dark mode logic of their own. Every colour is a CSS variable with one value under :root and another under .dark, and the whole interface switches when the dark class is on <html>. So when dark mode "doesn't work", either the class isn't there at the moment you look, or some of your colours have no dark value. Open the inspector, check <html class="…">, and you'll know which half of this page you need.

The same shadcn/ui dialog and accordion in light and dark mode, side by side
The test project for this page: shadcn 4.23 with Radix, the Nova style, on Vite. Same markup, the class on <html> is the only difference.

When the dark class is missing

Nothing sets the class

shadcn needs something that writes the class, remembers the choice and follows the system setting. In our test with CLI 4.23, shadcn init -t vite generated src/components/theme-provider.tsx and wrapped the app in it in main.tsx. Its default is "system", it stores the choice under the key theme, and you can press d to flip it while testing. If you set up the project by hand or deleted that wrapper, nothing adds the class. Check that main.tsx still renders <ThemeProvider> around <App />.

On Next.js the shadcn docs use next-themes. The two settings people miss are attribute="class" (its default is a data-theme attribute, which the shadcn variant doesn't look at) and suppressHydrationWarning on <html>:

app/layout.tsxTSX
import { ThemeProvider } from "next-themes" export default function RootLayout({ children }: { children: React.ReactNode }) {  return (    <html lang="en" suppressHydrationWarning>      <body>        <ThemeProvider attribute="class" defaultTheme="system" enableSystem disableTransitionOnChange>          {children}        </ThemeProvider>      </body>    </html>  )}

The page flashes light before it turns dark

The Vite provider applies the class in a React effect, which runs after the first paint. For a split second the page uses the light values. Set the class before React loads with an inline script in index.html, reading the same storage key the provider uses:

index.htmlHTML
<head>  <script>    const t = localStorage.getItem("theme");    const dark = t === "dark" || ((!t || t === "system") && matchMedia("(prefers-color-scheme: dark)").matches);    document.documentElement.classList.add(dark ? "dark" : "light");  </script></head>

next-themes injects an equivalent script for you, which is why it needs suppressHydrationWarning: the server-rendered <html> has no class and the browser's does.

Astro: dark mode resets on every page

This one we reproduced exactly. With Astro's <ClientRouter /> (view transitions), the class is on <html> on the first page and gone after you click a link. The router swaps in the new page's <html> attributes, and the new page's server HTML has no class. Our test logged "dark" before the click and "" after it.

Apply the theme to the incoming document before the swap:

src/layouts/Layout.astroAstro
<script is:inline>  function applyTheme(doc) {    const stored = localStorage.getItem("theme");    const dark = stored ? stored === "dark" : matchMedia("(prefers-color-scheme: dark)").matches;    doc.documentElement.classList.toggle("dark", dark);  }  applyTheme(document);  document.addEventListener("astro:before-swap", (e) => applyTheme(e.newDocument));</script>

With the last line added, the class survived navigation in the same test. Without view transitions you only need the first two statements.

When the class is there but colours are wrong

A colour has no .dark value

Most "half dark" pages come from colours added after init: a --brand or --success defined in :root and nowhere else. The component goes dark around it and that one colour stays light. Every variable you add needs a line in both blocks. The full pattern is in adding a colour to a shadcn theme.

Hard-coded Tailwind colours

bg-white, text-zinc-900 or border-gray-200 in your own components don't change in dark mode. Use the semantic utilities (bg-background, text-foreground, border-border, bg-muted) or add a dark: counterpart.

Dark mode inside one section doesn't work

If you put class="dark" on a single card or section and it stays light, check the colour mapping. It must be @theme inline. With plain @theme, the colours resolve once on :root and a nested .dark can't change them. We measured it: white with @theme, black with @theme inline, same markup.

A dark: class on <html> itself does nothing

shadcn's CSS defines the variant as @custom-variant dark (&:is(.dark *));. That matches elements inside .dark, not the element that has the class. Put dark: utilities on <body> or below. If you need it on the same element, change the variant to (&:where(.dark, .dark *)); Tailwind dark mode covers the options.

Questions

How do I add a dark mode toggle to shadcn/ui?

Use the hook from the provider: const { setTheme } = useTheme(), then call setTheme("dark"), "light" or "system" from a button or a dropdown. The Vite provider and next-themes both export useTheme with the same shape.

Why does next-themes show a hydration warning?

Because its script changes <html> before React hydrates. suppressHydrationWarning on <html> silences exactly that one element and nothing else.

Can I default to dark?

Pass defaultTheme="dark" to the provider, and change the fallback in your inline script to match, or the first paint will still follow the system.

Was this page helpful?

Still stuck? We answer every message ourselves. Write to us

This page is built from UI Pro parts: the docs article block (docs-quernly) with the code, terminal, install command, steps, contents and feedback components. They ship in the next UI Pro release; the install command and the contents rail are free.