We traverse the Figma page, get all the variables. We then use those variables and process the nodes to create style tokens. With the style and variable tokens we use this to transform into CSS/SASS.
// Starting from Figma's current page
const nodes = figma.currentPage
// Process each node to extract styles and variables
for (const node of nodes) {
// Get applicable processors for this node type
const processors = getProcessorsForNode(node)
// Process node to extract tokens
const tokens = await processNode(node, processors)
}
The system collects two types of data:
- Style Tokens: Direct CSS properties from nodes
- Variables: Reusable design tokens (colors, typography, etc.)
{
"type": "style",
"node": "TEXT",
"properties": {
"font-family": "Inter",
"font-size": "16px",
"justify-content": "flex-end"
}
}
{
"type": "variable",
"path": [ "background-solid-alpha-variable-style" ],
"property": "fills",
"name": "teal-800-50",
"value": "$teal-800-50",
"rawValue": "rgba(0, 70, 74, 0.5)",
"metadata": {
"figmaId": "104:9",
"variableId": "VariableID:5:22",
"variableName": "teal-800-50"
}
}
We return a list of processors for each type of node. We then pass the node and accumulated variables so that we can build up the appropriate tokens.
export function getProcessorsForNode(node: SceneNode): StyleProcessor[] {
switch (node.type) {
case "TEXT":
return [
...fontProcessors,
...textAlignProcessors
];
case "FRAME":
return [
backgroundProcessor,
...layoutProcessors,
...borderProcessors,
];
}
}
Each processor:
- Receives a node & variables
- Extracts specific properties
- Returns ProcessedValue or null
interface ProcessedValue {
value: string; // CSS/Variable value
rawValue: string; // Original Figma value
}
interface StyleProcessor {
property: string;
bindingKey?: string;
process: (variables: VariableToken[], node?: SceneNode)
=> Promise<ProcessedValue | null>;
}
We have a transformer for CSS and SASS (css being used for development output currently and not available in production mode). This takes the tokens and then transforms the data into the appropriate format.
- Variables are referenced where available
/* Example Output */
.frame {
display: flex;
flex-direction: column;
justify-content: center;
}
.text {
color: red;
font-family: Inter;
font-size: 16px;
}
Converts Figma Auto Layout to CSS Flexbox:
layoutMode
→flex-direction
primaryAxisAlignItems
→justify-content
counterAxisAlignItems
→align-items
Handles text styling and layout:
- Typography: font-family, size, weight
- Text alignment: vertical and horizontal
- Layout properties for text nodes
Processes fills and colors:
- Solid & gradient colors
- Opacity
- Color variables
src/tests/
├── __snapshots__/
│ └── text-processors.test.ts.snap
├── fixtures/
│ └── figma-test-data-alignment.json
└── text-processors.test.ts
// text-processors.test.ts
describe('Text Processor', () => {
it('processes text alignment', async () => {
const node = loadTestData('alignment');
const result = await processNode(node);
expect(result).toMatchSnapshot();
});
});