-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvariablesBinder.ts
More file actions
87 lines (76 loc) · 2.46 KB
/
variablesBinder.ts
File metadata and controls
87 lines (76 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { CONFIG } from "../config";
/**
* Binds a color variable to a layer's fill property
*
* @param component - The component containing the target layer
* @param layerName - Name of the layer to bind the variable to
* @param variable - The Figma variable to bind
*/
export function bindFillVariable(
component: ComponentNode,
layerName: string,
variable: Variable,
): void {
const target = component.findOne((node) => node.name === layerName);
if (!target || !("fills" in target)) {
console.warn(`Layer "${layerName}" not found or doesn't support fills`);
return;
}
const paint = figma.variables.setBoundVariableForPaint(
{ type: "SOLID", color: { r: 0, g: 0, b: 0 } },
"color",
variable,
);
target.fills = [paint];
}
/**
* Imports all required variables from the library
*
* @returns Object containing all imported variables
* @throws Error if variables cannot be imported
*/
async function importVariables(): Promise<{
dbLogo: Variable;
logoAddition: Variable;
}> {
const [dbLogo, logoAddition] = await Promise.all([
figma.variables.importVariableByKeyAsync(CONFIG.keys.dbLogo),
figma.variables.importVariableByKeyAsync(CONFIG.keys.logoAddition),
]);
return { dbLogo, logoAddition };
}
/**
* Binds design system variables to component layers and properties
* - Binds color variables to "DB Logo" and "Logo Addition" layers
* - Sets fixed height of 24px to component and SVG Container
* - Locks component aspect ratio
*
* @param component - The component to bind variables to
* @throws Error if variables cannot be imported or bound
*/
export async function bindDesignVariables(
component: ComponentNode,
): Promise<void> {
try {
const variables = await importVariables();
// Bind fill colors to specific layers
bindFillVariable(component, "DB Logo", variables.dbLogo);
bindFillVariable(component, "Logo Addition", variables.logoAddition);
// Set fixed height to SVG Container
const svgContainer = component.findOne(
(node) => node.name === "SVG Container",
);
if (svgContainer && "resize" in svgContainer) {
svgContainer.resize(svgContainer.width, 24);
}
// Set fixed height to component and lock aspect ratio
component.resize(component.width, 24);
component.lockAspectRatio();
component.constraints = {
horizontal: "MIN",
vertical: "MIN",
};
} catch (error) {
throw new Error("Variables could not be linked. Check Library.");
}
}