frontend-ui-design
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseFrontend UI Design
前端UI设计
Overview
概述
Guide the design and implementation of frontend user interfaces with consistent architecture, accessibility, responsive behavior, and performance. This skill covers component patterns, design system integration, state management selection, and WCAG compliance — producing components that are testable, accessible, and performant.
Announce at start: "I'm using the frontend-ui-design skill to design the UI."
指导前端用户界面的设计与实现,确保架构统一、可访问性达标、响应式表现正常且性能优异。本技能覆盖组件模式、设计系统集成、状态管理选型以及WCAG合规要求,能够产出可测试、可访问且高性能的组件。
开头告知: "我正在使用frontend-ui-design技能进行UI设计。"
Phase 1: Discovery
阶段1:需求调研
Ask these questions to understand the UI requirements:
| # | Question | What It Determines |
|---|---|---|
| 1 | What component or page are we building? | Scope and complexity |
| 2 | What framework/library? (React, Vue, Svelte, etc.) | Code patterns |
| 3 | Is there an existing design system or component library? | Constraints |
| 4 | What devices must be supported? (mobile, tablet, desktop) | Responsive strategy |
| 5 | Accessibility requirements? (WCAG level) | A11y standards |
| 6 | What data does this component need? | State management approach |
STOP after discovery — present a summary of constraints and approach before designing.
询问以下问题明确UI需求:
| # | 问题 | 作用 |
|---|---|---|
| 1 | 我们要构建什么组件或页面? | 明确范围和复杂度 |
| 2 | 使用什么框架/库?(React, Vue, Svelte等) | 确定代码模式 |
| 3 | 是否有现成的设计系统或组件库? | 明确约束条件 |
| 4 | 需要支持哪些设备?(移动端、平板、桌面端) | 确定响应式策略 |
| 5 | 可访问性要求是什么?(WCAG等级) | 明确A11y标准 |
| 6 | 该组件需要什么数据? | 确定状态管理方案 |
调研完成后停止——设计前先输出约束条件和实现方案的总结。
Phase 2: Component Architecture Selection
阶段2:组件架构选型
Architecture Pattern Decision Table
架构模式决策表
| Pattern | When to Use | When NOT to Use |
|---|---|---|
| Atomic Design | Building a component library from scratch | Adding one component to existing system |
| Compound Components | Multi-part component needing layout flexibility | Simple single-purpose component |
| Hooks Pattern | Same logic reused across different UIs | Logic tied to one specific component |
| Container/Presenter | Components need isolated testing or multiple data sources | Simple components with minimal logic |
| 模式 | 适用场景 | 不适用场景 |
|---|---|---|
| 原子设计(Atomic Design) | 从零搭建组件库 | 向现有系统新增单个组件 |
| 复合组件(Compound Components) | 需要布局灵活性的多部分组件 | 简单的单用途组件 |
| Hooks模式 | 不同UI之间需要复用相同逻辑 | 逻辑仅和某个特定组件绑定 |
| 容器/展示层分离 | 组件需要隔离测试或对接多个数据源 | 逻辑极少的简单组件 |
Atomic Design Levels
原子设计层级
| Level | Description | Examples |
|---|---|---|
| Atoms | Smallest building blocks, single purpose | Button, Input, Label, Icon |
| Molecules | Groups of atoms functioning together | SearchBar (Input + Button), FormField (Label + Input + Error) |
| Organisms | Complex sections composed of molecules | Header (Logo + Nav + SearchBar), ProductCard |
| Templates | Page layouts with placeholder content | DashboardLayout, AuthLayout |
| Pages | Templates populated with real data | HomePage, SettingsPage |
| 层级 | 描述 | 示例 |
|---|---|---|
| 原子(Atoms) | 最小的构建块,仅具备单一功能 | 按钮、输入框、标签、图标 |
| 分子(Molecules) | 多个原子组合共同实现功能 | 搜索栏(输入框 + 按钮)、表单项(标签 + 输入框 + 错误提示) |
| 有机体(Organisms) | 由分子组成的复杂区块 | 页头(Logo + 导航 + 搜索栏)、商品卡片 |
| 模板(Templates) | 带占位内容的页面布局 | 仪表盘布局、认证页布局 |
| 页面(Pages) | 填充了真实数据的模板 | 首页、设置页 |
Compound Components Example
复合组件示例
tsx
<Select value={selected} onChange={setSelected}>
<Select.Trigger />
<Select.Options>
<Select.Option value="a">Option A</Select.Option>
<Select.Option value="b">Option B</Select.Option>
</Select.Options>
</Select>Use when: a component has multiple sub-parts that must coordinate but consumers need layout flexibility.
tsx
<Select value={selected} onChange={setSelected}>
<Select.Trigger />
<Select.Options>
<Select.Option value="a">Option A</Select.Option>
<Select.Option value="b">Option B</Select.Option>
</Select.Options>
</Select>适用场景:组件包含多个需要协同的子部分,同时使用者需要灵活控制布局。
Hooks Pattern Example
Hooks模式示例
tsx
function useDialog() {
const [isOpen, setIsOpen] = useState(false);
const open = () => setIsOpen(true);
const close = () => setIsOpen(false);
return { isOpen, open, close };
}Use when: the same logic is needed across multiple components with different UI.
STOP after architecture selection — confirm the pattern choice before proceeding.
tsx
function useDialog() {
const [isOpen, setIsOpen] = useState(false);
const open = () => setIsOpen(true);
const close = () => setIsOpen(false);
return { isOpen, open, close };
}适用场景:多个UI不同的组件需要使用相同的逻辑。
架构选型完成后停止——继续推进前先确认模式选择。
Phase 3: Responsive Design
阶段3:响应式设计
Mobile-First Breakpoints
移动端优先断点
| Breakpoint | Target | Min-Width |
|---|---|---|
| sm | Mobile landscape | 640px |
| md | Tablet | 768px |
| lg | Desktop | 1024px |
| xl | Large desktop | 1280px |
| 2xl | Wide desktop | 1536px |
| 断点标识 | 适配目标 | 最小宽度 |
|---|---|---|
| sm | 横屏移动端 | 640px |
| md | 平板 | 768px |
| lg | 桌面端 | 1024px |
| xl | 大屏桌面端 | 1280px |
| 2xl | 宽屏桌面端 | 1536px |
Responsive Strategy Decision Table
响应式策略决策表
| Need | Use | Not |
|---|---|---|
| Layout changes based on viewport size | Media queries | Container queries |
| Component adapts to parent container size | Container queries | Media queries |
| Text scales smoothly between breakpoints | | Fixed font sizes |
| Images adapt to viewport | | Single fixed image |
| 需求 | 使用 | 不使用 |
|---|---|---|
| 布局随视口尺寸变化 | 媒体查询 | 容器查询 |
| 组件适配父容器尺寸 | 容器查询 | 媒体查询 |
| 文本在断点之间平滑缩放 | | 固定字体大小 |
| 图片适配视口 | | 单张固定尺寸图片 |
Fluid Typography
流式排版示例
css
font-size: clamp(1rem, 0.5rem + 1.5vw, 1.5rem);css
font-size: clamp(1rem, 0.5rem + 1.5vw, 1.5rem);Phase 4: Accessibility (WCAG 2.1 AA)
阶段4:可访问性(WCAG 2.1 AA)
Semantic HTML Decision Table
语义化HTML决策表
| Need | Use | NOT |
|---|---|---|
| Navigation | | |
| Button action | | |
| Page sections | | |
| Headings | | |
| List of items | | Nested |
| Form labels | | Placeholder text only |
| 需求 | 使用 | 不使用 |
|---|---|---|
| 导航模块 | | |
| 按钮操作 | | |
| 页面区块 | | |
| 标题 | 按顺序使用 | |
| 项目列表 | | 嵌套 |
| 表单标签 | | 仅使用占位符文本 |
ARIA Usage Rules
ARIA使用规则
| Rule | When |
|---|---|
| Use semantic HTML first | Always — ARIA is a fallback |
| Labels for elements without visible text |
| Associates descriptive text with an element |
| Announces dynamic content changes |
| Toggleable sections (accordions, menus) |
| Only when no semantic element exists |
| 规则 | 适用场景 |
|---|---|
| 优先使用语义化HTML | 所有场景——ARIA是降级方案 |
| 没有可见文本的元素的标签 |
| 为元素关联描述性文本 |
| 播报动态内容变更 |
| 可切换的区块(手风琴、菜单) |
| 仅当没有对应语义元素时使用 |
Keyboard Navigation Requirements
键盘导航要求
- All interactive elements focusable (naturally or )
tabindex="0" - Operable via keyboard (Enter, Space, Escape, Arrow keys)
- Visible focus indicator (never without replacement)
outline: none - Logical tab order matching visual order
- Focus traps for modals and dialogs
- 所有可交互元素可聚焦(原生支持或设置)
tabindex="0" - 可通过键盘操作(Enter、Space、Escape、方向键)
- 可见的聚焦指示器(不要仅设置而没有替代样式)
outline: none - 逻辑Tab顺序和视觉顺序一致
- 模态框和弹窗需要设置焦点陷阱
Color Contrast Requirements
颜色对比度要求
| Element | Minimum Ratio |
|---|---|
| Normal text | 4.5:1 |
| Large text (18px+ or 14px+ bold) | 3:1 |
| UI components | 3:1 against adjacent colors |
| Information conveyed by color | Must also use icons, patterns, or text |
| 元素 | 最低对比度 |
|---|---|
| 普通文本 | 4.5:1 |
| 大文本(18px+ 或14px+加粗) | 3:1 |
| UI组件 | 和相邻颜色对比度3:1 |
| 通过颜色传递的信息 | 必须同时搭配图标、图案或文本说明 |
Screen Reader Testing
屏幕阅读器测试
Test with at least one screen reader:
- macOS: VoiceOver (built-in)
- Windows: NVDA (free) or JAWS
- Verify: content announced in logical order, form errors associated with inputs, dynamic updates announced
至少使用一款屏幕阅读器测试:
- macOS:VoiceOver(系统自带)
- Windows:NVDA(免费)或JAWS
- 验证:内容按逻辑顺序播报、表单错误和输入框关联、动态更新可被播报
Phase 5: State Management & Performance
阶段5:状态管理与性能
State Management Decision Table
状态管理决策表
| State Type | Solution | When |
|---|---|---|
| Local | | State used by one component or direct children |
| Shared | Context, Zustand, Jotai | State shared across multiple unrelated components |
| Server | TanStack Query, SWR | Data fetched from API, needs caching/revalidation |
| Form | React Hook Form, Formik | Complex forms with validation and submission |
| URL | Search params, router state | State that should be bookmarkable/shareable |
| 状态类型 | 方案 | 适用场景 |
|---|---|---|
| 本地状态 | | 仅单个组件或其直接子组件使用的状态 |
| 共享状态 | Context、Zustand、Jotai | 多个不相关组件之间共享的状态 |
| 服务端状态 | TanStack Query、SWR | 从API获取、需要缓存/重新验证的数据 |
| 表单状态 | React Hook Form、Formik | 带校验和提交逻辑的复杂表单 |
| URL状态 | 搜索参数、路由状态 | 需要可收藏/可分享的状态 |
Selection Heuristic
选型启发规则
- Start with — only escalate when you hit a real limitation
useState - If prop drilling exceeds 2 levels, consider Context or state library
- If caching API responses, use a server state library (not Redux for server state)
- For forms with >3 fields and validation, use a form library
- 优先使用——仅当遇到实际限制时再升级方案
useState - 如果属性透传超过2层,考虑使用Context或状态库
- 如果需要缓存API响应,使用服务端状态库(不要用Redux处理服务端状态)
- 字段数>3且带校验的表单,使用表单库
Performance Optimization Checklist
性能优化检查清单
| Technique | When to Apply |
|---|---|
| Route-level code splitting |
| Below-the-fold images |
| Virtualization | Lists with >50 items |
| Expensive computations |
| Callbacks passed to memoized children |
Dynamic | Conditionally loaded heavy libraries |
| WebP/AVIF images | All image assets |
Explicit | Prevent layout shift |
| 技术 | 适用场景 |
|---|---|
| 路由级别代码分割 |
图片添加 | 首屏之外的图片 |
| 虚拟滚动 | 列表项超过50条的列表 |
| 计算成本高的运算 |
| 传递给 memo 化子组件的回调函数 |
动态 | 条件加载的重型库 |
| WebP/AVIF格式图片 | 所有图片资源 |
图片显式设置 | 避免布局偏移 |
Design System Integration
设计系统集成
Design Tokens — define foundational values, not hard-coded:
ts
const tokens = {
color: { primary: '#2563eb', secondary: '#64748b', error: '#dc2626' },
spacing: { xs: '0.25rem', sm: '0.5rem', md: '1rem', lg: '1.5rem', xl: '2rem' },
typography: { fontFamily: { sans: 'Inter, system-ui, sans-serif' } },
};Component Variants — consistent variant API:
tsx
<Button variant="primary" size="md">Save</Button>
<Button variant="outline" size="sm">Cancel</Button>Theme Support:
- CSS custom properties for runtime theme switching
- Support light + dark themes at minimum
- Respect as default
prefers-color-scheme - Allow user override stored in localStorage
STOP after design — present the full component specification for review.
设计令牌(Design Tokens)——定义基础值,不要硬编码:
ts
const tokens = {
color: { primary: '#2563eb', secondary: '#64748b', error: '#dc2626' },
spacing: { xs: '0.25rem', sm: '0.5rem', md: '1rem', lg: '1.5rem', xl: '2rem' },
typography: { fontFamily: { sans: 'Inter, system-ui, sans-serif' } },
};组件变体——统一的变体API:
tsx
<Button variant="primary" size="md">Save</Button>
<Button variant="outline" size="sm">Cancel</Button>主题支持:
- 使用CSS自定义属性实现运行时主题切换
- 最少支持浅色+深色主题
- 默认尊重系统设置
prefers-color-scheme - 允许用户覆盖偏好并存储在localStorage
设计完成后停止——输出完整的组件规格说明供评审。
Anti-Patterns / Common Mistakes
反模式/常见错误
| Mistake | Why It Is Wrong | What To Do Instead |
|---|---|---|
| Not keyboard accessible, no screen reader semantics | Use semantic HTML elements |
| Keyboard users cannot see focus | Replace with visible focus style |
| Fixed font sizes (px) | Cannot scale with user preferences | Use |
| Prop drilling through 4+ levels | Maintenance nightmare | Use Context or state library |
| Fetching in useEffect + useState | No caching, no dedup, race conditions | Use TanStack Query or SWR |
| Premature memoization | Adds complexity without measured benefit | Profile first, optimize measured bottlenecks |
| Desktop-first responsive design | Mobile experience is an afterthought | Start mobile-first, add complexity up |
| Color as sole information carrier | Inaccessible to colorblind users | Add icons, patterns, or text labels |
| No loading/error states | Users see blank screens or cryptic errors | Design loading, error, and empty states |
| 错误 | 问题 | 正确做法 |
|---|---|---|
用 | 不支持键盘访问,没有屏幕阅读器语义 | 使用语义化HTML元素 |
仅设置 | 键盘用户看不到聚焦状态 | 替换为可见的聚焦样式 |
| 固定字体大小(px单位) | 无法跟随用户偏好缩放 | 使用 |
| 4层以上的属性透传 | 维护成本极高 | 使用Context或状态库 |
| 在useEffect + useState中发起请求 | 无缓存、无去重、存在竞态条件 | 使用TanStack Query或SWR |
| 过早 memo 化 | 无明确收益的情况下增加复杂度 | 先 profiling,再优化可测量的瓶颈 |
| 桌面端优先的响应式设计 | 移动端体验被后置考虑 | 从移动端优先开始,逐步向上增加复杂度 |
| 仅用颜色传递信息 | 色盲用户无法识别 | 增加图标、图案或文本标签 |
| 缺少加载/错误状态 | 用户看到空白屏或晦涩的错误提示 | 设计加载、错误和空状态 |
Anti-Rationalization Guards
反不合理要求规则
- Do NOT skip accessibility — WCAG 2.1 AA is the minimum, not optional
- Do NOT use with onClick instead of semantic elements
<div> - Do NOT skip keyboard navigation testing
- Do NOT choose state management before understanding the actual need
- Do NOT skip the discovery phase — understand constraints first
- Do NOT optimize performance without measuring first
- 禁止跳过可访问性要求——WCAG 2.1 AA是最低要求,不是可选项
- 禁止使用带onClick的代替语义化元素
<div> - 禁止跳过键盘导航测试
- 禁止在了解实际需求前选择状态管理方案
- 禁止跳过调研阶段——先明确约束条件
- 禁止没有测量就提前优化性能
Integration Points
集成点
| Skill | Relationship |
|---|---|
| Upstream: API response shapes inform component data needs |
| Upstream: specs define component behavioral requirements |
| Downstream: component designs become implementation tasks |
| Downstream: component spec drives test-first implementation |
| Parallel: specialist knowledge for React/Next.js specifics |
| Upstream: UX design informs component requirements |
| Parallel: design system tokens feed component styling |
| Downstream: profile and optimize after implementation |
| 技能 | 关系 |
|---|---|
| 上游:API响应结构决定组件数据需求 |
| 上游:规格说明定义组件行为要求 |
| 下游:组件设计转化为开发任务 |
| 下游:组件规格驱动测试先行的开发 |
| 并行:React/Next.js专项知识支持 |
| 上游:UX设计决定组件需求 |
| 并行:设计系统令牌支撑组件样式 |
| 下游:开发完成后进行 profiling 和优化 |
Verification Gate
验证关口
Before claiming the UI design is complete:
- VERIFY component architecture pattern is explicitly chosen with rationale
- VERIFY responsive behavior is defined for all target breakpoints
- VERIFY accessibility requirements are specified (WCAG level, keyboard, color contrast)
- VERIFY state management approach is selected based on actual needs
- VERIFY loading, error, and empty states are designed
- VERIFY the user has approved the component specification
在声明UI设计完成前:
- 确认已明确选择组件架构模式并给出选型理由
- 确认已定义所有目标断点的响应式表现
- 确认已明确可访问性要求(WCAG等级、键盘导航、颜色对比度)
- 确认已基于实际需求选择状态管理方案
- 确认已设计加载、错误和空状态
- 确认用户已批准组件规格说明
Skill Type
技能类型
Flexible — Adapt component patterns, responsive strategy, and state management to project framework and constraints while preserving accessibility requirements and the discovery-first approach.
灵活适配——根据项目框架和约束调整组件模式、响应式策略和状态管理方案,同时保留可访问性要求和调研优先的工作流程。