About this course
<p>If you've created a Next.js project with shadcn/ui recently, there's a good chance dark mode is already set up.</p>
<p>I noticed this while looking through a fresh project. I opened <code>app/globals.css</code> expecting to add the theme colors, but the light and dark values were already there. Then I checked the <code>components</code> folder and found a <code>theme-provider.tsx</code> file that had already been created for me.</p>
<p>That raised a more useful question: if shadcn has already done most of the setup, what do you actually need to build?</p>
<p>In this guide, we'll look at the dark-mode setup that comes with a generated shadcn project, understand how the pieces work together, and build a theme switcher that users can control. We'll also look at how to customize the colors without adding light and dark classes to every component.</p>
<p>By the end, you'll know:</p>
<ul>
<li>Where shadcn stores its light and dark theme values</li>
<li>How CSS variables and Tailwind theme variables work together</li>
<li>What <code>next-themes</code> does in a Next.js project</li>
<li>How to add a two-state or three-option theme switcher</li>
<li>How to add your own theme tokens</li>
</ul>
<p>We'll start with the generated project and work through the theme setup before building the switcher.</p>
<p><strong>Before you start:</strong> This guide follows the Next.js project generated by <a href="https://ui.shadcn.com/create">shadcn/create</a>. Your project may look a little different if you use another framework, an older shadcn setup, or a different component style. I'll point out the parts you should check in your own project.</p>
<p>The examples and screenshots in this article are based on the companion project. Once you have the project locally, you can run <code>pnpm install</code> and <code>pnpm dev</code> to try it yourself.</p>
<img src="https://cdn.hashnode.com/uploads/covers/5e0f2c2e490269cb30227a2b/b000ee75-6dbb-480e-81cd-d7f39a07533c.png" alt="The companion demo app in light mode." style="display: block;" width="600" height="400" loading="lazy">
<img src="https://cdn.hashnode.com/uploads/covers/5e0f2c2e490269cb30227a2b/c99c2fb2-3a3b-48db-9846-59bf4be10834.png" alt="The companion demo app in dark mode." style="display: block;" width="600" height="400" loading="lazy">
<p><em>The same page is shown in both themes. Only the active theme values change.</em></p>
<p>The page uses the same JSX in both screenshots. The theme changes the values behind the CSS variables, and the browser updates the styles.</p>
<h2 id="heading-table-of-contents">Table of Contents</h2>
<ul>
<li><a href="#heading-project-setup-and-versions">Project Setup and Versions</a></li>
<li><a href="#heading-1-create-a-shadcn-project">1. Create a shadcn Project</a></li>
<li><a href="#heading-2-what-shadcn-has-already-configured">2. What shadcn Has Already Configured</a></li>
<li><a href="#heading-3-where-the-theme-colors-live">3. Where the Theme Colors Live</a><ul>
<li><a href="#heading-why-semantic-tokens-are-useful">Why Semantic Tokens Are Useful</a></li>
<li><a href="#heading-understanding-the-foreground-pairs">Understanding the Foreground Pairs</a></li>
</ul>
</li>
<li><a href="#heading-4-how-tailwind-gets-its-theme-classes">4. How Tailwind Gets its Theme Classes</a><ul>
<li><a href="#heading-what-the-dark-variant-does">What the dark: Variant Does</a></li>
</ul>
</li>
<li><a href="#heading-5-what-next-themes-does">5. What next-themes Does</a></li>
<li><a href="#heading-6-the-theme-provider-thats-already-in-your-project">6. The Theme Provider That's Already in Your Project</a><ul>
<li><a href="#heading-theres-a-keyboard-shortcut-too">There's a Keyboard Shortcut Too</a></li>
<li><a href="#heading-what-the-provider-options-mean">What the Provider Options Mean</a></li>
<li><a href="#heading-why-layouttsx-uses-suppresshydrationwarning">Why layout.tsx Uses suppressHydrationWarning</a></li>
</ul>
</li>
<li><a href="#heading-7-build-a-theme-switcher">7. Build a Theme Switcher</a><ul>
<li><a href="#heading-option-1-a-light-and-dark-toggle">Option 1: A light and Dark Toggle</a></li>
<li><a href="#heading-option-2-a-light-dark-and-system-menu">Option 2: A Light, Dark, and System Menu</a></li>
</ul>
</li>
<li><a href="#heading-8-why-the-icon-animation-may-not-work">8. Why the Icon Animation May Not Work</a></li>
<li><a href="#heading-9-customize-your-theme">9. Customize Your Theme</a><ul>
<li><a href="#heading-change-an-existing-token">Change an Existing Token</a></li>
<li><a href="#heading-add-a-custom-token">Add a Custom Token</a></li>
<li><a href="#heading-when-should-you-use-dark">When Should You Usedark:</a></li>
</ul>
</li>
<li><a href="#heading-conclusion">Conclusion</a></li>
<li><a href="#heading-references">References</a></li>
</ul>
<h2 id="heading-project-setup-and-versions">Project Setup and Versions</h2>
<p>The versions below are the versions used in the companion project, which was generated in August 2026. Shadcn's generated output changes over time, so check your own <code>package.json</code> if you're following along with a newer project.</p>
<table>
<thead>
<tr>
<th>Package</th>
<th>Version</th>
</tr>
</thead>
<tbody><tr>
<td><code>next</code></td>
<td><code>16.2.6</code></td>
</tr>
<tr>
<td><code>react</code></td>
<td><code>19.2.4</code></td>
</tr>
<tr>
<td><code>next-themes</code></td>
<td><code>0.4.6</code></td>
</tr>
<tr>
<td><code>@base-ui/react</code></td>
<td><code>1.7.0</code></td>
</tr>
<tr>
<td><code>tailwindcss</code></td>
<td><code>4</code></td>
</tr>
</tbody></table>
<p>The project's <code>components.json</code> includes <code>"style": "base-nova"</code> and <code>"baseColor": "stone"</code>. The <code>base-nova</code> style uses Base UI primitives. This matters when we build the dropdown switcher later, because the Base UI version uses the <code>render</code> prop where many older Radix-based examples use <code>asChild</code>.</p>
<h2 id="heading-1-create-a-shadcn-project">1. Create a shadcn Project</h2>
<p>You don't need to configure Next.js and Tailwind from scratch for this guide. Shadcn's project builder can generate a starter project with the theme setup included.</p>
<p>Open <a href="https://ui.shadcn.com/create">shadcn/create</a> and select <strong>Get Started</strong>.</p>
<img src="https://cdn.hashnode.com/uploads/covers/5e0f2c2e490269cb30227a2b/c16207c2-7609-40bc-8f4d-fbfc8c15685d.png" alt="The shadcn/create page with the Get Started dialog open." style="display: block;" width="600" height="400" loading="lazy">
<p>Choose the style, base color, chart color, fonts, icon library, and radius from the controls. These choices affect the theme values that will be added to your project. We'll look at those values in the next section.</p>
<img src="https://cdn.hashnode.com/uploads/covers/5e0f2c2e490269cb30227a2b/9f78e27d-41a8-4885-9cea-8f272f178229.png" alt="The shadcn theme builder with controls for style, colors, fonts, icons, and radius." style="display: block;" width="600" height="400" loading="lazy">
<p>When you're happy with the setup, select <strong>Get Code</strong>, choose <strong>Next.js</strong>, and copy the command.</p>
<img src="https://cdn.hashnode.com/uploads/covers/5e0f2c2e490269cb30227a2b/46c4b750-21f1-4da9-9416-dc0b3ba7707a.png" alt="The Get Code dialog with Next.js selected and the generated setup command visible." style="display: block;" width="600" height="400" loading="lazy">
<p>Run the command in your terminal. The preset ID in your command may be different from the example below:</p>
<pre><code class="language-bash">pnpm dlx shadcn@latest init --preset b0 --template next
</code></pre>
<p>If you're building a complete product, you may prefer to start with a ready-made <a href="https://shadcnstudio.com/templates">shadcn template</a> and adapt it to your app. The same theme setup covered in this guide can be used in a template-based project.</p>
<p>The generated project has a structure similar to this:</p>
<pre><code class="language-plaintext">shadcn-light-dark-theme/
├── app/
│ ├── globals.css # every theme token, light and dark
│ ├── layout.tsx # fonts and ThemeProvider already wired
│ └── page.tsx
├── components/
│ ├── theme-provider.tsx # next-themes, already configured
│ └── ui/button.tsx
├── lib/utils.ts
└── components.json
</code></pre>
<p>Start the development server:</p>
<pre><code class="language-bash">pnpm dev
</code></pre>
<p>Open <code>http://localhost:3000</code>. The companion project also includes a keyboard shortcut: pressing <code>d</code> switches between light and dark mode.</p>
<p>The important point is that the theme setup is already present. Before adding anything, let's see what the generator has configured.</p>
<h2 id="heading-2-what-shadcn-has-already-configured">2. What shadcn Has Already Configured</h2>
<p>A generated project includes most of the pieces you need for light and dark themes. Here's where to find them:</p>
<table>
<thead>
<tr>
<th>Part</th>
<th>Where to find it</th>
<th>Included</th>
</tr>
</thead>
<tbody><tr>
<td>Light theme values</td>
<td><code>app/globals.css</code> then <code>:root</code></td>
<td>Yes</td>
</tr>
<tr>
<td>Dark theme values</td>
<td><code>app/globals.css</code> then <code>.dark</code></td>
<td>Yes</td>
</tr>
<tr>
<td>Tailwind's <code>dark:</code> variant</td>
<td><code>app/globals.css</code> then <code>@custom-variant</code></td>
<td>Yes</td>
</tr>
<tr>
<td>Tailwind utilities for theme tokens</td>
<td><code>app/globals.css</code> then <code>@theme inline</code></td>
<td>Yes</td>
</tr>
<tr>
<td><code>next-themes</code> package</td>
<td><code>package.json</code></td>
<td>Yes</td>
</tr>
<tr>
<td>Theme provider</td>
<td><code>components/theme-provider.tsx</code></td>
<td>Yes</td>
</tr>
<tr>
<td>Provider added to the app</td>
<td><code>app/layout.tsx</code></td>
<td>Yes</td>
</tr>
<tr>
<td>Hydration warning handling</td>
<td><code>app/layout.tsx</code></td>
<td>Yes</td>
</tr>
<tr>
<td>A visible theme switcher</td>
<td>Your UI</td>
<td><strong>You add this</strong></td>
</tr>
</tbody></table>
<p>So the main piece you'll usually need to add is the control that lets someone choose a theme. The rest is already configured, although it's worth understanding before you start customizing it.</p>
<h2 id="heading-3-where-the-theme-colors-live">3. Where the Theme Colors Live</h2>
<p>Open <code>app/globals.css</code>. You'll find two groups of CSS variables: one inside <code>:root</code> and another inside <code>.dark</code>.</p>
<p>Here's a shortened example based on the generated project:</p>
<pre><code class="language-css">:root {
--background: oklch(1 0 0);
--foreground: oklch(0.147 0.004 49.25);
--card: oklch(1 0 0);
--card-foreground: oklch(0.147 0.004 49.25);
--primary: oklch(0.555 0.163 48.998);
--primary-foreground: oklch(0.987 0.022 95.277);
--muted: oklch(0.97 0.001 106.424);
--muted-foreground: oklch(0.553 0.013 58.071);
--border: oklch(0.923 0.003 48.717);
--radius: 0.625rem;
/* …about thirty in total */
}
.dark {
--background: oklch(0.147 0.004 49.25);
--foreground: oklch(0.985 0.001 106.423);
--card: oklch(0.216 0.006 56.043);
--card-foreground: oklch(0.985 0.001 106.423);
--primary: oklch(0.473 0.137 46.201);
--primary-foreground: oklch(0.987 0.022 95.277);
--muted: oklch(0.268 0.007 34.298);
--muted-foreground: oklch(0.709 0.01 56.259);
--border: oklch(1 0 0 / 10%);
}
</code></pre>
<p>The idea is straightforward:</p>
<pre><code class="language-plaintext">:root → light theme values
.dark → dark theme values
</code></pre>
<p>Both blocks use the same variable names. The values change depending on whether the <code>dark</code> class is present on the page.</p>
<p>For example, <code>--background</code> is white in the light theme and near-black in the dark theme. <code>--foreground</code> changes in the opposite direction so the text remains visible against the background.</p>
<p>You'll also see variables such as <code>--radius</code>. It isn't a color, but it's part of the theme configuration. In this setup, the radius is defined once because the corner shape doesn't need a separate value for dark mode.</p>
<h3 id="heading-why-semantic-tokens-are-useful">Why Semantic Tokens Are Useful</h3>
<p>Theme variables are especially useful when you're building components. Compare these two examples.</p>
<p>Without semantic tokens, you might add separate light and dark colors directly to the component:</p>
<pre><code class="language-tsx"><div className="bg-white text-black dark:bg-black dark:text-white">
Hello
</div>
</code></pre>
<p>That works, but you have to repeat the same decisions throughout your app. If your color palette changes, you'll need to find and update all those classes.</p>
<p>With semantic tokens, the component can use the theme variable instead:</p>
<pre><code class="language-tsx"><div className="bg-background text-foreground">
Hello
</div>
</code></pre>
<p>The component doesn't need to know whether the page is in light or dark mode. It uses <code>background</code> and <code>foreground</code>, and the active theme supplies the right values.</p>
<p>This is also why shadcn components such as <code>Card</code> can adapt without needing dark-mode classes of their own:</p>
<pre><code class="language-tsx"><Card>
<CardHeader>
<CardTitle>Hello</CardTitle>
</CardHeader>
</Card>
</code></pre>
<p>If you open <code>components/ui/card.tsx</code>, you'll see classes such as <code>bg-card</code> and <code>text-card-foreground</code>. The component uses the semantic tokens, while the theme defines what those tokens look like.</p>
<p>The same idea applies when you add more UI to the project. If you need examples of individual elements built for the shadcn ecosystem, you can refer to <a href="https://shadcnstudio.com/components">Shadcn Components</a> and adapt the ones that fit your interface. Keep the semantic tokens in place so those components follow your light and dark themes.</p>
<h3 id="heading-understanding-the-foreground-pairs">Understanding the Foreground Pairs</h3>
<p>Many theme tokens come in pairs. The first token describes a surface, and the <code>-foreground</code> token describes the text or content that appears on top of it.</p>
<table>
<thead>
<tr>
<th>Surface token</th>
<th>Foreground token</th>
</tr>
</thead>
<tbody><tr>
<td><code>--background</code></td>
<td><code>--foreground</code></td>
</tr>
<tr>
<td><code>--card</code></td>
<td><code>--card-foreground</code></td>
</tr>
<tr>
<td><code>--primary</code></td>
<td><code>--primary-foreground</code></td>
</tr>
<tr>
<td><code>--muted</code></td>
<td><code>--muted-foreground</code></td>
</tr>
</tbody></table>
<p>For example, <code>bg-primary text-primary-foreground</code> uses a matching background and text color. The theme defines both values together so the combination works across its light and dark palettes.</p>
<p>Some tokens, such as <code>--border</code>, <code>--input</code>, and <code>--ring</code>, don't need a foreground pair because they're used for borders, input outlines, and focus indicators.</p>
<p>The companion project displays the token pairs as simple tiles. Each tile uses the same <code>bg-<name> text-<name>-foreground</code> pattern.</p>
<img src="https://cdn.hashnode.com/uploads/covers/5e0f2c2e490269cb30227a2b/1797b932-d44d-41b2-832d-7855bdc34295.png" alt="Theme token pairs displayed in light mode." style="display: block;" width="600" height="400" loading="lazy">
<img src="https://cdn.hashnode.com/uploads/covers/5e0f2c2e490269cb30227a2b/53a19e7f-cea3-44e9-ac59-67c801297f63.png" alt="The same theme token pairs displayed in dark mode." style="display: block;" width="600" height="400" loading="lazy">
<p>The markup stays the same in both screenshots. Only the active CSS variable values change. The demo also includes a custom <code>--warning</code> token to show how you can extend the theme.</p>
<h2 id="heading-4-how-tailwind-gets-its-theme-classes">4. How Tailwind Gets its Theme Classes</h2>
<p>There's one more part of <code>globals.css</code> that's easy to overlook: the <code>@theme inline</code> block.</p>
<p>A CSS variable such as <code>--primary</code> is available to CSS, but defining it doesn't automatically create Tailwind classes such as <code>bg-primary</code> or <code>text-primary</code>. Tailwind needs to know which variables should become utility classes.</p>
<p>That's what this block does:</p>
<pre><code class="language-css">@theme inline {
--color-background: var(--background);
--color-foreground: var(--foreground);
--color-primary: var(--primary);
--color-primary-foreground: var(--primary-foreground);
--color-muted: var(--muted);
/* …one line per token */
--radius-sm: calc(var(--radius) * 0.6);
--radius-md: calc(var(--radius) * 0.8);
--radius-lg: var(--radius);
--radius-xl: calc(var(--radius) * 1.4);
}
</code></pre>
<p>For example, this line:</p>
<pre><code class="language-css">--color-primary: var(--primary);
</code></pre>
<p>connects the CSS variable <code>--primary</code> to Tailwind's color utilities. It allows you to use classes such as <code>bg-primary</code>, <code>text-primary</code>, and <code>border-primary</code>.</p>
<p>This is an important step when you add your own tokens. If you define <code>--brand</code> in <code>:root</code> but don't add a matching <code>--color-brand</code> entry in <code>@theme inline</code>, Tailwind won't generate the <code>bg-brand</code> utility for you. The CSS variable can exist and still not be available as a Tailwind utility.</p>
<p>The <code>inline</code> keyword is also important here. It allows the generated utilities to reference the underlying CSS variable, so they use the current value when the theme changes.</p>
<h3 id="heading-what-the-dark-variant-does">What the <code>dark:</code> Variant Does</h3>
<p>You'll also find this line in <code>globals.css</code>:</p>
<pre><code class="language-css">@custom-variant dark (&:is(.dark *));
</code></pre>
<p>This defines how Tailwind's <code>dark:</code> variant works in this setup. A class such as <code>dark:hidden</code> applies when the element is inside an ancestor with the <code>dark</code> class.</p>
<p>There are two details to remember:</p>
<ul>
<li>The theme is represented by a <code>dark</code> class, rather than a <code>data-theme</code> attribute.</li>
<li>The selector targets descendants of the element with the <code>dark</code> class.</li>
</ul>
<p>In this project, <code>next-themes</code> adds the class to the <code><html></code> element. That means the elements inside the page can use <code>dark:</code> utilities as expected.</p>
<p>Here's the relationship between the theme class, CSS variables, and Tailwind utilities:</p>
<pre><code class="language-plaintext">.dark on <html>
↓
--primary changes value (globals.css)
↓
--color-primary reads it (@theme inline)
↓
bg-primary compiles to var(--primary)
↓
every element using bg-primary repaints
</code></pre>
<p>When the theme changes, the browser updates the CSS variables and recalculates the styles that use them. Your components don't need to re-render just because a theme color changed.</p>
<p>So far, we've looked at the CSS side. The next question is what adds or removes the <code>dark</code> class.</p>
<h2 id="heading-5-what-next-themes-does">5. What <code>next-themes</code> Does</h2>
<p>CSS defines what the light and dark themes look like. It doesn't remember a visitor's choice or decide which theme should be active. That's handled by JavaScript.</p>
<p>The generated Next.js project uses <a href="https://github.com/pacocoursey/next-themes"><code>next-themes</code></a> for this.</p>
<p>The library handles a few things for you:</p>
<ul>
<li>Adds or removes the <code>dark</code> class on the <code><html></code> element.</li>
<li>Saves the selected theme in <code>localStorage</code>.</li>
<li>Supports a <code>system</code> option that follows the visitor's operating system preference.</li>
<li>Runs a small script before the page is displayed so the saved theme can be applied early.</li>
</ul>
<p>That last point helps prevent a flash of the wrong theme when the page loads. If you wait until a React <code>useEffect</code> runs to apply the theme, the browser may display the page before the effect runs. A visitor who prefers dark mode could briefly see the light version first.</p>
<p>The generated project already includes a theme provider and mounts it in the app layout. Let's take a look at what it does.</p>
<h2 id="heading-6-the-theme-provider-thats-already-in-your-project">6. The Theme Provider That's Already in Your Project</h2>
<p>The generated <code>components/theme-provider.tsx</code> file wraps <code>next-themes</code> and sets a few defaults. Here's the provider from the companion project:</p>
<pre><code class="language-tsx">"use client"
import * as React from "react"
import { ThemeProvider as NextThemesProvider, useTheme } from "next-themes"
function ThemeProvider({
children,
...props
}: React.ComponentProps<typeof NextThemesProvider>) {
return (
<NextThemesProvider
attribute="class"
defaultTheme="system"
enableSystem
disableTransitionOnChange
{...props}
>
<ThemeHotkey />
{children}
</NextThemesProvider>
)
}
</code></pre>
<p>The wrapper means you don't need to repeat the same configuration every time you use the provider. In <code>app/layout.tsx</code>, it can be mounted like this:</p>
<pre><code class="language-tsx"><ThemeProvider>{children}</ThemeProvider>
</code></pre>
<p>The <code>{...props}</code> spread comes after the default props. That means a value passed to <code>ThemeProvider</code> can override the wrapper's defaults.</p>
<h3 id="heading-theres-a-keyboard-shortcut-too">There's a Keyboard Shortcut Too</h3>
<p>The provider file also includes a <code>ThemeHotkey</code> component:</p>
<pre><code class="language-tsx">function isTypingTarget(target: EventTarget | null) {
if (!(target instanceof HTMLElement)) {
return false
}
return (
target.isContentEditable ||
target.tagName === "INPUT" ||
target.tagName === "TEXTAREA" ||
target.tagName === "SELECT"
)
}
function ThemeHotkey() {
const { resolvedTheme, setTheme } = useTheme()
React.useEffect(() => {
function onKeyDown(event: KeyboardEvent) {
if (event.defaultPrevented || event.repeat) return
if (event.metaKey || event.ctrlKey || event.altKey) return
if (event.key.toLowerCase() !== "d") return
if (isTypingTarget(event.target)) return
setTheme(resolvedTheme === "dark" ? "light" : "dark")
}
window.addEventListener("keydown", onKeyDown)
return () => window.removeEventListener("keydown", onKeyDown)
}, [resolvedTheme, setTheme])
return null
}
</code></pre>
<p>This component doesn't render anything on the page. It listens for the <code>d</code> key and switches between light and dark mode.</p>
<p>Notice the checks inside the keyboard handler. They prevent the shortcut from firing when the user is typing in an input, textarea, select, or editable element. The modifier-key check also prevents the shortcut from interfering with combinations such as <code>Cmd+D</code> or <code>Ctrl+D</code>.</p>
<p>If you don't want this shortcut in your project, remove <code><ThemeHotkey /></code> from the provider. If you keep it, consider mentioning it somewhere in your app so users know it's available.</p>
<h3 id="heading-what-the-provider-options-mean">What the Provider Options Mean</h3>
<table>
<thead>
<tr>
<th>Option</th>
<th>Purpose</th>
</tr>
</thead>
<tbody><tr>
<td><code>attribute="class"</code></td>
<td>Adds a class to <code><html></code> instead of a <code>data-theme</code> attribute.</td>
</tr>
<tr>
<td><code>defaultTheme="system"</code></td>
<td>Uses the operating system preference for a first-time visitor.</td>
</tr>
<tr>
<td><code>enableSystem</code></td>
<td>Enables the <code>system</code> theme option.</td>
</tr>
<tr>
<td><code>disableTransitionOnChange</code></td>
<td>Temporarily disables CSS transitions while the theme changes.</td>
</tr>
</tbody></table>
<p>The <code>attribute="class"</code> setting needs to match the CSS setup from the earlier section. The generated stylesheet looks for <code>.dark</code>, so <code>next-themes</code> needs to add a class. If you change the attribute to <code>data-theme</code>, the <code>.dark</code> selector won't match unless you also update your CSS.</p>
<p>If your switcher uses animated icons, this setting can affect their transitions. The behavior is worth considering when you decide how theme changes should look across the page.</p>
<h3 id="heading-why-layouttsx-uses-suppresshydrationwarning">Why <code>layout.tsx</code> Uses <code>suppressHydrationWarning</code></h3>
<p>You may also notice this on the <code><html></code> element in <code>app/layout.tsx</code>:</p>
<pre><code class="language-tsx"><html lang="en" suppressHydrationWarning className={cn("antialiased", …)}>
</code></pre>
<p>The theme script can update the <code><html></code> class before React hydrates the page. That means the HTML rendered on the server may have a different class from the HTML React sees in the browser.</p>
<p><code>suppressHydrationWarning</code> tells React to suppress that expected warning on this element. It's included in the generated setup for this reason. It applies to that element, rather than disabling hydration warnings throughout the app.</p>
<h2 id="heading-7-build-a-theme-switcher">7. Build a Theme Switcher</h2>
<p>At this point, the theme colors are configured and <code>next-themes</code> can apply the selected theme. What the project still needs is a visible control that lets users change it.</p>
<p>There are two common approaches. You can give users a button that switches between light and dark, or offer a menu with Light, Dark, and System options.</p>
<h3 id="heading-option-1-a-light-and-dark-toggle">Option 1: A light and Dark Toggle</h3>
<p>If your app only needs two modes, a single button is enough:</p>
<pre><code class="language-tsx">"use client"
import { Moon, Sun } from "lucide-react"
import { useTheme } from "next-themes"
import { Button } from "@/components/ui/button"
function ModeSwitch() {
const { resolvedTheme, setTheme } = useTheme()
return (
<Button
variant="ghost"
size="icon"
aria-label="Toggle theme"
onClick={() => setTheme(resolvedTheme === "dark" ? "light" : "dark")}
>
<Sun className="dark:hidden" />
<Moon className="hidden dark:block" />
</Button>
)
}
export { ModeSwitch }
</code></pre>
<p>A few details in this component are worth understanding.</p>
<p>First, use <code>resolvedTheme</code> for a two-state toggle. The <code>theme</code> value can be <code>"system"</code>, while <code>resolvedTheme</code> tells you which theme is currently active: <code>"light"</code> or <code>"dark"</code>. Since this button switches between two visible states, <code>resolvedTheme</code> is the value you want to check.</p>
<p>Second, keep both icons in the DOM. The <code>Sun</code> and <code>Moon</code> icons are shown or hidden with CSS classes. This avoids choosing an icon based on a theme value that may not be available during server rendering.</p>
<p>For comparison, this conditional approach can cause a mismatch between the server-rendered markup and the browser's first render:</p>
<pre><code class="language-tsx">{resolvedTheme === "dark" ? <Moon /> : <Sun />}
</code></pre>
<p>Finally, add an accessible label. Since the button only contains an icon, <code>aria-label</code> gives screen reader users a useful description of what the button does.</p>
<h3 id="heading-option-2-a-light-dark-and-system-menu">Option 2: A Light, Dark, and System Menu</h3>
<p>If you want users to choose their preferred mode, including the operating system setting, use a dropdown menu.</p>
<p>First, add the dropdown menu component if it's not already in your project:</p>
<pre><code class="language-bash">pnpm dlx shadcn@latest add dropdown-menu
</code></pre>
<p>Then create the switcher:</p>
<pre><code class="language-tsx">"use client"
import { ChevronDown, Monitor, Moon, Sun } from "lucide-react"
import { useTheme } from "next-themes"
import { Button } from "@/components/ui/button"
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuRadioGroup,
DropdownMenuRadioItem,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu"
const MODES = [
{ value: "light", label: "Light", icon: Sun },
{ value: "dark", label: "Dark", icon: Moon },
{ value: "system", label: "System", icon: Monitor },
]
function ModeToggle() {
const { theme, setTheme } = useTheme()
return (
<DropdownMenu>
<DropdownMenuTrigger
render={<Button variant="ghost" aria-label="Change theme" />}
>
<Sun className="dark:hidden" />
<Moon className="hidden dark:block" />
<ChevronDown className="size-3 text-muted-foreground" />
</DropdownMenuTrigger>
<DropdownMenuContent align="end" className="min-w-36">
<DropdownMenuRadioGroup
value={theme ?? "system"}
onValueChange={(value) => setTheme(value as string)}
>
{MODES.map(({ value, label, icon: Icon }) => (
<DropdownMenuRadioItem key={value} value={value}>
<Icon className="text-muted-foreground" />
{label}
</DropdownMenuRadioItem>
))}
</DropdownMenuRadioGroup>
</DropdownMenuContent>
</DropdownMenu>
)
}
export { ModeToggle }
</code></pre>
<p>The menu uses a radio group so the current option is shown as selected. This makes it easier for users to see whether Light, Dark, or System is active.</p>
<img src="https://cdn.hashnode.com/uploads/covers/5e0f2c2e490269cb30227a2b/537f5bff-efb1-4a39-8eb2-5c8cd2b21cc4.png" alt="The demo header showing the theme switchers, with the custom warning-token example below." style="display: block;" width="600" height="400" loading="lazy">
<img src="https://cdn.hashnode.com/uploads/covers/5e0f2c2e490269cb30227a2b/5e0e37c4-76ab-4138-8574-c5e632d44f5f.png" alt="5e0e37c4-76ab-4138-8574-c5e632d44f5f" style="display: block;" width="600" height="400" loading="lazy">
<p>There are two implementation details to pay attention to here.</p>
<p>First, base UI uses <code>render</code> instead of <code>asChild</code>. The companion project uses the <code>base-nova</code> style, which is built on Base UI. In this setup, the dropdown trigger uses the <code>render</code> prop to render the shadcn button. Many older examples use <code>asChild</code>, which is common in Radix-based projects. Check your own <code>components.json</code> and component imports before copying an example.</p>
<p>Use a radio group for mutually exclusive options. Light, Dark, and System are three choices where only one can be active at a time. <code>DropdownMenuRadioGroup</code> communicates that relationship and displays the selected option.</p>
<p>The expression <code>theme ?? "system"</code> uses <code>"system"</code> as a fallback if the theme value is still undefined. This keeps the radio group controlled while <code>next-themes</code> initializes.</p>
<p>You may have seen the <code>next-themes</code> advice to wait until a component has mounted before rendering anything based on <code>theme</code>. That advice is for UI the server renders. Here, the radio items only appear when someone opens the menu, which always happens after the page has hydrated, so the server never renders a theme value and there's nothing to mismatch.</p>
<p>You can now place the switcher in your header or another part of your layout. For example:</p>
<pre><code class="language-tsx">import { ModeToggle } from "@/components/mode-toggle"
<header className="flex h-14 items-center justify-between border-b px-6">
<h1 className="text-sm font-medium">My app</h1>
<ModeToggle />
</header>
</code></pre>
<p>Choose one switcher for your app, based on how much control you want to give users. You don't need to display both versions at once.</p>
<p>The switcher will usually sit inside a larger page, such as an account settings screen or dashboard. If you're building that kind of page, <a href="https://shadcnstudio.com/blocks">shadcn blocks</a> can give you useful layout examples. You can adapt a block to your needs and keep using the same theme tokens for its backgrounds, borders, and text.</p>
<h2 id="heading-8-why-the-icon-animation-may-not-work">8. Why the Icon Animation May Not Work</h2>
<p>You might want the sun and moon icons to rotate or scale when someone changes the theme. The shadcn examples include transition classes like these:</p>
<pre><code class="language-tsx"><Sun className="scale-100 rotate-0 transition-transform dark:scale-0 dark:-rotate-90" />
<Moon className="absolute scale-0 rotate-90 transition-transform dark:scale-100 dark:rotate-0" />
</code></pre>
<p>But the generated provider also uses <code>disableTransitionOnChange</code>. This option temporarily injects a stylesheet that disables transitions while the theme is changing. The rule used by the installed <code>next-themes</code> version is:</p>
<pre><code class="language-css">*,*::before,*::after{transition:none!important}
</code></pre>
<p>Because the rule uses <code>!important</code>, it can override the <code>transition-transform</code> classes on the icons during the theme change. The icons may switch immediately instead of animating.</p>
<p>There's a reason this option exists. Without it, every element with a CSS transition may animate its colors when the theme changes. Depending on your design, that can make the entire page feel slow or distracting.</p>
<p>You have a few options:</p>
<ul>
<li>Keep <code>disableTransitionOnChange</code> and use icons without transition effects.</li>
<li>Remove the option if you want the page and icons to transition together.</li>
<li>Keep the option and use a CSS animation or a motion library for the icon effect. Test this with your actual setup to make sure the animation behaves as expected.</li>
</ul>
<p>The important thing is to understand that the provider setting affects more than the theme colors. It can also affect transitions elsewhere in your interface.</p>
<h2 id="heading-9-customize-your-theme">9. Customize Your Theme</h2>
<p>Once you understand where the theme values live, changing your app's colors becomes much easier. You can update a token once and have every component that uses it pick up the new value.</p>
<h3 id="heading-change-an-existing-token">Change an Existing Token</h3>
<p>Imagine your brand uses blue as its primary color. You could set a blue background directly on one button:</p>
<pre><code class="language-tsx"><Button className="bg-blue-500 dark:bg-blue-700">Save</Button>
</code></pre>
<p>But this only changes that button. If you want the primary color to be consistent across your app, update the <code>--primary</code> token in <code>app/globals.css</code> instead:</p>
<pre><code class="language-css">:root {
--primary: oklch(0.55 0.18 265);
--primary-foreground: oklch(0.99 0 0);
}
.dark {
--primary: oklch(0.62 0.16 265);
--primary-foreground: oklch(0.17 0.02 265);
}
</code></pre>
<p>The same token is used by components and utilities throughout the project. Update both the <code>:root</code> and <code>.dark</code> values so the color works in both themes.</p>
<p>The two values don't have to be identical. A color that looks good on a light background may need a different lightness or saturation on a dark background. The generated theme uses different values for some tokens for exactly this reason.</p>
<p>The project also uses <code>oklch()</code> color values. In this color format, the first value represents lightness, which can make it easier to adjust how bright or dark a color appears while keeping its hue consistent.</p>
<p>You can customize the border radius in the same way. The generated <code>@theme inline</code> block derives several radius utilities from the <code>--radius</code> value, so changing it updates the overall corner scale:</p>
<pre><code class="language-css">--radius: 0.625rem;
</code></pre>
<h3 id="heading-add-a-custom-token">Add a Custom Token</h3>
<p>You can also add tokens that aren't included in the default theme. For example, let's add a <code>warning</code> color that can be used for a notice or alert.</p>
<p>First, define the token and its foreground color in both theme blocks. Then connect those variables to Tailwind in <code>@theme inline</code>:</p>
<pre><code class="language-css">:root {
--warning: oklch(0.84 0.16 84);
--warning-foreground: oklch(0.28 0.07 46);
}
.dark {
--warning: oklch(0.41 0.11 46);
--warning-foreground: oklch(0.99 0.02 95);
}
@theme inline {
--color-warning: var(--warning);
--color-warning-foreground: var(--warning-foreground);
}
</code></pre>
<p>You can now use the new classes in a component:</p>
<pre><code class="language-tsx"><div className="bg-warning text-warning-foreground rounded-lg p-3">
Your trial ends in 3 days.
</div>
</code></pre>
<p>The component uses the same classes in both screenshots. The theme supplies the appropriate background and text colors.</p>
<p>Remember to add the <code>--color-warning</code> and <code>--color-warning-foreground</code> entries to <code>@theme inline</code>. Without those entries, the CSS variables will exist, but Tailwind won't generate the corresponding utility classes.</p>
<p>It's also a good idea to define a foreground token whenever you add a new surface color. That way, the background and text colors are designed to work together.</p>
<h3 id="heading-when-should-you-use-dark">When Should You Use <code>dark:</code></h3>
<p>Once your theme uses semantic tokens, you won't need <code>dark:</code> classes for most background and text colors.</p>
<p>For example, instead of maintaining separate light and dark background classes:</p>
<pre><code class="language-tsx">// You're now maintaining this color in two places
<div className="bg-slate-100 dark:bg-slate-800">
// The token already knows
<div className="bg-muted">
</code></pre>
<p>Use a semantic token such as <code>bg-muted</code> when it represents the color you want. The token already has values for both themes.</p>
<p>There are still cases where <code>dark:</code> is useful. You might need to swap an image, adjust a shadow, or change a decorative element that doesn't fit into your semantic color tokens. Use it when the appearance genuinely needs a theme-specific rule.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>A generated shadcn project already includes most of the setup needed for light and dark mode. The part you usually need to build is the switcher that gives users control over the theme.</p>
<p>Here's a quick recap:</p>
<ol>
<li><code>:root</code> and <code>.dark</code> define the theme values. They use the same variable names with different values.</li>
<li><code>@theme inline</code> connects CSS variables to Tailwind utilities**.** Add a matching entry when you create a custom token.</li>
<li><code>next-themes</code> applies the active theme. In this setup, it adds or removes the <code>dark</code> class on <code><html></code>.</li>
<li>The generated project already includes the provider. Check <code>components/theme-provider.tsx</code> and <code>app/layout.tsx</code> before creating another one.</li>
<li>The visible switcher is the part you add. Use a simple toggle or a menu with Light, Dark, and System options.</li>
<li>Customize tokens instead of styling every component separately. This keeps your colors consistent across the app.</li>
</ol>
<p>Once you know how these pieces fit together, dark mode becomes easier to maintain. You can update the theme in one place, add new tokens when you need them, and let your components use those values without having to check which theme is active.</p>
<h2 id="heading-references">References</h2>
<ul>
<li><a href="https://ui.shadcn.com/create">shadcn/create</a>: Generate a shadcn project.</li>
<li><a href="https://ui.shadcn.com/docs/dark-mode/next">shadcn dark mode for Next.js</a>: Official Next.js dark-mode setup.</li>
<li><a href="https://ui.shadcn.com/docs/theming">shadcn theming</a>: Theme tokens and CSS variables.</li>
<li><a href="https://github.com/pacocoursey/next-themes">next-themes</a>: Theme provider documentation and source.</li>
<li><a href="https://tailwindcss.com/docs/theme">Tailwind CSS theme variables</a>: How theme variables create utility classes.</li>
<li><a href="https://base-ui.com/">Base UI</a>: Documentation for the primitives used by the <code>base-nova</code> style.</li>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/oklch">MDN: <code>oklch()</code></a>: Reference for the OKLCH color format.</li>
</ul>
<h4 id="heading-thanks-for-reading">Thanks for Reading</h4>
<p>The companion project includes both switcher examples, the custom <code>warning</code> token, and the page used for the screenshots. To try the examples locally, clone the repository, then run <code>pnpm install</code> followed by <code>pnpm dev</code> from the project directory.</p>
<p>Companion GitHub repository:- <a href="https://github.com/shadcnstudio/shadcn-dark-mode-guide">https://github.com/shadcnstudio/shadcn-dark-mode-guide</a></p>
<p>If you try the examples and run into something that behaves differently in your setup, leave a comment with your shadcn style and package versions. That context makes it much easier to work out what's happening.</p>