shadcn-vue
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chineseshadcn-vue
shadcn-vue
A framework for building ui, components and design systems. Components are added as source code to the user's project via the CLI.
IMPORTANT: Run all CLI commands using the project's package runner:,npx shadcn-vue@latest, orpnpm dlx shadcn-vue@latest— based on the project'sbunx --bun shadcn-vue@latest. Examples below usepackageManagerbut substitute the correct runner for the project.npx shadcn-vue@latest
一个用于构建UI、组件和设计系统的框架。组件通过CLI以源代码形式添加到用户项目中。
重要提示: 使用项目的包管理器运行所有CLI命令:、npx shadcn-vue@latest或pnpm dlx shadcn-vue@latest—— 具体取决于项目的bunx --bun shadcn-vue@latest。以下示例使用packageManager,请根据项目替换为正确的运行器。npx shadcn-vue@latest
Current Project Context
当前项目上下文
json
!`npx shadcn-vue@latest info --json`The JSON above contains the project config and installed components. Use to get documentation and example URLs for any component.
npx shadcn-vue@latest docs <component>json
!`npx shadcn-vue@latest info --json`上述JSON包含项目配置和已安装的组件。使用可获取任意组件的文档及示例URL。
npx shadcn-vue@latest docs <component>Principles
原则
- Use existing components first. Use to check registries before writing custom UI. Check community registries too.
npx shadcn-vue@latest search - Compose, don't reinvent. Settings page = Tabs + Card + form controls. Dashboard = Sidebar + Card + Chart + Table.
- Use built-in variants before custom styles. ,
variant="outline", etc.size="sm" - Use semantic colors. ,
bg-primary— never raw values liketext-muted-foreground.bg-blue-500
- 优先使用现有组件。在编写自定义UI之前,使用检查注册表,同时也要查看社区注册表。
npx shadcn-vue@latest search - 组合而非重造。设置页面 = Tabs + Card + 表单控件;仪表盘 = Sidebar + Card + Chart + Table。
- 优先使用内置变体而非自定义样式。例如、
variant="outline"等。size="sm" - 使用语义化颜色。如、
bg-primary—— 绝不要使用text-muted-foreground这类原始值。bg-blue-500
Critical Rules
核心规则
These rules are always enforced. Each links to a file with Incorrect/Correct code pairs.
这些规则始终强制执行。每条规则都链接到包含错误/正确代码对比的文件。
Styling & Tailwind → styling.md
样式与Tailwind → styling.md
- for layout, not styling. Never override component colors or typography.
class - No or
space-x-*. Usespace-y-*withflex. For vertical stacks,gap-*.flex flex-col gap-* - Use when width and height are equal.
size-*notsize-10.w-10 h-10 - Use shorthand. Not
truncate.overflow-hidden text-ellipsis whitespace-nowrap - No manual color overrides. Use semantic tokens (
dark:,bg-background).text-muted-foreground - Use for conditional classes. Don't write manual template literal ternaries.
cn() - No manual on overlay components. Dialog, Sheet, Popover, etc. handle their own stacking.
z-index
- 用于布局,而非样式。绝不要覆盖组件的颜色或排版。
class - 禁止使用或
space-x-*。使用space-y-*搭配flex。垂直堆叠使用gap-*。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() - 覆盖类组件不要手动设置。Dialog、Sheet、Popover等组件会自行处理层级堆叠。
z-index
Forms & Inputs → forms.md
表单与输入 → forms.md
- Forms use +
FieldGroup. Never use rawFieldwithdivorspace-y-*for form layout.grid gap-* - uses
InputGroup/InputGroupInput. Never rawInputGroupTextarea/InputinsideTextarea.InputGroup - Buttons inside inputs use +
InputGroup.InputGroupAddon - Option sets (2–7 choices) use . Don't loop
ToggleGroupwith manual active state.Button - +
FieldSetfor grouping related checkboxes/radios. Don't use aFieldLegendwith a heading.div - Field validation uses +
data-invalid.aria-invalidondata-invalid,Fieldon the control. For disabled:aria-invalidondata-disabled,Fieldon the control.disabled
- 表单使用+
FieldGroup。绝不要使用原始Field搭配div或space-y-*来布局表单。grid gap-* - 使用
InputGroup/InputGroupInput。绝不要在InputGroupTextarea内使用原始InputGroup/Input。Textarea - 输入框内的按钮使用+
InputGroup。InputGroupAddon - 选项集(2-7个选项)使用。不要循环渲染
ToggleGroup并手动维护激活状态。Button - 相关复选框/单选框分组使用+
FieldSet。不要使用带标题的FieldLegend。div - 字段验证使用+
data-invalid。在aria-invalid上添加Field,在控件上添加data-invalid。禁用状态:在aria-invalid上添加Field,在控件上添加data-disabled。disabled
Component Structure → composition.md
组件结构 → composition.md
- Items always inside their Group. →
SelectItem.SelectGroup→DropdownMenuItem.DropdownMenuGroup→CommandItem.CommandGroup - Dialog, Sheet, and Drawer always need a Title. ,
DialogTitle,SheetTitlerequired for accessibility. UseDrawerTitleif visually hidden.class="sr-only" - Use full Card composition. /
CardHeader/CardTitle/CardDescription/CardContent. Don't dump everything inCardFooter.CardContent - Button has no /
isPending. Compose withisLoading+Spinner+data-icon.disabled - must be inside
TabsTrigger. Never render triggers directly inTabsList.Tabs - always needs
Avatar. For when the image fails to load.AvatarFallback
- 子项必须包含在对应的Group中。→
SelectItem;SelectGroup→DropdownMenuItem;DropdownMenuGroup→CommandItem。CommandGroup - Dialog、Sheet和Drawer必须包含标题。、
DialogTitle、SheetTitle是无障碍访问的必需项。如果需要视觉隐藏,可使用DrawerTitle。class="sr-only" - 使用完整的Card组合。/
CardHeader/CardTitle/CardDescription/CardContent。不要将所有内容都放在CardFooter中。CardContent - Button没有/
isPending属性。通过isLoading+Spinner+data-icon组合实现加载状态。disabled - 必须放在
TabsTrigger内。绝不要直接在TabsList中渲染触发器。Tabs - 必须包含
Avatar。用于图片加载失败时的回显。AvatarFallback
Use Components, Not Custom Markup → composition.md
使用组件而非自定义标记 → composition.md
- Use existing components before custom markup. Check if a component exists before writing a styled .
div - Callouts use . Don't build custom styled divs.
Alert - Empty states use . Don't build custom empty state markup.
Empty - Toast via . Use
vue-sonnerfromtoast().vue-sonner - Use instead of
Separatoror<hr>.<div class="border-t"> - Use for loading placeholders. No custom
Skeletondivs.animate-pulse - Use instead of custom styled spans.
Badge
- 优先使用现有组件而非自定义标记。在编写带样式的之前,先确认是否已有对应组件。
div - 提示框使用。不要构建自定义样式的div。
Alert - 空状态使用。不要构建自定义空状态标记。
Empty - 提示消息使用。调用
vue-sonner中的vue-sonner方法。toast() - **使用**代替
Separator或<hr>。<div class="border-t"> - **使用**作为加载占位符。不要自定义
Skeleton的div。animate-pulse - **使用**代替自定义样式的span。
Badge
Icons → icons.md
图标 → icons.md
- Icons in use
Button.data-iconordata-icon="inline-start"on the icon.data-icon="inline-end" - No sizing classes on icons inside components. Components handle icon sizing via CSS. No or
size-4.w-4 h-4 - Pass icons as objects, not string keys. , not a string lookup.
:icon="CheckIcon"
- Button中的图标使用。在图标上添加
data-icon或data-icon="inline-start"。data-icon="inline-end" - 组件内的图标不要添加尺寸类。组件会通过CSS处理图标尺寸,不要使用或
size-4。w-4 h-4 - 以对象形式传递图标,而非字符串键。使用,不要使用字符串查找。
:icon="CheckIcon"
CLI
CLI
- Apply preset codes directly with the CLI. Use for existing projects, or
npx shadcn-vue@latest apply <code>when initializing.npx shadcn-vue@latest init --preset <code>
- 通过CLI直接应用预设代码。现有项目使用,初始化时使用
npx shadcn-vue@latest apply <code>。npx shadcn-vue@latest init --preset <code>
Key Patterns
核心模式
These are the most common patterns that differentiate correct shadcn-vue code. For edge cases, see the linked rule files above.
html
<!-- Form layout: FieldGroup + Field, not div + Label. -->
<FieldGroup>
<Field>
<FieldLabel for="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 class="flex flex-col gap-4"> <!-- correct -->
<div class="space-y-4"> <!-- wrong -->
<!-- Equal dimensions: size-*, not w-* h-*. -->
<Avatar class="size-10"> <!-- correct -->
<Avatar class="w-10 h-10"> <!-- wrong -->
<!-- Status colors: Badge variants or semantic tokens, not raw colors. -->
<Badge variant="secondary">+20.1%</Badge> <!-- correct -->
<span class="text-emerald-600">+20.1%</span> <!-- wrong -->这些是区分shadcn-vue代码是否规范的最常见模式。如需了解边缘情况,请查看上述链接的规则文件。
html
<!-- 表单布局:使用FieldGroup + Field,而非div + Label。 -->
<FieldGroup>
<Field>
<FieldLabel for="email">邮箱</FieldLabel>
<Input id="email" />
</Field>
</FieldGroup>
<!-- 验证:在Field上添加data-invalid,在控件上添加aria-invalid。 -->
<Field data-invalid>
<FieldLabel>邮箱</FieldLabel>
<Input aria-invalid />
<FieldDescription>邮箱格式无效。</FieldDescription>
</Field>
<!-- 按钮中的图标:使用data-icon,不要添加尺寸类。 -->
<Button>
<SearchIcon data-icon="inline-start" />
搜索
</Button>
<!-- 间距:使用gap-*,而非space-y-*。 -->
<div class="flex flex-col gap-4"> <!-- 正确 -->
<div class="space-y-4"> <!-- 错误 -->
<!-- 等尺寸:使用size-*,而非w-* h-*。 -->
<Avatar class="size-10"> <!-- 正确 -->
<Avatar class="w-10 h-10"> <!-- 错误 -->
<!-- 状态颜色:使用Badge变体或语义化令牌,而非原始颜色。 -->
<Badge variant="secondary">+20.1%</Badge> <!-- 正确 -->
<span class="text-emerald-600">+20.1%</span> <!-- 错误 -->Component Selection
组件选择
| Need | Use |
|---|---|
| Button/action | |
| Form inputs | |
| Toggle between 2–7 options | |
| Data display | |
| Navigation | |
| Overlays | |
| Feedback | |
| Command palette | |
| Charts | |
| Layout | |
| Empty states | |
| Menus | |
| Tooltips/info | |
| 需求 | 使用组件 |
|---|---|
| 按钮/操作 | 带合适变体的 |
| 表单输入 | |
| 在2-7个选项间切换 | |
| 数据展示 | |
| 导航 | |
| 覆盖层 | |
| 反馈 | |
| 命令面板 | |
| 图表 | |
| 布局 | |
| 空状态 | |
| 菜单 | |
| 提示框/信息卡片 | |
Key Fields
核心字段
The injected project context contains these key fields:
- → use the actual alias prefix for imports (e.g.
aliases,@/), never hardcode.~/ - →
tailwindVersionuses"v4"blocks;@theme inlineuses"v3".tailwind.config.js - → the global CSS file where custom CSS variables are defined. Always edit this file, never create a new one.
tailwindCssFile - → component visual treatment (e.g.
style,nova).vega - → primitive library (
base). Affects component APIs and available props.reka - → determines icon imports. Use
iconLibraryforlucide-vue-next,lucidefor@tabler/icons-vue, etc. Never assumetabler.lucide-vue-next - → exact file-system destinations for components, utils, hooks, etc.
resolvedPaths - → routing and file conventions (e.g. Nuxt vs Vite SPA).
framework - → use this for any non-shadcn-vue dependency installs (e.g.
packageManagervspnpm add date-fns).npm install date-fns
See cli.md — command for the full field reference.
info注入的项目上下文包含以下核心字段:
- → 使用实际的别名前缀进行导入(如
aliases、@/),绝不要硬编码。~/ - →
tailwindVersion使用"v4"块;@theme inline使用"v3"。tailwind.config.js - → 定义自定义CSS变量的全局CSS文件。始终编辑此文件,不要创建新文件。
tailwindCssFile - → 组件视觉风格(如
style、nova)。vega - → 基础库(
base)。影响组件API和可用属性。reka - → 决定图标导入方式。
iconLibrary使用lucide,lucide-vue-next使用tabler等。不要默认使用@tabler/icons-vue。lucide-vue-next - → 组件、工具、钩子等的准确文件系统路径。
resolvedPaths - → 路由和文件约定(如Nuxt vs Vite SPA)。
framework - → 安装非shadcn-vue依赖时使用(如
packageManagervspnpm add date-fns)。npm install date-fns
完整字段参考请查看cli.md — 命令。
infoComponent Docs, Examples, and Usage
组件文档、示例与用法
Run to get the URLs for a component's documentation, examples, and API reference. Fetch these URLs to get the actual content.
npx shadcn-vue@latest docs <component>bash
npx shadcn-vue@latest docs button dialog selectWhen creating, fixing, debugging, or using a component, always run and fetch the URLs first. This ensures you're working with the correct API and usage patterns rather than guessing.
npx shadcn-vue@latest docs运行可获取组件的文档、示例及API参考URL。获取这些URL以查看实际内容。
npx shadcn-vue@latest docs <component>bash
npx shadcn-vue@latest docs button dialog select在创建、修复、调试或使用组件时,请始终先运行并获取URL。 这能确保你使用正确的API和用法模式,而非凭猜测操作。
npx shadcn-vue@latest docsWorkflow
工作流程
- Get project context — already injected above. Run again if you need to refresh.
npx shadcn-vue@latest info - Check installed components first — before running , always check the
addlist from project context or list thecomponentsdirectory. Don't import components that haven't been added, and don't re-add ones already installed.resolvedPaths.ui - Find components — .
npx shadcn-vue@latest search - Get docs and examples — run to get URLs, then fetch them. Use
npx shadcn-vue@latest docs <component>to browse registry items you haven't installed. To preview changes to installed components, usenpx shadcn-vue@latest view.npx shadcn-vue@latest add --diff - Install or update — . When updating existing components, use
npx shadcn-vue@latest addand--dry-runto preview changes first (see Updating Components below).--diff - Fix imports in third-party components — After adding components from community registries, check the added non-UI files for hardcoded import paths like . These won't match the project's actual aliases. Use
@/components/ui/...to get the correctnpx shadcn-vue@latest infoalias (e.g.ui) and rewrite the imports accordingly. The CLI rewrites imports for its own UI files, but third-party registry components may use default paths that don't match the project.@workspace/ui/components - Review added components — After adding a component or block from any registry, always read the added files and verify they are correct. Check for missing sub-components (e.g. without
SelectItem), missing imports, incorrect composition, or violations of the Critical Rules. Also replace any icon imports with the project'sSelectGroupfrom the project context (e.g. if the registry item usesiconLibrarybut the project useslucide-vue-next, swap the imports and icon names accordingly). Fix all issues before moving on.hugeicons - Registry must be explicit — When the user asks to add a block or component, do not guess the registry. If no registry is specified (e.g. user says "add a login block" without specifying , etc.), ask which registry to use. Never default to a registry on behalf of the user.
@shadcn - Switching presets — Ask the user first: overwrite, merge, or skip?
- Overwrite: . Overwrites detected components, fonts, and CSS variables.
npx shadcn-vue@latest apply <code> - Merge: , then run
npx shadcn-vue@latest init --preset <code> --force --no-reinstallto list installed components, then for each installed component usenpx shadcn-vue@latest infoand--dry-runto smart merge it individually.--diff - Skip: . Only updates config and CSS, leaves components as-is.
npx shadcn-vue@latest init --preset <code> --force --no-reinstall - Important: Always run preset commands inside the user's project directory. only works in an existing project with a
applyfile. The CLI automatically preserves the current base (components.json) fromreka. If you must use a scratch/temp directory (e.g. forcomponents.jsoncomparisons), pass--dry-runexplicitly — preset codes do not encode the base.--base <current-base>
- Overwrite:
- 获取项目上下文 —— 已在上方注入。如需刷新,可再次运行。
npx shadcn-vue@latest info - 先检查已安装组件 —— 在运行之前,务必查看项目上下文中的
add列表,或列出components目录。不要导入未添加的组件,也不要重复添加已安装的组件。resolvedPaths.ui - 查找组件 —— 使用。
npx shadcn-vue@latest search - 获取文档和示例 —— 运行获取URL,然后获取内容。使用
npx shadcn-vue@latest docs <component>浏览尚未安装的注册表项。如需预览已安装组件的变更,使用npx shadcn-vue@latest view。npx shadcn-vue@latest add --diff - 安装或更新 —— 使用。更新现有组件时,先使用
npx shadcn-vue@latest add和--dry-run预览变更(见下方更新组件)。--diff - 修复第三方组件的导入 —— 从社区注册表添加组件后,检查添加的非UI文件中是否有硬编码的导入路径,如。这些路径可能与项目实际别名不匹配。使用
@/components/ui/...获取正确的npx shadcn-vue@latest info别名(如ui),并相应重写导入。CLI会自动重写自身UI文件的导入,但第三方注册表组件可能使用默认路径,与项目不匹配。@workspace/ui/components - 审核添加的组件 —— 从任何注册表添加组件或模块后,务必阅读添加的文件并验证其正确性。检查是否缺少子组件(如未包含在
SelectItem中)、缺少导入、组合错误或违反核心规则。同时,将图标导入替换为项目上下文的SelectGroup(如注册表项使用iconLibrary但项目使用lucide-vue-next,则相应替换导入和图标名称)。在继续操作前修复所有问题。hugeicons - 注册表必须明确指定 —— 当用户要求添加模块或组件时,不要猜测注册表。如果未指定注册表(如用户说"添加登录模块"但未指定等),请询问使用哪个注册表。绝不要替用户默认选择注册表。
@shadcn - 切换预设 —— 先询问用户:覆盖、合并还是跳过?
- 覆盖:。覆盖检测到的组件、字体和CSS变量。
npx shadcn-vue@latest apply <code> - 合并:,然后运行
npx shadcn-vue@latest init --preset <code> --force --no-reinstall列出已安装组件,再对每个已安装组件使用npx shadcn-vue@latest info和--dry-run进行智能合并。--diff - 跳过:。仅更新配置和CSS,保留组件不变。
npx shadcn-vue@latest init --preset <code> --force --no-reinstall - 重要提示:始终在用户项目目录内运行预设命令。仅在包含
apply文件的现有项目中有效。CLI会自动保留components.json中的当前基础库(components.json)。如果必须使用临时目录(如用于reka对比),请显式传递--dry-run—— 预设代码不包含基础库信息。--base <current-base>
- 覆盖:
Updating Components
更新组件
When the user asks to update a component from upstream while keeping their local changes, use and to intelligently merge. NEVER fetch raw files from GitHub manually — always use the CLI.
--dry-run--diff- Run to see all files that would be affected.
npx shadcn-vue@latest add <component> --dry-run - For each file, run to see what changed upstream vs local.
npx shadcn-vue@latest add <component> --diff <file> - Decide per file based on the diff:
- No local changes → safe to overwrite.
- Has local changes → read the local file, analyze the diff, and apply upstream updates while preserving local modifications.
- User says "just update everything" → use , but confirm first.
--overwrite
- Never use without the user's explicit approval.
--overwrite
当用户要求更新上游组件同时保留本地变更时,使用和进行智能合并。绝不要手动从GitHub获取原始文件——务必使用CLI。
--dry-run--diff- 运行查看所有受影响的文件。
npx shadcn-vue@latest add <component> --dry-run - 对每个文件,运行查看上游变更与本地版本的差异。
npx shadcn-vue@latest add <component> --diff <file> - 根据差异逐个文件决定:
- 无本地变更 → 可安全覆盖。
- 有本地变更 → 读取本地文件,分析差异,应用上游更新同时保留本地修改。
- 用户要求"全部更新" → 使用,但需先确认。
--overwrite
- 未经用户明确批准,绝不要使用。
--overwrite
Quick Reference
快速参考
bash
undefinedbash
undefinedCreate a new project.
创建新项目。
npx shadcn-vue@latest init --name my-app --preset nova
npx shadcn-vue@latest init --name my-app --preset a2r6bw --template vite
npx shadcn-vue@latest init --name my-app --preset nova
npx shadcn-vue@latest init --name my-app --preset a2r6bw --template vite
Initialize existing project.
初始化现有项目。
npx shadcn-vue@latest init --preset nova
npx shadcn-vue@latest init --defaults # shortcut: --template=nuxt --preset=nova (base style implied)
npx shadcn-vue@latest init --preset nova
npx shadcn-vue@latest init --defaults # 快捷方式:--template=nuxt --preset=nova(基础风格默认)
Apply a preset to an existing project.
向现有项目应用预设。
npx shadcn-vue@latest apply a2r6bw
npx shadcn-vue@latest apply a2r6bw
Add components.
添加组件。
npx shadcn-vue@latest add button card dialog
npx shadcn-vue@latest add --all
npx shadcn-vue@latest add button card dialog
npx shadcn-vue@latest add --all
Search registries.
搜索注册表。
npx shadcn-vue@latest search @shadcn -q "sidebar"
npx shadcn-vue@latest search @shadcn -q "sidebar"
Get component docs and example URLs.
获取组件文档和示例URL。
npx shadcn-vue@latest docs button dialog select
npx shadcn-vue@latest docs button dialog select
View registry item details (for items not yet installed).
查看注册表项详情(针对尚未安装的项)。
npx shadcn-vue@latest view @shadcn/button
**Named presets:** `nova`, `vega`, `maia`, `lyra`, `mira`, `luma`
**Templates:** `nuxt`, `vite`, `astro` and `laravel`
**Preset codes:** Version-prefixed base62 strings (e.g. `a2r6bw`), from [shadcn-vue.com](https://shadcn-vue.com).npx shadcn-vue@latest view @shadcn/button
**命名预设:** `nova`、`vega`、`maia`、`lyra`、`mira`、`luma`
**模板:** `nuxt`、`vite`、`astro` 和 `laravel`
**预设代码:** 带版本前缀的base62字符串(如`a2r6bw`),来自[shadcn-vue.com](https://shadcn-vue.com)。Detailed References
详细参考
- rules/forms.md — FieldGroup, Field, InputGroup, ToggleGroup, FieldSet, validation states
- rules/composition.md — Groups, overlays, Card, Tabs, Avatar, Alert, Empty, Toast, Separator, Skeleton, Badge, Button loading
- rules/icons.md — data-icon, icon sizing, passing icons as objects
- rules/styling.md — Semantic colors, variants, class, spacing, size, truncate, dark mode, cn(), z-index
- cli.md — Commands, flags, presets, templates
- customization.md — Theming, CSS variables, extending components
- 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 — 语义化颜色、变体、class、间距、size、truncate、深色模式、cn()、z-index
- cli.md — 命令、标志、预设、模板
- customization.md — 主题定制、CSS变量、组件扩展