Skip to content

AI-native design editor. Open-source Figma alternative.

fix(layout): resolve design token variables before passing to yoga-layout (#202)

Lay Flags7ae97935e259
diff --git a/packages/core/src/io/formats/pen/convert.ts b/packages/core/src/io/formats/pen/convert.ts
index ef8e8bc2d..460386cb1 100644
--- a/packages/core/src/io/formats/pen/convert.ts
+++ b/packages/core/src/io/formats/pen/convert.ts
@@ -86,8 +86,8 @@ export interface PenNode {
stroke?: PenStroke
effect?: PenEffect | PenEffect[]
layout?: string
- gap?: number
- padding?: number | number[]
+ gap?: number | string
+ padding?: number | string | (number | string)[]
justifyContent?: string
alignItems?: string
children?: PenNode[]
@@ -380,19 +380,22 @@ export function applyCornerRadius(
node.cornerRadius = parseSize(radius, 0, ctx).value
}
-export function applyPadding(node: SceneNode, padding: PenNode['padding']): void {
+export function applyPadding(node: SceneNode, padding: PenNode['padding'], ctx?: VarContext): void {
if (padding === undefined) return
+ const resolve = (v: number | string): number =>
+ typeof v === 'string' ? (isVarRef(v) && ctx ? ctx.resolveNumber(v) : (Number(v) || 0)) : v
if (Array.isArray(padding)) {
- node.paddingTop = padding[0] ?? 0
- node.paddingRight = padding[1] ?? 0
- node.paddingBottom = padding[2] ?? 0
- node.paddingLeft = padding[3] ?? 0
+ node.paddingTop = resolve(padding[0] ?? 0)
+ node.paddingRight = resolve(padding[1] ?? 0)
+ node.paddingBottom = resolve(padding[2] ?? 0)
+ node.paddingLeft = resolve(padding[3] ?? 0)
return
}
- node.paddingTop = padding
- node.paddingRight = padding
- node.paddingBottom = padding
- node.paddingLeft = padding
+ const resolved = resolve(padding)
+ node.paddingTop = resolved
+ node.paddingRight = resolved
+ node.paddingBottom = resolved
+ node.paddingLeft = resolved
}
export function parseSize(value: number | string | undefined, fallback: number, ctx?: VarContext) {
diff --git a/packages/core/src/io/formats/pen/read.ts b/packages/core/src/io/formats/pen/read.ts
index 922c9b3c3..8bc244b0d 100644
--- a/packages/core/src/io/formats/pen/read.ts
+++ b/packages/core/src/io/formats/pen/read.ts
@@ -81,12 +81,15 @@ function applyAutoLayout(
layoutMode: LayoutMode,
pen: PenNode,
widthSizing: LayoutSizing,
- heightSizing: LayoutSizing
+ heightSizing: LayoutSizing,
+ ctx?: VarContext
): void {
overrides.layoutMode = layoutMode
overrides.primaryAxisAlign = mapJustifyContent(pen.justifyContent)
overrides.counterAxisAlign = mapAlignItems(pen.alignItems)
- overrides.itemSpacing = pen.gap ?? 0
+ overrides.itemSpacing = typeof pen.gap === 'string' && isVarRef(pen.gap) && ctx
+ ? ctx.resolveNumber(pen.gap)
+ : (pen.gap ?? 0) as number
if (layoutMode === 'VERTICAL') {
overrides.primaryAxisSizing = heightSizing
@@ -236,7 +239,7 @@ function createSceneNode(
parentLayout === 'NONE' && w.sizing === 'FILL' ? ('FIXED' as LayoutSizing) : w.sizing
const heightSizing =
parentLayout === 'NONE' && h.sizing === 'FILL' ? ('FIXED' as LayoutSizing) : h.sizing
- applyAutoLayout(overrides, layout, pen, widthSizing, heightSizing)
+ applyAutoLayout(overrides, layout, pen, widthSizing, heightSizing, ctx)
}
const node = graph.createNode(mapNodeType(pen), parentId, overrides)
@@ -245,7 +248,7 @@ function createSceneNode(
if (pen.stroke) node.strokes = convertStroke(pen.stroke, ctx, node)
node.effects = convertEffects(pen.effect)
applyCornerRadius(node, pen.cornerRadius, ctx)
- applyPadding(node, pen.padding)
+ applyPadding(node, pen.padding, ctx)
if (isTextLike) {
applyTextProps(node, pen, ctx)
diff --git a/tests/engine/pen-var-padding.test.ts b/tests/engine/pen-var-padding.test.ts
new file mode 100644
index 000000000..bb7e35fa6
--- /dev/null
+++ b/tests/engine/pen-var-padding.test.ts
@@ -0,0 +1,137 @@
+import { describe, expect, test } from 'bun:test'
+
+import { applyPadding, isVarRef, type VarContext } from '@open-pencil/core/io/formats/pen'
+import type { SceneNode } from '@open-pencil/core'
+
+/**
+ * Regression test for open-pencil/open-pencil#201
+ *
+ * Design token variables ($--spacing-lg etc.) in padding and gap fields
+ * must be resolved to numeric values before reaching yoga-layout.
+ * Without the fix, yoga-layout crashes with:
+ * "Invalid value $--spacing-lg for setPadding"
+ */
+
+function makeNode(): SceneNode {
+ return {
+ paddingTop: 0,
+ paddingRight: 0,
+ paddingBottom: 0,
+ paddingLeft: 0,
+ boundVariables: {},
+ } as unknown as SceneNode
+}
+
+function makeVarContext(vars: Record<string, number>): VarContext {
+ const byName = new Map<string, { id: string; variable: { valuesByMode: Record<string, number> } }>()
+ for (const [name, value] of Object.entries(vars)) {
+ byName.set(name, { id: name, variable: { valuesByMode: { default: value } } })
+ }
+ return {
+ byName,
+ activeModeId: 'default',
+ collectionId: 'test',
+ modeByThemeName: new Map(),
+ resolveColor: () => ({ r: 0, g: 0, b: 0, a: 1 }),
+ resolveNumber(ref: string): number {
+ const entry = byName.get(ref.replace(/^\$/, ''))
+ if (!entry) return 0
+ const val = entry.variable.valuesByMode['default']
+ return typeof val === 'number' ? val : 0
+ },
+ resolveString: () => '',
+ setActiveTheme: () => {},
+ } as unknown as VarContext
+}
+
+const ctx = makeVarContext({
+ '--spacing-sm': 8,
+ '--spacing-md': 16,
+ '--spacing-lg': 24,
+})
+
+describe('applyPadding — variable resolution (#201)', () => {
+ test('resolves $--spacing-lg variable as single padding value', () => {
+ const node = makeNode()
+ applyPadding(node, '$--spacing-lg', ctx)
+
+ expect(node.paddingTop).toBe(24)
+ expect(node.paddingRight).toBe(24)
+ expect(node.paddingBottom).toBe(24)
+ expect(node.paddingLeft).toBe(24)
+ })
+
+ test('resolves variable references in padding array', () => {
+ const node = makeNode()
+ applyPadding(node, ['$--spacing-sm', '$--spacing-lg', '$--spacing-sm', '$--spacing-lg'], ctx)
+
+ expect(node.paddingTop).toBe(8)
+ expect(node.paddingRight).toBe(24)
+ expect(node.paddingBottom).toBe(8)
+ expect(node.paddingLeft).toBe(24)
+ })
+
+ test('mixed numeric and variable padding array', () => {
+ const node = makeNode()
+ applyPadding(node, [10, '$--spacing-lg', 10, '$--spacing-sm'], ctx)
+
+ expect(node.paddingTop).toBe(10)
+ expect(node.paddingRight).toBe(24)
+ expect(node.paddingBottom).toBe(10)
+ expect(node.paddingLeft).toBe(8)
+ })
+
+ test('numeric padding still works (regression)', () => {
+ const node = makeNode()
+ applyPadding(node, [10, 20, 10, 20])
+
+ expect(node.paddingTop).toBe(10)
+ expect(node.paddingRight).toBe(20)
+ expect(node.paddingBottom).toBe(10)
+ expect(node.paddingLeft).toBe(20)
+ })
+
+ test('single numeric padding still works (regression)', () => {
+ const node = makeNode()
+ applyPadding(node, 16)
+
+ expect(node.paddingTop).toBe(16)
+ expect(node.paddingRight).toBe(16)
+ expect(node.paddingBottom).toBe(16)
+ expect(node.paddingLeft).toBe(16)
+ })
+
+ test('undefined padding is a no-op', () => {
+ const node = makeNode()
+ node.paddingTop = 99
+ applyPadding(node, undefined, ctx)
+
+ expect(node.paddingTop).toBe(99)
+ })
+
+ test('unresolvable variable falls back to 0', () => {
+ const node = makeNode()
+ applyPadding(node, '$--unknown-var', ctx)
+
+ expect(node.paddingTop).toBe(0)
+ })
+})
+
+describe('isVarRef', () => {
+ test('recognizes $--xxx as variable reference', () => {
+ expect(isVarRef('$--spacing-lg')).toBe(true)
+ expect(isVarRef('$--color-primary')).toBe(true)
+ })
+
+ test('rejects non-variable strings', () => {
+ expect(isVarRef('24')).toBe(false)
+ expect(isVarRef('spacing-lg')).toBe(false)
+ expect(isVarRef('')).toBe(false)
+ })
+
+ test('rejects non-string values', () => {
+ expect(isVarRef(24)).toBe(false)
+ expect(isVarRef(undefined)).toBe(false)
+ expect(isVarRef(null)).toBe(false)
+ })
+})