Companion project for Implementing Dark Mode and Light Mode with Shadcn (blog.md in this repo).
Generated with pnpm dlx shadcn@latest init --preset … --template next, then finished off with the one piece the generator doesn't write: a theme switcher. Every claim in the post is running here.
pnpm install
pnpm devThen open the app and flip the theme three ways: the two-state button, the Light / Dark / System menu, or the d key.
The whole implementation is four files.
| File | What's in it |
|---|---|
app/globals.css |
:root and .dark (the theme), @theme inline (the Tailwind bridge), @custom-variant dark (the dark: prefix) |
components/theme-provider.tsx |
next-themes with attribute="class", plus the d hotkey. Written by the generator |
components/mode-switch.tsx |
Two-state toggle. ~20 lines. Reads resolvedTheme, not theme |
components/mode-toggle.tsx |
Light / Dark / System menu. Base UI, so render rather than asChild |
app/page.tsx is the walkthrough itself. The token grid, the live variable readout and the --warning demo are all built out of the tokens they describe.
- Add a token. Put
--brandin:rootand.dark, writebg-brand, and watch nothing happen. Then add--color-brand: var(--brand)to@theme inlineand watch it work. That silent failure is the single most useful thing in the post. - Change
--primary. Two lines inglobals.cssrecolors every button, badge and focus ring in both themes. - Change
--radius. One value in:root;@theme inlinederives the whole corner scale from it withcalc(). - Break it on purpose. Switch
attribute="class"toattribute="data-theme"intheme-provider.tsx. The switcher keeps reporting the right theme and every color stays in light mode.
pnpm lint
pnpm typecheck
pnpm buildpnpm dlx shadcn@latest add <component>Components land in components/ui/.