用Figma MCP生成项目设计系统规则

该技能通过Figma MCP服务器的create_design_system_rules工具生成项目级规则文件,把组件位置、样式与资源处理等约定写成AI编码代理可遵循的指令,使Figma设计转代码时保持团队一致性。

# 创建设计系统规则 ## 概览 这种技能帮助您生成适合您项目特定需要的定制设计系统规则。 这些规则引导 AI 编码代理在执行 Figma 设计时生成一致,高质量的代码, 确保您团队的常规, 组件模式, 以及建筑决定自动被遵循 。 ### 支持的规则文件 | 探员 | 规则文件 | |-------|-----------| | Claude 代码 | `CLAUDE.md` | | 代码 CLI | `AGENTS.md` | | 光标 | `.cursor/rules/figma-design-system.mdc` | ## 什么是设计系统规则? 设计系统规则是工程级别指令,将您的代码库的"非书面知识"编码为——体验开发者所了解并会传递给新的团队成员的那种专长: - 要使用的原始版式和组件 - 组件文件应位于何处 - 如何命名和安排组件 - 什么不该被硬化 - 如何处理设计符号和造型 - 具体项目的建筑格局 这些规则一经确定,将大大减少重复的提示,并确保所有法格玛执行任务的产出一致。 ## 先决条件 - Figma MCP 服务器必须连接和可访问 - 进入项目代码库进行分析 - 了解贵小组的组成公约(或建立公约的意愿) ## 何时使用此技能 使用此技能时: - 启动将使用 Figma 设计的新项目 - 将一个 AI 编码代理机装入一个已有模式的项目 - 将整个团队的图像到代码工作流程标准化 - 更新或完善现有设计系统公约 - 用户明确要求:"创建设计系统规则","设置Figma准则","为我的项目定制规则". ## 所需工作流量 **按顺序走这些步骤,不要跳过这些步骤。** ### Step 1 : 运行创建设计系统规则工具 呼叫 Figma MCP 服务器 `create_design_system_rules` 工具以获取基础快捷和模板。 **参数:** - `clientLanguages`: 以逗号分隔的工程所用语言列表(如"typescript,javascript","python","javascript") . - `clientFrameworks`: 正在使用框架(例如"反应","vue","svelte","angular","unknown"). 这个工具返回用于创建设计系统规则的指引和模板. 按照工具响应中提供的模板格式构建您的设计系统规则 。 ### Step 2:分析代码库 在最后确定规则之前,分析该项目,以了解现有模式: **构成部分组织:** - UI组件在哪里? (例如, `src/components/`, `app/ui/`, `lib/components/`) - 是否有专用的设计系统目录? - 组件如何排列? (按特征、类型、平面结构分列) **样式方法 :** - 使用什么 CSS 框架或方法? (Tailwind, CSS 模块, 样式组件等) - 设计符号定义在哪里 ? ( CSS 变量、 主题文件、 配置文件) - 是否有颜色、印刷或间距标志? **组件模式 :** - 使用什么命名惯例? (PascalCase, kebab case, 前缀) - 组件道具的结构如何? - 是否有共同的构成模式? **结构决定:** - 国家管理如何处理? - 使用什么路由系统? - 是否有特定的导入模式或路径别名? ### Step 3:生成项目——特定规则 根据您的代码库分析, 创建一整套规则。 包括: #### 一般构成部分规则 ```markdown - IMPORTANT: Always use components from `[YOUR_PATH]` when possible - Place new UI components in `[COMPONENT_DIRECTORY]` - Follow `[NAMING_CONVENTION]` for component names - Components must export as `[EXPORT_PATTERN]` ``` #### 造型规则 ```markdown - Use `[CSS_FRAMEWORK/APPROACH]` for styling - Design tokens are defined in `[TOKEN_LOCATION]` - IMPORTANT: Never hardcode colors - always use tokens from `[TOKEN_FILE]` - Spacing values must use the `[SPACING_SYSTEM]` scale - Typography follows the scale defined in `[TYPOGRAPHY_LOCATION]` ``` #### Figma MCP 融合规则 ```markdown ## Figma MCP Integration Rules These rules define how to translate Figma inputs into code for this project and must be followed for every Figma-driven change. ### Required Flow (do not skip) 1. Run get_design_context first to fetch the structured representation for the exact node(s) 2. If the response is too large or truncated, run get_metadata to get the high-level node map, then re-fetch only the required node(s) with get_design_context 3. Run get_screenshot for a visual reference of the node variant being implemented 4. Only after you have both get_design_context and get_screenshot, download any assets needed and start implementation 5. Translate the output (usually React + Tailwind) into this project's conventions, styles, and framework 6. Validate against Figma for 1:1 look and behavior before marking complete ### Implementation Rules - Treat the Figma MCP output (React + Tailwind) as a representation of design and behavior, not as final code style - Replace Tailwind utility classes with `[YOUR_STYLING_APPROACH]` when applicable - Reuse existing components from `[COMPONENT_PATH]` instead of duplicating functionality - Use the project's color system, typography scale, and spacing tokens consistently - Respect existing routing, state management, and data-fetch patterns - Strive for 1:1 visual parity with the Figma design - Validate the final UI against the Figma screenshot for both look and behavior ``` #### 资产处理规则 ```markdown ## Asset Handling - The Figma MCP server provides an assets endpoint which can serve image and SVG assets - IMPORTANT: If the Figma MCP server returns a localhost source for an image or SVG, use that source directly - IMPORTANT: DO NOT import/add new icon packages - all assets should be in the Figma payload - IMPORTANT: DO NOT use or create placeholders if a localhost source is provided - Store downloaded assets in `[ASSET_DIRECTORY]` ``` #### 项目特定公约 ```markdown ## Project-Specific Conventions - [Add any unique architectural patterns] - [Add any special import requirements] - [Add any testing requirements] - [Add any accessibility standards] - [Add any performance considerations] ``` ### Step 4 : 将规则保存到适当的规则文件 检测用户正在使用的 AI 编码代理, 并将生成的规则保存到相应的文件 : | 探员 | 规则文件 | 页:1 | |-------|-----------|-------| | Claude 代码 | `CLAUDE.md` 在项目根中 | 标记下的格式。也可以使用 `.claude/rules/figma-design-system.md` 用于模块化组织。 | | 代码 CLI | `AGENTS.md` 在项目根中 | 标记下限格式。 如果文件已经存在, 将添加为新区域 。 32 KiB 组合大小限制 。 | | 光标 | `.cursor/rules/figma-design-system.mdc` | 以 YAML 前题标记下(`description`, `globs`, `alwaysApply`). | 如果无法确定用户正在与哪位代理, 请检查工程中已有的规则文件, 或是询问用户 。 对于光标,请将规则与 YAML 前置事项包裹在一起 : ```markdown --- description: Rules for implementing Figma designs using the Figma MCP server. Covers component organization, styling conventions, design tokens, asset handling, and the required Figma-to-code workflow. globs: "src/components/**" alwaysApply: false --- [Generated rules here] ``` 自定义 `globs` 模式,以匹配项目中将存在 Figma-衍生代码的目录(例如, `"src/**/*.tsx"` 或 时 间 `["src/components/**", "src/pages/**"]`). 保存后,规则会被代理自动加载,并应用于所有Figma执行任务. ### Step 5 : 验证和电极 创建规则后: 1. 简单法格玛组件执行测试 2. 校验代理商是否正确遵守规则 3. 完善任何没有预期效果的规则 4. 与团队成员分享反馈 5. 随着项目的发展更新规则 ## 规则类别和实例 ### 基本规则( 总是包括) **组件发现 :** ```markdown - UI components are located in `src/components/ui/` - Feature components are in `src/components/features/` - Layout primitives are in `src/components/layout/` ``` **设计托肯用法 :** ```markdown - Colors are defined as CSS variables in `src/styles/tokens.css` - Never hardcode hex colors - use `var(--color-*)` tokens - Spacing uses the 4px base scale: `--space-1` (4px), `--space-2` (8px), etc. ``` **样式方法 :** ```markdown - Use Tailwind utility classes for styling - Custom styles go in component-level CSS modules - Theme customization is in `tailwind.config.js` ``` ### 建议的规则(高度宝贵) **组件模式 :** ```markdown - All components must accept a `className` prop for composition - Variant props should use union types: `variant: 'primary' | 'secondary'` - Icon components should accept `size` and `color` props ``` **进口公约:** ```markdown - Use path aliases: `@/components`, `@/styles`, `@/utils` - Group imports: React, third-party, internal, types - No relative imports beyond parent directory ``` **代码质量 :** ```markdown - Add JSDoc comments for exported components - Include PropTypes or TypeScript types for all props - Extract magic numbers to named constants ``` ### 可选规则(具体项目) **无障碍性:** ```markdown - All interactive elements must have aria-labels - Color contrast must meet WCAG AA standards - Keyboard navigation required for all interactions ``` **性能 :** ```markdown - Lazy load images with the `Image` component from `@/components/Image` - Use React.memo for components that receive complex props - Icons should be SVG components, not icon fonts ``` **测试 :** ```markdown - Include unit tests for new components in `__tests__/` directory - Use Testing Library queries (getByRole, getByLabelText) - Test all interactive states and variants ``` ## 实例 ### 示例 1:反应+尾风工程 用户称:"我的反应项目创造设计系统规则". **动作 :** 1. 运行 `create_design_system_rules(clientLanguages="typescript,javascript", clientFrameworks="react")` 2. 分析密码库结构 3. 生成规则 : ```markdown # Figma MCP Integration Rules ## Component Organization - UI components are in `src/components/ui/` - Page components are in `src/app/` - Use Tailwind for styling ## Figma Implementation Flow 1. Run get_design_context for the node 2. Run get_screenshot for visual reference 3. Map Figma colors to Tailwind colors defined in `tailwind.config.js` 4. Reuse components from `src/components/ui/` when possible 5. Validate against screenshot before completing ## Styling Rules - IMPORTANT: Use Tailwind utility classes, not inline styles - Colors are defined in `tailwind.config.js` theme.colors - Spacing uses Tailwind's default scale - Custom components go in `src/components/ui/` ## Asset Rules - IMPORTANT: Use localhost sources from Figma MCP server directly - Store static assets in `public/assets/` - DO NOT install new icon libraries ``` 4. 为用户代理保存到相应的规则文件 5. 使用简单按钮执行的测试 **结果 :** 该代理人现在在实施Figma设计时一贯遵守该项目的公约。 ### 示例 2: Vue + 自定义 CSS 项目 用户表示:"为我的Vue应用设定Figma规则". **动作 :** 1. 运行 `create_design_system_rules(clientLanguages="typescript,javascript", clientFrameworks="vue")` 2. 发现项目使用 CSS 模块和自定义设计符号系统 3. 生成规则 : ```markdown # Figma MCP Integration Rules ## Component Organization - Components are in `src/components/` - Composables are in `src/composables/` - Vue SFC structure: <script setup>, <template>, <style scoped> ## Design Tokens - IMPORTANT: All colors defined in `src/styles/tokens.css` as CSS variables - Use `var(--color-primary)`, `var(--color-secondary)`, etc. - Spacing: `var(--space-xs)` through `var(--space-xl)` - Typography: `var(--text-sm)` through `var(--text-2xl)` ## Figma Implementation Flow 1. Run get_design_context and get_screenshot 2. Translate React output to Vue 3 Composition API 3. Map Figma colors to CSS variables in `src/styles/tokens.css` 4. Use CSS Modules for component styles 5. Check for existing components in `src/components/` before creating new ones ## Styling Rules - Use CSS Modules (`.module.css` files) - IMPORTANT: Reference design tokens, never hardcode values - Scoped styles with CSS modules ``` 4. 为用户代理保存到相应的规则文件 5. 使用卡片组件验证 **结果 :** 该代理现在将Figma设计正确翻译为Vue组件,使用项目的设计令牌系统. ### 示例 3:设计系统团队 用户称:"我们设计系统库的Generate规则" **动作 :** 1. 运行 `create_design_system_rules(clientLanguages="typescript", clientFrameworks="react")` 2. 用设计系统包分析单列结构 3. 生成综合规则 : ```markdown # Design System Rules ## Repository Structure - Design system components: `packages/design-system/src/components/` - Documentation: `packages/docs/` - Design tokens: `packages/tokens/src/` ## Component Development - IMPORTANT: All components must be in `packages/design-system/src/components/` - Component file structure: `ComponentName/index.tsx`, `ComponentName.stories.tsx`, `ComponentName.test.tsx` - Export all components from `packages/design-system/src/index.ts` ## Design Tokens - Colors: `packages/tokens/src/colors.ts` - Typography: `packages/tokens/src/typography.ts` - Spacing: `packages/tokens/src/spacing.ts` - IMPORTANT: Never hardcode values - import from tokens package ## Documentation Requirements - Add Storybook story for every component - Include JSDoc with @example - Document all props with descriptions - Add accessibility notes ## Figma Integration 1. Get design context and screenshot from Figma 2. Map Figma tokens to design system tokens 3. Create or extend component in design system package 4. Add Storybook stories showing all variants 5. Validate against Figma screenshot 6. Update documentation ``` 4. 保存到相应的规则文件并与团队共享 5. 添加到团队文档中 **结果 :** 整个团队在将Figma的组件添加到设计系统中时遵循一致的模式. ## 最佳做法 ### 简单启动, 斜体 不要试图捕捉所有规则,从最重要的公约开始,在遇到不一致时加入规则. ### 具体 代替:"使用设计系统" 写:"总是使用按钮组件从 `src/components/ui/Button.tsx` 带有变种道具 (“ 主要” ) | "中学" | '鬼'). ### 让规则可操作 每项规则都应该告诉特工确切的要做什么,而不只是要避免什么. 好:"颜色定义在 `src/theme/colors.ts` - 导入和使用这些常量“ 坏:"别硬化的颜色" ### 关键规则使用 IMPORTANT 决不能用"IMPORTANT :"来违反的前缀规则,以确保代理优先处理. ```markdown - IMPORTANT: Never expose API keys in client-side code - IMPORTANT: Always sanitize user input before rendering ``` ### 记录为什么 当规则看起来武断时,解释其理由: ```markdown - Place all data-fetching in server components (reduces client bundle size and improves performance) - Use absolute imports with `@/` alias (makes refactoring easier and prevents broken relative paths) ``` ## 共同问题和解决办法 ### 问题:代理商没有遵守规则 **原因 :** 规则可能过于模糊或代理人没有适当装载。 **解决方案 :** - 使规则更加具体和可操作 - 校验规则保存在正确的配置文件中 - 重新启动您的代理或 IDE 以重新装入规则 - 在关键规则中添加“ IMPORTANT :” 前缀 ### 问题:规则相互冲突 **原因 :** 相互矛盾或相互重叠的规则。 **解决方案 :** - 审查所有冲突规则 - 建立明确的优先等级制度 - 删除冗余规则 - 将相关规则合并为单一、明确的声明 ### 问题:规则过多增加延迟性 **原因 :** 过多的规则增加了上下文大小和处理时间. **解决方案 :** - 集中关注20%解决规则80%一致性问题 - 删除很少适用的过于具体的规则 - 合并相关规则 - 使用渐进式披露( 基本规则先行, 链接文件中的高级规则) ### 问题:随着项目的发展,规则已过时 **原因 :** 代码库改变,但规则没有。 **解决方案 :** - 定期规则审查时间表(月度或季度) - 建筑决策改变时更新规则 - 版本控制您的规则文件 - 承诺消息中的文档规则更改 ## 理解设计系统规则 设计系统规则改变 AI 编码代理 如何与您的 Figma 设计工作 : **规则前 :** - 代理商对组件结构做出假设 - 不同执行方式不一致 - 与设计符号不符的硬码值 - 在随机位置创建组件 - 对项目公约的重复解释 **规则后:** - 代理自动跟随您的协议 - 连贯的构成部分结构和样式 - 从一开始就妥善使用设计标志 - 组织正确 - 零重复提示 花费在建立良好规则上的时间,在每一个法格玛执行任务中都以指数计回报。 ## 追加资源 - [Figma MCP 服务器文档](https://developers.figma.com/docs/figma-mcp-server/) - [图像变量和设计托肯斯](https://help.figma.com/hc/en-us/articles/15339657135383-Guide-to-variables-in-Figma)

查看原始来源