Canvas & Layout

Auto layout

Auto layout is a property of a frame. Turn it on and the frame places its children in a row or a column, in their order, with a gap between them and padding around them. Change a label, add an item or resize the frame, and everything moves to suit. It is the difference between a design that holds and one you repair by hand after every edit.

The same three children as a row, a column and a row that wraps
The same three children as a row, a column and a row that wraps

Turn it on#

By hand
  1. Select the layers that belong together and press ⇧A (Arrange › Add Auto Layout). Nox makes a row or a column of them, whichever they stand in more nearly, with the gap and padding they already have.
  2. Or select a frame: in the inspector's Layout section choose Auto, then Row, Column or Wrap.
  3. Set Gap and Pad. Open More for a padding on each side and for Space between.
  4. Click one of the nine places to say where the children stand in the frame.
The Layout section of the inspector for a row
The Layout section of the inspector for a row
With an agent

Say what the container is, not where its children go. Children of a layout need no x and y.

Make a toolbar row 480 wide and 56 tall: 16 padding at the sides, 12 between items, everything centered vertically. Put three 96 × 32 rounded tiles in it.

The agent calls create_nodes:

{
  "nodes": [
    {
      "kind": "frame", "name": "Toolbar", "x": 0, "y": 0, "width": 480, "height": 56, "fill": "#FFFFFF",
      "layout": {"direction": "horizontal", "gap": 12, "padding": [0, 16], "align": "start", "cross_align": "center"},
      "children": [
        {"kind": "rectangle", "name": "Inbox", "width": 96, "height": 32, "corner_radius": 8, "fill": "#2F6BFF"},
        {"kind": "rectangle", "name": "Drafts", "width": 96, "height": 32, "corner_radius": 8, "fill": "#12B76A"},
        {"kind": "rectangle", "name": "Sent", "width": 96, "height": 32, "corner_radius": 8, "fill": "#F79009"}
      ]
    }
  ]
}

padding is one number, [vertical, horizontal], or [top, right, bottom, left]. To turn a layout off, set layout to null: the children stay where they are.

Alignment and space between#

align places the children along the direction: start, center, end or space_between. cross_align places them across it: start, center, end, and in a row baseline, which stands texts of different sizes on one line.

Start, center, end and space between in one row
Start, center, end and space between in one row

With space between, the first child sits at one edge, the last at the other, and the rest spread evenly. The gap becomes the least distance. This is how a navigation bar keeps its logo left and its button right at any width.

Fixed, Hug, Fill and a share#

Every node in a layout has a width and a height, and each is made one of four ways.

Fixed, hug, fill and a share of 50% with the same three children
Fixed, hug, fill and a share of 50% with the same three children
SizingIn the toolsWhat it does
Fixeda numberThe size you give
Hug"hug"As large as the content: a frame's children plus gap and padding, a text's lines
Fill"fill"Along the direction, an equal share of what is left; across it, the whole inside of the frame
A share"50%"That part of the inside of the frame

By hand, the W and H menus at the foot of the Layout section offer Fixed, Hug and Fill; you can also type hug or fill into the W or H field. A share is set through the tools.

Two rules save most surprises:

  • A button hugs. Give it padding (say [0, 20]) and a fixed height of 44, and let its width hug the label. Translations and longer labels then cost nothing.
  • A child cannot fill a frame that hugs along the same direction: there is nothing left to share, so it keeps its content's size.

Equal-height cards#

Cards in a row rarely have the same amount of text. Set the row's height to hug and each card's height to fill. The row becomes as tall as the tallest card's content, and every card takes that height.

Three pricing cards hugging their own height, then filling the height of a row that hugs
Three pricing cards hugging their own height, then filling the height of a row that hugs
{
  "nodes": [
    {
      "kind": "frame", "name": "Plans", "x": 0, "y": 0, "width": 672, "height": "hug",
      "layout": {"direction": "horizontal", "gap": 16},
      "children": [
        {
          "kind": "frame", "name": "Starter", "width": "fill", "height": "fill", "fill": "#FFFFFF", "corner_radius": 14,
          "layout": {"direction": "vertical", "gap": 10, "padding": 20},
          "children": [
            {"kind": "text", "text": "Starter", "font_size": 15, "font_weight": 600, "fill": "#111318"},
            {"kind": "text", "text": "3 projects", "width": "fill", "font_size": 13, "fill": "#475467"}
          ]
        },
        {
          "kind": "frame", "name": "Team", "width": "fill", "height": "fill", "fill": "#FFFFFF", "corner_radius": 14,
          "layout": {"direction": "vertical", "gap": 10, "padding": 20},
          "children": [
            {"kind": "text", "text": "Team", "font_size": 15, "font_weight": 600, "fill": "#111318"},
            {"kind": "text", "text": "Unlimited projects\nShared libraries\nVersion history\nPriority support", "width": "fill", "font_size": 13, "fill": "#475467"}
          ]
        }
      ]
    }
  ]
}

By hand: select the row and set H to Hug; select the cards and set H to Fill.

Wrap#

A row with Wrap starts another line when a child does not fit in the frame's width. The frame needs a width of its own for this. row_gap is the room between lines, and is the same as gap if you leave it out. Use wrap for tags, chips and cards that should reflow (see Responsive patterns).

{"nodes": [{"kind": "frame", "name": "Tags", "x": 0, "y": 0, "width": 236, "height": "hug", "layout": {"direction": "horizontal", "wrap": true, "gap": 8, "row_gap": 8, "padding": 12}, "children": [
  {"kind": "rectangle", "width": 96, "height": 56, "corner_radius": 10, "fill": "#2F6BFF"},
  {"kind": "rectangle", "width": 96, "height": 56, "corner_radius": 10, "fill": "#12B76A"},
  {"kind": "rectangle", "width": 96, "height": 56, "corner_radius": 10, "fill": "#F79009"}]}]}

Children that stand free of the flow#

A badge on a corner should not push its neighbors aside. A child with free: true is skipped by the layout and placed by its own x and y, measured from the frame's corner.

A badge taking a place in the row, and the same badge standing free on the corner
A badge taking a place in the row, and the same badge standing free on the corner
Add a red 20 pt notification badge to the top-right corner of the selected row. It should overlap the corner and not move the other items.

The agent adds the badge with create_nodes, with the row as parent, and these properties on it: "free": true, "x": 196, "y": -8.

Min and max sizes#

min_size and max_size are [width, height] limits that apply wherever a layout decides a size: a child that fills, a frame or a text that hugs. 0 on an axis means no limit. By hand they are Min W, Min H, Max W and Max H under More below the size fields.

A text whose width hugs, with max_size: [220, 0], grows to 220 and then wraps: a chat bubble. A search field with width: "fill", min_size: [120, 0] and max_size: [180, 0] stretches with its toolbar but never becomes unusable or absurd. There is a picture of both in Pins and resizing.

Note

In a layout, an x or y you give to a child in the flow does not last. To change its place, change its order: drag it among its siblings, or reorder it in the layer list.