mcpField
S
shadcn

shadcn

github.com/julianromli设计与 UI21365 次安装4 天前更新
代码仓库

关于 shadcn

shadcn 是面向 UI 与设计系统开发的组件技能,指导在项目中查找、组合和添加组件源码。它强调遵循项目配置、组件文档及一致的结构与样式规则。

主要功能

  1. 01通过 CLI 查询并添加组件
  2. 02读取项目配置与已安装组件
  3. 03按文档核对组件用法
  4. 04遵循表单与组件组合规则
  5. 05使用语义化样式与图标配置

适用场景

  1. 01在应用中搭建设置页和仪表盘
  2. 02为项目添加或更新 UI 组件
  3. 03检查组件的可访问性与结构
  4. 04适配项目框架和图标配置

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/julianromli/ai-skills --skill shadcn

安装后在 Claude Code 中输入/shadcn确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

cli.md
customization.md
mcp.md
agents/
openai.yml
evals/
evals.json
rules/
base-vs-radix.md
composition.md
forms.md
icons.md
styling.md

详细介绍

shadcn/ui

一个用于构建界面、组件和设计系统的框架。组件通过 CLI 以源代码的形式添加到用户的项目中。

重要: 所有 CLI 命令都需通过项目的包运行器执行:npx shadcn@latest、pnpm dlx shadcn@latest 或 bunx --bun shadcn@latest,具体根据项目的 packageManager 选择。下文示例均使用 npx shadcn@latest,请替换为该项目对应的运行器。

当前项目上下文

!`npx shadcn@latest info --json`

上方 JSON 包含项目配置和已安装的组件。使用 npx shadcn@latest docs <component> 获取任意组件的文档和示例 URL。

原则

  1. 优先使用现有组件。 在编写自定义 UI 之前,使用 npx shadcn@latest search 检查各个注册表。也要检查社区注册表。
  2. 组合,而非重新发明。 设置页面 = Tabs + Card + 表单控件。仪表盘 = Sidebar + Card + Chart + Table。
  3. 先使用内置变体,再编写自定义样式。 如 variant="outline"、size="sm" 等。
  4. 使用语义化颜色。 如 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: 颜色。 使用语义化 token(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
  • 条目必须位于其分组内。 SelectItem → SelectGroup。DropdownMenuItem → DropdownMenuGroup。CommandItem → CommandGroup。
  • 自定义触发器使用 asChild(radix)或 render(base)。 请检查 npx shadcn@latest info 输出的 base 字段。→ base-vs-radix.md
  • Dialog、Sheet 和 Drawer 必须包含标题。 出于无障碍考虑,必须使用 DialogTitle、SheetTitle、DrawerTitle。若需视觉隐藏,使用 className="sr-only"。
  • 使用完整的 Card 组合。 CardHeader/CardTitle/CardDescription/CardContent/CardFooter。不要把所有内容都塞进 CardContent。
  • Button 没有 isPending/isLoading。 请组合 Spinner + data-icon + disabled 实现。
  • TabsTrigger 必须位于 TabsList 内。 切勿直接在 Tabs 中渲染触发器。
  • Avatar 必须包含 AvatarFallback。 用于图片加载失败时的回退显示。
使用组件,而非自定义标记 → composition.md
  • 在自定义标记之前先使用已有组件。 在编写带样式的 div 之前,先检查是否已有对应组件。
  • 提示框使用 Alert。 不要构建自定义样式的 div。
  • 空状态使用 Empty。 不要构建自定义的空状态标记。
  • Toast 通过 sonner 实现。 使用来自 sonner 的 toast()。
  • 使用 Separator,而非 <hr> 或 <div className="border-t">。
  • 使用 Skeleton 作为加载占位符。不要使用自定义的 animate-pulse div。
  • 使用 Badge,而非自定义样式的 span。
图标 → icons.md
  • Button 中的图标使用 data-icon。 在图标上使用 data-icon="inline-start" 或 data-icon="inline-end"。
  • 组件内部的图标不要设置尺寸类。 组件通过 CSS 处理图标尺寸。不要使用 size-4 或 w-4 h-4。
  • 以对象形式传递图标,而非字符串键。 使用 icon={CheckIcon},而非字符串查找。
命令行
  • 绝不要手动解码或获取预设代码。 直接将它们传给 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

组件选择

需求使用
按钮/操作使用带合适 variant 的 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(确认)
反馈sonner(toast)、Alert、Progress、Skeleton、Spinner
命令面板Dialog 内部的 Command
图表Chart(封装 Recharts)
布局Card、Separator、Resizable、ScrollArea、Accordion、Collapsible
空状态Empty
菜单DropdownMenu、ContextMenu、Menubar
工具提示/信息Tooltip、HoverCard、Popover

关键字段

注入的项目上下文中包含以下关键字段:

  • 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)。影响组件 API 和可用 props。
  • iconLibrary → 决定图标导入方式。lucide 使用 lucide-react,tabler 使用 @tabler/icons-react,以此类推。切勿假定为 lucide-react。
  • resolvedPaths → 组件、utils、hooks 等在文件系统中的确切目标路径。
  • framework → 路由和文件约定(例如 Next.js App Router 与 Vite SPA)。
  • packageManager → 安装任何非 shadcn 依赖时使用此项(例如 pnpm add date-fns 与 npm install date-fns)。

完整字段参考请参见 cli.md — info 命令。

组件文档、示例与用法

运行 npx shadcn@latest docs <component> 可获取组件文档、示例和 API 参考的 URL。抓取这些 URL 以获取实际内容。

npx shadcn@latest docs button dialog select

在创建、修复、调试或使用组件时,务必先运行 npx shadcn@latest docs 并抓取相关 URL。 这能确保你使用正确的 API 和用法模式,而非凭空猜测。

工作流

  1. 获取项目上下文 — 已在上方注入。需要刷新时,再次运行 npx shadcn@latest info。
  2. 先检查已安装的组件 — 运行 add 前,务必先检查项目上下文中的 components 列表,或列出 resolvedPaths.ui 目录。不要导入尚未添加的组件,也不要重复添加已安装的组件。
  3. 查找组件 — 运行 npx shadcn@latest search。
  4. 获取文档和示例 — 运行 npx shadcn@latest docs <component> 获取 URL,然后抓取这些页面。使用 npx shadcn@latest view 浏览尚未安装的注册表条目。要预览已安装组件的变更,请使用 npx shadcn@latest add --diff。
  5. 安装或更新 — 运行 npx shadcn@latest add。更新现有组件时,先使用 --dry-run 和 --diff 预览变更(请参阅下文的更新组件)。
  6. 修正第三方组件中的导入路径 — 从社区注册表(例如 @bundui、@magicui)添加组件后,检查新增的非 UI 文件中是否存在硬编码的导入路径,例如 @/components/ui/...。这些路径可能与项目的实际别名不匹配。使用 npx shadcn@latest info 获取正确的 ui 别名(例如 @workspace/ui/components),并相应地改写导入路径。CLI 会重写其自身 UI 文件中的导入路径,但第三方注册表组件可能使用与项目不匹配的默认路径。
  7. 检查新增组件 — 从任何注册表添加组件或区块后,务必阅读新增文件并确认其正确无误。检查是否缺少子组件(例如,有 SelectItem 却没有 SelectGroup)、是否缺少导入、组合是否正确,以及是否违反关键规则。还要将所有图标导入替换为项目上下文中的 iconLibrary(例如,如果注册表条目使用 lucide-react,而项目使用 hugeicons,就相应替换导入和图标名称)。解决所有问题后再继续。
  8. 必须明确指定注册表 — 用户要求添加区块或组件时,不要猜测注册表。如果用户未指定注册表(例如只说“添加一个登录区块”,未指定 @shadcn、@tailark 等),请询问要使用哪个注册表。绝不要擅自为用户选择默认注册表。
  9. 切换预设 — 先询问用户选择:重新安装、合并还是跳过?
    • 重新安装:npx shadcn@latest init --preset <code> --force --reinstall。覆盖所有组件。
    • 合并:运行 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,组件保持不变。
    • 重要:务必在用户的项目目录中运行预设命令。CLI 会根据 components.json 自动保留当前基底(base 或 radix)。如果必须使用临时目录(例如进行 --dry-run 比较),请明确传入 --base <current-base> — 预设代码不包含基底信息。

更新组件

当用户要求从上游更新组件,同时保留本地更改时,请使用 --dry-run 和 --diff 智能合并。绝不要手动从 GitHub 获取原始文件——始终使用 CLI。

  1. 运行 npx shadcn@latest add <component> --dry-run,查看所有可能受影响的文件。
  2. 对每个文件,运行 npx shadcn@latest add <component> --diff <file>,查看上游版本与本地版本之间的差异。
  3. 根据差异逐个文件决定:
    • 没有本地更改 → 可以安全覆盖。
    • 存在本地更改 → 阅读本地文件,分析差异,并在保留本地修改的同时应用上游更新。
    • 用户说“全部更新就好” → 使用 --overwrite,但请先确认。
  4. 未经用户明确批准,绝不要使用 --overwrite。

快速参考

# Create a new project.
npx shadcn@latest init --name my-app --preset base-nova
npx shadcn@latest init --name my-app --preset a2r6bw --template vite

# Create a monorepo project.
npx shadcn@latest init --name my-app --preset base-nova --monorepo
npx shadcn@latest init --name my-app --preset base-nova --template next --monorepo

# Initialize existing project.
npx shadcn@latest init --preset base-nova
npx shadcn@latest init --defaults  # shortcut: --template=next --preset=base-nova

# Add components.
npx shadcn@latest add button card dialog
npx shadcn@latest add @magicui/shimmer-button
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

# Search registries.
npx shadcn@latest search @shadcn -q "sidebar"
npx shadcn@latest search @tailark -q "stats"

# 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

命名预设: base-nova、radix-nova 模板: next、vite、start、react-router、astro(均支持 --monorepo)以及 laravel(不支持 monorepo) 预设代码: 以 a 开头的 Base62 字符串(例如 a2r6bw),来自 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/icons.md — data-icon、图标尺寸、将图标作为对象传递
  • rules/styling.md — 语义颜色、变体、className、间距、尺寸、文本截断、深色模式、cn()、z-index
  • rules/base-vs-radix.md — asChild 与 render、Select、ToggleGroup、Slider、Accordion
  • cli.md — 命令、标志、预设、模板
  • customization.md — 主题设置、CSS 变量、扩展组件

评论

支持 Markdown