Skip to content

Commit cb07de7

Browse files
committed
adopt park-ui architecture for theme colors, with generation from figma core.colors vars
1 parent 426a377 commit cb07de7

24 files changed

+1131
-3962
lines changed

libs/@hashintel/ds-theme/data/variables.json

Lines changed: 912 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import fs from "node:fs";
2+
import { join } from "node:path";
3+
import { camelCase, kebabCase } from "case-anything";
4+
import figmaVariables from "./data/variables.json" with { type: "json" };
5+
6+
const OUTPUT_DIR = "src/theme/colors";
7+
8+
type FigmaColorValue = {
9+
value: { _light: string; _dark: string };
10+
type: "color";
11+
};
12+
13+
type FigmaColorScale = Record<string, FigmaColorValue>;
14+
15+
type FigmaColorCore = Record<string, FigmaColorScale>;
16+
17+
const colorCore = figmaVariables["color.core"] as FigmaColorCore;
18+
19+
function transformColorScale(
20+
scale: FigmaColorScale
21+
): Record<string, { value: { _light: string; _dark: string } }> {
22+
return Object.fromEntries(
23+
Object.entries(scale).map(([step, { value }]) => [step, { value }])
24+
);
25+
}
26+
27+
function formatTokensForOutput(tokens: Record<string, unknown>): string {
28+
const formatValue = (value: unknown): string => {
29+
if (typeof value !== "object" || value === null) {
30+
return JSON.stringify(value);
31+
}
32+
33+
if (Array.isArray(value)) {
34+
return `[${value.map((v) => formatValue(v)).join(", ")}]`;
35+
}
36+
37+
const entries = Object.entries(value);
38+
const formatted = entries
39+
.map(([key, val]) => {
40+
const keyStr = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(key)
41+
? key
42+
: JSON.stringify(key);
43+
return `${keyStr}: ${formatValue(val)}`;
44+
})
45+
.join(", ");
46+
47+
return `{ ${formatted} }`;
48+
};
49+
50+
return formatValue(tokens);
51+
}
52+
53+
function writeColorFile(name: string, tokens: Record<string, unknown>): void {
54+
const fileName = kebabCase(name);
55+
const varName = camelCase(name);
56+
const filePath = join(process.cwd(), OUTPUT_DIR, `${fileName}.ts`);
57+
const formattedTokens = formatTokensForOutput(tokens);
58+
59+
const content = `import { defineSemanticTokens } from "@pandacss/dev";
60+
61+
export const ${varName} = defineSemanticTokens.colors(${formattedTokens});
62+
`;
63+
64+
fs.writeFileSync(filePath, content, "utf8");
65+
console.log(`📄 Created ${fileName}.ts`);
66+
}
67+
68+
function writeIndexFile(colorNames: string[]): void {
69+
const filePath = join(process.cwd(), OUTPUT_DIR, "index.ts");
70+
71+
const imports = colorNames
72+
.map((name) => `import { ${camelCase(name)} } from "./${kebabCase(name)}";`)
73+
.join("\n");
74+
75+
const exports = colorNames.map((name) => camelCase(name)).join(",\n ");
76+
77+
const content = `${imports}
78+
79+
export const colors = {
80+
${exports},
81+
};
82+
`;
83+
84+
fs.writeFileSync(filePath, content, "utf8");
85+
console.log(`📄 Created index.ts`);
86+
}
87+
88+
function main(): void {
89+
console.log("🎨 Generating semantic color tokens from Figma export...");
90+
91+
const outputPath = join(process.cwd(), OUTPUT_DIR);
92+
fs.rmSync(outputPath, { recursive: true, force: true });
93+
fs.mkdirSync(outputPath, { recursive: true });
94+
95+
const colorNames: string[] = [];
96+
97+
for (const [colorName, scale] of Object.entries(colorCore)) {
98+
const tokens = transformColorScale(scale);
99+
writeColorFile(colorName, tokens);
100+
colorNames.push(colorName);
101+
}
102+
103+
writeIndexFile(colorNames);
104+
105+
console.log(`✅ Generated ${colorNames.length} color files`);
106+
}
107+
108+
main();
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@hashintel/ds-theme",
33
"version": "0.0.1",
4-
"description": "HASH UI Preset",
4+
"description": "HASH PandaCSS Preset",
55
"repository": {
66
"type": "git",
77
"url": "https://github.com/hashintel/hash.git",
@@ -12,10 +12,13 @@
1212
"type": "module",
1313
"main": "src/index.ts",
1414
"scripts": {
15-
"generate:panda": "tsx scripts/toPanda.ts"
15+
"generate:colors": "tsx generate-colors.ts",
16+
"lint:tsc": "tsc --noEmit"
1617
},
1718
"devDependencies": {
18-
"es-toolkit": "1.41.0",
19-
"tsx": "4.20.6"
19+
"@pandacss/dev": "1.4.3",
20+
"case-anything": "3.1.0",
21+
"tsx": "4.20.6",
22+
"typescript": "5.9.3"
2023
}
2124
}

libs/@hashintel/ds-theme/scripts/lib/figma.types.ts

Lines changed: 0 additions & 81 deletions
This file was deleted.

0 commit comments

Comments
 (0)