rails-design-system
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCommunity Ruby on Rails Design System Best Practices
社区版Ruby on Rails设计系统最佳实践
Comprehensive design system guide for Ruby on Rails applications, maintained by Community. Contains 51 rules across 9 categories, prioritized by impact to guide automated refactoring and code generation. Covers the full Rails frontend stack: Turbo (Drive, Frames, Streams), Stimulus, ERB partials, design tokens, form builders, and view helpers. Complements (controllers, models, queries) and (CSS patterns) by covering the systematic UI component architecture layer.
rails-devtailwind这是一份由社区维护的Ruby on Rails应用设计系统综合指南,包含9个类别下的51条规则,按影响程度排序,可指导自动化重构和代码生成。覆盖完整的Rails前端技术栈:Turbo(Drive、Frames、Streams)、Stimulus、ERB局部视图、设计令牌、表单构建器和视图助手。补充了(控制器、模型、查询)和(CSS模式)的内容,聚焦系统化的UI组件架构层。
rails-devtailwindWhen to Apply
适用场景
Reference these guidelines when:
- Deciding whether to extract a partial, component, or helper
- Defining design tokens with Tailwind CSS
@theme - Creating or refactoring ERB partials with explicit locals
- Decomposing pages into Turbo Frames for targeted updates
- Using Turbo Streams for multi-element CRUD updates
- Coordinating Turbo navigation with Stimulus controllers
- Building ViewComponent or Phlex components for complex UI
- Implementing a custom FormBuilder for consistent forms
- Writing view helpers for badges, icons, and conditional classes
- Adding Stimulus controllers for interactive behaviors
- Managing JavaScript dependencies with Import Maps
- Auditing the codebase for UI duplication and naming drift
在以下场景中参考本指南:
- 决定是否提取局部视图、组件或助手
- 使用Tailwind CSS 定义设计令牌
@theme - 创建或重构带有显式局部变量的ERB局部视图
- 将页面拆分为Turbo Frames以实现定向更新
- 使用Turbo Streams实现多元素CRUD更新
- 协调Turbo导航与Stimulus控制器
- 为复杂UI构建ViewComponent或Phlex组件
- 实现自定义FormBuilder以保证表单一致性
- 编写用于徽章、图标和条件类的视图助手
- 添加Stimulus控制器实现交互行为
- 使用Import Maps管理JavaScript依赖
- 审计代码库以检查UI重复和命名偏差
Rule Categories by Priority
按优先级划分的规则类别
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Design Decisions | CRITICAL | |
| 2 | Design Tokens | CRITICAL | |
| 3 | Turbo Integration | HIGH | |
| 4 | Partial Patterns | HIGH | |
| 5 | Component Architecture | HIGH | |
| 6 | Form System | MEDIUM-HIGH | |
| 7 | Helper Patterns | MEDIUM | |
| 8 | Stimulus Behaviors | MEDIUM | |
| 9 | Consistency & Organization | LOW-MEDIUM | |
| 优先级 | 类别 | 影响程度 | 前缀 |
|---|---|---|---|
| 1 | 设计决策 | 严重 | |
| 2 | 设计令牌 | 严重 | |
| 3 | Turbo集成 | 高 | |
| 4 | 局部视图模式 | 高 | |
| 5 | 组件架构 | 高 | |
| 6 | 表单系统 | 中高 | |
| 7 | 助手模式 | 中 | |
| 8 | Stimulus行为 | 中 | |
| 9 | 一致性与组织 | 中低 | |
Quick Reference
快速参考
1. Design Decisions (CRITICAL)
1. 设计决策(严重)
- - Extract only after a pattern appears in 3+ places
decide-three-uses-rule - - Choose partials for simple reuse, components for complex logic
decide-partial-vs-component - - Use helpers for tiny HTML fragments, partials for layout blocks
decide-helper-vs-partial - - Prove patterns in production before abstracting
decide-prove-then-extract - - Avoid thin wrappers that add indirection without value
decide-avoid-wrapper-components - - Scope the design system to what the app actually needs
decide-design-system-scope
- - 仅当某一模式出现3次及以上时才进行提取
decide-three-uses-rule - - 简单复用选择局部视图,复杂逻辑选择组件
decide-partial-vs-component - - 小型HTML片段使用助手,布局块使用局部视图
decide-helper-vs-partial - - 在生产环境验证模式有效性后再进行抽象
decide-prove-then-extract - - 避免使用仅增加间接性而无实际价值的轻量包装组件
decide-avoid-wrapper-components - - 设计系统的范围应与应用实际需求匹配
decide-design-system-scope
2. Design Tokens (CRITICAL)
2. 设计令牌(严重)
- - Define tokens with Tailwind CSS @theme directive
token-tailwind-theme - - Name colors by purpose, not appearance
token-semantic-color-names - - Use a constrained spacing scale for consistent layout
token-spacing-scale - - Define a typography scale for headings, body, and UI text
token-typography-scale - - Create component-level tokens for repeated patterns
token-component-tokens - - Share token values between CSS and Ruby when needed
token-share-tokens-with-ruby
- - 使用Tailwind CSS @theme指令定义令牌
token-tailwind-theme - - 按用途而非外观为颜色命名
token-semantic-color-names - - 使用受限的间距比例保证布局一致性
token-spacing-scale - - 为标题、正文和UI文本定义排版比例
token-typography-scale - - 为重复模式创建组件级令牌
token-component-tokens - - 必要时在CSS与Ruby之间共享令牌值
token-share-tokens-with-ruby
3. Turbo Integration (HIGH)
3. Turbo集成(高)
- - Let Turbo Drive handle navigation by default
turbo-drive-defaults - - Decompose pages into Turbo Frames for targeted updates
turbo-frame-decompose - - Name Turbo Frames with dom_id conventions
turbo-frame-naming - - Choose Turbo Frames vs Turbo Streams by scope of change
turbo-frame-vs-stream - - Use Turbo Streams for multi-element page updates
turbo-stream-crud - - Coordinate Turbo and Stimulus without conflicts
turbo-stimulus-coordination
- - 默认由Turbo Drive处理导航
turbo-drive-defaults - - 将页面拆分为Turbo Frames以实现定向更新
turbo-frame-decompose - - 遵循dom_id约定为Turbo Frames命名
turbo-frame-naming - - 根据变更范围选择Turbo Frames或Turbo Streams
turbo-frame-vs-stream - - 使用Turbo Streams实现多元素页面更新
turbo-stream-crud - - 协调Turbo与Stimulus避免冲突
turbo-stimulus-coordination
4. Partial Patterns (HIGH)
4. 局部视图模式(高)
- - Always pass locals explicitly to partials
partial-explicit-locals - - Use presenter objects to encapsulate view logic
partial-presenter-objects - - Name partials by what they render, prefixed with underscore
partial-naming-conventions - - Use yield blocks for flexible partial layouts
partial-yield-blocks - - Use collection rendering with spacer templates
partial-collection-with-spacer - - Place cross-controller partials in app/views/shared
partial-shared-directory
- - 始终显式向局部视图传递局部变量
partial-explicit-locals - - 使用展示器对象封装视图逻辑
partial-presenter-objects - - 局部视图按渲染内容命名,前缀加下划线
partial-naming-conventions - - 使用yield块实现灵活的局部视图布局
partial-yield-blocks - - 使用带间隔模板的集合渲染方式
partial-collection-with-spacer - - 将跨控制器的局部视图放在app/views/shared目录下
partial-shared-directory
5. Component Architecture (HIGH)
5. 组件架构(高)
- - Use components when partials outgrow simple rendering
comp-when-to-use - - Define explicit typed arguments for every component
comp-explicit-args - - Use slots for caller-provided markup blocks
comp-slots-for-markup - - Test components by asserting on rendered HTML
comp-test-rendered-output
- - 当局部视图超出简单渲染需求时使用组件
comp-when-to-use - - 为每个组件定义显式的类型化参数
comp-explicit-args - - 使用插槽处理调用者提供的标记块
comp-slots-for-markup - - 通过断言渲染后的HTML来测试组件
comp-test-rendered-output
6. Form System (MEDIUM-HIGH)
6. 表单系统(中高)
- - Create a custom FormBuilder for consistent form rendering
form-custom-builder - - Set the custom builder as the application default
form-set-default-builder - - Display field errors inline with consistent markup
form-error-display - - Generate accessible labels and ARIA attributes automatically
form-accessible-labels - - Wrap label + input + error in a consistent group element
form-group-wrapper - - Standardize submit buttons through the form builder
form-button-consistency
- - 创建自定义FormBuilder保证表单渲染一致性
form-custom-builder - - 将自定义构建器设置为应用默认值
form-set-default-builder - - 使用一致的标记内联显示字段错误
form-error-display - - 自动生成可访问标签和ARIA属性
form-accessible-labels - - 将标签+输入框+错误信息包裹在统一的组元素中
form-group-wrapper - - 通过表单构建器标准化提交按钮
form-button-consistency
7. Helper Patterns (MEDIUM)
7. 助手模式(中)
- - Use tag helpers for small generated HTML fragments
helper-tag-helpers - - Use class_names for conditional CSS classes
helper-conditional-classes - - Create an icon helper for consistent icon rendering
helper-icon-helper - - Build a badge helper for status indicators
helper-badge-pattern - - Scope helpers to specific domains, not generic utilities
helper-scope-to-domain
- - 使用标签助手生成小型HTML片段
helper-tag-helpers - - 使用class_names处理条件CSS类
helper-conditional-classes - - 创建图标助手保证图标渲染一致性
helper-icon-helper - - 构建徽章助手用于状态指示
helper-badge-pattern - - 助手应限定在特定领域,而非通用工具
helper-scope-to-domain
8. Stimulus Behaviors (MEDIUM)
8. Stimulus行为(中)
- - Write general-purpose controllers, not one-off scripts
stim-general-purpose - - Configure behavior through data attributes, not JavaScript
stim-data-attribute-config - - Keep controllers small and single-responsibility
stim-small-controllers - - Compose multiple controllers on one element
stim-composable-controllers - - Use outlets for cross-controller communication
stim-use-outlets - - Use stimulus-components before writing custom controllers
stim-leverage-library
- - 编写通用控制器,而非一次性脚本
stim-general-purpose - - 通过数据属性而非JavaScript配置行为
stim-data-attribute-config - - 保持控制器小巧,遵循单一职责原则
stim-small-controllers - - 在单个元素上组合多个控制器
stim-composable-controllers - - 使用outlets实现控制器间通信
stim-use-outlets - - 优先使用stimulus-components而非编写自定义控制器
stim-leverage-library
9. Consistency & Organization (LOW-MEDIUM)
9. 一致性与组织(中低)
- - Follow consistent naming across partials, components, and helpers
org-naming-conventions - - Organize design system files in predictable locations
org-file-structure - - Periodically audit views for duplicated patterns
org-deduplication-audit - - Use Import Maps for zero-build JavaScript delivery
org-import-maps - - Preview components with Lookbook in development
org-preview-with-lookbook - - Document design system decisions in ADRs
org-document-design-decisions
- - 在局部视图、组件和助手间遵循一致的命名规范
org-naming-conventions - - 将设计系统文件组织在可预测的位置
org-file-structure - - 定期审计视图以检查重复模式
org-deduplication-audit - - 使用Import Maps实现零构建JavaScript交付
org-import-maps - - 在开发环境中使用Lookbook预览组件
org-preview-with-lookbook - - 在ADR中记录设计系统决策
org-document-design-decisions
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
阅读单个参考文件获取详细说明和代码示例:
- 章节定义 - 类别结构和影响级别
- 规则模板 - 添加新规则的模板
Reference Files
参考文件
| File | Description |
|---|---|
| references/_sections.md | Category definitions and ordering |
| assets/templates/_template.md | Template for new rules |
| metadata.json | Version and reference information |
| 文件 | 描述 |
|---|---|
| references/_sections.md | 类别定义和排序 |
| assets/templates/_template.md | 新规则模板 |
| metadata.json | 版本和参考信息 |