shadcn/uiDark ModeError Fix2026 Updated

shadcn/ui Dark Mode Not Working— next-themes Setup [Fixed 2026]

Also covers: ThemeProvider · CSS variables dark block · hydration mismatch · FOIT · theme toggle button

January 20266 min readshadcn/ui · next-themes · Next.js

⚡ The Symptom

Error
// Theme toggle fires but components stay in light mode
// OR:
// Dark mode works but flashes white on every page load
// OR:
// Hydration mismatch warning in console

✅ Setup sequence

terminal
npm install next-themes

Why shadcn/ui Dark Mode Doesn't Toggle

shadcn/ui uses Tailwind's class dark mode strategy — it reads the dark class on the html element. If that class never gets added, no dark mode applies. The two most common causes are: ThemeProvider not wrapping the app, and the dark CSS variable block missing from globals.css.

1

Install and Configure next-themes

Foundation — theme state manager
2 min
tailwind.config.ts — ensure class-based dark mode
import type { Config } from "tailwindcss"

const config: Config = {
  darkMode: ["class"],   // ← must be "class" not "media" for next-themes
  content: [
    "./app/**/*.{ts,tsx}",
    "./components/**/*.{ts,tsx}",
  ],
  // ... rest of config
}

export default config
2

Wrap Your App in ThemeProvider

ThemeProvider must wrap the entire app
3 min
components/theme-provider.tsx
"use client"

import { ThemeProvider as NextThemesProvider } from "next-themes"
import type { ThemeProviderProps } from "next-themes/dist/types"

export function ThemeProvider({ children, ...props }: ThemeProviderProps) {
  return <NextThemesProvider {...props}>{children}</NextThemesProvider>
}
app/layout.tsx — wrap with ThemeProvider
import { ThemeProvider } from "@/components/theme-provider"

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en" suppressHydrationWarning>   {/* ← suppressHydrationWarning is required */}
      <body>
        <ThemeProvider
          attribute="class"          // adds/removes "dark" class on <html>
          defaultTheme="system"      // follow OS preference by default
          enableSystem               // enable system theme detection
          disableTransitionOnChange  // prevent flash during theme change
        >
          {children}
        </ThemeProvider>
      </body>
    </html>
  )
}
3

Add Dark CSS Variables to globals.css

Dark class applied but colors don't change
3 min
app/globals.css — dark mode block
@layer base {
  :root {
    --background: 0 0% 100%;
    --foreground: 222.2 84% 4.9%;
    --primary: 222.2 47.4% 11.2%;
    --primary-foreground: 210 40% 98%;
    --border: 214.3 31.8% 91.4%;
    /* ... all light mode variables */
  }

  /* ← This dark block is required — without it nothing changes in dark mode */
  .dark {
    --background: 222.2 84% 4.9%;
    --foreground: 210 40% 98%;
    --primary: 210 40% 98%;
    --primary-foreground: 222.2 47.4% 11.2%;
    --border: 217.2 32.6% 17.5%;
    /* ... all dark mode overrides */
  }
}

The shadcn init command generates a complete .dark {} block for your chosen color palette. If you ran init, this block should already be in globals.css. Check if it's there before adding manually.

4

Fix Hydration Mismatch

Console warning about class attribute mismatch
2 min

When Next.js renders on the server, it doesn't know the user's theme preference (stored in localStorage). The suppressHydrationWarning attribute on html tells React not to warn about this expected mismatch.

Mounting guard for theme-dependent UI
"use client"

import { useTheme } from "next-themes"
import { useEffect, useState } from "react"

export function ThemeToggle() {
  const { theme, setTheme } = useTheme()
  const [mounted, setMounted] = useState(false)

  // ✅ Wait for component to mount before reading theme
  // Prevents hydration mismatch in the toggle button icon
  useEffect(() => setMounted(true), [])

  if (!mounted) return null   // render nothing on server

  return (
    <button onClick={() => setTheme(theme === "dark" ? "light" : "dark")}>
      {theme === "dark" ? "🌙" : "☀️"}
    </button>
  )
}
5

Build a Complete Theme Toggle Button

Polished toggle with Sun/Moon icons
5 min
components/theme-toggle.tsx
"use client"

import { useTheme } from "next-themes"
import { useEffect, useState } from "react"
import { Button } from "@/components/ui/button"
import {
  DropdownMenu, DropdownMenuContent,
  DropdownMenuItem, DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu"

export function ThemeToggle() {
  const { setTheme } = useTheme()
  const [mounted, setMounted] = useState(false)
  useEffect(() => setMounted(true), [])

  if (!mounted) return <Button variant="ghost" size="icon" disabled><span className="sr-only">Toggle theme</span></Button>

  return (
    <DropdownMenu>
      <DropdownMenuTrigger asChild>
        <Button variant="ghost" size="icon">
          {/* Sun icon — visible in light mode */}
          <span className="h-4 w-4 dark:hidden">☀️</span>
          {/* Moon icon — visible in dark mode */}
          <span className="h-4 w-4 hidden dark:block">🌙</span>
          <span className="sr-only">Toggle theme</span>
        </Button>
      </DropdownMenuTrigger>
      <DropdownMenuContent align="end">
        <DropdownMenuItem onClick={() => setTheme("light")}>Light</DropdownMenuItem>
        <DropdownMenuItem onClick={() => setTheme("dark")}>Dark</DropdownMenuItem>
        <DropdownMenuItem onClick={() => setTheme("system")}>System</DropdownMenuItem>
      </DropdownMenuContent>
    </DropdownMenu>
  )
}

Prevention

Frequently Asked Questions

How does shadcn/ui implement dark mode?+
shadcn/ui uses Tailwind's class-based dark mode strategy. Adding the 'dark' class to the <html> element activates all dark: Tailwind utilities. The CSS variables in globals.css have a .dark block that overrides the light mode values. next-themes handles adding/removing the dark class.
Do I need next-themes for dark mode with shadcn/ui?+
next-themes is the recommended package for shadcn/ui dark mode. It handles persisting the user's theme preference in localStorage, server-side rendering, and preventing flash of incorrect theme (FOIT). You can implement dark mode without it, but next-themes handles the edge cases correctly.
Why does my dark mode flash white on page load?+
This is a Flash of Incorrect Theme (FOIT). next-themes prevents it by injecting a script that sets the correct class before React hydrates. Ensure you add suppressHydrationWarning to the <html> element in layout.tsx. Without it, React warns about the class mismatch between server and client.
Why do shadcn components not change color in dark mode?+
If the dark class is added to <html> but components don't change, the .dark {} CSS variable block in globals.css is missing. shadcn/ui components use CSS variables for all colors — the dark block overrides these to the dark palette values.
Can I force dark mode always without a toggle?+
Yes. In tailwind.config.ts, set darkMode: 'class' and add the dark class permanently to your <html> tag. Or use darkMode: 'media' to follow the system preference automatically without any toggle or ThemeProvider.
How do I access the current theme in a component?+
Use the useTheme hook from next-themes: const { theme, setTheme } = useTheme(). Remember that useTheme must be used in a Client Component. The theme value is 'light', 'dark', or 'system'.

Need Expert Help?

We Build Dark Mode UI with shadcn/ui

Softplix engineers set up shadcn/ui with full dark mode, custom themes, and accessible design systems. Let us help.

Talk to an Engineer