Search Docs
Loading...
Skip to content

Auto Layout

Give a page a layout, and its children position and size themselves. The design then resizes correctly with no manual fixes.

Add a Frame and a Layout#

A page can hold another page. That inner page is a frame: it has a fill, a stroke and effects, and it clips its children by default. It has no bleed, margins or title.

The layout/* properties of a page position its children. layout/mode is None, Horizontal, Vertical or Grid. The child order is the flow order.

const card = engine.block.create('page');
engine.block.appendChild(page, card);
engine.block.setEnum(card, 'layout/mode', 'Vertical');
engine.block.setFloat(card, 'layout/gap', 16);
engine.block.setFloat(card, 'layout/padding/left', 24);

The values stay when layout/mode changes. With None, the children keep their computed positions as absolute positions.

A child in a flow has no position of its own, so setPositionX and setPositionY return an error on it.

Size Children#

Each child sets how it sizes itself on each axis. Fixed keeps its size, Hug follows its content, and Fill takes the free space. A text block with Fill wraps at the width that it gets.

engine.block.setEnum(text, 'layoutChild/widthSizing', 'Fill');
engine.block.setHeightMode(text, 'Auto');
engine.block.setFloat(text, 'layoutChild/maxWidth', 600);

A container with an Auto size hugs its children. When a child of a hug container is set to Fill, that axis of the container becomes fixed.

Use a Grid#

A grid has column and row tracks. layout/gap is the space between columns, and layout/gap/counterAxis is the space between rows. A track is a length such as 100, a share such as 1fr, a percentage such as 50%, auto, min-content or max-content.

engine.block.setEnum(dashboard, 'layout/mode', 'Grid');
engine.block.setString(dashboard, 'layout/grid/columns', '220 1fr 1fr');
engine.block.setString(dashboard, 'layout/grid/rows', '72 1fr');
engine.block.setFloat(dashboard, 'layout/gap', 16);
engine.block.setInt(header, 'layoutChild/gridColumn', 2);
engine.block.setInt(header, 'layoutChild/gridColumnSpan', 2);
engine.block.setEnum(header, 'layoutChild/widthSizing', 'Fill');

Children Outside the Flow#

Set layoutChild/ignoreLayout to keep a child in a container but out of its flow. A resize constraint then decides how the child follows its parent: Start, End, Center, Both or Scale. Constraints also work for children of a page without a layout.

engine.block.setBool(badge, 'layoutChild/ignoreLayout', true);
engine.block.setEnum(badge, 'layoutChild/horizontalConstraint', 'End');

Scale the Content With the Page#

Set layout/scaleContent to ShorterSide on a page. When the page changes size, its content draws scaled by the change of the shorter side. Font sizes, spacing, corner radii, strokes and fixed sizes follow, and the layout arranges the content for the new aspect ratio. The scale changes no property of the content.

engine.block.setEnum(page, 'layout/scaleContent', 'ShorterSide');
engine.block.setWidth(page, 1080);
engine.block.setHeight(page, 1920);

When the content of a layout still does not fit, the page scales it down until it fits. Set layout/scaleContent back to None to write the current scale into the content.

Show Blocks by Size#

A block inside a page can show only for a range of sizes. layoutChild/visibility/minAspectRatio and maxAspectRatio compare the aspect ratio of the page. minParentWidth, maxParentWidth, minParentHeight and maxParentHeight compare the size of the parent, and minParentAspectRatio and maxParentAspectRatio its aspect ratio.

engine.block.setFloat(sidebar, 'layoutChild/visibility/minAspectRatio', 1.2);
engine.block.setInt(subtitle, 'layoutChild/visibility/hideOrder', 1);
engine.block.setInt(badge, 'layoutChild/visibility/hideOrder', 2);
const hidden = engine.block.getBool(subtitle, 'layoutChild/visibility/hidden');

hideOrder hides blocks when the content of a layout does not fit, the smallest positive order first. A hidden block shows again when the page size changes and the block fits. layoutChild/visibility/hidden tells whether a rule hides the block now.

Animate a Layout#

Keyframes on width and height move the other blocks of a layout: the children of the block and its neighbours in the flow follow the animated size. layout/gap, layout/gap/counterAxis and the four layout/padding/* properties take keyframes too.

engine.block.setKeyframes(card, 'width', [
{ time: 0, value: { type: 'Float', value: 400 } },
{ time: 1, value: { type: 'Float', value: 600 } }
]);
engine.block.setKeyframes(card, 'layout/gap', [
{ time: 0, value: { type: 'Float', value: 8 } },
{ time: 1, value: { type: 'Float', value: 24 } }
]);

A width or height track in Relative mode changes only how the block itself draws.

Frames and Pages#

The opacity, blend mode, masks and clipped of a frame apply to its children too. Use scene.getPages for the top-level pages only, because findByType('page') also returns frames. A group in a flow takes one slot, and its members stay together.

Components#

Convert a frame to a main component, and create instances of it. An instance follows every property that it does not override. Its children cannot be added, reordered or destroyed. Hide a child with setVisible.

engine.block.convertToComponent(card);
engine.block.addComponentProperty(card, 'title', titleText, 'text/text');
const instance = engine.block.createInstance(card);
engine.block.appendChild(page, instance);
engine.block.setComponentProperty(instance, 'title', 'Hello');

To offer variants, combine components with combineAsVariants, name their values with setVariantProperty, and switch an instance with setInstanceVariant. Use resetInstance to remove overrides, and detachInstance to turn an instance into plain blocks.