Skip to content

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

fix: keep bound text and visibility in step with their variables (#954)

Danila Poyarkov6a9b69f67769
diff --git a/CHANGELOG.md b/CHANGELOG.md
index deda1d4b1..a9e3e6027 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -187,7 +187,7 @@
- Keep an AI reply running in the chat panel, with its Stop button, when you switch pages, instead of detaching the panel from the reply in progress.
- Undo an AI edit while another page is on screen; undo previously did nothing until you returned to the page the AI changed.
- Type `parameterConsumptionMap`, `propRefValue`, and `expressionValue` in the Kiwi `NodeChange` codec, which `fig.kiwi` declares but the TypeScript definitions omitted, so reading them no longer needs a cast.
-- Read `.fig` text bound to a string variable, in a component and through an instance override, and keep the bound value where Figma does instead of applying a literal override the layer's binding retires. Text bindings also survive export.
+- Read `.fig` text bound to a string variable, in a component and through an instance override, and keep the bound value where Figma does instead of applying a literal override the layer's binding retires. Text bindings also survive export. Bound text and visibility then follow the variable when its value or the shown mode changes, on the canvas and in saved `.fig` files; a bound font family still changes only when the file is reopened.
- Draw a `.fig` fill or stroke bound to a colour variable at the variable's own transparency, which was previously ignored in favour of the paint's own opacity — so a translucent token drew opaque, and an opacity left over from an override the binding supersedes drew in its place.
- Export the instance overrides you make in OpenPencil to `.fig` beyond text and fill colour — strokes, size, padding and spacing, sizing modes, text styles, visibility, name, opacity, and variable bindings, including the variable bound to an overridden fill — addressed through nested instances so Figma applies each one to the right layer.
- Reject malformed effects assigned to `node.effects` in the plugin API with an error naming the invalid field, as Figma does, instead of storing them. `node.effects` now reads back in Figma's shape: layer blurs are `LAYER_BLUR` with `blurType`, and blurs no longer carry shadow fields (#786).
diff --git a/packages/core/src/layout/variables.ts b/packages/core/src/layout/variables.ts
index efe0ada18..2f58ab62e 100644
--- a/packages/core/src/layout/variables.ts
+++ b/packages/core/src/layout/variables.ts
@@ -1,5 +1,5 @@
import {
- reconcileNumericVariableBindings,
+ reconcileVariableBindings,
type BindingScope,
type SceneGraph
} from '@open-pencil/scene-graph'
@@ -14,7 +14,7 @@ import { createLayoutRunner } from './mutations'
*/
export function reconcileVariableLayouts(graph: SceneGraph, scope: BindingScope): void {
const { runLayoutForNode } = createLayoutRunner(() => graph)
- for (const id of reconcileNumericVariableBindings(graph, scope)) {
+ for (const id of reconcileVariableBindings(graph, scope)) {
// A deliberate binding change also invalidates a placed instance's saved Hug size.
computeLayout(graph, id)
runLayoutForNode(id)
diff --git a/packages/core/tests/editor/variables/value-bindings.test.ts b/packages/core/tests/editor/variables/value-bindings.test.ts
new file mode 100644
index 000000000..c0e7c08bc
--- /dev/null
+++ b/packages/core/tests/editor/variables/value-bindings.test.ts
@@ -0,0 +1,76 @@
+import { expect, test } from 'bun:test'
+
+import { createEditor } from '@open-pencil/core/editor'
+import { SceneGraph, setInstanceOverride } from '@open-pencil/scene-graph'
+
+import { expectDefined } from '#core-tests/helpers/assert'
+
+function boundText() {
+ const graph = new SceneGraph()
+ const copy = graph.createCollection('Copy')
+ const french = graph.createMode(copy.id, 'French') ?? ''
+ const label = graph.createVariable('Button/Label', 'STRING', copy.id, 'Save')
+ label.valuesByMode[french] = 'Enregistrer'
+ const shown = graph.createVariable('Button/Shown', 'BOOLEAN', copy.id, true)
+ shown.valuesByMode[french] = false
+ const page = graph.getPages()[0].id
+ const text = graph.createNode('TEXT', page, {
+ text: 'Save',
+ boundVariables: { text: label.id, visible: shown.id }
+ })
+ return { editor: createEditor({ graph }), copy, french, label, text }
+}
+
+test('text bound to a variable follows an edited value and its undo', () => {
+ const { editor, copy, label, text } = boundText()
+
+ editor.updateVariableValue(label.id, copy.defaultModeId, 'Save changes')
+ expect(text.text).toBe('Save changes')
+
+ editor.undo.undo()
+ expect(text.text).toBe('Save')
+})
+
+test('bound text and visibility follow the mode the collection shows', () => {
+ const { editor, copy, french, text } = boundText()
+
+ editor.setActiveMode(copy.id, french)
+
+ expect(text.text).toBe('Enregistrer')
+ expect(text.visible).toBe(false)
+})
+
+test('bound text follows a mode set on the layer', () => {
+ const { editor, copy, french, text } = boundText()
+
+ editor.updateNodeWithUndo(text.id, { variableModes: { [copy.id]: french } }, 'Set mode')
+
+ expect(text.text).toBe('Enregistrer')
+})
+
+test('text and visibility an instance overrides keep their own values', () => {
+ const graph = new SceneGraph()
+ const page = graph.getPages()[0].id
+ const copy = graph.createCollection('Copy')
+ const label = graph.createVariable('Label', 'STRING', copy.id, 'Save')
+ const shown = graph.createVariable('Shown', 'BOOLEAN', copy.id, true)
+ const button = graph.createNode('COMPONENT', page)
+ graph.createNode('TEXT', button.id, {
+ text: 'Save',
+ boundVariables: { text: label.id, visible: shown.id }
+ })
+ const placed = expectDefined(graph.createInstance(button.id, page))
+ const layer = expectDefined(graph.getNode(placed.childIds[0]))
+ const editor = createEditor({ graph })
+
+ // Typing in the layer records its text as the instance's own, as the text editor does.
+ setInstanceOverride(placed.instanceOverrides, placed.id, layer.id, 'text', 'Keep')
+ graph.updateNode(layer.id, { text: 'Keep' })
+ editor.toggleNodeVisibility(layer.id)
+
+ editor.updateVariableValue(label.id, copy.defaultModeId, 'Save changes')
+ editor.updateVariableValue(shown.id, copy.defaultModeId, true)
+
+ expect(layer.text).toBe('Keep')
+ expect(layer.visible).toBe(false)
+})
diff --git a/packages/docs/development/roadmap.md b/packages/docs/development/roadmap.md
index 99f172c5a..838d0f7b6 100644
--- a/packages/docs/development/roadmap.md
+++ b/packages/docs/development/roadmap.md
@@ -225,7 +225,7 @@ Figma's design documentation groups features into these areas:
| Font variations / OpenType features | ✅ | ✅ | — | ✅ | — | Imported `fontVariations`, common ligature/caps/numeric OpenType fields, and raw `toggledOnOTFeatures` / `toggledOffOTFeatures` are applied to CanvasKit text styles and exported; UI controls are not exposed. |
| Variables: collections/modes/aliases | ✅ | ◐ | ◐ | ✅ | ✅ | Color/number/string/boolean model exists; inspector coverage is still incomplete. |
| Variables bound to fills/strokes | ✅ | ✅ | ✅ | ✅ | ✅ | Common color bindings render and edit. |
-| Variables bound to text/layout/visibility/effects | ◐ | ◐ | ◐ | ◐ | ✅ | Text content and numeric layout bindings resolve on read; colour and numeric bindings also re-resolve when a variable or mode changes, string ones do not. Effect bindings are not modelled. |
+| Variables bound to text/layout/visibility/effects | ◐ | ◐ | ◐ | ◐ | ✅ | Text content and numeric layout bindings resolve on read; colour, numeric, text, and visibility bindings also re-resolve when a variable or mode changes, font family ones do not. Effect bindings are not modelled. |
| Variables in prototypes / expressions / conditionals | — | — | — | — | — | Depends on prototype system, which is not implemented. |
| Libraries / publish / update review | ↩ | — | ◐ | ↩ | — | Figma library metadata survives round-trip. OpenPencil supports its own component-library publishing, catalogs, revision previews, and linked-instance updates, not synchronization with Figma-hosted libraries. |
| Prototype flows / starting points | — | — | — | — | — | Not modeled. |
diff --git a/packages/fig/src/document/bindings/text.ts b/packages/fig/src/document/bindings/text.ts
index da147be34..e9f6ed9f8 100644
--- a/packages/fig/src/document/bindings/text.ts
+++ b/packages/fig/src/document/bindings/text.ts
@@ -1,10 +1,8 @@
import type { SceneGraph, SceneNode } from '@open-pencil/scene-graph'
/**
- * Resolve text bound to a string variable once occurrence hierarchy and modes exist. Read
- * time is the only time: unlike a numeric binding, nothing re-resolves a string one when the
- * variable or the node's mode changes later, so edited text goes stale until the next read.
- * Figma stores a bound layer's resolved characters, but an instance override carries the
+ * Resolve text bound to a string variable once occurrence hierarchy and modes exist; later
+ * variable and mode changes re-resolve it through the editor's binding reconcile. Figma stores a bound layer's resolved characters, but an instance override carries the
* binding alone, and a literal override of a bound layer is retired rather than applied —
* so the binding, not the claim, decides what a bound layer reads.
*/
diff --git a/packages/fig/src/node-change/export/resolved-bindings.ts b/packages/fig/src/node-change/export/resolved-bindings.ts
index 6d5524eeb..2b78d8ab2 100644
--- a/packages/fig/src/node-change/export/resolved-bindings.ts
+++ b/packages/fig/src/node-change/export/resolved-bindings.ts
@@ -1,6 +1,7 @@
import {
resolvedNumericBindings,
resolvedPaintBindings,
+ resolvedValueBindings,
type SceneGraph,
type SceneNode
} from '@open-pencil/scene-graph'
@@ -15,7 +16,8 @@ import {
export function nodeWithResolvedBindings(graph: SceneGraph, node: SceneNode): SceneNode {
const changes = {
...resolvedNumericBindings(graph, node, 'default'),
- ...resolvedPaintBindings(graph, node, 'default')
+ ...resolvedPaintBindings(graph, node, 'default'),
+ ...resolvedValueBindings(graph, node, 'default')
}
return Object.keys(changes).length > 0 ? { ...node, ...changes } : node
}
diff --git a/packages/scene-graph/src/index.ts b/packages/scene-graph/src/index.ts
index cb31fae72..299224cf1 100644
--- a/packages/scene-graph/src/index.ts
+++ b/packages/scene-graph/src/index.ts
@@ -323,8 +323,20 @@ export class SceneGraph {
return Variables.resolveNumberVariableForNode(this, nodeId, variableId, fallback)
}
- resolveStringVariableForNode(nodeId: string, variableId: string): string | undefined {
- return Variables.resolveStringVariableForNode(this, nodeId, variableId)
+ resolveVariableForNode(
+ nodeId: string,
+ variableId: string,
+ fallback?: VariableModeFallback
+ ): VariableValue | undefined {
+ return Variables.resolveVariableForNode(this, nodeId, variableId, fallback)
+ }
+
+ resolveStringVariableForNode(
+ nodeId: string,
+ variableId: string,
+ fallback?: VariableModeFallback
+ ): string | undefined {
+ return Variables.resolveStringVariableForNode(this, nodeId, variableId, fallback)
}
getVariablesForCollection(collectionId: string): Variable[] {
diff --git a/packages/scene-graph/src/variables.ts b/packages/scene-graph/src/variables.ts
index 7a54494eb..782605068 100644
--- a/packages/scene-graph/src/variables.ts
+++ b/packages/scene-graph/src/variables.ts
@@ -307,15 +307,26 @@ export function resolveNumberVariableForNode(
return typeof value === 'number' ? value : undefined
}
-export function resolveStringVariableForNode(
+/** A variable's value in the mode a node is in, aliases followed. */
+export function resolveVariableForNode(
graph: SceneGraph,
nodeId: string,
- variableId: string
-): string | undefined {
+ variableId: string,
+ fallback: VariableModeFallback = 'active'
+): VariableValue | undefined {
const variable = graph.variables.get(variableId)
if (!variable) return undefined
- const modeId = getNodeVariableModeId(graph, nodeId, variable.collectionId)
- const value = resolveVariable(graph, variableId, modeId)
+ const modeId = getNodeVariableModeId(graph, nodeId, variable.collectionId, fallback)
+ return resolveVariable(graph, variableId, modeId)
+}
+
+export function resolveStringVariableForNode(
+ graph: SceneGraph,
+ nodeId: string,
+ variableId: string,
+ fallback: VariableModeFallback = 'active'
+): string | undefined {
+ const value = resolveVariableForNode(graph, nodeId, variableId, fallback)
return typeof value === 'string' ? value : undefined
}
diff --git a/packages/scene-graph/src/variables/bindings.ts b/packages/scene-graph/src/variables/bindings.ts
index 50061e797..8de633e40 100644
--- a/packages/scene-graph/src/variables/bindings.ts
+++ b/packages/scene-graph/src/variables/bindings.ts
@@ -57,6 +57,31 @@ export function resolvedNumericBindings(
return updates
}
+/**
+ * The text and visibility a node's string and boolean bindings resolve to in its mode, for the
+ * fields whose stored value differs. Text or visibility an instance overrides, which typing in a
+ * layer or toggling its eye records, keeps its own value, as an overridden width or height does.
+ * A bound font family is left as stored: changing it needs the font loaded, which resolving a
+ * binding cannot do.
+ */
+export function resolvedValueBindings(
+ graph: SceneGraph,
+ node: SceneNode,
+ fallback: VariableModeFallback = 'active'
+): Partial<SceneNode> {
+ const updates: Partial<SceneNode> = {}
+ const resolve = (field: 'text' | 'visible') => {
+ const variableId = node.boundVariables[field]
+ if (!variableId || hasInstanceOverride(graph, node.id, field)) return undefined
+ return graph.resolveVariableForNode(node.id, variableId, fallback)
+ }
+ const text = node.type === 'TEXT' ? resolve('text') : undefined
+ if (typeof text === 'string' && text !== node.text) updates.text = text
+ const visible = resolve('visible')
+ if (typeof visible === 'boolean' && visible !== node.visible) updates.visible = visible
+ return updates
+}
+
/**
* A bound paint takes its whole color from the variable, alpha included, the way Figma stores
* it: the variable's RGB at full alpha, and its alpha as the paint's opacity.
@@ -142,17 +167,17 @@ function scopedNodes(graph: SceneGraph, scope: BindingScope | undefined): Iterab
}
/**
- * Resolve live numeric bindings in scene units, without authoring instance overrides. A scope
- * limits it to the layers a change can reach, so layers whose saved values differ from their
- * bindings elsewhere in the document are left as saved.
+ * Resolve live numeric, text, and visibility bindings, numbers in scene units, without authoring
+ * instance overrides. A scope limits it to the layers a change can reach, so layers whose saved
+ * values differ from their bindings elsewhere in the document are left as saved.
*/
-export function reconcileNumericVariableBindings(
- graph: SceneGraph,
- scope?: BindingScope
-): string[] {
+export function reconcileVariableBindings(graph: SceneGraph, scope?: BindingScope): string[] {
const changed: string[] = []
for (const node of scopedNodes(graph, scope)) {
- const updates = resolvedNumericBindings(graph, node)
+ const updates = {
+ ...resolvedNumericBindings(graph, node),
+ ...resolvedValueBindings(graph, node)
+ }
if (Object.keys(updates).length === 0) continue
graph.updateNode(node.id, updates)
changed.push(node.id)
diff --git a/packages/scene-graph/tests/variable/binding/scope.test.ts b/packages/scene-graph/tests/variable/binding/scope.test.ts
index 00d8f38d4..2999ddc81 100644
--- a/packages/scene-graph/tests/variable/binding/scope.test.ts
+++ b/packages/scene-graph/tests/variable/binding/scope.test.ts
@@ -1,7 +1,7 @@
import { describe, expect, test } from 'bun:test'
import {
- reconcileNumericVariableBindings,
+ reconcileVariableBindings,
SceneGraph,
variablesResolvingThrough
} from '@open-pencil/scene-graph'
@@ -41,7 +41,7 @@ describe('binding scopes', () => {
test('a variable scope resolves only the layers bound to it or its aliases', () => {
const { graph, base, frame, child, elsewhere } = drifted()
- expect(reconcileNumericVariableBindings(graph, { variables: [base.id] })).toEqual([frame.id])
+ expect(reconcileVariableBindings(graph, { variables: [base.id] })).toEqual([frame.id])
expect(frame.width).toBe(40)
expect([child.width, elsewhere.width]).toEqual([10, 10])
})
@@ -49,7 +49,7 @@ describe('binding scopes', () => {
test('a subtree scope resolves the layers inside it and nothing else', () => {
const { graph, frame, child, elsewhere } = drifted()
- reconcileNumericVariableBindings(graph, { subtrees: [frame.id] })
+ reconcileVariableBindings(graph, { subtrees: [frame.id] })
expect([frame.width, child.width, elsewhere.width]).toEqual([40, 24, 10])
})
diff --git a/tests/e2e/canvas/instance-scale-visual.spec.ts b/tests/e2e/canvas/instance-scale-visual.spec.ts
index b6a880ab7..bfc02a1e0 100644
--- a/tests/e2e/canvas/instance-scale-visual.spec.ts
+++ b/tests/e2e/canvas/instance-scale-visual.spec.ts
@@ -5,7 +5,7 @@ import { computeAllLayouts, computeLayout } from '@open-pencil/core/layout'
import { materializeDocument } from '@open-pencil/fig'
import type { NodeChange } from '@open-pencil/kiwi/fig/codec'
import {
- reconcileNumericVariableBindings,
+ reconcileVariableBindings,
rescaleNodeTree,
type SceneNode,
type SceneGraph
@@ -112,7 +112,7 @@ test('saved inherited expressions follow token edits without double scaling', as
expect(nested.paddingLeft).toBe(5)
const before = renderTree(graph, rootId)
variable.valuesByMode[graph.getNodeVariableModeId(nested.id, variable.collectionId)] = 40
- reconcileNumericVariableBindings(graph)
+ reconcileVariableBindings(graph)
computeLayout(graph, nested.id)
computeLayout(graph, rootId)
expect(nested.paddingLeft).toBe(10)
@@ -158,7 +158,7 @@ for (const operation of ['component', 'rescale'] as const) {
const nested = graph.getChildren(rootId)[0]
graph.bindVariable(rootId, 'paddingRight', '293742:7')
graph.bindVariable(nested.id, 'paddingRight', '293742:7')
- reconcileNumericVariableBindings(graph)
+ reconcileVariableBindings(graph)
computeLayout(graph, nested.id)
computeLayout(graph, rootId)
const before = renderTree(graph, rootId)