Skip to content

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

fix(dom-css): map flex gaps by axis

Danila Poyarkov74a6d7ab7f8f
diff --git a/packages/dom-css/src/from-scene-graph.ts b/packages/dom-css/src/from-scene-graph.ts
index 05253eff9..1a52510ae 100644
--- a/packages/dom-css/src/from-scene-graph.ts
+++ b/packages/dom-css/src/from-scene-graph.ts
@@ -116,6 +116,23 @@ function addPadding(style: DesignStyleDeclaration, node: SceneNode): void {
if (paddingLeft > 0) style['padding-left'] = `${paddingLeft}px`
}
+function addFlexGap(style: DesignStyleDeclaration, node: SceneNode): void {
+ if (node.itemSpacing <= 0 && node.counterAxisSpacing <= 0) return
+ if (node.counterAxisSpacing <= 0) {
+ style.gap = `${node.itemSpacing}px`
+ return
+ }
+
+ if (node.layoutMode === 'HORIZONTAL') {
+ if (node.itemSpacing > 0) style['column-gap'] = `${node.itemSpacing}px`
+ style['row-gap'] = `${node.counterAxisSpacing}px`
+ return
+ }
+
+ if (node.itemSpacing > 0) style['row-gap'] = `${node.itemSpacing}px`
+ style['column-gap'] = `${node.counterAxisSpacing}px`
+}
+
function styleFromSceneNode(node: SceneNode): DesignStyleDeclaration {
const style = sceneNodeSizeStyle(node)
addPositioning(style, node)
@@ -139,8 +156,7 @@ function styleFromSceneNode(node: SceneNode): DesignStyleDeclaration {
if (justifyContent) style['justify-content'] = justifyContent
if (alignItems) style['align-items'] = alignItems
if (node.layoutWrap === 'WRAP') style['flex-wrap'] = 'wrap'
- if (node.itemSpacing > 0) style.gap = `${node.itemSpacing}px`
- if (node.counterAxisSpacing > 0) style['row-gap'] = `${node.counterAxisSpacing}px`
+ addFlexGap(style, node)
addPadding(style, node)
}
diff --git a/packages/dom-css/src/to-scene-graph.ts b/packages/dom-css/src/to-scene-graph.ts
index 4a7bad8ec..c7fbd1261 100644
--- a/packages/dom-css/src/to-scene-graph.ts
+++ b/packages/dom-css/src/to-scene-graph.ts
@@ -149,6 +149,17 @@ function layoutAlignSelfFromCSS(value: string | undefined): SceneNode['layoutAli
return 'AUTO'
}
+function applyFlexGap(node: SceneNode, style: DesignStyleDeclaration): void {
+ const gap = firstCSSNumber(style, 'gap')
+ const rowGap = firstCSSNumber(style, 'row-gap')
+ const columnGap = firstCSSNumber(style, 'column-gap')
+ const isHorizontal = node.layoutMode === 'HORIZONTAL'
+ const isWrapped = node.layoutWrap === 'WRAP'
+
+ node.itemSpacing = (isHorizontal ? columnGap : rowGap) ?? gap ?? 0
+ node.counterAxisSpacing = (isHorizontal ? rowGap : columnGap) ?? (isWrapped ? (gap ?? 0) : 0)
+}
+
function applyPositioning(node: SceneNode, style: DesignStyleDeclaration): void {
const position = pickStyle(style, 'position')
if (position === 'absolute' || position === 'fixed') node.layoutPositioning = 'ABSOLUTE'
@@ -203,8 +214,7 @@ function applyElementStyle(node: SceneNode, style: DesignStyleDeclaration): void
node.primaryAxisAlign = primaryAxisAlignFromCSS(pickStyle(style, 'justify-content'))
node.counterAxisAlign = counterAxisAlignFromCSS(pickStyle(style, 'align-items'))
node.layoutWrap = pickStyle(style, 'flex-wrap') === 'wrap' ? 'WRAP' : 'NO_WRAP'
- node.itemSpacing = firstCSSNumber(style, 'gap', 'column-gap', 'row-gap') ?? 0
- node.counterAxisSpacing = firstCSSNumber(style, 'row-gap') ?? 0
+ applyFlexGap(node, style)
}
}
diff --git a/packages/dom-css/tests/conversion.test.ts b/packages/dom-css/tests/conversion.test.ts
index 2279118e5..efeedf9cd 100644
--- a/packages/dom-css/tests/conversion.test.ts
+++ b/packages/dom-css/tests/conversion.test.ts
@@ -252,6 +252,44 @@ describe('@open-pencil/dom-css conversion', () => {
expectStyleRoundTripHTML(graph)
})
+ it('maps flex row and column gaps by axis', () => {
+ const graph = designDocumentToSceneGraph({
+ type: 'document',
+ children: [
+ {
+ type: 'element',
+ tagName: 'div',
+ attrs: { class: 'column-wrap' },
+ computedStyle: {
+ display: 'flex',
+ 'flex-direction': 'column',
+ 'flex-wrap': 'wrap',
+ 'row-gap': '14px',
+ 'column-gap': '24px'
+ },
+ children: []
+ }
+ ]
+ })
+ const page = graph.getPages()[0]
+ const stack = expectFrame(page ? graph.getChildren(page.id)[0] : undefined)
+
+ expect(stack.layoutMode).toBe('VERTICAL')
+ expect(stack.layoutWrap).toBe('WRAP')
+ expect(stack.itemSpacing).toBe(14)
+ expect(stack.counterAxisSpacing).toBe(24)
+
+ const roundTrip = sceneGraphToDesignDocument(graph)
+ const root = roundTrip.children[0]
+ expect(root?.type).toBe('element')
+ if (root?.type !== 'element') return
+ const roundTripStack = root.children[0]
+ expect(roundTripStack?.type).toBe('element')
+ if (roundTripStack?.type !== 'element') return
+ expect(roundTripStack.inlineStyle?.['row-gap']).toBe('14px')
+ expect(roundTripStack.inlineStyle?.['column-gap']).toBe('24px')
+ })
+
it('maps flex wrapping, align-self, clipping, and absolute positioning', () => {
const graph = designDocumentToSceneGraph({
type: 'document',