Skip to content

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

fix(layout): match Figma auto-layout reflow

Danila Poyarkovd522dc0529a9
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 18ea195b8..33dced748 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -34,6 +34,7 @@
### Fixes
+- Match Figma auto-layout reflow when deleting children or hiding optional instance slots, including HUG-height component instances.
- Fix desktop clipboard copy/cut/paste by using Tauri's system clipboard bridge when browser clipboard events are unavailable.
- Add AI provider connection testing with clearer setup errors for OpenAI-compatible endpoints.
- Increase per-test timeout for slow `gold-preview.fig` fixture tests (`clipboard roundtrip`, `group reclassification`, `glyph blob preservation`, `auto-layout text measurement`, and `render/canvas/cache`) so they no longer flake on slower CI runners.
diff --git a/packages/core/src/editor/clipboard.ts b/packages/core/src/editor/clipboard.ts
index 3176aa6c3..db1984633 100644
--- a/packages/core/src/editor/clipboard.ts
+++ b/packages/core/src/editor/clipboard.ts
@@ -195,17 +195,26 @@ export function createClipboardActions(ctx: EditorContext) {
}
if (entries.length === 0) return
+ const relayoutParents = () => {
+ for (const parentId of new Set(entries.map((entry) => entry.parentId))) {
+ ctx.runLayoutForNode(parentId)
+ }
+ }
+
const prevSelection = new Set(ctx.state.selectedIds)
for (const { id } of entries) ctx.graph.deleteNode(id)
+ relayoutParents()
ctx.undo.push({
label: 'Delete',
forward: () => {
for (const { id } of entries) ctx.graph.deleteNode(id)
+ relayoutParents()
ctx.setSelectedIds(new Set())
},
inverse: () => {
restoreDeletedEntries(ctx, entries)
+ relayoutParents()
ctx.setSelectedIds(prevSelection)
}
})
diff --git a/packages/core/src/editor/structure/state.ts b/packages/core/src/editor/structure/state.ts
index 275d0ee46..9912e2293 100644
--- a/packages/core/src/editor/structure/state.ts
+++ b/packages/core/src/editor/structure/state.ts
@@ -5,6 +5,7 @@ export function createStructureStateActions(ctx: EditorContext) {
const node = ctx.graph.getNode(id)
if (!node) return
ctx.graph.updateNode(id, { visible: !node.visible })
+ if (node.parentId) ctx.runLayoutForNode(node.parentId)
}
function toggleNodeLock(id: string) {
diff --git a/packages/core/src/layout.ts b/packages/core/src/layout.ts
index a688ea424..d653d726c 100644
--- a/packages/core/src/layout.ts
+++ b/packages/core/src/layout.ts
@@ -75,11 +75,15 @@ function computeLayoutsBottomUp(graph: SceneGraph, nodeId: string, visited: Set<
computeLayoutsBottomUp(graph, childId, visited)
}
- if (node.layoutMode !== 'NONE' && node.type !== 'INSTANCE') {
+ if (node.layoutMode !== 'NONE' && !preservesImportedInstanceLayout(node)) {
computeLayout(graph, nodeId)
}
}
+function preservesImportedInstanceLayout(node: SceneNode): boolean {
+ return node.type === 'INSTANCE' && node.source.format === 'fig'
+}
+
// --- Flex layout ---
function buildYogaTree(
diff --git a/tests/engine/editor/auto-layout-reflow.test.ts b/tests/engine/editor/auto-layout-reflow.test.ts
new file mode 100644
index 000000000..46340e570
--- /dev/null
+++ b/tests/engine/editor/auto-layout-reflow.test.ts
@@ -0,0 +1,63 @@
+import { describe, expect, test } from 'bun:test'
+
+import { createEditor } from '@open-pencil/core/editor'
+import { computeAllLayouts } from '@open-pencil/core/layout'
+
+import { getNodeOrThrow } from '#tests/helpers/assert'
+import { autoFrame, rect } from '#tests/helpers/layout'
+
+describe('editor auto-layout reflow', () => {
+ test('deleteSelected reflows fixed vertical auto-layout siblings', () => {
+ const editor = createEditor()
+ const page = editor.state.currentPageId
+ const frame = autoFrame(editor.graph, page, {
+ layoutMode: 'VERTICAL',
+ width: 300,
+ height: 300,
+ itemSpacing: 0
+ })
+ rect(editor.graph, frame.id, 300, 40)
+ const body = rect(editor.graph, frame.id, 300, 100, { layoutGrow: 1 })
+ const footerA = rect(editor.graph, frame.id, 300, 40)
+ const footerB = rect(editor.graph, frame.id, 300, 40)
+ computeAllLayouts(editor.graph, page)
+
+ editor.select([footerB.id])
+ editor.deleteSelected()
+
+ expect(getNodeOrThrow(editor.graph, body.id).height).toBe(220)
+ expect(getNodeOrThrow(editor.graph, footerA.id).y).toBe(260)
+ })
+
+ test('toggleNodeVisibility reflows HUG auto-layout instance slots', () => {
+ const editor = createEditor()
+ const page = editor.state.currentPageId
+ const component = editor.graph.createNode('COMPONENT', page, {
+ layoutMode: 'VERTICAL',
+ primaryAxisSizing: 'HUG',
+ counterAxisSizing: 'FIXED',
+ width: 300,
+ height: 1,
+ itemSpacing: 0
+ })
+ rect(editor.graph, component.id, 300, 40, { name: 'Slot / Pinned at Top' })
+ rect(editor.graph, component.id, 300, 74, { name: 'Slot / Content' })
+ rect(editor.graph, component.id, 300, 40, { name: 'Slot / Pinned at Bottom' })
+ const instance = editor.graph.createInstance(component.id, page)
+ if (!instance) throw new Error('Expected instance')
+ computeAllLayouts(editor.graph, page)
+
+ const content = editor.graph
+ .getChildren(instance.id)
+ .find((child) => child.name === 'Slot / Content')
+ const bottom = editor.graph
+ .getChildren(instance.id)
+ .find((child) => child.name === 'Slot / Pinned at Bottom')
+ if (!content || !bottom) throw new Error('Expected instance slot children')
+
+ editor.toggleNodeVisibility(content.id)
+
+ expect(getNodeOrThrow(editor.graph, instance.id).height).toBe(80)
+ expect(getNodeOrThrow(editor.graph, bottom.id).y).toBe(40)
+ })
+})
diff --git a/tests/engine/layout/auto-layout/figma-oracle-regressions.test.ts b/tests/engine/layout/auto-layout/figma-oracle-regressions.test.ts
new file mode 100644
index 000000000..681e5c25e
--- /dev/null
+++ b/tests/engine/layout/auto-layout/figma-oracle-regressions.test.ts
@@ -0,0 +1,69 @@
+import { describe, expect, test } from 'bun:test'
+
+import { computeAllLayouts, SceneGraph } from '@open-pencil/core'
+
+import { getNodeOrThrow } from '#tests/helpers/assert'
+import { autoFrame, pageId, rect } from '#tests/helpers/layout'
+
+describe('Figma auto-layout oracle regressions', () => {
+ test('deleting a trailing child reflows fill siblings in fixed vertical auto-layout', () => {
+ const graph = new SceneGraph()
+ const page = pageId(graph)
+ const frame = autoFrame(graph, page, {
+ layoutMode: 'VERTICAL',
+ width: 300,
+ height: 300,
+ itemSpacing: 0
+ })
+ rect(graph, frame.id, 300, 40, { name: 'header h40' })
+ const body = rect(graph, frame.id, 300, 100, { name: 'body fill', layoutGrow: 1 })
+ const footerA = rect(graph, frame.id, 300, 40, { name: 'footerA h40' })
+ const footerB = rect(graph, frame.id, 300, 40, { name: 'footerB h40' })
+
+ computeAllLayouts(graph, page)
+ expect(getNodeOrThrow(graph, body.id).height).toBe(180)
+ expect(getNodeOrThrow(graph, footerA.id).y).toBe(220)
+
+ graph.deleteNode(footerB.id)
+ computeAllLayouts(graph, page)
+
+ expect(getNodeOrThrow(graph, body.id).height).toBe(220)
+ expect(getNodeOrThrow(graph, footerA.id).y).toBe(260)
+ expect(getNodeOrThrow(graph, frame.id).height).toBe(300)
+ })
+
+ test('hidden child in HUG auto-layout instance is excluded from layout', () => {
+ const graph = new SceneGraph()
+ const page = pageId(graph)
+ const component = graph.createNode('COMPONENT', page, {
+ name: 'Optional slot component',
+ layoutMode: 'VERTICAL',
+ primaryAxisSizing: 'HUG',
+ counterAxisSizing: 'FIXED',
+ width: 300,
+ height: 1,
+ itemSpacing: 0
+ })
+ rect(graph, component.id, 300, 40, { name: 'Slot / Pinned at Top' })
+ rect(graph, component.id, 300, 74, { name: 'Slot / Content' })
+ rect(graph, component.id, 300, 40, { name: 'Slot / Pinned at Bottom' })
+ const instance = graph.createInstance(component.id, page, { x: 360, y: 0 })
+ if (!instance) throw new Error('Expected instance to be created')
+
+ computeAllLayouts(graph, page)
+ expect(getNodeOrThrow(graph, instance.id).height).toBe(154)
+ const content = graph.getChildren(instance.id).find((child) => child.name === 'Slot / Content')
+ const bottom = graph
+ .getChildren(instance.id)
+ .find((child) => child.name === 'Slot / Pinned at Bottom')
+ if (!content || !bottom) throw new Error('Expected instance slot children')
+ expect(getNodeOrThrow(graph, bottom.id).y).toBe(114)
+
+ graph.updateNode(content.id, { visible: false })
+ computeAllLayouts(graph, page)
+
+ expect(getNodeOrThrow(graph, instance.id).height).toBe(80)
+ expect(getNodeOrThrow(graph, bottom.id).y).toBe(40)
+ expect(getNodeOrThrow(graph, content.id).height).toBe(74)
+ })
+})