关于 pencil-to-code
将 Pencil 的 .pen 设计导出为 React、Vue 或 HTML 代码的技能,默认生成 React 与 Tailwind。它还可从设计变量提取 Tailwind 主题配置,并支持截图对照验证。
主要功能
- 01导出 React、Vue 或 HTML
- 02将设计节点映射为组件
- 03提取变量生成 Tailwind 主题
- 04映射布局、间距、颜色与字体
- 05可选截图对照验证
适用场景
- 01将 Pencil 画板转换为前端组件
- 02从 .pen 变量创建 Tailwind 主题
- 03检查生成页面与设计稿的视觉差异
MCP 相关
工作流通过 `mcp__pencil__batch_get` 读取设计结构和变量,并可用 `mcp__pencil__get_screenshot` 获取画板截图。
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ phrazzld/ claude-config --skill pencil-to-code安装后在 Claude Code 中输入/pencil-to-code确认已加载。
附带文件references/node-mapping.md
详细介绍
Pencil 转代码
将 Pencil .pen 设计导出为可用于生产环境的 React/Tailwind 代码。
界面
输入:
- 要导出的 Frame ID(或整个文档)
- 目标框架:React(默认)、Vue、HTML
- 可选:组件名称、输出路径
输出:
- 带 Tailwind 类的 React 组件
- Tailwind 主题配置(从 .pen 变量生成)
- 可选:用于验证的截图对比
工作流
1. 读取设计结构
// Get full frame tree
mcp__pencil__batch_get({
filePath: "<path>.pen",
nodeIds: ["frameId"],
readDepth: 10,
resolveInstances: true,
resolveVariables: true
})
// Get design variables/theme
mcp__pencil__get_variables({ filePath: "<path>.pen" })
2. 提取设计令牌
将 .pen 变量转换为 Tailwind 主题:
/* From .pen variables */
@theme {
--color-primary: [from variables.colors.primary];
--color-background: [from variables.colors.background];
--font-sans: [from variables.fonts.body];
/* ... */
}
参考:references/token-extraction.md
3. 将节点映射到组件
| .pen 节点类型 | React 输出 |
|---|---|
带布局的 frame | <div className="flex ..."> |
带子节点的 frame | 带子节点的组件 |
text | <p>、<h1-6> 或 <span> |
ref(实例) | 可复用组件 |
rectangle | 带填充的 <div> |
ellipse | <div className="rounded-full"> |
image | <img> 或 fill: url() |
参考:references/node-mapping.md
4. 生成代码
组件结构:
// components/[ComponentName].tsx
import { cn } from "@/lib/utils"
interface [ComponentName]Props {
className?: string
// Extracted props from design
}
export function [ComponentName]({ className, ...props }: [ComponentName]Props) {
return (
<div className={cn("[tailwind classes]", className)}>
{/* Nested structure */}
</div>
)
}
Tailwind 映射:
.pen property → Tailwind class
--------------------------------
fill: #000 → bg-black
layout: horizontal → flex flex-row
gap: 16 → gap-4
padding: [16,24,16,24] → py-4 px-6
fontSize: 24 → text-2xl
fontWeight: 700 → font-bold
cornerRadius: [8,8,8,8] → rounded-lg
5. 验证(可选)
// Screenshot the .pen frame
mcp__pencil__get_screenshot({ nodeId: "frameId" })
// Compare visually with rendered React
// (Manual step or browser automation)
6. 输出
## Generated Components
### [ComponentName]
- File: `components/[ComponentName].tsx`
- Props: [list extracted props]
### Theme Configuration
- File: `app/globals.css` (additions)
## Verification
Screenshot comparison: [attached]
转换规则
布局系统
.pen Tailwind
--------------------------------------
layout: "vertical" → flex flex-col
layout: "horizontal" → flex flex-row
layout: "grid" → grid
alignItems: "center" → items-center
justifyContent: "center" → justify-center
gap: 8 → gap-2
gap: 16 → gap-4
gap: 24 → gap-6
width: "fill_container" → w-full
height: "fill_container" → h-full
间距(8 点网格)
.pen padding Tailwind
----------------------------------------
[8,8,8,8] → p-2
[16,16,16,16] → p-4
[16,24,16,24] → py-4 px-6
[24,32,24,32] → py-6 px-8
字体排版
.pen Tailwind
----------------------------------------
fontSize: 12 → text-xs
fontSize: 14 → text-sm
fontSize: 16 → text-base
fontSize: 20 → text-xl
fontSize: 24 → text-2xl
fontSize: 32 → text-3xl
fontSize: 48 → text-5xl
fontWeight: 400 → font-normal
fontWeight: 500 → font-medium
fontWeight: 600 → font-semibold
fontWeight: 700 → font-bold
颜色
.pen Tailwind
----------------------------------------
fill: "#FFFFFF" → bg-white
fill: "#000000" → bg-black
fill: variable → bg-[var(--color-name)]
textColor: "#6B7280" → text-gray-500
stroke: "#E5E5E5" → border-gray-200
边框圆角
.pen cornerRadius Tailwind
----------------------------------------
[0,0,0,0] → rounded-none
[4,4,4,4] → rounded
[8,8,8,8] → rounded-lg
[16,16,16,16] → rounded-2xl
[9999,9999,9999,9999] → rounded-full
限制
- 单一职责:导出为代码,不修改设计。
- Tailwind 4 @theme:以 CSS 为先的令牌系统
- React + TypeScript:默认输出目标
- 语义化 HTML:选择合适的元素
- 无障碍性:在可检测到的情况下添加 aria 属性
参考
references/node-mapping.md— 完整的节点 → 组件映射references/token-extraction.md— 变量 → 主题提取design-tokens/— 令牌系统约定
集成
由以下内容调用:
design-exploration编排器(选择方向后)- 用户直接调用
组合:
design-tokens(用于主题结构)
评论
支持 Markdown