Get the FREE Ultimate OpenClaw Setup Guide →

frontend

Scanned
npx machina-cli add skill xiaobei930/cc-best/frontend --openclaw
Files (1)
SKILL.md
10.1 KB

前端开发模式

本技能提供前端开发的最佳实践和模式,支持多框架按需加载。

触发条件

  • 创建或修改前端组件
  • 设计 UI/UX 交互
  • 实现状态管理
  • 性能优化
  • 无障碍开发

框架专属模式

根据项目技术栈,加载对应的框架专属文件:

技术栈加载文件框架
Vuevue.mdVue 3, Nuxt 3
Reactreact.mdReact 18, Next.js
Sveltesvelte.mdSvelte, SvelteKit
Angularangular.mdAngular 17+

加载方式: 检测项目中的 package.json 依赖确定技术栈。


通用组件模式

组件分类

┌─────────────────────────────────────────────────────┐
│                    组件金字塔                         │
├─────────────────────────────────────────────────────┤
│  页面组件 (Pages/Views)                              │
│  ├─ 负责路由和布局                                   │
│  ├─ 组合多个功能组件                                 │
│  └─ 管理页面级状态                                   │
├─────────────────────────────────────────────────────┤
│  功能组件 (Features/Containers)                      │
│  ├─ 包含业务逻辑                                     │
│  ├─ 连接状态管理                                     │
│  └─ 调用 API                                        │
├─────────────────────────────────────────────────────┤
│  UI 组件 (UI/Presentational)                        │
│  ├─ 纯展示,无业务逻辑                               │
│  ├─ 通过 props 接收数据                              │
│  └─ 通过 events 通知父组件                           │
├─────────────────────────────────────────────────────┤
│  基础组件 (Base/Primitives)                         │
│  ├─ Button, Input, Card 等                          │
│  ├─ 高度可复用                                       │
│  └─ 设计系统基础                                     │
└─────────────────────────────────────────────────────┘

组件设计原则

原则说明
单一职责一个组件只做一件事
Props 向下数据从父组件流向子组件
Events 向上事件从子组件通知父组件
组合优于继承使用 slot/children 组合组件
可预测相同 props 产生相同输出

命名规范

components/
├── ui/                    # 基础组件(PascalCase)
│   ├── Button.vue/tsx
│   ├── Input.vue/tsx
│   └── Card.vue/tsx
├── feature/               # 功能组件(PascalCase)
│   ├── UserCard.vue/tsx
│   └── OrderList.vue/tsx
└── layout/                # 布局组件(PascalCase)
    ├── Header.vue/tsx
    └── Sidebar.vue/tsx

状态管理模式

状态分层

层级范围存储方式示例
组件状态单个组件useState/ref表单输入、UI 状态
共享状态多个组件Context/Store用户信息、主题
服务器状态来自 APIQuery Cache列表数据、详情
URL 状态路由参数Router页码、筛选条件
持久状态跨会话localStorage用户偏好

状态管理选择

简单共享状态 → Context/Provide
  ↓ 复杂度增加
中等应用 → Pinia/Zustand
  ↓ 复杂度增加
大型应用 → Redux/Vuex + 规范化

服务器状态管理

API 请求 → Query Library(TanStack Query)
  ├─ 自动缓存
  ├─ 重新验证
  ├─ 乐观更新
  └─ 错误重试

性能优化模式

渲染优化

技术用途框架实现
Memoization避免重复计算useMemo/computed
Lazy Loading延迟加载组件lazy/defineAsyncComponent
Virtual List大列表渲染react-window/vue-virtual-scroller
Code Splitting按需加载代码动态 import

资源优化

图片优化:
├─ 响应式图片(srcset)
├─ 懒加载(loading="lazy")
├─ 现代格式(WebP/AVIF)
└─ 图片 CDN

脚本优化:
├─ Tree Shaking
├─ Bundle 分析
├─ 预加载关键资源
└─ 延迟非关键脚本

性能指标

指标目标说明
LCP< 2.5s最大内容绘制
FID< 100ms首次输入延迟
CLS< 0.1累积布局偏移
TTI< 3.8s可交互时间

无障碍模式 (a11y)

基础清单

  • 语义化 HTML - 使用正确的元素(button, nav, main...)
  • 键盘可访问 - 所有交互可用键盘完成
  • 焦点管理 - 焦点顺序合理,焦点可见
  • ARIA 标签 - 为动态内容添加适当标签
  • 颜色对比 - 符合 WCAG 2.1 AA 标准(4.5:1)
  • 替代文本 - 图片有 alt,图标有 aria-label

常见 ARIA 模式

<!-- 按钮 -->
<button aria-label="关闭菜单" aria-expanded="false">
  <Icon name="close" aria-hidden="true" />
</button>

<!-- 表单 -->
<input
  aria-labelledby="label-id"
  aria-describedby="hint-id error-id"
  aria-invalid="true"
/>

<!-- 动态内容 -->
<div aria-live="polite" aria-busy="false">加载完成</div>

<!-- 对话框 -->
<div role="dialog" aria-modal="true" aria-labelledby="dialog-title">
  <h2 id="dialog-title">对话框标题</h2>
</div>

键盘交互

元素按键行为
按钮Enter/Space激活
链接Enter导航
菜单↑↓移动焦点
对话框Esc关闭
Tab列表←→切换标签

表单模式

表单验证

客户端验证:
├─ 实时验证(onChange)
├─ 提交验证(onSubmit)
├─ 字段级错误
└─ 表单级错误

验证库:
├─ React: react-hook-form + zod
├─ Vue: vee-validate + zod
└─ 通用: yup/zod

表单状态

{
  values: {},        // 字段值
  errors: {},        // 验证错误
  touched: {},       // 字段已交互
  isSubmitting: false,
  isValid: true
}

错误处理

<!-- 字段错误 -->
<div class="field">
  <label for="email">邮箱</label>
  <input id="email" aria-invalid="true" aria-describedby="email-error" />
  <span id="email-error" role="alert" class="error">
    请输入有效的邮箱地址
  </span>
</div>

<!-- 表单级错误 -->
<div role="alert" class="form-error">提交失败:网络错误,请重试</div>

动画模式

动画类型

类型用途实现
进入/退出元素出现/消失CSS Transition
状态变化hover/activeCSS Transition
列表动画添加/删除/排序FLIP 动画
页面切换路由过渡路由动画
复杂动画多阶段动画Framer Motion/GSAP

动画原则

  1. 有意义 - 动画应该传达信息
  2. 快速 - 持续时间 150-300ms
  3. 可禁用 - 尊重 prefers-reduced-motion
  4. 不阻塞 - 不影响用户操作

减少动画

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

设计美学(避免 AI Slop)

详见 design-guide.md — AI 通用审美定义、设计思维方向矩阵、反模式清单与审查清单。


测试模式

前端测试策略详见 rules/frontend/frontend-testing.mdrules/frontend/frontend-e2e.md(按文件类型自动加载)。


框架专属内容

详细的框架专属实现请参考:

专项优化

  • 性能优化: performance.md - Core Web Vitals (LCP/INP/CLS)
  • Tailwind CSS: tailwind.md - Tailwind 最佳实践 + v4 迁移指南
  • 设计指南: design-guide.md - Designer 角色设计方法论与审查清单

Maintenance

  • Sources: MDN, WCAG 2.1, web.dev, 各框架官方文档
  • Last updated: 2025-01-22
  • Pattern: 通用清单 + 按需加载框架专属

记住: 前端开发的核心是用户体验——性能、无障碍、交互细节缺一不可,独特的设计比通用模板更有价值。

Source

git clone https://github.com/xiaobei930/cc-best/blob/main/skills/frontend/SKILL.mdView on GitHub

Overview

This skill collects frontend best practices and patterns, covering component design, state management, performance, and accessibility. It supports multi-framework projects by loading framework-specific guidelines (Vue, React, Svelte, Angular) based on your package.json, helping teams build cohesive client-side apps.

How This Skill Works

Patterns are organized into framework-specific files (vue.md, react.md, svelte.md, angular.md) plus universal component guidelines. At runtime, the system detects your project stack from package.json and surfaces the matching files and conventions for components, state, and performance optimizations.

When to Use It

  • Creating or modifying frontend components
  • Designing UI/UX interactions
  • Implementing state management across the app
  • Performing performance optimizations
  • Focusing on accessibility development

Quick Start

  1. Step 1: Inspect your project to identify the framework (via package.json).
  2. Step 2: Load the corresponding framework.md (vue.md, react.md, etc.) and apply its patterns.
  3. Step 3: Apply core frontend patterns: component pyramid, proper state layers, and performance improvements.

Best Practices

  • Follow the single-responsibility principle for components
  • Prop data flows downward and events bubble upward
  • Prefer composition over inheritance using slots/children
  • Name and organize components with a clear folder structure (ui, feature, layout)
  • Load framework-specific patterns only after detecting the project stack

Example Use Cases

  • Refactor a component using a pages/views, features/containers, and UI/presentational split
  • Build a design-system with Button/Input/Card in base primitives and UI components
  • Migrate to framework-specific pattern files loaded via package.json detection
  • Move from prop drilling to Context/Store for shared state
  • Implement server state with a query library and caching for lists/details

Frequently Asked Questions

Add this skill to your agents
Sponsor this space

Reach thousands of developers