SHADCN/UITAILWIND V4NEXT.JS 16

shadcn/ui with Tailwind v4: themes, dark mode and variants

Build a pricing page with shadcn/ui on a brand-new Next.js 16 project, then make it yours: your brand colour and corner radius, a light, dark and system theme menu, a colour of your own and a component variant to go with it. Every file is shown in full, and every step was run on a fresh project before this was published.

Ali Murtaza · 18 min read ·

Tested step by step on with Next.js 16.4.6, the shadcn CLI 4.23.0 and Tailwind CSS 4.3.5

By the end of this guide you’ll have a pricing page with three plans, a monthly/yearly switch and a theme menu, built from shadcn/ui components. Then you’ll change how the whole page looks without touching a single component: a new brand colour, rounder corners, a dark mode that follows the visitor’s system, and a green “Save 17%” badge that uses a colour you added yourself.

The idea that makes all of it work is small. Every shadcn/ui component paints itself with a handful of named colours, like primary, muted and border, and each of those names is a CSS variable in one file, app/globals.css. Change a variable and every component that uses it changes with it. Once you see that, theming stops being a chore.

Every file is shown whole, so you can copy it as it is. The guide was run from start to finish on a fresh create-next-app project and on a fresh shadcn init project, with Next.js 16.4, the shadcn CLI 4.23, Tailwind CSS 4.3 and next-themes 0.4.

Before you start

  • Node.js 20.9 or newer. Check with node -v.
  • A new Next.js 16 app. Either npx create-next-app@latest my-app --yes or npx shadcn@latest init works. Accept the defaults.
  • A code editor. VS Code with the Tailwind CSS IntelliSense extension shows each colour beside its class, which helps a lot here.

Run every command from your project’s folder, the one with package.json in it.

1. Add shadcn/ui to your project

If you created your project with npx shadcn@latest init, this is already done. Skip to step 2.

On a create-next-app project, run the setup command. --defaults accepts the standard choices: the Nova style, neutral greys, and Base UI underneath the components.

Terminalbash
npx shadcn@latest init --defaults

It finishes with “Project initialization completed.” Here’s what it did:

  • Wrote components.json, the settings the shadcn command line reads every time you add a component: where components go, which style, which icons.
  • Rewrote app/globals.css with the theme: about thirty colour variables for light mode, the same again for dark mode, and the lines that turn them into Tailwind classes. You’ll read it in step 5.
  • Added components/ui/button.tsx and lib/utils.ts, and installed the packages they need.
  • There is no tailwind.config.js. With Tailwind v4 the whole theme lives in CSS, which is why components.json lists the config as an empty string.
The components are copied into your project as source code, not installed as a package. components/ui/button.tsx is now your file. That’s what makes the customising in steps 6 to 9 possible, and it’s also why you should change them with some care.

2. Fix the layout and add dark mode support

Two things in the root layout need to change before you build anything.

  • The font. The theme reads the font from a variable called --font-sans, but the create-next-app layout names it --font-geist-sans. Until the names match, the page falls back to the browser’s default font, which is usually Times.
  • Dark mode. shadcn’s dark colours apply when the <html> element has the class dark. Something has to add and remove that class, remember the visitor’s choice, and follow their system setting. The next-themes package does exactly that.

Install next-themes (the shadcn init starter already has it, and running this again changes nothing):

Terminalbash
npm install next-themes

next-themes has to run in the browser, but your layout is a server component. A small client wrapper solves that. Create components/theme-provider.tsx (the shadcn init starter has one already; replace it with this):

components/theme-provider.tsxtsx
"use client"import { ThemeProvider as NextThemesProvider } from "next-themes"export function ThemeProvider({ children, ...props}: React.ComponentProps<typeof NextThemesProvider>) { return <NextThemesProvider {...props}>{children}</NextThemesProvider>}

Now replace app/layout.tsx with this:

app/layout.tsxtsx
import type { Metadata } from "next"import { Geist } from "next/font/google"import { ThemeProvider } from "@/components/theme-provider"import "./globals.css"// shadcn's theme reads the font from --font-sans, so name it that.const geist = Geist({ variable: "--font-sans", subsets: ["latin"] })export const metadata: Metadata = { title: "Pricing", description: "Plans for every team.",}export default function RootLayout({ children,}: Readonly<{ children: React.ReactNode }>) { return ( <html lang="en" className={geist.variable} suppressHydrationWarning> <body className="antialiased"> <ThemeProvider attribute="class" defaultTheme="system" enableSystem disableTransitionOnChange > {children} </ThemeProvider> </body> </html> )}
  • attribute="class" makes next-themes set class="dark" on <html>. Leave it out and it sets data-theme="dark" instead, which shadcn’s styles don’t look at, so nothing changes.
  • defaultTheme="system" and enableSystem start every new visitor on their operating system’s setting.
  • disableTransitionOnChange switches off CSS transitions for the moment the theme changes, so buttons don’t fade between colours one by one.
  • suppressHydrationWarning is needed because next-themes adds the class to <html> before React loads, so the browser’s <html> differs from the server’s on purpose. It applies to that one element only.

3. Add the components

The pricing page uses five more components. Add them all in one command:

Terminalbash
npx shadcn@latest add card badge switch label dropdown-menu

It lists the five files it created, all in components/ui/. Open components/ui/badge.tsx and have a look: it’s an ordinary React component with a list of variants (default, secondary, destructive and so on). You’ll add one of your own in step 8.

4. Build the pricing page

Start with one plan card. Rather than repeat the same Card markup three times, put it in a component of your own. Create components/plan-card.tsx:

components/plan-card.tsxtsx
import { CheckIcon } from "lucide-react"import { Button } from "@/components/ui/button"import { Card, CardAction, CardContent, CardDescription, CardFooter, CardHeader, CardTitle,} from "@/components/ui/card"import { cn } from "@/lib/utils"type PlanCardProps = { name: string description: string price: string period: string features: string[] cta: string featured?: boolean badge?: React.ReactNode}export function PlanCard({ name, description, price, period, features, cta, featured = false, badge,}: PlanCardProps) { return ( <Card className={cn("w-full", featured && "ring-2 ring-primary")}> <CardHeader> <CardTitle>{name}</CardTitle> <CardDescription>{description}</CardDescription> {badge ? <CardAction>{badge}</CardAction> : null} </CardHeader> <CardContent className="flex flex-1 flex-col gap-4"> <p className="flex items-baseline gap-1"> <span className="text-3xl font-semibold tracking-tight">{price}</span> <span className="text-muted-foreground">{period}</span> </p> <ul className="flex flex-col gap-2"> {features.map((feature) => ( <li key={feature} className="flex items-center gap-2"> <CheckIcon className="size-4 text-primary" aria-hidden /> {feature} </li> ))} </ul> </CardContent> <CardFooter> <Button size="lg" variant={featured ? "default" : "outline"} className="w-full" > {cta} </Button> </CardFooter> </Card> )}
  • Card comes in parts (CardHeader, CardTitle, CardDescription, CardAction, CardContent, CardFooter). Use the parts rather than putting everything in CardContent: each one sets its own spacing, and CardAction sits in the header’s top-right corner.
  • cn() joins class names and settles conflicts. The Card component already has a thin ring-1; because cn puts your classes last and merges them, ring-2 ring-primary replaces it on the featured plan instead of fighting it.
  • Notice what isn’t here: no colour codes. text-primary, ring-primary and text-muted-foreground are theme names, so this card will pick up your brand colour in step 6 without being edited.

Next, the three plans and the monthly/yearly switch. The switch needs state, so this one is a client component. Create components/pricing.tsx:

components/pricing.tsxtsx
"use client"import { useState } from "react"import { PlanCard } from "@/components/plan-card"import { Badge } from "@/components/ui/badge"import { Label } from "@/components/ui/label"import { Switch } from "@/components/ui/switch"export function Pricing() { const [yearly, setYearly] = useState(false) const period = yearly ? "/year" : "/month" return ( <section className="flex flex-col items-center gap-8"> <div className="flex items-center gap-3"> <Switch id="yearly" checked={yearly} onCheckedChange={setYearly} /> <Label htmlFor="yearly">Bill yearly</Label> <Badge variant="secondary">Save 17%</Badge> </div> <div className="grid w-full gap-6 md:grid-cols-3"> <PlanCard name="Starter" description="For trying it out." price="$0" period={period} features={["1 project", "Community support"]} cta="Start free" /> <PlanCard name="Pro" description="For a working product." price={yearly ? "$150" : "$15"} period={period} features={["Unlimited projects", "Email support", "Custom domain"]} cta="Upgrade to Pro" featured badge={<Badge>Most popular</Badge>} /> <PlanCard name="Team" description="For a team of five or more." price={yearly ? "$400" : "$40"} period={period} features={["Everything in Pro", "Shared billing", "Priority support"]} cta="Contact sales" /> </div> </section> )}

The id on the Switch and the htmlFor on the Label connect the two, so clicking the words “Bill yearly” flips the switch, and a screen reader announces the switch by that name.

Then the theme menu: a button with a sun or a moon that opens Light, Dark and System. Create components/mode-toggle.tsx:

components/mode-toggle.tsxtsx
"use client"import { MoonIcon, SunIcon } from "lucide-react"import { useTheme } from "next-themes"import { Button } from "@/components/ui/button"import { DropdownMenu, DropdownMenuContent, DropdownMenuGroup, DropdownMenuLabel, DropdownMenuRadioGroup, DropdownMenuRadioItem, DropdownMenuTrigger,} from "@/components/ui/dropdown-menu"export function ModeToggle() { const { theme, setTheme } = useTheme() return ( <DropdownMenu> <DropdownMenuTrigger render={<Button variant="outline" size="icon" aria-label="Theme" />} > {/* CSS picks the icon, so the server and the browser agree. */} <SunIcon className="dark:hidden" /> <MoonIcon className="hidden dark:block" /> </DropdownMenuTrigger> <DropdownMenuContent align="end"> <DropdownMenuGroup> <DropdownMenuLabel>Theme</DropdownMenuLabel> <DropdownMenuRadioGroup value={theme} onValueChange={setTheme}> <DropdownMenuRadioItem value="light">Light</DropdownMenuRadioItem> <DropdownMenuRadioItem value="dark">Dark</DropdownMenuRadioItem> <DropdownMenuRadioItem value="system">System</DropdownMenuRadioItem> </DropdownMenuRadioGroup> </DropdownMenuGroup> </DropdownMenuContent> </DropdownMenu> )}
  • render={<Button … />} tells the menu’s trigger to draw itself as your Button. The components from init --defaults are built on Base UI, which uses render for this. Older guides, built on Radix, write asChild instead, which these components don’t have.
  • The sun and moon are both rendered, and CSS hides one: dark:hidden hides the sun in dark mode, hidden dark:block shows the moon only then. Choosing the icon with theme in JavaScript would draw the wrong one on the server, which doesn’t know the visitor’s theme, and React would warn about the mismatch.
  • DropdownMenuLabel must sit inside a DropdownMenuGroup, as it does here. Outside one, opening the menu crashes the page (see “If something goes wrong” below).

Last, the page itself. Replace app/page.tsx with this:

app/page.tsxtsx
import { ModeToggle } from "@/components/mode-toggle"import { Pricing } from "@/components/pricing"export default function Home() { return ( <main className="mx-auto flex min-h-svh max-w-5xl flex-col gap-10 px-6 py-10"> <div className="flex justify-end"> <ModeToggle /> </div> <header className="flex flex-col items-center gap-3 text-center"> <h1 className="text-4xl font-semibold tracking-tight"> Simple pricing </h1> <p className="max-w-md text-muted-foreground"> Start free. Upgrade when your team needs more. </p> </header> <Pricing /> </main> )}

Start the development server:

Terminalbash
npm run dev

Open http://localhost:3000. You should see “Simple pricing”, three cards in a row (stacked on a phone) with the Pro card outlined, and the switch above them. Flip the switch: Pro goes from $15/month to $150/year. Open the sun menu and pick Dark: the whole page turns dark, and the sun becomes a moon. Pick System and it follows your computer’s setting.

Everything is black, white and grey for now. That’s shadcn’s neutral default, and it’s what you’ll change next.

5. How the theme works

Open app/globals.css. It’s long, but it has only three kinds of part. Here they are, cut down to the lines for one colour:

app/globals.css (excerpt)css
@import "tailwindcss";@import "tw-animate-css";@import "shadcn/tailwind.css";@custom-variant dark (&:is(.dark *));@theme inline { --color-primary: var(--primary); --color-primary-foreground: var(--primary-foreground); --radius-lg: var(--radius); …}:root { --primary: oklch(0.205 0 0); --primary-foreground: oklch(0.985 0 0); --radius: 0.625rem; …}.dark { --primary: oklch(0.922 0 0); --primary-foreground: oklch(0.205 0 0); …}
  • :root holds the light theme: a value for each name. --primary is the colour of main buttons and highlights; --primary-foreground is the colour of text on top of it. Every colour comes in such a pair, so text on a coloured surface always has a colour chosen to be readable on it.
  • .dark holds the dark theme: the same names, different values. When <html> has the class dark, these win.
  • @theme inline turns the names into Tailwind classes. The line --color-primary: var(--primary) is what makes bg-primary, text-primary and ring-primary exist. inline means the class uses the variable itself, var(--primary), rather than a copy of its value, so the class changes the moment the variable does. That’s why a dark theme needs no dark: classes in your components.
  • @custom-variant dark is what makes dark: classes (like the sun icon’s dark:hidden) mean “inside an element with the class dark”, rather than Tailwind’s default of following the system setting.
  • --radius is one length, and @theme inline derives rounded-sm through rounded-4xl from it. Change it once and every corner changes in proportion.

The values are written in OKLCH: oklch(lightness chroma hue). Lightness runs from 0 (black) to 1 (white), chroma is how colourful it is (0 is grey), and hue is an angle on the colour wheel: about 25 is red, 155 green, 265 blue, 300 purple. The reason shadcn uses it is that the lightness number matches what your eye sees, so two colours with the same lightness look equally light, whatever their hue. That makes it much easier to pick a dark-mode version or swap a brand colour without wrecking the contrast.

6. Give it your brand colour and radius

You could edit the values in the :root and .dark blocks directly. A tidier way is to leave shadcn’s defaults alone and add your changes in one block at the end of the file. A later rule with the same selector wins, so these values replace the defaults, and everything that’s yours is in one place.

Add this to the end of app/globals.css:

app/globals.css (add at the end)css
/* Our theme. Everything above is shadcn's default; these rules come later, so they win. Change the brand here and every component follows. */:root { --primary: oklch(0.51 0.23 277); --primary-foreground: oklch(0.985 0 0); --ring: oklch(0.51 0.23 277); --radius: 0.75rem;}.dark { --primary: oklch(0.72 0.15 277); --primary-foreground: oklch(0.21 0.06 277); --ring: oklch(0.72 0.15 277);}
  • --primary is an indigo (hue 277). In light mode it’s fairly dark (0.51), so white text on it is easy to read.
  • In dark mode the same hue is lighter (0.72) and a little less saturated, so it doesn’t glare on a dark page, and the text on it turns dark indigo instead of white.
  • --ring is the focus outline you see when you tab to a button or the switch. Matching it to the brand keeps keyboard focus visible and on-brand.
  • --radius goes from 0.625rem to 0.75rem: slightly rounder cards and buttons everywhere.

Save, and look at the page without reloading. The Pro card’s outline, the “Upgrade to Pro” button, the “Most popular” badge, every tick in the lists and the switch (when it’s on) are now indigo, in both themes. You didn’t edit a component.

Picking your own colour: keep the lightness near 0.5 for a light-mode --primary with white text, and near 0.7 for dark mode with dark text. Change the hue to taste (25 red, 155 green, 230 teal, 265 blue, 300 purple) and the contrast stays about the same. oklch.com shows any value as a colour.

7. Add a colour of your own

The theme has no colour for good news, and the “Save 17%” badge should be green. Writing text-green-600 on it would work, but it would be the one thing on the page that ignores your theme and dark mode. Add a success colour instead, the same way shadcn adds its own: a value for each theme, and a line in @theme inline to turn it into classes.

Add this to the end of app/globals.css:

app/globals.css (add at the end)css
:root { --success: oklch(0.48 0.12 155); --success-foreground: oklch(0.985 0 0);}.dark { --success: oklch(0.78 0.15 155); --success-foreground: oklch(0.22 0.05 155);}@theme inline { --color-success: var(--success); --color-success-foreground: var(--success-foreground);}

That gives you bg-success, text-success, border-success, bg-success/10 (10% opacity) and the rest, all switching with the theme. A second @theme inline block is fine: Tailwind reads them all.

8. Add a variant of your own

You could now write className="bg-success/10 text-success" on the badge. That works once. If “success” badges will show up all over your app, make it a variant, so every one of them looks the same and you can change them all in one place.

Open components/ui/badge.tsx. Inside variants, under variant, find the line that starts with link: and add this line after it:

components/ui/badge.tsx (add after link:)tsx
success: "bg-success/10 text-success dark:bg-success/20",
  • bg-success/10 is a pale green wash and text-success is the full green, the same soft style as the built-in destructive badge.
  • dark:bg-success/20 makes the wash a little stronger on a dark page, where 10% would almost vanish.
  • TypeScript picks the new name up by itself: variant now accepts "success", and your editor offers it.

Now use it. Replace components/pricing.tsx with this; the only change is the badge’s variant:

components/pricing.tsxtsx
"use client"import { useState } from "react"import { PlanCard } from "@/components/plan-card"import { Badge } from "@/components/ui/badge"import { Label } from "@/components/ui/label"import { Switch } from "@/components/ui/switch"export function Pricing() { const [yearly, setYearly] = useState(false) const period = yearly ? "/year" : "/month" return ( <section className="flex flex-col items-center gap-8"> <div className="flex items-center gap-3"> <Switch id="yearly" checked={yearly} onCheckedChange={setYearly} /> <Label htmlFor="yearly">Bill yearly</Label> <Badge variant="success">Save 17%</Badge> </div> <div className="grid w-full gap-6 md:grid-cols-3"> <PlanCard name="Starter" description="For trying it out." price="$0" period={period} features={["1 project", "Community support"]} cta="Start free" /> <PlanCard name="Pro" description="For a working product." price={yearly ? "$150" : "$15"} period={period} features={["Unlimited projects", "Email support", "Custom domain"]} cta="Upgrade to Pro" featured badge={<Badge>Most popular</Badge>} /> <PlanCard name="Team" description="For a team of five or more." price={yearly ? "$400" : "$40"} period={period} features={["Everything in Pro", "Shared billing", "Priority support"]} cta="Contact sales" /> </div> </section> )}

“Save 17%” is now green on a pale green, and a lighter green on a darker wash in dark mode.

9. Change components without losing your changes

You’ve now changed how components look in four different ways. Each has its place, and choosing the right one is what keeps a theme easy to maintain as the app grows:

  • The theme (steps 6 and 7) for anything that should change everywhere: colours, radius, fonts. Nothing in components/ui changes.
  • className on one use for layout and one-offs: w-full on a button, the featured plan’s ring-2. cn makes your class win over the component’s own.
  • A wrapper component (PlanCard) for a combination you repeat. It builds on Card and Button without changing them.
  • A new variant inside components/ui (step 8) for a style the component itself should offer. This is the only one of the four that edits shadcn’s code.

That last one matters when shadcn releases a newer version of a component. Running add again with --overwrite replaces the file, and your variant with it. Before you ever do that, ask the command line what would change:

Terminalbash
npx shadcn@latest add badge --diff

It changes nothing and prints a diff. Lines starting with - are yours that an update would remove. Here you’ll see your success: line marked -. If you do update, copy those lines back in afterwards. Keeping edits inside components/ui few and small is what keeps this easy.

10. Check it and build

  • Press Tab from the top of the page. The theme button, the switch and each plan button should get a visible indigo outline in turn. That’s --ring.
  • Check the contrast of anything you recoloured. In Chrome, right-click “Save 17%” → Inspect, then click the colour square next to color in the Styles panel: it shows the contrast ratio. 4.5 or more is the bar for normal text (WCAG AA). With the values above, “Save 17%” is about 5.2 in light mode and 7.3 in dark, and the indigo buttons about 6.0 and 7.0.
  • Narrow the window to phone width: the cards stack in one column.

Then stop the dev server with Ctrl+C and make sure everything compiles the way Vercel will build it:

Terminalbash
npm run lintnpx tsc --noEmitnpm run build

Lint and the type check finish without errors. The build ends with a list of routes, / marked ○ (Static): the page is built once, ahead of time, and the theme still works because it’s all CSS and one small script from next-themes.

If something goes wrong

  • The page is in Times (a serif font). The layout still names its font --font-geist-sans. Use the layout from step 2, which names it --font-sans.
  • Picking Dark does nothing. Check attribute="class" on ThemeProvider in app/layout.tsx. Without it, next-themes writes data-theme="dark" on <html>, and shadcn’s styles only look for the class.
  • Opening the theme menu crashes with “Base UI: MenuGroupContext is missing.” A DropdownMenuLabel is outside a DropdownMenuGroup. Wrap the label and the items in one, as in step 4.
  • TypeScript says Property 'asChild' does not exist. These components are built on Base UI, which uses render={<Button />} instead. asChild is the Radix way, from older guides.
  • bg-success does nothing, with no error. Tailwind only makes classes for names in @theme. Check the @theme inline block from step 7 is in app/globals.css and spelled --color-success.
  • A warning about className not matching on <html>. suppressHydrationWarning is missing from <html> in the layout.

Questions people ask

Do I still need tailwind.config.js with Tailwind v4?

No. With Tailwind v4 the theme lives in your CSS file. The @theme inline block in app/globals.css is what turns shadcn’s variables into classes like bg-primary, and a new colour is two variables plus one line there. That’s why components.json lists the Tailwind config as an empty string. You only need a config file for a plugin that asks for one.

How do I change the colours of every shadcn/ui component at once?

Change the CSS variables, not the components. Every component paints itself with names like primary and muted, and their values live in the :root (light) and .dark blocks of app/globals.css. Put your own values in a block at the end of that file and they win over the defaults, so every button, badge, switch and focus ring follows in both themes.

Why does shadcn/ui use OKLCH colours?

Because the lightness number in OKLCH matches what your eye sees. Two colours with the same lightness look equally light whatever their hue, so you can swap a blue brand for a purple one and keep the same contrast with the text on it. Making a dark-mode version is mostly raising the lightness. Every current browser supports it.

Will updating a shadcn/ui component erase my changes?

It can. Running add with --overwrite replaces the file, including any variant you added. Run npx shadcn@latest add <name> --diff first: it changes nothing and lists the lines an update would remove, marked with a minus. Keep most changes in the theme, in className and in wrapper components, and only small, deliberate edits inside components/ui.

Where to go next

  • Make those plan buttons take real money: Stripe payments in Next.js builds checkout, webhooks and orders on a fresh Next.js 16 project.
  • Put the pricing page behind a real product with accounts: Better Auth in Next.js.
  • Browse the components and their examples at ui.shadcn.com, and try other colour themes with npx shadcn@latest apply, which rewrites the variables for you.
Found this useful? Give it a heart, or share it with someone who’d use it.
WRITTEN BY
Ali Murtaza

I build SaaS products with Next.js and teach them start to finish, in full courses here and free builds on YouTube.

KEEP READING