Skip to content

Commit 38c7505

Browse files
committed
get figma token
1 parent 61c6d2d commit 38c7505

4 files changed

Lines changed: 129 additions & 4 deletions

File tree

scripts/figma_parser.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import os
2+
import requests
3+
import json
4+
from dotenv import load_dotenv
5+
6+
load_dotenv() # Load environment variables from .env
7+
8+
# 配置参数
9+
FIGMA_TOKEN = os.getenv("FIGMA_TOKEN") # 从环境变量读取Token,避免硬编码
10+
FILE_ID = "ZuZWPWcPLSqaX8d1p9uTpX" # 替换为你的Figma文件ID
11+
NODE_IDS = "3-1377" # 替换为你的按钮组件节点ID(多个用逗号分隔)
12+
13+
def fetch_figma_data():
14+
"""调用Figma API获取节点数据"""
15+
url = f"https://api.figma.com/v1/files/{FILE_ID}/nodes?ids={NODE_IDS}"
16+
headers = {"X-Figma-Token": FIGMA_TOKEN}
17+
18+
response = requests.get(url, headers=headers)
19+
response.raise_for_status() # 如果HTTP请求失败则抛出异常
20+
21+
return response.json()
22+
23+
def parse_button_data(raw_data):
24+
"""解析Figma数据,提取按钮颜色和尺寸"""
25+
buttons = []
26+
27+
for node_id, node_info in raw_data["nodes"].items():
28+
doc = node_info["document"]
29+
name_parts = doc["name"].split("/") # 假设组件命名为 "Button/Primary"
30+
31+
# 提取颜色(取第一个填充色)
32+
fills = doc.get("fills", [{}])[0].get("color", {})
33+
color = {
34+
"r": fills.get("r", 0),
35+
"g": fills.get("g", 0),
36+
"b": fills.get("b", 0),
37+
"a": fills.get("a", 1)
38+
}
39+
40+
# 提取尺寸
41+
bbox = doc.get("absoluteBoundingBox", {})
42+
width = bbox.get("width", 100) # 默认值100px
43+
height = bbox.get("height", 40) # 默认值40px
44+
45+
buttons.append({
46+
"component": name_parts[0],
47+
"variant": name_parts[1] if len(name_parts) > 1 else "default",
48+
"color": color,
49+
"size": {"width": width, "height": height}
50+
})
51+
52+
return buttons
53+
54+
def save_to_json(data, output_path="../design_system/output.json"):
55+
"""保存解析后的数据到JSON文件"""
56+
with open(output_path, "w") as f:
57+
json.dump(data, f, indent=2)
58+
print(f"✅ 数据已保存至 {os.path.abspath(output_path)}")
59+
60+
if __name__ == "__main__":
61+
# 执行流程
62+
raw_data = fetch_figma_data()
63+
buttons = parse_button_data(raw_data)
64+
save_to_json(buttons)

src/app/page.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Button } from '@/components/core/Button/Button';
2+
import FigmaButton from '@/components/core/Button/FigmaButton';
23

34
export default function Home() {
45
return (
@@ -9,6 +10,7 @@ export default function Home() {
910
</div>
1011

1112
<Button>new button</Button>
13+
<FigmaButton>FigmaButton !</FigmaButton>
1214
</main>
1315
</div>
1416
);

src/components/core/Button/Button.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@ const buttonVariants = cva(
66
variants: {
77
variant: {
88
default: 'bg-primary-500 hover:bg-primary-600 text-white',
9-
disabled: 'bg-gray-300 cursor-not-allowed',
9+
secondary: 'bg-white text-gray-800 border-gray-400',
1010
},
1111
size: {
1212
default: 'px-md py-sm',
1313
large: 'px-6 py-3',
1414
},
15+
disabled: {
16+
false: null,
17+
true: ['opacity-50', 'cursor-not-allowed'],
18+
},
1519
},
1620
defaultVariants: {
1721
variant: 'default',
@@ -22,7 +26,7 @@ const buttonVariants = cva(
2226
type ButtonVariants = VariantProps<typeof buttonVariants>;
2327

2428
interface ButtonProps
25-
extends React.ButtonHTMLAttributes<HTMLButtonElement>,
29+
extends Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, 'disabled'>,
2630
ButtonVariants {}
2731

2832
export const Button = ({
@@ -35,11 +39,11 @@ export const Button = ({
3539
return (
3640
<button
3741
className={buttonVariants({
38-
variant: disabled ? 'disabled' : variant,
42+
variant: variant,
3943
size,
4044
className,
4145
})}
42-
disabled={disabled}
46+
disabled={disabled || undefined}
4347
{...props}
4448
/>
4549
);
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import React from 'react';
2+
import tokens from '../../../../design_system/output.json';
3+
4+
interface ButtonProps {
5+
variant?: 'primary' | 'secondary'; // Adjust variants as per your design tokens
6+
children: React.ReactNode;
7+
}
8+
9+
interface TokenItem {
10+
variant: 'default' | 'primary' | 'secondary';
11+
size: {
12+
width: number;
13+
height: number;
14+
};
15+
color: {
16+
r: number;
17+
g: number;
18+
b: number;
19+
a: number;
20+
};
21+
}
22+
23+
const FigmaButton: React.FC<ButtonProps> = ({
24+
variant = 'default',
25+
children,
26+
}) => {
27+
const buttonConfig = (tokens as TokenItem[]).find(
28+
(item) => item.variant === variant
29+
);
30+
31+
console.log(tokens, buttonConfig);
32+
if (!buttonConfig) {
33+
// Handle the case where no matching variant is found
34+
return <div>Variant not found{buttonConfig}</div>; // Or return null, or a default button
35+
}
36+
37+
return (
38+
<button
39+
style={{
40+
width: buttonConfig.size.width + 'px',
41+
height: buttonConfig.size.height + 'px',
42+
backgroundColor: `rgba(
43+
${buttonConfig.color.r * 255},
44+
${buttonConfig.color.g * 255},
45+
${buttonConfig.color.b * 255},
46+
${buttonConfig.color.a}
47+
)`,
48+
}}
49+
>
50+
{children}
51+
</button>
52+
);
53+
};
54+
55+
export default FigmaButton;

0 commit comments

Comments
 (0)