Guard set_layout on NONE frames, remove implicit auto-layout on childCount >= 2

- set_layout returns error if direction omitted on layoutMode: NONE frame
- Remove shouldEnableAutoLayout childCount >= 2 heuristic — require explicit flex prop
This commit is contained in:
Anton A S 2026-03-13 14:54:12 +03:00
parent d688d04bef
commit aa6a8fa5a9
2 changed files with 11 additions and 11 deletions

View file

@ -140,8 +140,7 @@ async function renderNode(graph: SceneGraph, tree: TreeNode, parentId: string):
const parentLayout = parent?.layoutMode ?? 'NONE'
const isText = nodeType === 'TEXT'
const childCount = tree.children.filter((c) => typeof c !== 'string' && isTreeNode(c)).length
const overrides = propsToOverrides(tree.props, isText, parentLayout, childCount)
const overrides = propsToOverrides(tree.props, isText, parentLayout)
if (isText) {
const textContent = tree.children.filter((c): c is string => typeof c === 'string').join('')
@ -356,12 +355,10 @@ function applyAutoLayoutSizing(
function shouldEnableAutoLayout(
props: Record<string, unknown>,
isText: boolean,
childCount: number
isText: boolean
): boolean {
if (props.flex !== undefined) return true
if (!isText && hasAutoLayoutTriggerProps(props)) return true
if (!isText && !props.grid && childCount >= 2) return true
return false
}
@ -371,8 +368,7 @@ function applyLayoutOverrides(
w: unknown,
h: unknown,
isText: boolean,
parentLayout: SceneNode['layoutMode'],
childCount: number
parentLayout: SceneNode['layoutMode']
): void {
if (props.grid) {
applyGridOverrides(props, o, w, h)
@ -385,7 +381,7 @@ function applyLayoutOverrides(
applyGridChildOverrides(props, o)
}
if (shouldEnableAutoLayout(props, isText, childCount)) {
if (shouldEnableAutoLayout(props, isText)) {
applyAutoLayoutSizing(o, props, w, h)
}
@ -519,8 +515,7 @@ function applyShapeAndEffectOverrides(props: Record<string, unknown>, o: Partial
function propsToOverrides(
props: Record<string, unknown>,
isText: boolean,
parentLayout: SceneNode['layoutMode'],
childCount = 0
parentLayout: SceneNode['layoutMode']
): Partial<SceneNode> {
const o: Partial<SceneNode> = {}
@ -528,7 +523,7 @@ function propsToOverrides(
const { w, h } = applySizeOverrides(props, o, parentLayout)
applyVisualOverrides(props, o)
applyLayoutOverrides(props, o, w, h, isText, parentLayout, childCount)
applyLayoutOverrides(props, o, w, h, isText, parentLayout)
if (isText) applyTextOverrides(props, o, parentLayout)
applyShapeAndEffectOverrides(props, o)

View file

@ -194,6 +194,11 @@ export const setLayout = defineTool({
const node = figma.getNodeById(args.id)
if (!node) return { error: `Node "${args.id}" not found` }
const raw = figma.graph.getNode(args.id)
if (!args.direction && raw?.layoutMode === 'NONE') {
return { error: 'Frame has no auto-layout. Pass direction ("HORIZONTAL" or "VERTICAL") to enable it.' }
}
if (args.direction) node.layoutMode = args.direction as 'HORIZONTAL' | 'VERTICAL'
if (args.spacing !== undefined) node.itemSpacing = args.spacing
if (args.align !== undefined) node.primaryAxisAlignItems = args.align