Polski

Tutorial · Tailwind CSS · React

Tailwind CSS v4 in React and Vite – tokens in @theme, variants with cva, dark mode and accessibility

We set up Tailwind CSS v4 in a React + Vite app and build a small component set for a dumpling shop – design tokens in @theme, semantic tokens for dark mode, buttons with variants (cva + tailwind-merge), accessible form fields and a native dialog. Finally – when to pick Tailwind and when MUI.

By · · 18 min read

Tailwind CSS v4 is an engine rewritten from scratch: configuration moved from tailwind.config.js into CSS, design tokens became plain CSS variables, and the Vite integration is a single plugin. In this guide you'll build a dumpling shop's menu page with a small but solid component set – with variants, dark mode and accessibility from the very first line.

Prerequisites: CSS basics (flexbox, grid, CSS variables), React with hooks and TypeScript (see the React guide). No Tailwind v3 knowledge needed – we point out the differences as we go.

1
Vite plugin – no `tailwind.config.js`, no PostCSS
22 kB
CSS for the whole page – only the classes actually used in code
oklch
colour space – v4's default palette
0
modal libraries – the native `<dialog>`

Step 1. Installing in Vite

npm create vite@latest pierogi-tailwind -- --template react-ts
cd pierogi-tailwind
npm install tailwindcss @tailwindcss/vite
npm install class-variance-authority clsx tailwind-merge @fontsource-variable/inter

Project configuration

package.json
{
  "name": "pierogi-tailwind",
  "private": true,
  "version": "0.1.0",
  "type": "module",
  "scripts": {
    "dev": "vite",
    "build": "tsc --noEmit && vite build",
    "preview": "vite preview"
  },
  "dependencies": {
    "@fontsource-variable/inter": "^5.3.0",
    "class-variance-authority": "^0.7.1",
    "clsx": "^2.1.1",
    "react": "^19.3.1",
    "react-dom": "^19.3.1",
    "tailwind-merge": "^3.7.1"
  },
  "devDependencies": {
    "@tailwindcss/vite": "^4.3.5",
    "@types/react": "^19.3.0",
    "@types/react-dom": "^19.3.0",
    "@vitejs/plugin-react": "^6.3.0",
    "tailwindcss": "^4.3.5",
    "typescript": "~7.0.2",
    "vite": "^8.4.0"
  }
}
tsconfig.json
{
  "compilerOptions": {
    "target": "es2023",
    "lib": ["ES2023", "DOM", "DOM.Iterable"],
    "module": "esnext",
    "moduleResolution": "bundler",
    "types": ["vite/client"],
    "jsx": "react-jsx",
    "noEmit": true,
    "allowImportingTsExtensions": true,
    "verbatimModuleSyntax": true,
    "skipLibCheck": true,
    "noUncheckedIndexedAccess": true
  },
  "include": ["src", "vite.config.ts"]
}
vite.config.ts
import tailwindcss from "@tailwindcss/vite";
import react from "@vitejs/plugin-react";
import { defineConfig } from "vite";

// Tailwind v4: one Vite plugin, no tailwind.config.js and no PostCSS config.
export default defineConfig({
  plugins: [react(), tailwindcss()],
});

Tailwind v4 detects source files by itself (skipping whatever is in .gitignore), so there's no content list any more. If you use classes from a package in node_modules (e.g. your own component library), add @source "../node_modules/my-library"; in CSS.

Step 2. Design tokens in @theme

The whole theme lives in the CSS file. It has two layers:

  1. Brand tokens in @theme – the brand colour scale, the font, the card radius, a shadow, an animation. Every variable in a namespace (--color-*, --font-*, --radius-*, --shadow-*, --animate-*) creates matching utilities.
  2. Semantic tokens in @theme inline – canvas, surface, border, fg, muted, accent. They point at variables (--surface) that the .dark block swaps for dark counterparts.

Tokens, dark mode and a custom utility

src/index.css
@import "tailwindcss";

/* Dark mode driven by a .dark class on <html> instead of only prefers-color-scheme. */
@custom-variant dark (&:where(.dark, .dark *));

/* 1. Brand tokens – every variable here becomes utilities: bg-brand-600, font-sans, rounded-card… */
@theme {
  --font-sans: "Inter Variable", ui-sans-serif, system-ui, sans-serif;

  --color-brand-50: oklch(0.97 0.02 350);
  --color-brand-100: oklch(0.93 0.05 350);
  --color-brand-300: oklch(0.8 0.12 352);
  --color-brand-500: oklch(0.64 0.2 355);
  --color-brand-600: oklch(0.56 0.2 357);
  --color-brand-700: oklch(0.49 0.18 358);
  --color-brand-900: oklch(0.36 0.12 358);

  --radius-card: 0.875rem;
  --shadow-card: 0 1px 2px oklch(0 0 0 / 0.06), 0 4px 16px oklch(0 0 0 / 0.06);
  --animate-pop: pop 160ms ease-out;

  @keyframes pop {
    from { transform: scale(0.96); opacity: 0; }
    to { transform: scale(1); opacity: 1; }
  }
}

/* 2. Semantic tokens – components use bg-surface / text-muted, never raw greys.
   `inline` makes utilities reference the runtime variable, so .dark can swap it. */
@theme inline {
  --color-canvas: var(--canvas);
  --color-surface: var(--surface);
  --color-border: var(--border);
  --color-fg: var(--fg);
  --color-muted: var(--muted);
  --color-accent: var(--accent);
  --color-accent-fg: var(--accent-fg);
  --color-danger: var(--danger);
}

:root {
  --canvas: oklch(0.985 0.003 350);
  --surface: oklch(1 0 0);
  --border: oklch(0.92 0.005 350);
  --fg: oklch(0.22 0.01 350);
  --muted: oklch(0.5 0.01 350);
  --accent: var(--color-brand-600);
  --accent-fg: oklch(1 0 0);
  --danger: oklch(0.55 0.2 27);
}

.dark {
  --canvas: oklch(0.17 0.01 350);
  --surface: oklch(0.21 0.012 350);
  --border: oklch(0.3 0.012 350);
  --fg: oklch(0.95 0.005 350);
  --muted: oklch(0.7 0.01 350);
  --accent: var(--color-brand-300);
  --accent-fg: oklch(0.2 0.02 350);
  --danger: oklch(0.7 0.17 25);
}

@layer base {
  html { color-scheme: light; }
  html.dark { color-scheme: dark; }
  body { @apply bg-canvas font-sans text-fg antialiased; }
}

/* A custom utility that works with variants: hover:focus-ring, md:focus-ring… */
@utility focus-ring {
  @apply outline-2 outline-offset-2 outline-accent;
}
Diagram: Two layers of tokens: components only know the semantic names

Why inline? A plain @theme generates bg-surface { background: var(--color-surface) }, and --color-surface is resolved once, on :root. The inline version writes var(--surface) straight into the utility, so the value follows the .dark class. Colours are written in oklch – its lightness (L) changes linearly for the eye, so scales and dark variants are easier to tune than in HEX or HSL.

@utility focus-ring is a custom utility. Unlike a plain CSS rule, it works with variants: focus-visible:focus-ring, md:focus-ring.

Step 3. Components with variants: cva + tailwind-merge

A button has several dimensions: intent (primary, secondary, ghost, danger), size and states. A chain of conditions in JSX quickly becomes unreadable. class-variance-authority (cva) turns it into a table: base classes, variants, combinations and defaults, and VariantProps gives you the prop types for free.

The cn() helper and the button

src/lib/cn.ts
import { clsx, type ClassValue } from "clsx";
import { twMerge } from "tailwind-merge";

// clsx joins conditional classes, twMerge resolves conflicts: cn("px-4", "px-2") === "px-2".
export function cn(...inputs: ClassValue[]) {
  return twMerge(clsx(inputs));
}
src/components/ui/Button.tsx
import { cva, type VariantProps } from "class-variance-authority";
import type { ComponentProps } from "react";
import { cn } from "../../lib/cn.ts";

// All visual decisions in one table: base classes + variants + defaults.
export const buttonVariants = cva(
  [
    "inline-flex items-center justify-center gap-2 rounded-lg font-semibold whitespace-nowrap",
    "transition-colors duration-150 select-none",
    "focus-visible:focus-ring disabled:pointer-events-none disabled:opacity-50",
  ],
  {
    variants: {
      intent: {
        primary: "bg-accent text-accent-fg hover:bg-accent/90",
        secondary: "border border-border bg-surface text-fg hover:bg-canvas",
        ghost: "text-fg hover:bg-fg/5",
        danger: "bg-danger text-white hover:bg-danger/90",
      },
      size: {
        sm: "h-8 px-3 text-sm",
        md: "h-10 px-4 text-sm",
        lg: "h-12 px-6 text-base",
        icon: "size-10",
      },
    },
    compoundVariants: [{ intent: "ghost", size: "sm", class: "px-2" }],
    defaultVariants: { intent: "primary", size: "md" },
  },
);

type ButtonProps = ComponentProps<"button"> & VariantProps<typeof buttonVariants> & { loading?: boolean };

// React 19: `ref` arrives as a regular prop inside ...props – no forwardRef.
export function Button({ className, intent, size, loading = false, disabled, children, ...props }: ButtonProps) {
  return (
    <button
      type="button"
      className={cn(buttonVariants({ intent, size }), className)}
      disabled={disabled || loading}
      aria-busy={loading || undefined}
      {...props}
    >
      {loading && (
        <span className="size-4 animate-spin rounded-full border-2 border-current border-t-transparent" aria-hidden />
      )}
      {children}
    </button>
  );
}

The rest of this article is for members

Create a free account or sign in to read it all. Still ahead:

  • Step 4. Cards, badges and accessible fields
  • Step 5. Dark mode without the flash
  • Step 6. Accessibility: the native dialog and state variants
  • Step 7. Putting the page together
  • Tailwind or MUI?
  • Common mistakes
  • Check yourself
  • What next

Create a free account · Sign in