关于 shadcn
面向 shadcn/ui 应用开发者的实践指南,涵盖组件架构、无障碍、样式、表单和数据展示。应用特定原语的建议前,指南强调先确认项目组件使用 Radix UI 还是 Base UI。
主要功能
- 018 类优先级规则
- 02Radix UI 与 Base UI 指南
- 03无障碍实践
- 04样式与主题建议
- 05表单和数据展示模式
适用场景
- 01构建或组合 shadcn/ui 组件
- 02审查无障碍与样式
- 03实现表单和数据展示
MCP 相关
指南包含一条规则,建议使用当前 CLI 管理组件生命周期、注册表和 MCP。
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ pymodel/ react-frontend-skills --skill shadcn安装后在 Claude Code 中输入/shadcn确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
references/ally-ensure-color-contrast.mdally-maintain-focus-management.mdally-preserve-aria-attributes.mdally-preserve-keyboard-navigation.mdally-provide-sr-only-labels.mdarch-current-cli-registry.mdarch-extend-variants-with-cva.mdarch-forward-refs-for-composable-components.mdarch-isolate-component-variants.mdarch-preserve-radix-primitive-structure.mdarch-unified-radix-package.mdarch-use-asChild-for-custom-triggers.mdarch-use-cn-for-class-merging.mdcomp-combine-command-with-popover.mdcomp-compose-with-compound-components.mdcomp-create-reusable-form-fields.mdcomp-nest-dialogs-correctly.mdcomp-use-drawer-for-mobile-modals.mdcomp-use-slot-pattern-for-flexibility.mddata-empty-states-with-guidance.mddata-paginate-server-side.mddata-use-skeleton-loading-states.mddata-use-tanstack-table-for-complex-tables.mddata-virtualize-large-lists.mdform-handle-async-validation.mdform-reset-form-state-correctly.mdform-show-validation-errors-correctly.mdform-use-react-hook-form-integration.mdform-use-zod-for-schema-validation.mdperf-avoid-unnecessary-rerenders-in-forms.md详细介绍
shadcn/ui 社区最佳实践
面向 shadcn/ui 应用的最新最佳实践指南。在应用针对特定基元的指导前,请检查已提交到代码库的组件使用的是 Radix UI 还是 Base UI;当前 shadcn 同时支持两者(新项目默认使用 Base UI),且它们的组合 API 有所不同。组件的安装和升级需通过当前 CLI(v4)进行,它可以解析带命名空间的注册表。
适用场景
在以下情况下参考这些指南:
- 编写新的 shadcn/ui 组件或组合基元
- 使用 React Hook Form 和 Zod 验证实现表单
- 构建数据表格或处理大型数据集的展示
- 自定义主题或添加深色模式支持
- 审查代码是否符合无障碍要求
按优先级划分的规则类别
| 优先级 | 类别 | 影响 | 前缀 |
|---|---|---|---|
| 1 | 组件架构 | 严重 | arch- |
| 2 | 保持无障碍性 | 严重 | ally- |
| 3 | 样式与主题 | 高 | style- |
| 4 | 表单模式 | 高 | form- |
| 5 | 数据展示 | 中高 | data- |
| 6 | 组件组合 | 中 | comp- |
| 7 | 性能优化 | 中 | perf- |
| 8 | 状态管理 | 低至中 | state- |
快速参考
1. 组件架构(严重)
arch-use-asChild-for-custom-triggers- 使用生成的基元组合 APIarch-current-cli-registry- 使用当前 CLI 管理组件生命周期(v4、注册表、MCP)arch-unified-radix-package- 从统一的 radix-ui 软件包导入基元arch-preserve-radix-primitive-structure- 保持 Radix 复合组件层级结构arch-extend-variants-with-cva- 使用 Class Variance Authority 实现类型安全的变体arch-use-cn-for-class-merging- 使用 cn() 工具安全合并 Tailwind 类arch-forward-refs-for-composable-components- 转发 ref,以便集成表单和焦点功能arch-isolate-component-variants- 将基础样式与特定变体样式分开
2. 保持无障碍性(严重)
ally-preserve-aria-attributes- 保留 Radix ARIA 属性ally-provide-sr-only-labels- 为图标按钮添加屏幕阅读器标签ally-maintain-focus-management- 保留模态框中的焦点陷阱ally-preserve-keyboard-navigation- 保留 WAI-ARIA 键盘交互模式ally-ensure-color-contrast- 保持符合 WCAG 的颜色对比度
3. 样式与主题(高)
style-use-css-variables-for-theming- 使用 CSS 变量设置主题颜色style-avoid-important-overrides- 优先使用变体和类组合,而非 important 覆盖style-use-tailwind-theme-extend- 在 CSS 中定义 Tailwind v4 主题令牌style-consistent-spacing-scale- 使用一致的 Tailwind 间距比例style-responsive-design-patterns- 采用移动优先的响应式设计style-dark-mode-support- 使用 CSS 变量支持深色模式
4. 表单模式(高)
form-use-react-hook-form-integration- 与 React Hook Form 集成form-use-zod-for-schema-validation- 使用 Zod 进行类型安全的验证form-show-validation-errors-correctly- 在适当的时机显示错误form-handle-async-validation- 对异步验证调用进行防抖处理form-reset-form-state-correctly- 提交后重置表单状态
5. 数据展示(中高)
data-use-tanstack-table-for-complex-tables- 使用 TanStack Table 进行排序和筛选data-virtualize-large-lists- 对包含 100+ 项的列表进行虚拟化data-use-skeleton-loading-states- 使用 Skeleton 表示加载状态data-paginate-server-side- 在服务端对大型数据集进行分页data-empty-states-with-guidance- 提供有操作指引的空状态
6. 组件组合(中)
comp-compose-with-compound-components- 使用复合组件模式comp-use-drawer-for-mobile-modals- 在移动设备上使用 Drawercomp-combine-command-with-popover- 使用 Command 创建可搜索的选择器comp-nest-dialogs-correctly- 正确管理嵌套对话框的焦点comp-create-reusable-form-fields- 提取可复用的表单字段组件comp-use-slot-pattern-for-flexibility- 使用 slot 模式实现灵活的内容组合
7. 性能优化(中)
perf-lazy-load-heavy-components- 延迟加载超过 50KB 的组件perf-memoize-expensive-renders- 对列表项和开销较大的组件进行记忆化perf-optimize-icon-imports- 直接导入 Lucide 图标perf-avoid-unnecessary-rerenders-in-forms- 隔离对表单字段的监听perf-debounce-search-inputs- 对搜索和筛选输入进行防抖处理
8. 状态管理(低中)
state-prefer-uncontrolled-for-simple-inputs- 简单表单使用非受控模式state-lift-state-to-appropriate-level- 将状态提升到最低公共祖先state-use-controlled-dialog-state- 通过受控状态实现对话框的编程式访问state-colocate-state-with-components- 将状态放在使用它的组件附近
使用方法
先在项目根目录运行:
npx -y @pymodel/react-frontend-skills detect # installed vs. targeted versions
对于上述与任务相关的规则,请阅读 references/<rule-id>.md:了解其重要性,以及错误与正确的代码示例。
评论
支持 Markdown