Page icon

flex Layout

The most flexible and complex layout available, allowing components to be laid out in a flexible grid, with components spanning multiple rows or columns. It is a powerful layout for complex interface areas.

{
  "type": "view",
  "layout": "flex",
  "spacing": 4,
  "rows": 3,
  "cols": 3,
  "components": [
    {
      "constraints": { "grid": { "col": 0, "row": 0, "colspan": 2 } },
      "type": "button",
      "text": "First"
    },
    {
      "constraints": { "grid": { "col": 2, "row": 0, "colspan": 1 } },
      "type": "button",
      "text": "Second"
    },
    {
      "constraints": { "grid": { "col": 0, "row": 1, "colspan": 1 } },
      "type": "button",
      "text": "Third"
    },
    {
      "constraints": { "grid": { "col": 1, "row": 1, "colspan": 2 } },
      "type": "button",
      "text": "Fourth"
    }
  ]
}

Components are explicitly positioned and sized within the constraints of the grid.

Note

While the flex layout offers ultimate flexibility, we recommend approaching user interface design in a modular manner: Break your UI down into simpler layouts such as the border and inline and composite these views into your final UI.

Properties

rows
INTEGER
Default: 2, Range: 1-128

How many rows for this layout.

cols
INTEGER
Default: 2, Range: 1-128

How many cols for this layout.

padding
INTEGER
Default: 0, Range: 0-64

Inset the content around the perimeter of the view by the padding amount.

spacing
INTEGER
Default: 0, Range: 0-64

Add spacing between the components in the view.

Constraints

Constraints set on each component added to the view:

grid
OBJECT
Required for flex layout

Properties used by the flex layout, allowing a significant amount of flexibility.

col
INTEGER
Default: 0, Range: 0-1024

The column position within the grid.

row
INTEGER
Default: 0, Range: 0-1024

The row position within the grid.

colspan
INTEGER
Default: 1, Range: 1-1024

How many columns to span within the grid.

rowspan
INTEGER
Default: 1, Range: 1-1024

How many rows to span within the grid.

colweight
DOUBLE
Default: 0.0, Range: 0.0-1.0

How much weight this column has during resize, range 0.0 - 1.0. If colspan is more than 1, the weight is shared amongs columns.

rowweight
DOUBLE
Default: 0.0, Range: 0.0-1.0

How much weight this row has during resize, range 0.0 - 1.0. If rowspan is more than 1, the weight is shared amongs rows.

Each component can use the shared constraints to refine their position within the layout.

Resizing Behaviour

The flex layout resizes rows and columns proportionally based on the components added to it.

You can control how much weighting each row or column has with the colweight and rowweight grid constraints. By default these weightings are set to 0.0, so adding a weight to a single column or row will make it take up the maximum available remaining space. You can balance weights between multiple rows and columns to get interesting or desirable proportional layouts as the window resizes.

If you set a weight on a row or column with a rowspan or colspan larger than 1, this weighting is divided by the span amount. The algorithm will use the maximum set weightings for components sharing the same row or column.

Examples

The icon for the Try Alfred UI workflow

Note

While it is recommended to be explicit, the default type of a component is view and its layout property is stack. Here we took advantage of that to have components as simple as { "background": "White" }, which translates to a basic stack layout with a colour. For the flex views we omit type but define layout.

{
  "background": "Black",
  "components": [
    {
      "layout": "flex",
      "cols": 3,
      "rows": 3,
      "padding": 10,
      "spacing": 10,
      "components": [
        {
          "constraints": { "grid": { "colspan": 2, "rowspan": 2 } },
          "background": "systemRedColor"
        },
        {
          "constraints": { "grid": { "col": 2 } },
          "background": "systemYellowColor"
        },
        {
          "constraints": { "grid": { "col": 2, "row": 1 } },
          "layout": "flex",
          "cols": 2,
          "rows": 1,
          "spacing": 10,
          "components": [
            { "background": "White" },
            {
              "constraints": { "grid": { "col": 1 } },
              "background": "White"
            }
          ]
        },
        {
          "constraints": { "grid": { "row": 2, "colspan": 2 } },
          "components": [
            {
              "layout": "flex",
              "cols": 2,
              "rows": 2,
              "spacing": 10,
              "components": [
                {
                  "constraints": { "grid": { "col": 1 } },
                  "background": "White"
                },
                {
                  "constraints": { "grid": { "row": 1 } },
                  "background": "White"
                },
                {
                  "constraints": { "grid": { "col": 1, "row": 1 } },
                  "layout": "flex",
                  "cols": 1,
                  "rows": 2,
                  "components": [
                    { "background": "White" }
                  ]
                }
              ]
            }
          ]
        },
        {
          "constraints": { "grid": { "col": 2, "row": 2 } },
          "background": "systemBlueColor"
        }
      ]
    }
  ]
}