shadcn
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chineseshadcn/ui Community Best Practices
shadcn/ui 社区最佳实践
Comprehensive best practices guide for shadcn/ui applications, maintained by the shadcn/ui community. Contains 58 rules across 10 categories, prioritized by impact to guide automated refactoring and code generation.
这是由 shadcn/ui 社区维护的 shadcn/ui 应用综合最佳实践指南,包含10个类别共58条规则,按影响优先级排序,可指导自动化重构和代码生成。
When to Apply
适用场景
Reference these guidelines when:
- Installing and configuring shadcn/ui in a project
- 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
- 编写新的 shadcn/ui 组件或组合原语
- 使用 React Hook Form 和 Zod 实现表单
- 构建数据表格或处理大型数据集展示
- 定制主题或添加深色模式支持
- 评审代码的可访问性合规性
Rule Categories by Priority
按优先级划分的规则类别
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | CLI & Project Setup | CRITICAL | |
| 2 | Component Architecture | CRITICAL | |
| 3 | Accessibility Preservation | CRITICAL | |
| 4 | Styling & Theming | HIGH | |
| 5 | Form Patterns | HIGH | |
| 6 | Data Display | MEDIUM-HIGH | |
| 7 | Layout & Navigation | MEDIUM | |
| 8 | Component Composition | MEDIUM | |
| 9 | Performance Optimization | MEDIUM | |
| 10 | State Management | LOW-MEDIUM | |
| 优先级 | 类别 | 影响程度 | 前缀 |
|---|---|---|---|
| 1 | CLI 与项目配置 | 关键 | |
| 2 | 组件架构 | 关键 | |
| 3 | 可访问性保障 | 关键 | |
| 4 | 样式与主题 | 高 | |
| 5 | 表单模式 | 高 | |
| 6 | 数据展示 | 中高 | |
| 7 | 布局与导航 | 中 | |
| 8 | 组件组合 | 中 | |
| 9 | 性能优化 | 中 | |
| 10 | 状态管理 | 中低 | |
Quick Reference
快速参考
1. CLI & Project Setup (CRITICAL)
1. CLI 与项目配置(关键)
- - Configure components.json before adding components
setup-components-json - - Configure TypeScript path aliases to match components.json
setup-path-aliases - - Create the cn utility before using components
setup-cn-utility - - Use CLI to add components instead of copy-paste
setup-use-cli-not-copy - - Enable CSS variables for consistent theming
setup-css-variables-theme - - Set RSC flag based on framework support
setup-rsc-configuration
- - 添加组件前配置components.json
setup-components-json - - 配置TypeScript路径别名以匹配components.json
setup-path-aliases - - 使用组件前创建cn工具函数
setup-cn-utility - - 使用CLI添加组件而非复制粘贴
setup-use-cli-not-copy - - 启用CSS变量以实现一致的主题定制
setup-css-variables-theme - - 根据框架支持设置RSC标志
setup-rsc-configuration
2. Component Architecture (CRITICAL)
2. 组件架构(关键)
- - Use asChild prop for custom trigger elements
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
- - 使用asChild属性实现自定义触发元素
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
3. Accessibility Preservation (CRITICAL)
3. 可访问性保障(关键)
- - 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 - - Always include DialogTitle for screen readers
ally-dialog-title-required - - Associate labels with form controls
ally-form-field-labels - - Use aria-invalid for form error states
ally-aria-invalid-errors - - Wrap Checkbox with Label for click target
ally-checkbox-label-association - - Preserve focus visible styles for keyboard navigation
ally-focus-visible-styles
- - 保留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 - - 始终为模态框添加DialogTitle以支持屏幕阅读器
ally-dialog-title-required - - 将标签与表单控件关联
ally-form-field-labels - - 使用aria-invalid标识表单错误状态
ally-aria-invalid-errors - - 用Label包裹Checkbox以扩大点击区域
ally-checkbox-label-association - - 保留键盘导航的focus visible样式
ally-focus-visible-styles
4. Styling & Theming (HIGH)
4. 样式与主题(高)
- - Use CSS variables for theme colors
style-use-css-variables-for-theming - - Never use !important for style overrides
style-avoid-important-overrides - - Extend Tailwind theme for design tokens
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 - - 扩展Tailwind主题以实现设计令牌
style-use-tailwind-theme-extend - - 使用统一的Tailwind间距比例
style-consistent-spacing-scale - - 应用移动端优先的响应式设计
style-responsive-design-patterns - - 使用CSS变量支持深色模式
style-dark-mode-support
5. Form Patterns (HIGH)
5. 表单模式(高)
- - 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
6. Data Display (MEDIUM-HIGH)
6. 数据展示(中高)
- - 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
7. Layout & Navigation (MEDIUM)
7. 布局与导航(中)
- - Wrap layout with SidebarProvider
layout-sidebar-provider - - Configure sidebar collapsible behavior
layout-sidebar-collapsible - - Organize sidebar navigation with groups
layout-sidebar-groups - - Use Sheet for mobile navigation overlay
layout-sheet-mobile-nav - - Implement breadcrumbs for deep navigation
layout-breadcrumb-navigation
- - 用SidebarProvider包裹布局
layout-sidebar-provider - - 配置侧边栏的可折叠行为
layout-sidebar-collapsible - - 使用分组组织侧边栏导航
layout-sidebar-groups - - 使用Sheet实现移动端导航弹窗
layout-sheet-mobile-nav - - 实现面包屑导航以支持深层页面跳转
layout-breadcrumb-navigation
8. Component Composition (MEDIUM)
8. 组件组合(中)
- - 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与Popover实现可搜索选择器
comp-combine-command-with-popover - - 正确管理嵌套模态框的焦点
comp-nest-dialogs-correctly - - 提取可复用的表单字段组件
comp-create-reusable-form-fields - - 使用插槽模式实现灵活的内容定制
comp-use-slot-pattern-for-flexibility
9. Performance Optimization (MEDIUM)
9. 性能优化(中)
- - 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 - - 对列表项和性能开销大的组件进行 memoization
perf-memoize-expensive-renders - - 直接导入Lucide图标
perf-optimize-icon-imports - - 隔离表单字段的监听以避免不必要的重渲染
perf-avoid-unnecessary-rerenders-in-forms - - 对搜索和过滤输入进行防抖处理
perf-debounce-search-inputs
10. State Management (LOW-MEDIUM)
10. 状态管理(中低)
- - 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:
- Section definitions - Category structure and impact levels
- Rule template - Template for adding new rules
阅读单个参考文件获取详细说明和代码示例:
- Section definitions - 类别结构与影响级别说明
- Rule template - 添加新规则的模板
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 |
| references/_sections.md | Category definitions and ordering |
| assets/templates/_template.md | Template for new rules |
| metadata.json | Version and reference information |
| 文件 | 描述 |
|---|---|
| AGENTS.md | 包含所有规则的完整编译指南 |
| references/_sections.md | 类别定义与排序说明 |
| assets/templates/_template.md | 新规则模板 |
| metadata.json | 版本与参考信息 |