Dark mode
A .dark class on an ancestor switches every role. Set it with next-themes or a few lines of script.
How it works
The theme writes every role twice: once under :root and once under .dark.
When an ancestor — normally <html> — has the dark class, the same variable
names resolve to their dark values:
:root {
--weak-50: oklch(0.975 0.0022 264); /* nearly white */
}
.dark {
--weak-50: oklch(0.283 0.0121 264); /* nearly black */
}bg-weak-50 reads var(--weak-50), so it follows. That is all dark mode is.
Why components have no dark: classes
Because nothing in a component needs to change. A component says
text-sub-600 ("secondary text"); dark mode decides which color secondary text
is. Writing dark:text-gray-400 next to it would duplicate that decision in
every file, and the two would drift. tdpxl's source has no dark: variants at
all, and its rules forbid them.
The CLI still adds @custom-variant dark (&:is(.dark *)); to your stylesheet,
so the dark: variant follows the same class in your own code if you need it.
Next.js with next-themes
npm install next-themes// app/layout.tsx
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>
)
}attribute="class" is the important part: it toggles dark on <html>.
suppressHydrationWarning is needed because next-themes sets that class before
React hydrates. Switch themes with the hook:
"use client"
import { useTheme } from "next-themes"
import { Button } from "@/components/ui/button"
export function ThemeToggle() {
const { resolvedTheme, setTheme } = useTheme()
const next = resolvedTheme === "dark" ? "light" : "dark"
return (
<Button intent="neutral" emphasis="ghost" onClick={() => setTheme(next)}>
Switch to {next} theme
</Button>
)
}Vite with a class toggle
Without a framework provider, set the class yourself. Run this inline script in
index.html, before your app loads, so the first paint already has the right
colors:
<script>
const stored = localStorage.getItem("theme")
const prefersDark = window.matchMedia("(prefers-color-scheme: dark)").matches
document.documentElement.classList.toggle("dark", stored ? stored === "dark" : prefersDark)
</script>And toggle it from React:
import { Button } from "@/components/ui/button"
export function ThemeToggle() {
function toggle() {
const dark = document.documentElement.classList.toggle("dark")
localStorage.setItem("theme", dark ? "dark" : "light")
}
return (
<Button intent="neutral" emphasis="ghost" onClick={toggle}>
Toggle theme
</Button>
)
}Toasts
The Sonner Toaster reads its theme from
next-themes. A .dark class alone does not configure it: inside next-themes'
ThemeProvider it follows the theme automatically, and anywhere else pass
theme="light" or theme="dark" to Toaster yourself.
Checking both schemes
Contrast is tested per scheme. If you change a role, check it in both — see Theming and Accessibility.