New skills: - next-best-practices: Next.js 15+ RSC, async patterns, self-hosting (vercel-labs) - better-auth-best-practices: Official Better-Auth with Drizzle adapter (better-auth) - verification-before-completion: Evidence-based completion claims (obra/superpowers) - shadcn-ui: Component patterns with Tailwind v4 adaptation note (developer-kit) - writing-skills: TDD methodology for skill authoring (obra/superpowers) README reorganized by category with Mosaic Stack alignment section. Total: 9 skills (4 existing + 5 new). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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