shadcn-v5
shadcn/ui v5 component patterns, best practices, and CLI commands for building professional UIs. Trigger: When working with shadcn/ui components, adding components via CLI, styling with Tailwind, or composing component structures.
- License 许可
- Apache-2.0
- 最近更新
- 2026-08-26
- Stars
- ★ 1
- Forks
- 0
01 前言 Front Matter
- name
- shadcn-v5
- license
- Apache-2.0
- metadata
- { "author": "gentleman-programming", "version": "1.0" }
- description
- shadcn/ui v5 component patterns, best practices, and CLI commands for building professional UIs. Trigger: When working with shadcn/ui components, adding components via CLI, styling with Tailwind, or composing component structures.
02 资源文件 12
- 参考文档.skill-meta.json509 B
- 资产文件assets/01-button-component.tsx419 B
- 资产文件assets/01-example.tsx25 B
- 资产文件assets/components-reference.md17.5 KB
- 资产文件assets/quick-reference.md3.6 KB
- 参考文档README.md431 B
- 参考文档references/01-component-cli.md74 B
- 参考文档references/01-guide.md38 B
- 参考文档references/02-customization.md80 B
- 参考文档references/02-patterns.md29 B
- 参考文档references/03-composition.md68 B
- 参考文档references/references.md1.6 KB
03 SKILL.md 正文
When to Use
- Adding or managing shadcn/ui components via CLI
- Composing component structures (Dialog, Select, Card, Tabs)
- Styling with Tailwind CSS and semantic colors
- Implementing dark mode and theming
- Building forms with Input, Select, Checkbox, etc.
- Following best practices for component composition
Critical Rules
❌ NEVER DO
- NO custom
<button>if shadcn Button exists → Useimport { Button } from '@/components/ui/button' - NO hardcoded colors → Use semantic tokens (
bg-primary,text-muted-foreground) - NO manual className merging → Use
cn("class1", "class2")utility - NO custom implementations of standard components → Use CLI to add components
✅ ALWAYS DO
- Check components/ui first before creating any UI element
- Use CLI to add components:
npx shadcn@latest add [component] - Use
cn()for className merging:className={cn("base-class", condition && "conditional-class")} - Use lucide-react for icons (or project icon library)
- Follow semantic color naming:
bg-background,text-foreground,text-muted-foreground
Component Selection Quick Reference
| Need | Use |
|---|---|
| Button/action | Button with variant |
| Form inputs | Input, Select, Switch, Checkbox, RadioGroup, Textarea |
| Toggle 2-5 options | ToggleGroup + ToggleGroupItem |
| Data display | Table, Card, Badge, Avatar |
| Navigation | Sidebar, Tabs, Breadcrumb, Pagination |
| Overlays | Dialog, Sheet, Drawer, AlertDialog |
| Feedback | sonner (toast), Alert, Progress, Skeleton, Spinner |
| Command palette | Command inside Dialog |
| Layout | Card, Separator, Resizable, ScrollArea, Accordion |
| Empty states | Empty |
| Menus | DropdownMenu, ContextMenu, Menubar |
| Tooltips | Tooltip, HoverCard, Popover |
Styling Patterns
Semantic Colors (use these, NOT raw values)
// ✅ CORRECT - semantic tokens
<div className="bg-background text-foreground">
<Button variant="destructive">Delete</Button>
<Badge variant="secondary">New</Badge>
// ❌ WRONG - raw color values
<div className="bg-blue-500 text-gray-900">
<span className="text-red-600">Error</span>
Spacing: gap-* NOT space-y-*
// ✅ CORRECT
<div className="flex flex-col gap-4">
// ❌ WRONG
<div className="space-y-4">
Equal dimensions: size-* NOT w-* h-*
// ✅ CORRECT
<Avatar className="size-10">
// ❌ WRONG
<Avatar className="w-10 h-10">
Icons in Buttons: data-icon attribute
<Button>
<SearchIcon data-icon="inline-start" />
Search
</Button>
Truncate: use shorthand
// ✅ CORRECT
<p className="truncate">
// ❌ WRONG
<p className="overflow-hidden text-ellipsis whitespace-nowrap">
Component Composition Rules
Items ALWAYS inside their Group
// ✅ CORRECT
<SelectGroup>
<SelectItem value="1">Option 1</SelectItem>
</SelectGroup>
// ❌ WRONG
<SelectItem value="1">Option 1</SelectItem>
Dialog, Sheet, Drawer ALWAYS need Title
// ✅ CORRECT
<Dialog>
<DialogTitle>Confirm Action</DialogTitle>
<DialogContent>...</DialogContent>
</Dialog>
// ❌ WRONG - missing title for accessibility
<Dialog>
<DialogContent>...</DialogContent>
</Dialog>
Full Card composition
// ✅ CORRECT
<Card>
<CardHeader>
<CardTitle>Title</CardTitle>
<CardDescription>Description</CardDescription>
</CardHeader>
<CardContent>Content</CardContent>
<CardFooter>Footer</CardFooter>
</Card>
// ❌ WRONG - dumping everything in CardContent
<Card>
<CardContent>Title + Content + Footer</CardContent>
</Card>
TabsTrigger must be inside TabsList
// ✅ CORRECT
<Tabs>
<TabsList>
<TabsTrigger value="tab1">Tab 1</TabsTrigger>
</TabsList>
</Tabs>
// ❌ WRONG
<Tabs>
<TabsTrigger value="tab1">Tab 1</TabsTrigger>
</Tabs>
Avatar always needs AvatarFallback
// ✅ CORRECT
<Avatar>
<AvatarImage src={user.avatar} />
<AvatarFallback>JD</AvatarFallback>
</Avatar>
Forms Pattern
Always use FieldGroup + Field for form layout
// ✅ CORRECT
<FieldGroup>
<Field>
<FieldLabel htmlFor="email">Email</FieldLabel>
<Input id="email" />
<FieldDescription>Enter your email</FieldDescription>
</Field>
</FieldGroup>
// ❌ WRONG - div with space-y
<div className="space-y-4">
<Label>Email</Label>
<Input />
</div>
Validation: data-invalid on Field, aria-invalid on control
<Field data-invalid>
<FieldLabel>Email</FieldLabel>
<Input aria-invalid />
<FieldError>Invalid email</FieldError>
</Field>
CLI Commands
# Initialize shadcn in project
npx shadcn@latest init
# Add components
npx shadcn@latest add button card dialog select
npx shadcn@latest add @magicui/shimmer-button # from registry
# Check project info and installed components
npx shadcn@latest info --json
# Get component documentation URLs
npx shadcn@latest docs button dialog select
# View registry item details
npx shadcn@latest view @shadcn/button
# Preview changes before adding/updating
npx shadcn@latest add button --dry-run
npx shadcn@latest add button --diff button.tsx
# Search registries
npx shadcn@latest search @shadcn -q "sidebar"
Project Context Fields
When working with shadcn projects, these fields from npx shadcn@latest info are critical:
| Field | Description | Action |
|---|---|---|
aliases |
Import path prefix (e.g., @/) |
Use for imports, never hardcode |
isRSC |
React Server Components enabled | Add "use client" for client components |
tailwindVersion |
v4 uses @theme inline, v3 uses config |
Follow correct pattern |
base |
radix or base primitive library |
Affects component APIs |
iconLibrary |
Icon library (e.g., lucide-react) |
Use correct import |
packageManager |
npm, pnpm, bun |
Use for dependency installs |
Dark Mode Setup (class-based)
// 1. Create ThemeProvider
function ThemeProvider({ children, defaultTheme = "dark", storageKey = "vite-ui-theme" }) {
const [theme, setTheme] = useState(() => localStorage.getItem(storageKey) || defaultTheme)
useEffect(() => {
const root = window.document.documentElement
root.classList.remove("light", "dark")
root.classList.add(theme)
}, [theme])
return (
<Provider value={{ theme, setTheme }}>
{children}
</Provider>
)
}
// 2. Wrap app in main.tsx
<ThemeProvider defaultTheme="dark" storageKey="vite-ui-theme">
<App />
</ThemeProvider>
// 3. Toggle theme
const toggleTheme = () => setTheme(prev => prev === "dark" ? "light" : "dark")
References
- shadcn/ui official docs: https://ui.shadcn.com/
- shadcn/ui GitHub: https://github.com/shadcn-ui/ui
- All Components List: https://ui.shadcn.com/docs/components
- Tailwind CSS: https://tailwindcss.com/
- lucide-react icons: https://lucide.dev/
- OKLCH color format: https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/oklch
Resources
See references/ for detailed guides and code examples in assets/.
External Resources
- [Official Documentation]
04 同仓库更多 Skills
zustand-5
Zustand 5 state management patterns. Trigger: When managing React state with Zustand.
zod-4
Zod 4 schema validation patterns. Trigger: When using Zod for validation - breaking changes from v3.
x-bookmark-quote-posts
Workflow para revisar bookmarks de X/Twitter y convertirlos en drafts de quote posts. Accede a la sección de bookmarks vía browser headless, extrae posts recientes, y genera drafts con voz personal del usuario. No publica ni muta X — solo genera contenido para revisión. Trigger: X bookmarks, Twitter bookmarks, quote posts, bookmark automation, crear quote posts, revisar bookmarks, growth content X.
writing-skills
Use when creating new skills, editing existing skills, or verifying skills work before deployment
writing-plans
Use when you have a spec or requirements for a multi-step task, before touching code
work-unit-commits
Plan commits as reviewable work units. Trigger: implementation, commit splitting, chained PRs, or keeping tests and docs with code.