963 markdown files reformatted with the repository's pinned prettier so pnpm format:check covers the folded tree like every other repo file. The formatter's embedded-language pass also normalized code fences (TS semicolons, closed HTML tags in examples, lowercased CSS hex colors, one renumbered list that skipped an index). Alphanumeric token deltas vs the fold commit were audited file-by-file; all are formatter-equivalent markup normalizations plus the four sanitized skills.
3.5 KiB
3.5 KiB
shadcn/ui Learning Guide
This guide helps you learn shadcn/ui from basics to advanced patterns.
Learning Path
1. Understanding the Philosophy
shadcn/ui is different from traditional component libraries:
- Copy-paste components: Components are copied into your project, not installed as packages
- Full customization: You own the code and can modify it freely
- Built on Radix UI: Provides accessibility primitives
- Styled with Tailwind: Uses utility classes for consistent styling
2. Core Concepts to Master
Class Variance Authority (CVA)
Most components use CVA for variant management:
const buttonVariants = cva('base-classes', {
variants: {
variant: {
default: 'variant-classes',
destructive: 'destructive-classes',
},
size: {
default: 'size-classes',
sm: 'small-classes',
},
},
defaultVariants: {
variant: 'default',
size: 'default',
},
});
cn Utility Function
The cn function combines classes and resolves conflicts:
import { clsx, type ClassValue } from 'clsx';
import { twMerge } from 'tailwind-merge';
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}
3. Installation Checklist
- Initialize a new project (Next.js, Vite, or Remix)
- Install Tailwind CSS
- Run
npx shadcn@latest init - Configure CSS variables
- Install first component:
npx shadcn@latest add button
4. Essential Components to Learn First
- Button - Learn variants and sizes
- Input - Form inputs with labels
- Card - Container components
- Form - Form handling with React Hook Form
- Dialog - Modal windows
- Select - Dropdown selections
- Toast - Notifications
5. Common Patterns
Form Pattern
Every form follows this structure:
1. Define Zod schema
2. Create form with useForm
3. Wrap with Form component
4. Add FormField for each input
5. Handle submission
Component Customization Pattern
To customize a component:
- Copy component to your project
- Modify the variants
- Add new props if needed
- Update types
6. Best Practices
- Always use TypeScript
- Follow the existing component structure
- Use semantic HTML when possible
- Test with screen readers for accessibility
- Keep components small and focused
7. Advanced Topics
- Creating custom components from scratch
- Building complex forms with validation
- Implementing dark mode
- Optimizing for performance
- Testing components
Practice Exercises
Exercise 1: Basic Setup
- Create a new Next.js project
- Set up shadcn/ui
- Install and customize a Button component
- Add a new variant "gradient"
Exercise 2: Form Building
- Create a contact form with:
- Name input (required)
- Email input (email validation)
- Message textarea (min length)
- Submit button with loading state
Exercise 3: Component Combination
- Build a settings page using:
- Card for layout
- Sheet for mobile menu
- Select for dropdowns
- Switch for toggles
- Toast for notifications
Exercise 4: Custom Component
- Create a custom Badge component
- Support variants: default, secondary, destructive, outline
- Support sizes: sm, default, lg
- Add icon support