Skip to content

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

Auto-layout with Yoga WASM integration

Danila Poyarkov4de83542d3ea
diff --git a/src/components/PropertiesPanel.vue b/src/components/PropertiesPanel.vue
index 9ecb0bb9d..ad7fa6543 100644
--- a/src/components/PropertiesPanel.vue
+++ b/src/components/PropertiesPanel.vue
@@ -202,6 +202,124 @@ function colorHex(c: Color) {
</div>
</div>
+ <!-- Auto Layout -->
+ <div v-if="node.type === 'FRAME'" class="section">
+ <div class="section-header">
+ <label class="section-label">Auto layout</label>
+ <div class="layout-toggles">
+ <button
+ class="layout-btn"
+ :class="{ active: node.layoutMode === 'NONE' }"
+ title="No auto layout"
+ @click="store.setLayoutMode(node.id, 'NONE')"
+ >
+ ✕
+ </button>
+ <button
+ class="layout-btn"
+ :class="{ active: node.layoutMode === 'VERTICAL' }"
+ title="Vertical"
+ @click="store.setLayoutMode(node.id, 'VERTICAL')"
+ >
+ ↕
+ </button>
+ <button
+ class="layout-btn"
+ :class="{ active: node.layoutMode === 'HORIZONTAL' }"
+ title="Horizontal"
+ @click="store.setLayoutMode(node.id, 'HORIZONTAL')"
+ >
+ ↔
+ </button>
+ </div>
+ </div>
+
+ <template v-if="node.layoutMode !== 'NONE'">
+ <div class="input-row" style="margin-top: 6px">
+ <label class="prop-input">
+ <span class="prop-label" title="Gap">⇥</span>
+ <input
+ type="number"
+ :value="node.itemSpacing"
+ min="0"
+ @change="updateProp('itemSpacing', +($event.target as HTMLInputElement).value)"
+ />
+ </label>
+ </div>
+
+ <div class="input-row" style="margin-top: 4px">
+ <label class="prop-input">
+ <span class="prop-label" title="Padding top">↑</span>
+ <input
+ type="number"
+ :value="node.paddingTop"
+ min="0"
+ @change="updateProp('paddingTop', +($event.target as HTMLInputElement).value)"
+ />
+ </label>
+ <label class="prop-input">
+ <span class="prop-label" title="Padding right">→</span>
+ <input
+ type="number"
+ :value="node.paddingRight"
+ min="0"
+ @change="updateProp('paddingRight', +($event.target as HTMLInputElement).value)"
+ />
+ </label>
+ </div>
+ <div class="input-row">
+ <label class="prop-input">
+ <span class="prop-label" title="Padding bottom">↓</span>
+ <input
+ type="number"
+ :value="node.paddingBottom"
+ min="0"
+ @change="updateProp('paddingBottom', +($event.target as HTMLInputElement).value)"
+ />
+ </label>
+ <label class="prop-input">
+ <span class="prop-label" title="Padding left">←</span>
+ <input
+ type="number"
+ :value="node.paddingLeft"
+ min="0"
+ @change="updateProp('paddingLeft', +($event.target as HTMLInputElement).value)"
+ />
+ </label>
+ </div>
+
+ <div class="input-row" style="margin-top: 4px">
+ <label class="prop-input">
+ <span class="prop-label" title="Align">⊞</span>
+ <select
+ :value="node.primaryAxisAlign"
+ @change="updateProp('primaryAxisAlign', ($event.target as HTMLSelectElement).value)"
+ >
+ <option value="MIN">Start</option>
+ <option value="CENTER">Center</option>
+ <option value="MAX">End</option>
+ <option value="SPACE_BETWEEN">Space between</option>
+ </select>
+ </label>
+ </div>
+ <div class="input-row">
+ <label class="prop-input">
+ <span class="prop-label" title="Cross align">⊟</span>
+ <select
+ :value="node.counterAxisAlign"
+ @change="updateProp('counterAxisAlign', ($event.target as HTMLSelectElement).value)"
+ >
+ <option value="MIN">Start</option>
+ <option value="CENTER">Center</option>
+ <option value="MAX">End</option>
+ <option value="STRETCH">Stretch</option>
+ <option value="BASELINE">Baseline</option>
+ </select>
+ </label>
+ </div>
+ </template>
+ </div>
+
<!-- Appearance -->
<div class="section">
<label class="section-label">Appearance</label>
@@ -481,6 +599,45 @@ function colorHex(c: Color) {
color: var(--text);
}
+.layout-toggles {
+ display: flex;
+ gap: 2px;
+}
+
+.layout-btn {
+ background: transparent;
+ border: 1px solid var(--border);
+ color: var(--text-muted);
+ font-size: 12px;
+ cursor: pointer;
+ padding: 2px 6px;
+ border-radius: 4px;
+ line-height: 1;
+}
+
+.layout-btn.active {
+ background: var(--accent, #3b82f6);
+ color: white;
+ border-color: var(--accent, #3b82f6);
+}
+
+.layout-btn:hover:not(.active) {
+ background: var(--hover);
+ color: var(--text);
+}
+
+.prop-input select {
+ flex: 1;
+ min-width: 0;
+ background: var(--input-bg);
+ border: 1px solid var(--border);
+ border-radius: 4px;
+ color: var(--text);
+ padding: 3px 4px;
+ font: inherit;
+ font-size: 11px;
+}
+
.stroke-weight {
width: 36px;
background: var(--input-bg);
diff --git a/src/engine/clipboard.ts b/src/engine/clipboard.ts
index acb515080..d97db569c 100644
--- a/src/engine/clipboard.ts
+++ b/src/engine/clipboard.ts
@@ -1,7 +1,7 @@
import { decodeBinarySchema, compileSchema, ByteBuffer } from 'kiwi-schema'
import { inflateSync, deflateSync } from 'fflate'
-import type { SceneGraph, SceneNode, Fill, Stroke, Color } from './scene-graph'
+import type { SceneGraph, SceneNode, Fill, Stroke, Color, LayoutMode, LayoutSizing, LayoutAlign, LayoutCounterAlign } from './scene-graph'
interface FigmaClipboardMeta {
fileKey: string
@@ -288,7 +288,21 @@ export function importClipboardNodes(
fontSize: nc.fontSize ?? 14,
fontFamily: nc.fontName?.family ?? 'Inter',
textAlignHorizontal:
- (nc.textAlignHorizontal as 'LEFT' | 'CENTER' | 'RIGHT' | 'JUSTIFIED') ?? 'LEFT'
+ (nc.textAlignHorizontal as 'LEFT' | 'CENTER' | 'RIGHT' | 'JUSTIFIED') ?? 'LEFT',
+ layoutMode: mapLayoutMode(nc.stackMode as string),
+ itemSpacing: (nc.stackSpacing as number) ?? 0,
+ paddingTop: (nc.stackVerticalPadding as number) ?? (nc.stackPadding as number) ?? 0,
+ paddingBottom: (nc.stackPaddingBottom as number) ?? (nc.stackVerticalPadding as number) ?? (nc.stackPadding as number) ?? 0,
+ paddingLeft: (nc.stackHorizontalPadding as number) ?? (nc.stackPadding as number) ?? 0,
+ paddingRight: (nc.stackPaddingRight as number) ?? (nc.stackHorizontalPadding as number) ?? (nc.stackPadding as number) ?? 0,
+ primaryAxisSizing: mapSizing(nc.stackPrimarySizing as string),
+ counterAxisSizing: mapSizing(nc.stackCounterSizing as string),
+ primaryAxisAlign: mapPrimaryAlign(nc.stackPrimaryAlignItems as string ?? nc.stackJustify as string),
+ counterAxisAlign: mapCounterAlign(nc.stackCounterAlignItems as string ?? nc.stackCounterAlign as string),
+ layoutWrap: (nc.stackWrap as string) === 'WRAP' ? 'WRAP' as const : 'NO_WRAP' as const,
+ counterAxisSpacing: (nc.stackCounterSpacing as number) ?? 0,
+ layoutPositioning: (nc.stackPositioning as string) === 'ABSOLUTE' ? 'ABSOLUTE' as const : 'AUTO' as const,
+ layoutGrow: (nc.stackChildPrimaryGrow as number) ?? 0
})
created.set(figmaId, node.id)
@@ -317,6 +331,33 @@ export function importClipboardNodes(
return createdIds
}
+function mapLayoutMode(mode?: string): LayoutMode {
+ if (mode === 'HORIZONTAL') return 'HORIZONTAL'
+ if (mode === 'VERTICAL') return 'VERTICAL'
+ return 'NONE'
+}
+
+function mapSizing(sizing?: string): LayoutSizing {
+ if (sizing === 'RESIZE_TO_FIT' || sizing === 'RESIZE_TO_FIT_WITH_IMPLICIT_SIZE') return 'HUG'
+ if (sizing === 'FILL') return 'FILL'
+ return 'FIXED'
+}
+
+function mapPrimaryAlign(align?: string): LayoutAlign {
+ if (align === 'CENTER') return 'CENTER'
+ if (align === 'MAX') return 'MAX'
+ if (align === 'SPACE_BETWEEN' || align === 'SPACE_EVENLY') return 'SPACE_BETWEEN'
+ return 'MIN'
+}
+
+function mapCounterAlign(align?: string): LayoutCounterAlign {
+ if (align === 'CENTER') return 'CENTER'
+ if (align === 'MAX') return 'MAX'
+ if (align === 'STRETCH') return 'STRETCH'
+ if (align === 'BASELINE') return 'BASELINE'
+ return 'MIN'
+}
+
function mapNodeType(type?: string): SceneNode['type'] {
switch (type) {
case 'FRAME':
@@ -472,6 +513,24 @@ function sceneNodeToKiwi(
nc.frameMaskDisabled = node.type === 'GROUP'
}
+ if (node.layoutMode !== 'NONE') {
+ nc.stackMode = node.layoutMode
+ nc.stackSpacing = node.itemSpacing
+ nc.stackVerticalPadding = node.paddingTop
+ nc.stackHorizontalPadding = node.paddingLeft
+ nc.stackPaddingBottom = node.paddingBottom
+ nc.stackPaddingRight = node.paddingRight
+ nc.stackPrimarySizing = node.primaryAxisSizing === 'HUG' ? 'RESIZE_TO_FIT' : 'FIXED'
+ nc.stackCounterSizing = node.counterAxisSizing === 'HUG' ? 'RESIZE_TO_FIT_WITH_IMPLICIT_SIZE' : undefined
+ }
+
+ if (node.layoutPositioning === 'ABSOLUTE') {
+ nc.stackPositioning = 'ABSOLUTE'
+ }
+ if (node.layoutGrow > 0) {
+ nc.stackChildPrimaryGrow = node.layoutGrow
+ }
+
const result: KiwiNodeChange[] = [nc]
const children = graph.getChildren(node.id)
for (let i = 0; i < children.length; i++) {
diff --git a/src/engine/fig-import.ts b/src/engine/fig-import.ts
index 658857751..9ef0800a2 100644
--- a/src/engine/fig-import.ts
+++ b/src/engine/fig-import.ts
@@ -1,7 +1,11 @@
import { SceneGraph } from './scene-graph'
import type { NodeChange, Paint, Effect as KiwiEffect, GUID } from '../kiwi/codec'
-import type { NodeType, Fill, Stroke, Effect, Color } from './scene-graph'
+import type { NodeType, Fill, Stroke, Effect, Color, LayoutMode, LayoutSizing, LayoutAlign, LayoutCounterAlign } from './scene-graph'
+
+function ext(nc: NodeChange): Record<string, unknown> {
+ return nc as unknown as Record<string, unknown>
+}
function guidToString(guid: GUID): string {
return `${guid.sessionID}:${guid.localID}`
@@ -83,6 +87,46 @@ function mapNodeType(type?: string): NodeType {
}
}
+function mapStackMode(mode?: string): LayoutMode {
+ switch (mode) {
+ case 'HORIZONTAL': return 'HORIZONTAL'
+ case 'VERTICAL': return 'VERTICAL'
+ default: return 'NONE'
+ }
+}
+
+function mapStackSizing(sizing?: string): LayoutSizing {
+ switch (sizing) {
+ case 'RESIZE_TO_FIT':
+ case 'RESIZE_TO_FIT_WITH_IMPLICIT_SIZE':
+ return 'HUG'
+ case 'FILL':
+ return 'FILL'
+ default:
+ return 'FIXED'
+ }
+}
+
+function mapStackJustify(justify?: string): LayoutAlign {
+ switch (justify) {
+ case 'CENTER': return 'CENTER'
+ case 'MAX': return 'MAX'
+ case 'SPACE_BETWEEN':
+ case 'SPACE_EVENLY': return 'SPACE_BETWEEN'
+ default: return 'MIN'
+ }
+}
+
+function mapStackCounterAlign(align?: string): LayoutCounterAlign {
+ switch (align) {
+ case 'CENTER': return 'CENTER'
+ case 'MAX': return 'MAX'
+ case 'STRETCH': return 'STRETCH'
+ case 'BASELINE': return 'BASELINE'
+ default: return 'MIN'
+ }
+}
+
export function importNodeChanges(nodeChanges: NodeChange[]): SceneGraph {
const graph = new SceneGraph()
@@ -159,7 +203,21 @@ export function importNodeChanges(nodeChanges: NodeChange[]): SceneGraph {
textAlignHorizontal:
(nc.textAlignHorizontal as 'LEFT' | 'CENTER' | 'RIGHT' | 'JUSTIFIED') ?? 'LEFT',
lineHeight: nc.lineHeight?.value ?? null,
- letterSpacing: nc.letterSpacing?.value ?? 0
+ letterSpacing: nc.letterSpacing?.value ?? 0,
+ layoutMode: mapStackMode(nc.stackMode),
+ itemSpacing: nc.stackSpacing ?? 0,
+ paddingTop: nc.stackVerticalPadding ?? nc.stackPadding ?? 0,
+ paddingBottom: nc.stackPaddingBottom ?? nc.stackVerticalPadding ?? nc.stackPadding ?? 0,
+ paddingLeft: nc.stackHorizontalPadding ?? nc.stackPadding ?? 0,
+ paddingRight: nc.stackPaddingRight ?? nc.stackHorizontalPadding ?? nc.stackPadding ?? 0,
+ primaryAxisSizing: mapStackSizing(nc.stackPrimarySizing),
+ counterAxisSizing: mapStackSizing(nc.stackCounterSizing),
+ primaryAxisAlign: mapStackJustify(nc.stackPrimaryAlignItems ?? nc.stackJustify),
+ counterAxisAlign: mapStackCounterAlign(nc.stackCounterAlignItems ?? nc.stackCounterAlign),
+ layoutWrap: ext(nc).stackWrap === 'WRAP' ? 'WRAP' : 'NO_WRAP',
+ counterAxisSpacing: (ext(nc).stackCounterSpacing as number) ?? 0,
+ layoutPositioning: ext(nc).stackPositioning === 'ABSOLUTE' ? 'ABSOLUTE' : 'AUTO',
+ layoutGrow: (ext(nc).stackChildPrimaryGrow as number) ?? 0
})
// Create children (find all nodes whose parent is this node)
diff --git a/src/engine/layout.ts b/src/engine/layout.ts
new file mode 100644
index 000000000..e659e55d7
--- /dev/null
+++ b/src/engine/layout.ts
@@ -0,0 +1,301 @@
+import Yoga, {
+ Align,
+ Direction,
+ FlexDirection,
+ Gutter,
+ Justify,
+ Edge,
+ Wrap,
+ type Node as YogaNode
+} from 'yoga-layout'
+
+import type { SceneGraph, SceneNode } from './scene-graph'
+
+export function computeLayout(graph: SceneGraph, frameId: string): void {
+ const frame = graph.getNode(frameId)
+ if (!frame || frame.layoutMode === 'NONE') return
+
+ const yogaRoot = buildYogaTree(graph, frame)
+ yogaRoot.calculateLayout(undefined, undefined, Direction.LTR)
+ applyYogaLayout(graph, frame, yogaRoot)
+ freeYogaTree(yogaRoot)
+}
+
+export function computeAllLayouts(graph: SceneGraph): void {
+ const visited = new Set<string>()
+ computeLayoutsBottomUp(graph, graph.rootId, visited)
+}
+
+function computeLayoutsBottomUp(graph: SceneGraph, nodeId: string, visited: Set<string>): void {
+ const node = graph.getNode(nodeId)
+ if (!node || visited.has(nodeId)) return
+ visited.add(nodeId)
+
+ for (const childId of node.childIds) {
+ computeLayoutsBottomUp(graph, childId, visited)
+ }
+
+ if (node.layoutMode !== 'NONE') {
+ computeLayout(graph, nodeId)
+ }
+}
+
+function buildYogaTree(graph: SceneGraph, frame: SceneNode): YogaNode {
+ const root = Yoga.Node.create()
+
+ if (frame.primaryAxisSizing === 'FIXED') {
+ if (frame.layoutMode === 'HORIZONTAL') root.setWidth(frame.width)
+ else root.setHeight(frame.height)
+ }
+ if (frame.counterAxisSizing === 'FIXED') {
+ if (frame.layoutMode === 'HORIZONTAL') root.setHeight(frame.height)
+ else root.setWidth(frame.width)
+ }
+
+ root.setFlexDirection(
+ frame.layoutMode === 'HORIZONTAL' ? FlexDirection.Row : FlexDirection.Column
+ )
+ root.setFlexWrap(frame.layoutWrap === 'WRAP' ? Wrap.Wrap : Wrap.NoWrap)
+ root.setJustifyContent(mapJustify(frame.primaryAxisAlign))
+ root.setAlignItems(mapAlign(frame.counterAxisAlign))
+
+ root.setPadding(Edge.Top, frame.paddingTop)
+ root.setPadding(Edge.Right, frame.paddingRight)
+ root.setPadding(Edge.Bottom, frame.paddingBottom)
+ root.setPadding(Edge.Left, frame.paddingLeft)
+
+ root.setGap(Gutter.Column, frame.layoutMode === 'HORIZONTAL' ? frame.itemSpacing : frame.counterAxisSpacing)
+ root.setGap(Gutter.Row, frame.layoutMode === 'HORIZONTAL' ? frame.counterAxisSpacing : frame.itemSpacing)
+
+ const children = graph.getChildren(frame.id)
+ for (let i = 0; i < children.length; i++) {
+ const child = children[i]
+
+ if (child.layoutPositioning === 'ABSOLUTE') continue
+
+ const yogaChild = Yoga.Node.create()
+
+ if (child.layoutMode !== 'NONE') {
+ configureChildAsAutoLayout(yogaChild, child, frame, graph)
+ } else {
+ configureChildAsLeaf(yogaChild, child, frame)
+ }
+
+ root.insertChild(yogaChild, root.getChildCount())
+ }
+
+ return root
+}
+
+function configureChildAsAutoLayout(
+ yogaChild: YogaNode,
+ child: SceneNode,
+ parent: SceneNode,
+ graph: SceneGraph
+): void {
+ const isRow = parent.layoutMode === 'HORIZONTAL'
+
+ const primarySizing = child.primaryAxisSizing
+ const counterSizing = child.counterAxisSizing
+
+ if (isRow) {
+ setSizing(yogaChild, 'width', primarySizing, child.width, child.layoutGrow)
+ setSizing(yogaChild, 'height', counterSizing, child.height, 0)
+ } else {
+ setSizing(yogaChild, 'width', counterSizing, child.width, 0)
+ setSizing(yogaChild, 'height', primarySizing, child.height, child.layoutGrow)
+ }
+
+ if (child.layoutAlignSelf === 'STRETCH') {
+ yogaChild.setAlignSelf(Align.Stretch)
+ }
+
+ yogaChild.setFlexDirection(
+ child.layoutMode === 'HORIZONTAL' ? FlexDirection.Row : FlexDirection.Column
+ )
+ yogaChild.setFlexWrap(child.layoutWrap === 'WRAP' ? Wrap.Wrap : Wrap.NoWrap)
+ yogaChild.setJustifyContent(mapJustify(child.primaryAxisAlign))
+ yogaChild.setAlignItems(mapAlign(child.counterAxisAlign))
+
+ yogaChild.setPadding(Edge.Top, child.paddingTop)
+ yogaChild.setPadding(Edge.Right, child.paddingRight)
+ yogaChild.setPadding(Edge.Bottom, child.paddingBottom)
+ yogaChild.setPadding(Edge.Left, child.paddingLeft)
+
+ yogaChild.setGap(Gutter.Column, child.layoutMode === 'HORIZONTAL' ? child.itemSpacing : child.counterAxisSpacing)
+ yogaChild.setGap(Gutter.Row, child.layoutMode === 'HORIZONTAL' ? child.counterAxisSpacing : child.itemSpacing)
+
+ const grandchildren = graph.getChildren(child.id)
+ for (const gc of grandchildren) {
+ if (gc.layoutPositioning === 'ABSOLUTE') continue
+ const yogaGC = Yoga.Node.create()
+ if (gc.layoutMode !== 'NONE') {
+ configureChildAsAutoLayout(yogaGC, gc, child, graph)
+ } else {
+ configureChildAsLeaf(yogaGC, gc, child)
+ }
+ yogaChild.insertChild(yogaGC, yogaChild.getChildCount())
+ }
+}
+
+function configureChildAsLeaf(
+ yogaChild: YogaNode,
+ child: SceneNode,
+ parent: SceneNode
+): void {
+ const isRow = parent.layoutMode === 'HORIZONTAL'
+
+ if (child.layoutGrow > 0) {
+ yogaChild.setFlexGrow(child.layoutGrow)
+ if (isRow) yogaChild.setHeight(child.height)
+ else yogaChild.setWidth(child.width)
+ } else {
+ yogaChild.setWidth(child.width)
+ yogaChild.setHeight(child.height)
+ }
+
+ if (child.layoutAlignSelf === 'STRETCH') {
+ yogaChild.setAlignSelf(Align.Stretch)
+ if (isRow) yogaChild.setHeight('auto' as unknown as number)
+ else yogaChild.setWidth('auto' as unknown as number)
+ }
+}
+
+function setSizing(
+ yogaNode: YogaNode,
+ axis: 'width' | 'height',
+ sizing: string,
+ fixedValue: number,
+ grow: number
+): void {
+ switch (sizing) {
+ case 'FIXED':
+ if (axis === 'width') yogaNode.setWidth(fixedValue)
+ else yogaNode.setHeight(fixedValue)
+ break
+ case 'HUG':
+ // auto-size
+ break
+ case 'FILL':
+ yogaNode.setFlexGrow(grow > 0 ? grow : 1)
+ yogaNode.setFlexShrink(1)
+ break
+ }
+}
+
+function applyYogaLayout(graph: SceneGraph, frame: SceneNode, yogaRoot: YogaNode): void {
+ if (frame.primaryAxisSizing === 'HUG' || frame.counterAxisSizing === 'HUG') {
+ const computedW = yogaRoot.getComputedWidth()
+ const computedH = yogaRoot.getComputedHeight()
+ const updates: Partial<SceneNode> = {}
+
+ if (frame.primaryAxisSizing === 'HUG') {
+ if (frame.layoutMode === 'HORIZONTAL') updates.width = computedW
+ else updates.height = computedH
+ }
+ if (frame.counterAxisSizing === 'HUG') {
+ if (frame.layoutMode === 'HORIZONTAL') updates.height = computedH
+ else updates.width = computedW
+ }
+
+ graph.updateNode(frame.id, updates)
+ }
+
+ const children = graph.getChildren(frame.id)
+ let yogaIndex = 0
+ for (const child of children) {
+ if (child.layoutPositioning === 'ABSOLUTE') continue
+
+ const yogaChild = yogaRoot.getChild(yogaIndex)
+ yogaIndex++
+
+ graph.updateNode(child.id, {
+ x: yogaChild.getComputedLeft(),
+ y: yogaChild.getComputedTop(),
+ width: yogaChild.getComputedWidth(),
+ height: yogaChild.getComputedHeight()
+ })
+
+ if (child.layoutMode !== 'NONE') {
+ applyYogaLayoutNested(graph, child, yogaChild)
+ }
+ }
+}
+
+function applyYogaLayoutNested(
+ graph: SceneGraph,
+ frame: SceneNode,
+ yogaNode: YogaNode
+): void {
+ if (frame.primaryAxisSizing === 'HUG' || frame.counterAxisSizing === 'HUG') {
+ const computedW = yogaNode.getComputedWidth()
+ const computedH = yogaNode.getComputedHeight()
+ const updates: Partial<SceneNode> = {}
+
+ if (frame.primaryAxisSizing === 'HUG') {
+ if (frame.layoutMode === 'HORIZONTAL') updates.width = computedW
+ else updates.height = computedH
+ }
+ if (frame.counterAxisSizing === 'HUG') {
+ if (frame.layoutMode === 'HORIZONTAL') updates.height = computedH
+ else updates.width = computedW
+ }
+
+ graph.updateNode(frame.id, updates)
+ }
+
+ const children = graph.getChildren(frame.id)
+ let yogaIndex = 0
+ for (const child of children) {
+ if (child.layoutPositioning === 'ABSOLUTE') continue
+
+ const yogaChild = yogaNode.getChild(yogaIndex)
+ yogaIndex++
+
+ graph.updateNode(child.id, {
+ x: yogaChild.getComputedLeft(),
+ y: yogaChild.getComputedTop(),
+ width: yogaChild.getComputedWidth(),
+ height: yogaChild.getComputedHeight()
+ })
+
+ if (child.layoutMode !== 'NONE') {
+ applyYogaLayoutNested(graph, child, yogaChild)
+ }
+ }
+}
+
+function freeYogaTree(node: YogaNode): void {
+ for (let i = node.getChildCount() - 1; i >= 0; i--) {
+ freeYogaTree(node.getChild(i))
+ }
+ node.free()
+}
+
+function mapJustify(align: string): Justify {
+ switch (align) {
+ case 'CENTER':
+ return Justify.Center
+ case 'MAX':
+ return Justify.FlexEnd
+ case 'SPACE_BETWEEN':
+ return Justify.SpaceBetween
+ default:
+ return Justify.FlexStart
+ }
+}
+
+function mapAlign(align: string): Align {
+ switch (align) {
+ case 'CENTER':
+ return Align.Center
+ case 'MAX':
+ return Align.FlexEnd
+ case 'STRETCH':
+ return Align.Stretch
+ case 'BASELINE':
+ return Align.Baseline
+ default:
+ return Align.FlexStart
+ }
+}
diff --git a/src/engine/scene-graph.ts b/src/engine/scene-graph.ts
index dd0dcd333..ab312698d 100644
--- a/src/engine/scene-graph.ts
+++ b/src/engine/scene-graph.ts
@@ -46,6 +46,12 @@ export interface Effect {
visible: boolean
}
+export type LayoutMode = 'NONE' | 'HORIZONTAL' | 'VERTICAL'
+export type LayoutSizing = 'FIXED' | 'HUG' | 'FILL'
+export type LayoutAlign = 'MIN' | 'CENTER' | 'MAX' | 'SPACE_BETWEEN'
+export type LayoutCounterAlign = 'MIN' | 'CENTER' | 'MAX' | 'STRETCH' | 'BASELINE'
+export type LayoutWrap = 'NO_WRAP' | 'WRAP'
+
export interface SceneNode {
id: string
type: NodeType
@@ -82,6 +88,23 @@ export interface SceneNode {
textAlignHorizontal: 'LEFT' | 'CENTER' | 'RIGHT' | 'JUSTIFIED'
lineHeight: number | null
letterSpacing: number
+
+ layoutMode: LayoutMode
+ layoutWrap: LayoutWrap
+ primaryAxisAlign: LayoutAlign
+ counterAxisAlign: LayoutCounterAlign
+ primaryAxisSizing: LayoutSizing
+ counterAxisSizing: LayoutSizing
+ itemSpacing: number
+ counterAxisSpacing: number
+ paddingTop: number
+ paddingRight: number
+ paddingBottom: number
+ paddingLeft: number
+
+ layoutPositioning: 'AUTO' | 'ABSOLUTE'
+ layoutGrow: number
+ layoutAlignSelf: 'AUTO' | 'STRETCH'
}
let nextLocalID = 1
@@ -122,6 +145,21 @@ function createDefaultNode(type: NodeType, overrides: Partial<SceneNode> = {}):
textAlignHorizontal: 'LEFT',
lineHeight: null,
letterSpacing: 0,
+ layoutMode: 'NONE',
+ layoutWrap: 'NO_WRAP',
+ primaryAxisAlign: 'MIN',
+ counterAxisAlign: 'MIN',
+ primaryAxisSizing: 'FIXED',
+ counterAxisSizing: 'FIXED',
+ itemSpacing: 0,
+ counterAxisSpacing: 0,
+ paddingTop: 0,
+ paddingRight: 0,
+ paddingBottom: 0,
+ paddingLeft: 0,
+ layoutPositioning: 'AUTO',
+ layoutGrow: 0,
+ layoutAlignSelf: 'AUTO',
...overrides
}
}
diff --git a/src/stores/editor.ts b/src/stores/editor.ts
index 4a375ddd0..77a7cd1a8 100644
--- a/src/stores/editor.ts
+++ b/src/stores/editor.ts
@@ -9,10 +9,11 @@ import {
prefetchFigmaSchema
} from '../engine/clipboard'
import { readFigFile } from '../engine/fig-file'
+import { computeLayout, computeAllLayouts } from '../engine/layout'
import { SceneGraph } from '../engine/scene-graph'
import { UndoManager } from '../engine/undo'
-import type { SceneNode, NodeType, Fill } from '../engine/scene-graph'
+import type { SceneNode, NodeType, Fill, LayoutMode } from '../engine/scene-graph'
import type { SnapGuide } from '../engine/snap'
export type Tool = 'SELECT' | 'FRAME' | 'RECTANGLE' | 'ELLIPSE' | 'LINE' | 'TEXT' | 'PEN' | 'HAND'
@@ -173,6 +174,7 @@ export function createEditorStore() {
try {
const imported = await readFigFile(file)
graph = imported
+ computeAllLayouts(graph)
undo.clear()
state.selectedIds = new Set()
state.panX = 0
@@ -184,8 +186,49 @@ export function createEditorStore() {
}
}
+ function runLayoutForNode(id: string) {
+ const node = graph.getNode(id)
+ if (!node) return
+
+ if (node.layoutMode !== 'NONE') {
+ computeLayout(graph, id)
+ }
+
+ let parent = node.parentId ? graph.getNode(node.parentId) : undefined
+ while (parent) {
+ if (parent.layoutMode !== 'NONE') {
+ computeLayout(graph, parent.id)
+ }
+ parent = parent.parentId ? graph.getNode(parent.parentId) : undefined
+ }
+ }
+
function updateNode(id: string, changes: Partial<SceneNode>) {
graph.updateNode(id, changes)
+ runLayoutForNode(id)
+ requestRender()
+ }
+
+ function setLayoutMode(id: string, mode: LayoutMode) {
+ const node = graph.getNode(id)
+ if (!node) return
+
+ const updates: Partial<SceneNode> = { layoutMode: mode }
+ if (mode !== 'NONE' && node.layoutMode === 'NONE') {
+ updates.itemSpacing = 0
+ updates.paddingTop = 0
+ updates.paddingRight = 0
+ updates.paddingBottom = 0
+ updates.paddingLeft = 0
+ updates.primaryAxisSizing = 'HUG'
+ updates.counterAxisSizing = 'HUG'
+ updates.primaryAxisAlign = 'MIN'
+ updates.counterAxisAlign = 'MIN'
+ }
+
+ graph.updateNode(id, updates)
+ if (mode !== 'NONE') computeLayout(graph, id)
+ runLayoutForNode(id)
requestRender()
}
@@ -416,6 +459,7 @@ export function createEditorStore() {
commitTextEdit,
openFigFile,
updateNode,
+ setLayoutMode,
createShape,
duplicateSelected,
writeCopyData,