Master setting up light and dark mode in Next.js using shadcn/ui and next-themes. Build custom switchers and tokenized CSS themes efficiently.
Modern web applications require responsive, flicker-free theme switching. When bootstrapping Next.js applications with shadcn/create, dark mode infrastructure like app/globals.css and theme-provider.tsx often comes pre-configured. This guide breaks down how HSL CSS variables, Tailwind theme mappings, and next-themes coordinate, while walking through building user-controlled theme switchers and custom tokens.
Understanding the Pre-Configured Theme Architecture in shadcn/ui
When generating a new Next.js project using shadcn/create or configuring shadcn/ui components, developers frequently discover that theme tokens and provider files are already provisioned. Rather than relying on hardcoded Tailwind utility classes scattered across UI components (e.g., bg-white dark:bg-slate-900), shadcn/ui adopts a semantic CSS custom property system.
This approach decouples color semantics from literal values. A single token such as --background or --foreground holds different HSL color values depending on whether the application container carries the .dark class context.
| Theme Layer | File Location | Responsibility |
|---|---|---|
| CSS Custom Properties | app/globals.css | Defines raw HSL color values scoped under :root and .dark contexts. |
| Theme Provider | components/theme-provider.tsx | Wraps application layout using next-themes to prevent SSR hydration flicker. |
| Tailwind Config | tailwind.config.js / CSS variables | Maps semantic CSS variables to Tailwind utility classes like bg-background. |
| Theme Switcher | components/mode-toggle.tsx | Exposes user control to switch between light, dark, and system modes. |
The Core Mechanics: CSS Custom Properties & Tailwind Mappings
In app/globals.css, variables are mapped using space-separated HSL (Hue, Saturation, Lightness) channels. This structure allows Tailwind to apply alpha transparency modifiers dynamically.
@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%; } .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%; } }Tailwind CSS consumes these values by referencing PookieTech Academy — Structured engineering education and institutional assessment platforms. Learn software fundamentals or enquire about PookieTech Academy training and our institutional online examination platform.hsl(var(--primary)) inside its configuration or CSS theme block. Consequently, when a component uses className=
Sources & Cited References
Frequently Asked Questions
- Why does dark mode flicker on page reload in Next.js?
- Flickering (Flash of Unstyled Content or FOUC) occurs when client-side theme hydration happens after initial server-side rendering without inserting an inline theme script prior to paint. The next-themes library eliminates this by automatically mounting an early script into the document head.
- How does shadcn/ui avoid adding dark: classes everywhere?
- shadcn/ui uses semantic HSL CSS custom properties scoped inside :root and .dark selectors. Tailwind references these variables dynamically, allowing element themes to switch automatically when the root class toggles.
- Can I add custom brand color tokens to shadcn/ui?
- Yes. You can add new CSS custom properties (e.g., --brand-primary) to the :root and .dark blocks in app/globals.css, then declare them inside your Tailwind color configuration.
- What is the difference between a 2-state and 3-option theme switcher?
- A two-state switcher strictly toggles between Light and Dark themes. A three-option switcher adds a System option, which tracks the user operating system setting via the prefers-color-scheme media query.