mcpField
S
shadcn

shadcn

shadcn开发126.8k271.1k 次安装1 天前更新
代码仓库

关于 shadcn

shadcn/ui 是一个用于构建 UI 组件和设计系统的框架,组件以源代码形式通过命令行添加到项目中。它面向 React 项目开发者,并提供针对 Tailwind 和组件结构的编码规则。

主要功能

  1. 01以源代码形式通过 CLI 添加组件
  2. 02项目上下文信息读取与组件文档查询
  3. 03基于语义颜色的样式规范
  4. 04表单、对话框等组件组合规则
  5. 05图表与聊天界面组件

适用场景

  1. 01在 React 项目中构建表单和仪表盘界面
  2. 02检查并修复组件用法是否符合规范

安装

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

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

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

包含的文件

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

cli.md
customization.md
mcp.md
registry.md
agents/
openai.yml
evals/
evals.json
rules/
base-vs-radix.md
chat.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 遵循项目基础库。 Base UI 项目使用 toast 组件中的 toast。Radix 和 React Aria 项目使用 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},而不是通过字符串查找。
聊天与消息 → chat.md
  • 聊天 UI 组合使用聊天基础组件。 会话使用 MessageScroller,消息行使用 Message,消息气泡使用 Bubble。切勿手写气泡 div 或原始滚动容器。
  • MessageScroller 负责滚动行为。 流式跟随、锚定以及跳转到最新消息(MessageScrollerButton)均内置支持。不要编写 useStickToBottom 或 ResizeObserver Hook。
  • 附件使用 Attachment,系统提示与分隔线使用 Marker。 不要使用 Item 卡片,也不要使用 Separator 加文字标签的组合。
命令行
  • 切勿手动解码预设代码或拼接预设 URL。 使用 npx shadcn@latest preset decode <code>、preset url <code> 或 preset open <code>。如需结合项目进行预设检测,使用 npx shadcn@latest preset resolve。
  • 直接通过命令行应用预设代码。 对已有项目使用 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,并选择合适的 variant
表单输入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
聊天 / 对话 UIMessageScroller、Message、Bubble、Attachment、Marker

关键字段

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

  • 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 → 组件、工具函数、Hooks 等文件的确切文件系统路径。
  • framework → 路由和文件约定(例如 Next.js App Router 或 Vite SPA)。
  • packageManager → 安装任何非 shadcn 依赖时使用(例如 pnpm add date-fns 或 npm install date-fns)。
  • preset → 当前项目已解析的 preset 代码和值。如果只需要 preset 信息,使用 npx shadcn@latest preset resolve --json。

完整字段说明参见 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,然后抓取这些 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、owner/repo 等),请询问使用哪个注册表。切勿代替用户默认选择注册表。
  9. 切换预设:先询问用户:覆盖、部分、合并还是跳过?
    • 检查当前预设: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(base 或 radix)。如果必须使用临时目录(例如进行 --dry-run 对比),请显式传入 --base <current-base>,因为预设代码不编码 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=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 模板: 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、按钮加载状态
  • rules/chat.md — MessageScroller、Message、Bubble、Attachment、Marker;流式传输、锚定、跳转至最新消息
  • 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 变量、扩展组件

评论

支持 Markdown