关于 shadcn
用于使用 shadcn/ui 构建界面的技能,组件以源码形式加入项目。它指导 Agent 检查项目配置和文档、复用现有组件,并遵循组合与无障碍模式。
主要功能
- 01适配项目配置的 CLI 流程
- 02以源码形式安装组件
- 03查找组件与注册表
- 04样式和组合指引
- 05无障碍表单与聊天模式
适用场景
- 01构建设置页或仪表盘界面
- 02创建带验证的表单
- 03组合聊天与对话界面
- 04按项目配置调整组件
安装
复制下面的命令,在终端中运行。
npx skills add https:/ / github.com/ laguagu/ claude-code-nextjs-skills --skill shadcn安装后在 Claude Code 中输入/shadcn确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
cli.mdcustomization.mddesign-system-page.mddesign-system.mdmcp.mdregistry.mdagents/openai.yamlassets/showcase/color-pair.tsxpreview-states.cssstate-matrix.tsxevals/evals.jsonrules/base-vs-radix.mdchat.mdcomposition.mdforms.mdicons.mdstyling.mdscripts/showcase-coverage.mjs详细介绍
shadcn/ui
用于构建 UI、组件和设计系统的框架。组件通过 CLI 以源代码形式添加到用户的项目中。
重要: 使用项目的包运行器运行所有 CLI 命令:
npx shadcn@latest、pnpm dlx shadcn@latest或bunx --bun shadcn@latest,具体取决于项目的packageManager。以下示例使用npx shadcn@latest,请替换为项目对应的运行器。
对于新项目,默认使用 Bun。现有项目保留其已配置的包管理器。
当前项目上下文
使用项目的包运行器运行 shadcn@latest info --json,并读取返回的配置和组件列表。不要假设每个客户端都会在技能运行时执行 Shell 插值。使用 shadcn@latest docs <component> 获取与项目基础配置匹配的文档。
原则
- 优先使用现有组件。 使用
npx shadcn@latest search检查注册表,再编写自定义 UI。也要检查社区注册表。 - 组合使用,不要重复造轮子。 设置页面 = Tabs + Card + 表单控件。仪表板 = Sidebar + Card + Chart + Table。
- 优先使用内置变体,再编写自定义样式。 例如
variant="outline"、size="sm"等。 - 使用语义化颜色。 使用
bg-primary、text-muted-foreground,不要使用bg-blue-500这样的原始值。
关键规则
将以下规则作为此设计系统的默认规范;明确的项目要求优先。每条规则都链接到包含错误/正确代码对的文件。
样式与 Tailwind → styling.md
className用于布局,而非样式。 切勿覆盖组件颜色或排版。- 不要使用
space-x-*或space-y-*。 使用带有gap-*的flex。对于纵向堆叠,使用flex flex-col gap-*。 - 宽度和高度相同时使用
size-*。 使用size-10,而不是w-10 h-10。 - 使用
truncate简写类。 不要使用overflow-hidden text-ellipsis whitespace-nowrap。 - 不要手动覆盖
dark:颜色。 使用语义化令牌(bg-background、text-muted-foreground)。 - 使用
cn()处理条件类名。 不要手动编写模板字面量三元表达式。 - 不要手动为覆盖层组件设置
z-index。 Dialog、Sheet、Popover 等会自行处理堆叠顺序。
表单与输入 → forms.md
- 表单使用
FieldGroup+Field。 表单布局中切勿使用带有space-y-*或grid gap-*的原始div。 InputGroup使用InputGroupInput/InputGroupTextarea。 切勿在InputGroup中直接使用Input/Textarea。- 输入框内的按钮使用
InputGroup+InputGroupAddon。 - 选项组(2–7 个选项)使用
ToggleGroup。 不要循环渲染Button并手动维护激活状态。 - 使用
FieldSet+FieldLegend对相关复选框/单选框分组。 不要使用带标题的div。 - 字段验证使用
data-invalid+aria-invalid。data-invalid设置在Field上,aria-invalid设置在控件上。对于禁用状态:data-disabled设置在Field上,disabled设置在控件上。
组件结构 → composition.md
- 条目始终放在对应的 Group 中。
SelectItem→SelectGroup。DropdownMenuItem→DropdownMenuGroup。CommandItem→CommandGroup。 - 使用已安装基础组件的组合 API。 Radix 使用
asChild;Base UI 使用render。对于 React Aria,应获取 Aria 组件文档,而不是自动套用上述任一模式。检查shadcn@latest info返回的base字段。→ base-vs-radix.md - Dialog、Sheet 和 Drawer 始终需要 Title。 为满足无障碍要求,必须提供
DialogTitle、SheetTitle、DrawerTitle。如果需要视觉隐藏,请使用className="sr-only"。 - 使用对应的 Card 插槽。 仅在存在相应内容时添加页眉、描述和页脚。
- Button 没有
isPending/isLoading。 使用Spinner+data-icon+disabled组合实现。 TabsTrigger必须放在TabsList中。 切勿直接在Tabs中渲染触发器。Avatar始终需要AvatarFallback。 用于图片加载失败时的情况。
使用组件,而不是自定义标记 → composition.md
- 优先使用现有组件,而不是自定义标记。 编写带样式的
div前,先检查是否已有对应组件。 - 提示框使用
Alert。 不要构建自定义样式的 div。 - 空状态使用
Empty。 不要构建自定义空状态标记。 - Toast 遵循项目的基础方案。 对于 Base UI 项目,使用
toast组件中的toast。对于 Radix 和 React Aria 项目,使用sonner中的toast()。 - 使用
Separator,而不是<hr>或<div className="border-t">。 - 使用
Skeleton作为加载占位符。不要使用自定义的animate-pulsediv。 - 使用
Badge,而不是自定义样式的 span。
图标 → icons.md
Button中的图标使用data-icon。 在图标上设置data-icon="inline-start"或data-icon="inline-end"。- 组件内部的图标不要设置尺寸类。 组件会通过 CSS 处理图标尺寸。不要使用
size-4或w-4 h-4。 - 将图标作为对象传入,而不是字符串键。 使用
icon={CheckIcon},而不是字符串查找。
聊天与消息 → chat.md
- 聊天 UI 由聊天基础组件组合而成。 对话使用
MessageScroller,消息行使用Message,内容表面使用Bubble。不要手写气泡div或使用原始滚动容器。 MessageScroller负责滚动行为。 流式跟随、锚定和跳转到最新消息(MessageScrollerButton)均已内置。不要编写useStickToBottom/ResizeObserverhook。- 附件使用
Attachment;系统备注和分隔线使用Marker。 不要使用Item卡片或Separator+ 标签。 - 助手活动使用
Trace。 推理、工具调用、来源和步骤都作为Trace中的TraceLine,每组消息部分对应一个Trace。交互行为包裹该行:使用TraceCollapsible、sheet 触发器或链接。不要手写Collapsible或使用Marker。
CLI
- 绝不要手动解码预设代码或构建预设 URL。 使用
npx shadcn@latest preset decode <code>、preset url <code>或preset open <code>。要根据项目检测预设,请使用npx shadcn@latest preset resolve。 - 直接通过 CLI 应用预设代码。 对于现有项目,使用
npx shadcn@latest apply <code>;初始化项目时,使用npx shadcn@latest init --preset <code>。
关键模式
这些是区分正确 shadcn/ui 代码的最常见模式。边缘情况请参阅上方链接的规则文件。
// Form layout: FieldGroup + Field, not div + Label.
<FieldGroup>
<Field>
<FieldLabel htmlFor="email">Email</FieldLabel>
<Input id="email" />
</Field>
</FieldGroup>
// Validation: data-invalid on Field, aria-invalid on the control.
<Field data-invalid>
<FieldLabel>Email</FieldLabel>
<Input aria-invalid />
<FieldDescription>Invalid email.</FieldDescription>
</Field>
// Icons in buttons: data-icon, no sizing classes.
<Button>
<SearchIcon data-icon="inline-start" />
Search
</Button>
// Spacing: gap-*, not space-y-*.
<div className="flex flex-col gap-4"> // correct
<div className="space-y-4"> // wrong
// Equal dimensions: size-*, not w-* h-*.
<Avatar className="size-10"> // correct
<Avatar className="w-10 h-10"> // wrong
// Status colors: Badge variants or semantic tokens, not raw colors.
<Badge variant="secondary">+20.1%</Badge> // correct
<span className="text-emerald-600">+20.1%</span> // wrong
组件选择
| 需求 | 使用 |
|---|---|
| 按钮/操作 | Button,并使用合适的变体 |
| 表单输入 | Input、Select、Combobox、Switch、Checkbox、RadioGroup、Textarea、InputOTP、Slider |
| 在 2–5 个选项间切换 | ToggleGroup + ToggleGroupItem |
| 数据展示 | Table、Card、Badge、Avatar |
| 导航 | Sidebar、NavigationMenu、Breadcrumb、Tabs、Pagination |
| 覆盖层 | Dialog(模态框)、Sheet(侧边面板)、Drawer(底部面板)、AlertDialog(确认) |
| 反馈 | toast(Base UI)、sonner(Radix/Aria)、Alert、Progress、Skeleton、Spinner |
| 命令面板 | 在 Dialog 中使用 Command |
| 图表 | Chart(封装 Recharts) |
| 布局 | Card、Separator、Resizable、ScrollArea、Accordion、Collapsible |
| 空状态 | Empty |
| 菜单 | DropdownMenu、ContextMenu、Menubar |
| 工具提示/信息 | Tooltip、HoverCard、Popover |
| 聊天/对话界面 | MessageScroller、Message、Bubble、Attachment、Marker、Trace |
关键字段
info --json 项目上下文包含以下关键字段:
aliases→ 导入时使用实际的别名前缀(例如@/、~/),切勿硬编码。isRSC→ 当值为true时,使用useState、useEffect、事件处理程序或浏览器 API 的组件需要在文件顶部添加"use client"。提供此指令的建议时,务必参考此字段。tailwindVersion→"v4"使用@theme inline块;"v3"使用tailwind.config.js。tailwindCssFile→ 定义自定义 CSS 变量的全局 CSS 文件。始终编辑此文件,切勿新建文件。style→ 组件的视觉风格(例如nova、vega)。base→ 原语库(radix、base或aria)。它会影响组件 API 和可用属性。iconLibrary→ 决定图标导入方式。lucide使用lucide-react,tabler使用@tabler/icons-react,依此类推。切勿默认使用lucide-react。resolvedPaths→ 组件、工具函数、钩子等的确切文件系统路径。framework→ 路由和文件约定(例如 Next.js App Router 与 Vite SPA)。packageManager→ 安装任何非 shadcn 依赖项时使用此字段(例如使用pnpm add date-fns而非npm install date-fns)。preset→ 当前项目的已解析预设代码和值。仅需预设信息时,使用npx shadcn@latest preset resolve --json。
完整字段参考请参阅 cli.md — info 命令。
组件文档、示例和用法
运行 npx shadcn@latest docs <component>,获取组件的文档、示例和 API 参考链接。获取这些链接以查看实际内容。
npx shadcn@latest docs button dialog select
创建、修复、调试或使用组件时,务必先运行 npx shadcn@latest docs 并获取链接。 这样可以确保你使用的是正确的 API 和用法,而不是凭空猜测。
工作流
- 获取项目上下文 — 使用项目运行器运行
shadcn@latest info --json。 - 先检查已安装的组件 — 运行
add之前,务必先检查项目上下文中的components列表,或列出resolvedPaths.ui目录。不要导入尚未添加的组件,也不要重复添加已安装的组件。 - 查找组件 — 运行
npx shadcn@latest search。 - 获取文档和示例 — 运行
npx shadcn@latest docs <component>获取 URL,然后访问这些 URL。使用npx shadcn@latest view浏览尚未安装的注册表项。若要预览对已安装组件的更改,请使用npx shadcn@latest add --diff。 - 安装或更新 — 运行
npx shadcn@latest add。更新现有组件时,先使用--dry-run和--diff预览更改(请参阅下文的更新组件)。 - 修复第三方组件中的导入路径 — 添加社区注册表(例如
@bundui、@magicui)中的组件后,检查新增的非 UI 文件中是否有硬编码的导入路径,例如@/components/ui/...。这些路径可能与项目实际使用的别名不匹配。使用npx shadcn@latest info获取正确的ui别名(例如@workspace/ui/components),并据此重写导入路径。CLI 会重写其自身 UI 文件中的导入路径,但第三方注册表组件可能使用与项目不匹配的默认路径。 - 检查新增组件 — 从任何注册表添加组件或区块后,务必阅读新增文件并确认内容正确。检查是否缺少子组件(例如,有
SelectItem却没有SelectGroup)、是否缺少导入、组合方式是否有误,或是否违反了关键规则。还要将所有图标导入替换为项目上下文中的iconLibrary(例如,如果注册表项使用lucide-react,但项目使用hugeicons,则相应替换导入路径和图标名称)。完成所有问题的修复后再继续。 - 根据上下文选择注册表 — 标准组件使用项目配置的注册表或
@shadcn。如果不同来源会对所需结果造成显著影响,请先询问。 - 切换预设 — 先询问用户选择哪种方式:覆盖、部分更新、合并还是跳过?
- 检查当前预设:
npx shadcn@latest preset resolve。需要结构化值时,使用--json。 - 检查待应用的预设:
npx shadcn@latest preset decode <code>。使用preset url <code>或preset open <code>分享或打开预设构建器。 - 覆盖:
npx shadcn@latest apply <code>。覆盖检测到的组件、字体和 CSS 变量。 - 部分更新:
npx shadcn@latest apply <code> --only theme,font。仅更新所选预设部分,不重新安装 UI 组件。支持的值为theme和font;可以用逗号组合。故意不支持icon,因为更改图标可能需要完整重新安装组件并执行转换。 - 合并:
npx shadcn@latest init --preset <code> --force --no-reinstall,然后运行npx shadcn@latest info列出已安装的组件,再对每个已安装组件使用--dry-run和--diff,逐个进行智能合并。 - 跳过:
npx shadcn@latest init --preset <code> --force --no-reinstall。仅更新配置和 CSS,组件保持不变。 - 重要:务必在用户的项目目录中运行预设命令。
apply仅适用于包含components.json文件的现有项目。CLI 会自动保留components.json中当前使用的基础(base、radix或aria)。如果必须使用临时目录(例如进行--dry-run比较),请显式传入--base <current-base>— 预设代码中不包含基础信息。
- 检查当前预设:
基于 DESIGN.md 构建设计系统
当用户要求根据 DESIGN.md(或品牌规范)构建设计系统时,无论是否指定预设,都请从头到尾遵循 design-system.md:
init --preset <code> --template <vite|next> --name <app> --no-monorepo -y。如果没有预设代码,请选择最接近的风格,并传入--base base --preset <style>。然后运行add --all -y。- 编辑前,先阅读 DESIGN.md 的 frontmatter、注意事项和禁忌,以及已知缺漏。
- 将其中的设计令牌映射到
tailwindCssFile中的 shadcn 变量。固定圆角比例、阴影、字体和type-*工具类。 - 通过编辑组件的
cva变体来调整样式。同步更改控件高度。 - 按照 design-system-page.md 构建设计系统页面:经过对比度检查的颜色角色、字体样例、固定状态矩阵、边缘情况、正确与错误示例对,以及配方。使用
scripts/showcase-coverage.mjs检查覆盖范围。 - 在浏览器中验证:类型检查通过、控制台无错误、页面从顶部开始加载、无水平溢出、深色模式正常。
更新组件
当用户要求从上游更新组件,同时保留本地更改时,请使用 --dry-run 和 --diff 智能合并。切勿手动从 GitHub 获取原始文件——始终使用 CLI。
- 运行
npx shadcn@latest add <component> --dry-run,查看所有可能受影响的文件。 - 对每个文件运行
npx shadcn@latest add <component> --diff <file>,查看上游版本与本地版本之间的更改。 - 根据差异逐个文件决定:
- 无本地更改 → 可以安全覆盖。
- 有本地更改 → 阅读本地文件、分析差异,并应用上游更新,同时保留本地修改。
- 用户说“全部直接更新” → 使用
--overwrite,但须先确认。
- 未经用户明确批准,绝不使用
--overwrite。
快速参考
# Create a new project.
npx shadcn@latest init --name my-app --base base --preset nova
npx shadcn@latest init --name my-app --preset a2r6bw --template vite
# Create a design system app (then follow design-system.md).
npx shadcn@latest init --name my-ds --preset b0 --template vite --no-monorepo -y
npx shadcn@latest init --name my-ds --base base --preset maia --template vite --no-monorepo -y
npx shadcn@latest add --all -y
# Create a monorepo project.
npx shadcn@latest init --name my-app --base base --preset nova --monorepo
npx shadcn@latest init --name my-app --base base --preset nova --template next --monorepo
# Initialize existing project.
npx shadcn@latest init --preset nova
npx shadcn@latest init --defaults # shortcut: --template=next --preset=nova (base style implied)
# Apply a preset to an existing project.
npx shadcn@latest apply a2r6bw
npx shadcn@latest apply a2r6bw --only theme
npx shadcn@latest apply a2r6bw --only font
npx shadcn@latest apply a2r6bw --only theme,font
# Inspect preset codes and project preset state.
npx shadcn@latest preset decode a2r6bw
npx shadcn@latest preset url a2r6bw
npx shadcn@latest preset open a2r6bw
npx shadcn@latest preset resolve
npx shadcn@latest preset resolve --json
# Add components.
npx shadcn@latest add button card dialog
npx shadcn@latest add @magicui/shimmer-button
npx shadcn@latest add owner/repo/item
npx shadcn@latest add --all
# Preview changes before adding/updating.
npx shadcn@latest add button --dry-run
npx shadcn@latest add button --diff button.tsx
npx shadcn@latest add @acme/form --view button.tsx
npx shadcn@latest add owner/repo/item --dry-run
# Search registries.
npx shadcn@latest search @shadcn -q "sidebar"
npx shadcn@latest search @tailark -q "stats"
npx shadcn@latest search owner/repo -q "login"
npx shadcn@latest search # all configured registries
npx shadcn@latest search @shadcn -q "menu" -t ui # filter by item type
# Get component docs and example URLs.
npx shadcn@latest docs button dialog select
# View registry item details (for items not yet installed).
npx shadcn@latest view @shadcn/button
npx shadcn@latest view owner/repo/item
命名预设: nova、vega、maia、lyra、mira、luma、sera、rhea(检查日期:2026-10-01;请参阅当前预设定义)。
模板: next、vite、start、react-router、astro(均支持 --monorepo)以及 laravel(不支持 monorepo)
预设代码: 带版本前缀的 base62 字符串(例如 a2r6bw 或 b0),来自 ui.shadcn.com。
详细参考
- rules/forms.md — FieldGroup、Field、InputGroup、ToggleGroup、FieldSet、验证状态
- rules/composition.md — 组、覆盖层、Card、Tabs、Avatar、Alert、Empty、Toast、Separator、Skeleton、Badge、Button 加载状态
- rules/chat.md — MessageScroller、Message、Bubble、Attachment、Marker、Trace;流式传输、锚定、跳转至最新消息、助手活动行
- rules/icons.md — data-icon、图标尺寸、将图标作为对象传入
- rules/styling.md — 语义颜色、变体、className、间距、尺寸、截断、深色模式、cn()、z-index
- rules/base-vs-radix.md — asChild 与 render、Select、ToggleGroup、Slider、Accordion
- cli.md — 命令、标志、预设、模板
- registry.md — 编写源注册表、
include、项目定义、依赖项、GitHub 注册表规则 - customization.md — 主题设置、CSS 变量、扩展组件
- design-system.md:将 DESIGN.md(无论是否指定预设)转换为主题化应用:风格选择、设计令牌映射、圆角、字体、字体比例、控件高度、展示页陷阱、验证
- design-system-page.md:设计系统页面的内容和构建方法:基础元素、组件层级、固定状态矩阵、对比度配对、边缘情况、正确与错误示例、配方