shadcn-ui
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chineseshadcn/ui
shadcn/ui
Overview
概述
Guides building accessible, customizable UI with shadcn/ui as a code generation platform (not an npm dependency). Components are copied into your project via the CLI and fully owned. Supports Radix UI or Base UI primitives, Tailwind CSS 4 with oklch color tokens, React 19 direct ref patterns, and type-safe form validation with the Field component and Zod.
When to use: Adding shadcn/ui components, theming with CSS variables and oklch colors, building forms with React Hook Form or TanStack Form, creating custom registries, composing accessible component variants, setting up dark mode.
When NOT to use: Non-React frameworks (use shadcn-vue or shadcn-svelte instead), projects using a different component library (MUI, Chakra), projects not using Tailwind CSS.
本指南介绍如何将shadcn/ui作为代码生成平台(而非npm依赖)构建可访问、可定制的UI。组件通过CLI复制到你的项目中,完全由你方所有。支持Radix UI或Base UI原语、带有oklch颜色令牌的Tailwind CSS 4、React 19直接ref模式,以及基于Field组件和Zod的类型安全表单验证。
适用场景: 添加shadcn/ui组件、使用CSS变量和oklch颜色进行主题配置、使用React Hook Form或TanStack Form构建表单、创建自定义注册表、组合可访问的组件变体、设置暗黑模式。
不适用场景: 非React框架(请改用shadcn-vue或shadcn-svelte)、使用其他组件库的项目(如MUI、Chakra)、未使用Tailwind CSS的项目。
Quick Reference
快速参考
| Pattern | API / Approach | Key Points |
|---|---|---|
| Init project | | Auto-detects framework, configures CSS variables |
| Add component | | Copies source code into your project |
| Custom project | | Pick library (Radix/Base UI), style, theme, fonts |
| Check updates | | Shows upstream changes for your components |
| Search registry | | Browse and install from namespaced registries |
| Build registry | | Generate registry JSON from |
| Theme tokens | | oklch color values, dark mode via |
| Dark mode | | Swaps CSS variables under |
| Component variant | | Type-safe variant definitions |
| Polymorphic slot | | Merge props onto child element |
| Direct ref | | |
| Form (current) | | Replaces legacy |
| Form (legacy) | | Still works but no longer actively developed |
| Class merging | | Safely merge Tailwind classes without conflicts |
| 模式 | API / 实现方式 | 关键点 |
|---|---|---|
| 初始化项目 | | 自动检测框架,配置CSS变量 |
| 添加组件 | | 将源代码复制到你的项目中 |
| 自定义项目 | | 选择库(Radix/Base UI)、样式、主题、字体 |
| 检查更新 | | 显示组件的上游变更 |
| 搜索注册表 | | 浏览并安装命名空间注册表中的组件 |
| 构建注册表 | | 从 |
| 主题令牌 | | oklch颜色值,通过 |
| 暗黑模式 | | 在 |
| 组件变体 | | 类型安全的变体定义 |
| 多态插槽 | Radix提供的 | 将属性合并到子元素上 |
| 直接Ref | 将 | React 19中 |
| 表单(当前版本) | | 替代旧版 |
| 表单(旧版) | | 仍可使用但不再积极开发 |
| 类名合并 | 基于 | 安全合并Tailwind类名,避免冲突 |
Common Mistakes
常见错误
| Mistake | Correct Pattern |
|---|---|
| Installing shadcn/ui as an npm dependency | Use the CLI ( |
Using | Pass |
| Using HSL values for color tokens | Use oklch format: |
Wrapping variables with | Reference CSS variables directly: |
Putting tokens directly in | Use |
Using legacy | Use |
| Skipping Zod validation in form components | Always pair forms with Zod schemas for type-safe validation |
Using | Use Radix or Base UI primitives for semantic HTML and accessibility |
Modifying components in | Components live in your source tree -- own and customize directly |
Using old CLI command | Use |
| 错误 | 正确做法 |
|---|---|
| 将shadcn/ui作为npm依赖安装 | 使用CLI( |
在React 19组件中使用 | 直接将 |
| 为颜色令牌使用HSL值 | 使用oklch格式: |
在工具类中用 | 直接引用CSS变量: |
将令牌直接放入 | 在 |
为新表单使用旧版 | 结合React Hook Form或TanStack Form使用 |
| 在表单组件中跳过Zod验证 | 始终将表单与Zod模式配对,实现类型安全验证 |
使用 | 使用Radix或Base UI原语以确保语义化HTML和可访问性 |
修改 | 组件位于你的源代码目录中——可直接拥有并自定义 |
使用旧版CLI命令 | 所有CLI操作均使用 |
Delegation
任务分工
- Component discovery and primitive lookup: Use agent
Explore - Theme migration or multi-file refactoring: Use agent
Task - Design system architecture planning: Use agent
Plan - Registry setup and distribution: Use agent
Task
- 组件发现与原语查找:使用agent
Explore - 主题迁移或多文件重构:使用agent
Task - 设计系统架构规划:使用agent
Plan - 注册表设置与分发:使用agent
Task
References
参考资料
- Theming and dark mode
- Component patterns and variants
- Form patterns with Field, React Hook Form, and Zod
- CLI and registry
- 主题与暗黑模式
- 组件模式与变体
- 基于Field、React Hook Form和Zod的表单模式
- CLI与注册表