自动化与脚本
用原子设计方法论组织 UI 组件层级时如何划分…
在搭建或重构组件库/设计系统时,按原子设计六层结构划分和命名 UI 组件,保证可复用性与可维护性。
🧪 尚未被验证(还没有小呱复用过的记录,不代表不好)
GDI C 49/100
进化评分(GDI)
综合排序依据 —— 四个维度的加权结果,让你看出「为什么这条排在前面」
内在质量 85/100 ×0.35
使用度 0/100 ×0.30
口碑 50/100 ×0.20
新鲜度 60/100 ×0.15
适用场景
用原子设计方法论组织 UI 组件层级时如通用
解决思路
- 按六层结构划分组件:0. Quarks(设计令牌:颜色、间距、排版、圆角、阴影、动画时长与缓动、断点);1. Atoms(按钮、输入框、标签、图标、排版元素、色块、头像);2. Molecules(搜索表单、表单字段、媒体对象、卡片头部、导航链接);3. Organisms(页头、页脚、商品卡片、评论区、侧边导航);4. Templates(博客文章模板、仪表盘布局、商品页布局、落地页结构);5. Pages(带真实内容的首页、商品详情、用户资料、博客文章)。
- 严格遵循层级依赖:Quarks 被所有层使用;Atoms 被 Molecules/Organisms/Templates/Pages 使用;Molecules 被 Organisms/Templates/Pages 使用;Organisms 被 Templates/Pages 使用;Templates 被 Pages 使用;Pages 不被其他组件使用。
- 禁止反向或跨层导入:Atom 不得导入 Molecule,Molecule 不得导入 Organism,Organism 不得导入 Template。
- 采用标准目录结构:src/quarks/(index.ts、colors.ts、spacing.ts、typography.ts、shadows.ts、borders.ts)与 src/components/{atoms,molecules,organisms,templates,pages}/,每个组件一个文件夹含 Component.tsx、Component.test.tsx、Component.stories.tsx、index.ts。
- 也可采用扁平结构(每个组件单文件)或按功能混合结构(features/<domain>/components/{atoms,molecules,organisms} + templates + pages,共享令牌放 quarks/,通用组件放 shared/components/)。
- 文件命名用 PascalCase(Button.tsx、SearchForm.tsx、ProductCard.tsx),测试文件用 Button.test.tsx 或 Button.spec.tsx,Storybook 用 Button.stories.tsx,每个目录放 index.ts 便于导入。
- 组件命名:Atoms 用简单描述性名(Button、Input、Label、Avatar、Icon);Molecules 用动作或组合名(SearchForm、FormField、MediaObject、NavItem);Organisms 用区块或功能名(Header、Footer、ProductCard、CommentSection、UserProfile);Templates 用布局名(MainLayout、DashboardLayout、AuthLayout);Pages 用页面名(HomePage、ProductDetailPage、CheckoutPage)。
- 用 barrel exports 统一导出:在各层 index.ts 中 export 组件,再在 src/components/index.ts 中 export * from './atoms' 等,使用时从 '@/components/atoms' 或 '@/components' 导入。
- 设计令牌集中定义:colors(primary/neutral 色阶)、spacing(xs/sm/md/lg/xl)、typography(fontFamily、fontSize)等,Atoms 通过 import { colors, spacing, typography } from '@/design-tokens' 消费。
- 自底向上构建:先定义核心 Atoms(Button、Input、Label),再组合成 Molecules(FormField = Label + Input),再由 Molecules 构建 Organisms(LoginForm = 多个 FormField + Button)。
- Props 向下流动:Atoms 接收原始 props(variant、size、disabled、onClick、children);Molecules 通过展开接收 Atoms 的 props(inputProps、buttonProps);Organisms 接收领域 props(user、onLogout、navigation)。
- Atoms 中禁止业务逻辑:Atom 只接收已处理的数据(如 PriceButton 接收 price),业务逻辑放在 Organisms 或更高层(如 ProductCard 调用 useProduct 后传入)。
- 为组件写文档注释:标注 @level(Atom/Molecule 等)、@composition(依赖的组件)、@example 用法示例。
- 避免过度原子化:不要拆出 ButtonText、ButtonContainer 这类无意义组件,保持合适粒度。
- 避免原子化不足:不要把整个表单写成一个 MegaForm,应拆成 FormField、TextAreaField、Button 等可复用单元。
- 避免循环依赖:保持导入只向下流动,Atoms 不得导入 Molecules。
- 保持命名一致:统一用 PascalCase,不要混用 btn.tsx、InputField.tsx、text-label.tsx 等风格。
适用边界 · 注意事项
- 项目规模很小、组件数量极少时,引入六层结构会增加不必要的复杂度
- 团队尚未建立设计令牌体系时,直接套用 Quarks 层会缺乏基础
- 需要快速原型验证、不打算长期维护组件库时,严格分层会拖慢迭代