Build production-ready React/Next.js UIs with shadcn/ui components. Full lifecycle - install, customize, compose, debug, optimize. Covers components, forms, tables, theming, animations, and hooks.
Overview
How shadcn/ui Works
shadcn/ui is NOT a component library - it's a collection of re-usable components you copy into your project and own. This philosophy changes everything about how you work with it.
1. You Own the Code
Components are copied directly into your components/ui/ directory. You can and should modify them. Don't wrap components - edit them directly. This is intentional.
```bash
# Install a component
npx shadcn@latest add button
# Components land in your project
# src/components/ui/button.tsx
```
2. Composition Over Monoliths
Build complex UIs by composing primitives. Every component follows consistent patterns:
Radix UI primitives for accessibility
CVA (class-variance-authority) for variants
cn() utility for conditional classes
data-slot attributes for styling hooks
```tsx
// Composition pattern - build from primitives
Title
Description
{/ Content /}
```
3. The asChild Pattern
Use asChild prop to merge behavior onto a different element. Critical for navigation, forms, and custom triggers.
```tsx
// asChild merges Button behavior onto Link
Dashboard
// asChild on DialogTrigger
```
4. Variant-Driven Design
Use CVA for consistent variant APIs. Every button, badge, and alert follows this pattern:
```tsx
const buttonVariants = cva(
"inline-flex items-center justify-center rounded-md...", // Base
{
variants: {
variant: {
default: "bg-primary text-primary-foreground",
destructive: "bg-destructive text-white",
outline: "border bg-background",
ghost: "hover:bg-accent",
},
size: {
default: "h-9 px-4",
sm: "h-8 px-3",
lg: "h-10 px-6",
icon: "size-9",
},
},
defaultVariants: {
variant: "default",
size: "default",
},
}
)
```
5. CSS Variables for Theming
All colors use CSS variables. Theme by changing variables, not component code:
```css
/ Light mode /
:root {
--background: 0 0% 100%;
--foreground: 222.2 84% 4.9%;
--primary: 222.2 47.4% 11.2%;
--primary-foreground: 210 40% 98%;
}
/ Dark mode /
.dark {
--background: 222.2 84% 4.9%;
--foreground: 210 40% 98%;
}
```
6. React 19 Patterns
shadcn/ui uses modern React patterns - no forwardRef (React 19), data-slot attributes, and function components: