Skip to content

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

Populate instance children from components on .fig import

Danila Poyarkov362212b7123c
diff --git a/packages/core/src/kiwi/fig-import.ts b/packages/core/src/kiwi/fig-import.ts
index 38494a2d9..035dcdcbc 100644
--- a/packages/core/src/kiwi/fig-import.ts
+++ b/packages/core/src/kiwi/fig-import.ts
@@ -54,6 +54,7 @@ export function importNodeChanges(
}
const created = new Set<string>()
+ const guidToNodeId = new Map<string, string>()
function createSceneNode(ncId: string, graphParentId: string) {
if (created.has(ncId)) return
@@ -66,6 +67,7 @@ export function importNodeChanges(
if (nodeType === 'DOCUMENT' || nodeType === 'VARIABLE') return
const node = graph.createNode(nodeType, graphParentId, props)
+ guidToNodeId.set(ncId, node.id)
for (const childId of getChildren(ncId)) {
createSceneNode(childId, node.id)
@@ -168,6 +170,29 @@ export function importNodeChanges(
importVariables()
+ // Remap componentId from original Figma GUIDs to imported node IDs
+ for (const node of graph.getAllNodes()) {
+ if (node.type !== 'INSTANCE' || !node.componentId) continue
+ const remapped = guidToNodeId.get(node.componentId)
+ if (remapped) node.componentId = remapped
+ }
+
+ // Populate instance children from their components.
+ // Multiple passes needed: cloning creates new instance nodes that
+ // themselves need population.
+ let populated = 1
+ while (populated > 0) {
+ populated = 0
+ for (const node of graph.getAllNodes()) {
+ if (node.type !== 'INSTANCE' || !node.componentId || node.childIds.length > 0) continue
+ const comp = graph.getNode(node.componentId)
+ if (comp && comp.childIds.length > 0) {
+ graph.populateInstanceChildren(node.id, node.componentId)
+ populated++
+ }
+ }
+ }
+
// Ensure at least one page exists
if (graph.getPages(true).length === 0) {
graph.addPage('Page 1')
diff --git a/packages/core/src/layout.ts b/packages/core/src/layout.ts
index 48f7d83b7..616cca2b3 100644
--- a/packages/core/src/layout.ts
+++ b/packages/core/src/layout.ts
@@ -1,6 +1,7 @@
import Yoga, {
Align,
Direction,
+ Display,
FlexDirection,
Gutter,
Justify,
@@ -89,7 +90,9 @@ function buildYogaTree(graph: SceneGraph, frame: SceneNode): YogaNode {
const yogaChild = Yoga.Node.create()
- if (child.layoutMode !== 'NONE') {
+ if (!child.visible) {
+ yogaChild.setDisplay(Display.None)
+ } else if (child.layoutMode !== 'NONE') {
configureChildAsAutoLayout(yogaChild, child, frame, graph)
} else {
configureChildAsLeaf(yogaChild, child, frame)
@@ -150,7 +153,9 @@ function configureChildAsAutoLayout(
for (const gc of grandchildren) {
if (gc.layoutPositioning === 'ABSOLUTE') continue
const yogaGC = Yoga.Node.create()
- if (gc.layoutMode !== 'NONE') {
+ if (!gc.visible) {
+ yogaGC.setDisplay(Display.None)
+ } else if (gc.layoutMode !== 'NONE') {
configureChildAsAutoLayout(yogaGC, gc, child, graph)
} else {
configureChildAsLeaf(yogaGC, gc, child)
diff --git a/tests/engine/layout.test.ts b/tests/engine/layout.test.ts
index 96599ca7b..30e929012 100644
--- a/tests/engine/layout.test.ts
+++ b/tests/engine/layout.test.ts
@@ -469,6 +469,90 @@ describe('Auto Layout', () => {
})
})
+ describe('hidden children', () => {
+ test('hidden children collapse to zero size in auto layout', () => {
+ const graph = new SceneGraph()
+ const frame = autoFrame(graph, pageId(graph), {
+ width: 300,
+ height: 100,
+ itemSpacing: 10,
+ })
+ rect(graph, frame.id, 50, 50, { visible: false })
+ rect(graph, frame.id, 80, 40)
+ rect(graph, frame.id, 50, 50, { visible: false })
+
+ computeLayout(graph, frame.id)
+
+ const children = graph.getChildren(frame.id)
+ expect(children[0].width).toBe(0)
+ expect(children[0].height).toBe(0)
+ expect(children[1].x).toBe(0)
+ expect(children[1].width).toBe(80)
+ expect(children[2].width).toBe(0)
+ expect(children[2].height).toBe(0)
+ })
+
+ test('hidden children do not consume spacing', () => {
+ const graph = new SceneGraph()
+ const frame = autoFrame(graph, pageId(graph), {
+ width: 400,
+ height: 100,
+ itemSpacing: 20,
+ })
+ rect(graph, frame.id, 50, 50, { visible: false })
+ rect(graph, frame.id, 100, 50)
+ rect(graph, frame.id, 100, 50)
+
+ computeLayout(graph, frame.id)
+
+ const children = graph.getChildren(frame.id)
+ expect(children[1].x).toBe(0)
+ expect(children[2].x).toBe(120)
+ })
+
+ test('hug frame ignores hidden children for sizing', () => {
+ const graph = new SceneGraph()
+ const frame = autoFrame(graph, pageId(graph), {
+ primaryAxisSizing: 'HUG',
+ counterAxisSizing: 'HUG',
+ })
+ rect(graph, frame.id, 200, 200, { visible: false })
+ rect(graph, frame.id, 50, 30)
+
+ computeLayout(graph, frame.id)
+
+ const f = graph.getNode(frame.id)!
+ expect(f.width).toBe(50)
+ expect(f.height).toBe(30)
+ })
+
+ test('hidden nested auto-layout children collapse', () => {
+ const graph = new SceneGraph()
+ const outer = autoFrame(graph, pageId(graph), {
+ width: 300,
+ height: 100,
+ itemSpacing: 16,
+ })
+ const inner = autoFrame(graph, outer.id, {
+ primaryAxisSizing: 'FIXED',
+ counterAxisSizing: 'FIXED',
+ width: 50,
+ height: 50,
+ visible: false,
+ })
+ rect(graph, inner.id, 30, 30)
+ rect(graph, outer.id, 80, 40)
+
+ computeLayout(graph, outer.id)
+
+ const children = graph.getChildren(outer.id)
+ const innerNode = graph.getNode(inner.id)!
+ expect(innerNode.width).toBe(0)
+ expect(innerNode.height).toBe(0)
+ expect(children[1].x).toBe(0)
+ })
+ })
+
describe('self-alignment', () => {
test('layoutAlignSelf STRETCH overrides counter axis', () => {
const graph = new SceneGraph()