Skip to content

About

Learn how shadcn/ui dark mode works by exploring theme tokens, CSS variables, next themes integration, theme toggles, and custom color systems with Tailwind CSS v4.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Dark mode with shadcn/ui

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.

Run it

pnpm install
pnpm dev

Then open the app and flip the theme three ways: the two-state button, the Light / Dark / System menu, or the d key.

What to read

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.

Things worth poking at

  • Add a token. Put --brand in :root and .dark, write bg-brand, and watch nothing happen. Then add --color-brand: var(--brand) to @theme inline and watch it work. That silent failure is the single most useful thing in the post.
  • Change --primary. Two lines in globals.css recolors every button, badge and focus ring in both themes.
  • Change --radius. One value in :root; @theme inline derives the whole corner scale from it with calc().
  • Break it on purpose. Switch attribute="class" to attribute="data-theme" in theme-provider.tsx. The switcher keeps reporting the right theme and every color stays in light mode.

Checks

pnpm lint
pnpm typecheck
pnpm build

Adding components

pnpm dlx shadcn@latest add <component>

Components land in components/ui/.

About

Learn how shadcn/ui dark mode works by exploring theme tokens, CSS variables, next themes integration, theme toggles, and custom color systems with Tailwind CSS v4.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages