shadcn
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chineseshadcn/ui Community Best Practices
shadcn/ui 社区最佳实践
Current best-practices guide for shadcn/ui applications. Before applying primitive-specific guidance, inspect whether the checked-in components use Radix UI or Base UI; current shadcn supports both and their composition APIs differ.
当前shadcn/ui应用的最佳实践指南。在应用特定原语的指南之前,请检查已签入的组件是使用Radix UI还是Base UI;当前shadcn同时支持两者,且它们的组合API有所不同。
When to Apply
适用场景
Reference these guidelines when:
- Writing new shadcn/ui components or composing primitives
- Implementing forms with React Hook Form and Zod validation
- Building data tables or handling large dataset displays
- Customizing themes or adding dark mode support
- Reviewing code for accessibility compliance
在以下场景中参考本指南:
- 编写新的shadcn/ui组件或组合原语
- 使用React Hook Form和Zod验证实现表单
- 构建数据表格或处理大型数据集展示
- 自定义主题或添加深色模式支持
- 审查代码的可访问性合规性
Rule Categories by Priority
按优先级划分的规则类别
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Component Architecture | CRITICAL | |
| 2 | Accessibility Preservation | CRITICAL | |
| 3 | Styling & Theming | HIGH | |
| 4 | Form Patterns | HIGH | |
| 5 | Data Display | MEDIUM-HIGH | |
| 6 | Component Composition | MEDIUM | |
| 7 | Performance Optimization | MEDIUM | |
| 8 | State Management | LOW-MEDIUM | |
| 优先级 | 类别 | 影响程度 | 前缀 |
|---|---|---|---|
| 1 | 组件架构 | 关键 | |
| 2 | 可访问性保留 | 关键 | |
| 3 | 样式与主题 | 高 | |
| 4 | 表单模式 | 高 | |
| 5 | 数据展示 | 中高 | |
| 6 | 组件组合 | 中 | |
| 7 | 性能优化 | 中 | |
| 8 | 状态管理 | 中低 | |
Quick Reference
快速参考
1. Component Architecture (CRITICAL)
1. 组件架构(关键)
- - Use the generated primitive's composition API
arch-use-asChild-for-custom-triggers - - Maintain Radix compound component hierarchy
arch-preserve-radix-primitive-structure - - Use Class Variance Authority for type-safe variants
arch-extend-variants-with-cva - - Use cn() utility for safe Tailwind class merging
arch-use-cn-for-class-merging - - Forward refs for form and focus integration
arch-forward-refs-for-composable-components - - Separate base styles from variant-specific styles
arch-isolate-component-variants
- - 使用生成的原语组合API
arch-use-asChild-for-custom-triggers - - 维护Radix复合组件层级
arch-preserve-radix-primitive-structure - - 使用Class Variance Authority实现类型安全的变体
arch-extend-variants-with-cva - - 使用cn()工具安全合并Tailwind类
arch-use-cn-for-class-merging - - 转发ref以实现表单和焦点集成
arch-forward-refs-for-composable-components - - 将基础样式与变体特定样式分离
arch-isolate-component-variants
2. Accessibility Preservation (CRITICAL)
2. 可访问性保留(关键)
- - Keep Radix ARIA attributes intact
ally-preserve-aria-attributes - - Add screen reader labels for icon buttons
ally-provide-sr-only-labels - - Preserve focus trapping in modals
ally-maintain-focus-management - - Keep WAI-ARIA keyboard patterns
ally-preserve-keyboard-navigation - - Maintain WCAG color contrast ratios
ally-ensure-color-contrast
- - 保持Radix ARIA属性完整
ally-preserve-aria-attributes - - 为图标按钮添加屏幕阅读器标签
ally-provide-sr-only-labels - - 保留模态框中的焦点捕获
ally-maintain-focus-management - - 保留WAI-ARIA键盘交互模式
ally-preserve-keyboard-navigation - - 维持WCAG颜色对比度标准
ally-ensure-color-contrast
3. Styling & Theming (HIGH)
3. 样式与主题(高)
- - Use CSS variables for theme colors
style-use-css-variables-for-theming - - Prefer variants and class composition over important overrides
style-avoid-important-overrides - - Define Tailwind v4 theme tokens in CSS
style-use-tailwind-theme-extend - - Use consistent Tailwind spacing scale
style-consistent-spacing-scale - - Apply mobile-first responsive design
style-responsive-design-patterns - - Support dark mode with CSS variables
style-dark-mode-support
- - 使用CSS变量定义主题颜色
style-use-css-variables-for-theming - - 优先使用变体和类组合而非!important覆盖
style-avoid-important-overrides - - 在CSS中定义Tailwind v4主题令牌
style-use-tailwind-theme-extend - - 使用统一的Tailwind间距比例
style-consistent-spacing-scale - - 应用移动端优先的响应式设计
style-responsive-design-patterns - - 使用CSS变量支持深色模式
style-dark-mode-support
4. Form Patterns (HIGH)
4. 表单模式(高)
- - Integrate with React Hook Form
form-use-react-hook-form-integration - - Use Zod for type-safe validation
form-use-zod-for-schema-validation - - Show errors at appropriate times
form-show-validation-errors-correctly - - Debounce async validation calls
form-handle-async-validation - - Reset form state after submission
form-reset-form-state-correctly
- - 集成React Hook Form
form-use-react-hook-form-integration - - 使用Zod实现类型安全的验证
form-use-zod-for-schema-validation - - 在合适时机显示错误信息
form-show-validation-errors-correctly - - 防抖异步验证调用
form-handle-async-validation - - 提交后重置表单状态
form-reset-form-state-correctly
5. Data Display (MEDIUM-HIGH)
5. 数据展示(中高)
- - Use TanStack Table for sorting/filtering
data-use-tanstack-table-for-complex-tables - - Virtualize lists with 100+ items
data-virtualize-large-lists - - Use Skeleton for loading states
data-use-skeleton-loading-states - - Paginate large datasets server-side
data-paginate-server-side - - Provide actionable empty states
data-empty-states-with-guidance
- - 使用TanStack Table实现排序/筛选
data-use-tanstack-table-for-complex-tables - - 对100+条目的列表进行虚拟化处理
data-virtualize-large-lists - - 使用Skeleton实现加载状态
data-use-skeleton-loading-states - - 对大型数据集进行服务端分页
data-paginate-server-side - - 提供可操作的空状态提示
data-empty-states-with-guidance
6. Component Composition (MEDIUM)
6. 组件组合(中)
- - Use compound component patterns
comp-compose-with-compound-components - - Use Drawer on mobile devices
comp-use-drawer-for-mobile-modals - - Create searchable selects with Command
comp-combine-command-with-popover - - Manage nested dialog focus correctly
comp-nest-dialogs-correctly - - Extract reusable form field components
comp-create-reusable-form-fields - - Use slot pattern for flexible content
comp-use-slot-pattern-for-flexibility
- - 使用复合组件模式
comp-compose-with-compound-components - - 在移动端使用Drawer组件
comp-use-drawer-for-mobile-modals - - 结合Command组件创建可搜索的选择器
comp-combine-command-with-popover - - 正确管理嵌套对话框的焦点
comp-nest-dialogs-correctly - - 提取可复用的表单字段组件
comp-create-reusable-form-fields - - 使用插槽模式实现灵活的内容布局
comp-use-slot-pattern-for-flexibility
7. Performance Optimization (MEDIUM)
7. 性能优化(中)
- - Lazy load components over 50KB
perf-lazy-load-heavy-components - - Memoize list items and expensive components
perf-memoize-expensive-renders - - Use direct imports for Lucide icons
perf-optimize-icon-imports - - Isolate form field watching
perf-avoid-unnecessary-rerenders-in-forms - - Debounce search and filter inputs
perf-debounce-search-inputs
- - 懒加载超过50KB的组件
perf-lazy-load-heavy-components - - 对列表项和性能开销大的组件进行记忆化处理
perf-memoize-expensive-renders - - 直接导入Lucide图标
perf-optimize-icon-imports - - 隔离表单字段监听以避免不必要的重渲染
perf-avoid-unnecessary-rerenders-in-forms - - 对搜索和筛选输入进行防抖处理
perf-debounce-search-inputs
8. State Management (LOW-MEDIUM)
8. 状态管理(中低)
- - Use uncontrolled for simple forms
state-prefer-uncontrolled-for-simple-inputs - - Lift state to lowest common ancestor
state-lift-state-to-appropriate-level - - Control dialogs for programmatic access
state-use-controlled-dialog-state - - Keep state close to where it's used
state-colocate-state-with-components
- - 简单表单优先使用非受控组件
state-prefer-uncontrolled-for-simple-inputs - - 将状态提升到合适的层级
state-lift-state-to-appropriate-level - - 控制对话框状态以支持程序化访问
state-use-controlled-dialog-state - - 将状态放在靠近使用它的组件处
state-colocate-state-with-components
How to Use
使用方法
Read individual reference files for detailed explanations and code examples:
阅读单个参考文件获取详细说明和代码示例:
Full Compiled Document
完整编译文档
For a single-file reference containing all rules, see AGENTS.md.
如需包含所有规则的单文件参考,请查看 AGENTS.md。
Reference Files
参考文件
| File | Description |
|---|---|
| AGENTS.md | Complete compiled guide with all rules |
| 文件 | 描述 |
|---|---|
| AGENTS.md | 包含所有规则的完整编译指南 |