Sub topic 9
Theming with CSS variables and dark mode
This chapter assumes you have run npx shadcn@latest init and that your project uses Tailwind CSS. The examples use the Tailwind v3 setup, where colors are stored as HSL channels. Newer templates may use oklch values, but the structure is the same.
How the theme reaches a component
shadcn/ui components never hard-code colors such as bg-slate-900. The button from the previous chapters uses semantic names like bg-primary and text-primary-foreground. Each name points to a CSS variable. Change the variable and every component that uses it changes.
Defining the variables
The init command writes variables like these into your global stylesheet. Values are bare HSL channels without the hsl() wrapper.
@layer base {
:root {
--background: 0 0% 100%;
--foreground: 222 47% 11%;
--primary: 222 47% 11%;
--primary-foreground: 210 40% 98%;
--accent: 210 40% 96%;
--input: 214 32% 91%;
--radius: 0.5rem;
}
.dark {
--background: 222 47% 11%;
--foreground: 210 40% 98%;
--primary: 210 40% 98%;
--primary-foreground: 222 47% 11%;
--accent: 217 33% 17%;
--input: 217 33% 17%;
}
}Tailwind needs to know these names. The mapping wraps each variable in hsl(). Wrapping at this point lets opacity modifiers such as hover:bg-primary/90 work.
import type { Config } from "tailwindcss"
const config: Config = {
darkMode: ["class"],
content: ["./src/**/*.{ts,tsx}"],
theme: {
extend: {
colors: {
background: "hsl(var(--background))",
foreground: "hsl(var(--foreground))",
primary: {
DEFAULT: "hsl(var(--primary))",
foreground: "hsl(var(--primary-foreground))",
},
accent: "hsl(var(--accent))",
input: "hsl(var(--input))",
},
borderRadius: {
md: "calc(var(--radius) - 2px)",
},
},
},
}
export default configEach color follows a pairing convention. A color such as primary has a matching primary-foreground for text drawn on top of it. Keep that pairing when you add new colors, so contrast stays correct in both themes.
Switching to dark mode
With darkMode: ["class"], the .dark rules apply whenever an ancestor has the dark class. Put that class on the <html> element.
type Theme = "light" | "dark"
export function setTheme(theme: Theme) {
document.documentElement.classList.toggle("dark", theme === "dark")
localStorage.setItem("theme", theme)
}
export function initTheme() {
const saved = localStorage.getItem("theme") as Theme | null
const prefersDark = window.matchMedia("(prefers-color-scheme: dark)").matches
setTheme(saved ?? (prefersDark ? "dark" : "light"))
}Components need no dark: prefixes, because the variables change underneath them.
Common errors
- Colors do not change. The variable name in the stylesheet differs from the one in the Tailwind mapping. Check for typos.
- Opacity modifiers fail. The variable holds a full
hsl(...)value, but the mapping wraps it again. Use one convention consistently. - Dark mode never applies.
darkModeis missing from the config, or thedarkclass sits on an element that is not an ancestor of the component.
Creating a new theme
To rebrand, edit only the variable values. For a second theme, add another class selector, such as .theme-green, with its own variables, and toggle that class the same way.
Exercise
You add a --success and a --success-foreground variable to :root and .dark, but bg-success produces no styles. What step is missing, and why?
Show answer
The Tailwind config has no success entry in theme.extend.colors. Tailwind only generates utilities for names it knows, so you must map success to hsl(var(--success)) and success.foreground to hsl(var(--success-foreground)).