Skip to content

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

fix(layout): keep auto-width text on whole lines in a narrower parent (#1461)

IvyTechnx6218eb7f973b
diff --git a/changes/1461/fixed.md b/changes/1461/fixed.md
new file mode 100644
index 000000000..af7350fd6
--- /dev/null
+++ b/changes/1461/fixed.md
@@ -0,0 +1 @@
+- Auto-width text in an auto layout frame narrower than the text keeps each line whole, as in Figma, instead of wrapping inside the frame.
diff --git a/packages/core/src/layout.ts b/packages/core/src/layout.ts
index 28027f492..c39648df9 100644
--- a/packages/core/src/layout.ts
+++ b/packages/core/src/layout.ts
@@ -562,6 +562,11 @@ function configureTextLeaf(
const autoResize = child.textAutoResize
const isRow = parent.layoutMode === 'HORIZONTAL'
const growsWidth = configureGrowingText(yogaChild, child, parent, fixedDerivedMainAxis)
+ const stretchesCross =
+ child.layoutAlignSelf === 'STRETCH' ||
+ (child.layoutAlignSelf === 'AUTO' && parent.counterAxisAlign === 'STRETCH')
+ // Layout sets the width of text that grows along a row or stretches across a column.
+ const fillsWidth = (!isRow && stretchesCross) || growsWidth
const cache = new Map<number, { width: number; height: number }>()
const UNCONSTRAINED_KEY = -1
@@ -574,8 +579,11 @@ function configureTextLeaf(
return
}
+ // Auto-width text keeps each line whole, as Figma's does, in a parent narrower than the text;
+ // only a width that layout or a max width sets wraps it.
+ const wraps = fillsWidth || child.maxWidth != null
yogaChild.setMeasureFunc((width, widthMode, _height, _heightMode) => {
- const maxW = widthMode === MeasureMode.Undefined ? undefined : width
+ const maxW = wraps && widthMode !== MeasureMode.Undefined ? width : undefined
const cacheKey = maxW === undefined ? UNCONSTRAINED_KEY : Math.round(maxW)
const cached = cache.get(cacheKey)
if (cached) return cached
@@ -585,14 +593,10 @@ function configureTextLeaf(
return result
})
} else if (autoResize === 'HEIGHT') {
- const stretchesCross =
- child.layoutAlignSelf === 'STRETCH' ||
- (child.layoutAlignSelf === 'AUTO' && parent.counterAxisAlign === 'STRETCH')
- // Let Yoga stretch or grow fill-width text instead of fixing its stored width.
- const fillsWidth = (!isRow && stretchesCross) || growsWidth
const fixedWidth = fixedDerivedMainAxis
? (child.derivedLayout?.width ?? child.width)
: child.width
+ // Let Yoga stretch or grow fill-width text instead of fixing its stored width.
if (child.layoutGrow <= 0 && !fillsWidth) {
yogaChild.setWidth(fixedWidth)
}
diff --git a/packages/core/tests/layout/auto-layout/text/auto-width.test.ts b/packages/core/tests/layout/auto-layout/text/auto-width.test.ts
new file mode 100644
index 000000000..f91b05737
--- /dev/null
+++ b/packages/core/tests/layout/auto-layout/text/auto-width.test.ts
@@ -0,0 +1,81 @@
+import { afterEach, describe, expect, test } from 'bun:test'
+
+import { computeAllLayouts, setTextMeasurer } from '@open-pencil/core/layout'
+import { SceneGraph } from '@open-pencil/scene-graph'
+
+const TEXT = 'Twenty characters!!!'
+
+/** 10px a character on one line; a narrower width wraps it onto lines 20px tall. */
+function measureByLength() {
+ setTextMeasurer((node, maxWidth) => {
+ const width = (node.text?.length ?? 0) * 10
+ if (maxWidth !== undefined && width > maxWidth) {
+ return { width: maxWidth, height: 20 * Math.ceil(width / maxWidth) }
+ }
+ return { width, height: 20 }
+ })
+}
+
+function size(graph: SceneGraph, id: string) {
+ const node = graph.getNode(id)
+ return { width: node?.width, height: node?.height }
+}
+
+function frame(graph: SceneGraph, layoutMode: 'HORIZONTAL' | 'VERTICAL', width: number) {
+ return graph.createNode('FRAME', graph.getPages()[0].id, {
+ layoutMode,
+ width,
+ height: 100,
+ primaryAxisSizing: 'FIXED',
+ counterAxisSizing: 'FIXED'
+ })
+}
+
+afterEach(() => setTextMeasurer(null))
+
+// `@figma/plugin-typings` on textAutoResize: in WIDTH_AND_HEIGHT, "Characters do not wrap."
+describe('auto-width text in an auto layout frame narrower than the text', () => {
+ test.each(['HORIZONTAL', 'VERTICAL'] as const)('keeps its line whole in a %s frame', (mode) => {
+ measureByLength()
+ const graph = new SceneGraph()
+ const parent = frame(graph, mode, 100)
+ const text = graph.createNode('TEXT', parent.id, {
+ text: TEXT,
+ textAutoResize: 'WIDTH_AND_HEIGHT'
+ })
+
+ computeAllLayouts(graph)
+
+ expect(size(graph, text.id)).toEqual({ width: 200, height: 20 })
+ })
+
+ test('wraps where layout stretches it across a column', () => {
+ measureByLength()
+ const graph = new SceneGraph()
+ const column = frame(graph, 'VERTICAL', 100)
+ const text = graph.createNode('TEXT', column.id, {
+ text: TEXT,
+ textAutoResize: 'WIDTH_AND_HEIGHT',
+ layoutAlignSelf: 'STRETCH'
+ })
+
+ computeAllLayouts(graph)
+
+ expect(size(graph, text.id)).toEqual({ width: 100, height: 40 })
+ })
+
+ test('wraps at its max width', () => {
+ measureByLength()
+ const graph = new SceneGraph()
+ const row = frame(graph, 'HORIZONTAL', 300)
+ const text = graph.createNode('TEXT', row.id, {
+ text: TEXT,
+ textAutoResize: 'WIDTH_AND_HEIGHT',
+ maxWidth: 120
+ })
+
+ computeAllLayouts(graph)
+
+ expect(size(graph, text.id)).toEqual({ width: 120, height: 40 })
+ })
+})