# Grid

> The one Layout widget of GolemUI Forms. It stacks fields, puts them side by side in a row or wraps them in columns, and lines up their labels, controls and errors.

Grid is the one [Layout Field](https://golemui.com/dx/widgets-reference/layout-fields/overview.md) for arranging other
widgets. It stacks them, puts them side by side in a row or wraps them in columns. In a row or
in columns, it lines up the labels, the controls and the errors of the fields next to each other,
so a long label or an error never moves the controls beside it.

With no props, a grid stacks its children, one below the other:


```ts title="grid.ts"

export default [
  gui.layouts.grid([
    gui.inputs.textInput('c1', {
      label: 'First field',
      uid: 'c1',
    }),
    gui.inputs.textInput('c2', {
      label: 'Second field',
      uid: 'c2',
    }),
  ], {
    uid: 'grid_stack',
  }),
];
```

_Live demo on the docs page. Form definition: [grid.ts](https://golemui.com/template/widgets-dx/grid/grid.ts)._

## Props

Use these `props` to customize your Grid widget.

| prop        | type             | description                                                                                                                                                                          |
| ----------- | ---------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `columns`   | number \| `auto` | Wraps the children in that many equal columns, from `1` to `12`, or in as many as fit with `'auto'`. Overrides `direction`                                                           |
| `direction` | string           | `'column'` stacks the children; `'row'` puts them on one line. Defaults to `'column'`                                                                                                |
| `gap`       | string           | The space between the children: `'none'`, `'xs'`, `'sm'`, `'md'`, `'lg'` or `'xl'`. Defaults to `'md'`                                                                               |
| `justify`   | string           | In a row: `'stretch'` shares the width by each child's `size`; `'start'`, `'center'`, `'end'` and `'space-between'` keep each child at its own width and place them. Defaults to `'stretch'` |

In the DSL, `gui.layouts.horizontalGrid` is a row and `gui.layouts.verticalGrid` a stack. See the
[`gui.layouts` reference](https://golemui.com/dx/form-definition/reference/gui-layouts.md). Every prop also takes
[state suffixes](https://golemui.com/dx/form-definition/states.md), such as `"direction.compact": "column"`.

### Row

Set `direction` to `"row"` to put the children side by side, sharing the width equally.


```ts title="grid.ts"

export default [
  gui.layouts.grid([
    gui.inputs.textInput('r1', {
      label: 'Left Field',
      uid: 'r1',
    }),
    gui.inputs.textInput('r2', {
      label: 'Right Field',
      uid: 'r2',
    }),
  ], {
    direction: 'row',
    uid: 'grid_row',
  }),
];
```

_Live demo on the docs page. Form definition: [grid-row.ts](https://golemui.com/template/widgets-dx/grid/grid-row.ts)._

### Child sizes

In a row, a child's `size` sets its share of the width: a child with `size: 2` is twice as wide
as one with the default `size` of `1`.


```ts title="grid.ts"

export default [
  gui.layouts.grid([
    gui.inputs.textInput('s1', {
      label: 'Default Size (1)',
      uid: 's1',
    }),
    gui.inputs.textInput('s2', {
      label: 'Size 2',
      size: 2,
      uid: 's2',
    }),
  ], {
    direction: 'row',
    uid: 'grid_sizes',
  }),
];
```

_Live demo on the docs page. Form definition: [grid-sizes.ts](https://golemui.com/template/widgets-dx/grid/grid-sizes.ts)._

### Columns

Set `columns` to a number from `1` to `12` to wrap the children in that many equal columns. They
fill the columns in order, and a child with a `size` spans that many of them.


```ts title="grid.ts"

export default [
  gui.layouts.grid([
    gui.inputs.textInput('street', {
      label: 'Street',
      size: 2,
      uid: 'street',
    }),
    gui.inputs.textInput('floor', {
      label: 'Floor',
      uid: 'floor',
    }),
    gui.inputs.textInput('city', {
      label: 'City',
      uid: 'city',
    }),
    gui.inputs.textInput('postcode', {
      label: 'Postcode',
      uid: 'postcode',
    }),
    gui.inputs.textInput('country', {
      label: 'Country',
      uid: 'country',
    }),
  ], {
    columns: 3,
    uid: 'grid_columns',
  }),
];
```

_Live demo on the docs page. Form definition: [grid-columns.ts](https://golemui.com/template/widgets-dx/grid/grid-columns.ts)._

With `columns: "auto"`, the grid fits as many columns as there is room for, each at least 12rem
wide (the `--gui-layout-column-min` token), and wraps the rest onto new lines. `size` has no
effect here.


```ts title="grid.ts"

export default [
  gui.layouts.grid([
    gui.inputs.textInput('email', {
      label: 'Email',
      uid: 'email',
    }),
    gui.inputs.textInput('phone', {
      label: 'Phone',
      uid: 'phone',
    }),
    gui.inputs.textInput('website', {
      label: 'Website',
      uid: 'website',
    }),
    gui.inputs.textInput('company', {
      label: 'Company',
      uid: 'company',
    }),
    gui.inputs.textInput('jobTitle', {
      label: 'Job title',
      uid: 'jobTitle',
    }),
  ], {
    columns: 'auto',
    uid: 'grid_columns_auto',
  }),
];
```

_Live demo on the docs page. Form definition: [grid-columns-auto.ts](https://golemui.com/template/widgets-dx/grid/grid-columns-auto.ts)._

### Gaps

`gap` sets the space between the children, in steps of the spacing scale:

| `gap`  | Space          |
| ------ | -------------- |
| `none` | 0              |
| `xs`   | 0.25rem        |
| `sm`   | 0.5rem         |
| `md`   | 1rem (default) |
| `lg`   | 1.5rem         |
| `xl`   | 2rem           |


```ts title="grid.ts"

export default [
  gui.layouts.grid([
    gui.inputs.textInput('g1', {
      label: 'Field 1',
      uid: 'g1',
    }),
    gui.inputs.textInput('g2', {
      label: 'Field 2',
      uid: 'g2',
    }),
    gui.inputs.textInput('g3', {
      label: 'Field 3',
      uid: 'g3',
    }),
    gui.inputs.textInput('g4', {
      label: 'Field 4',
      uid: 'g4',
    }),
  ], {
    columns: 2,
    gap: 'xl',
    uid: 'grid_gaps',
  }),
];
```

_Live demo on the docs page. Form definition: [grid-gaps.ts](https://golemui.com/template/widgets-dx/grid/grid-gaps.ts)._

### Justify

In a row, any `justify` other than `"stretch"` keeps each child at its own width and places them
along the row. `"end"` is the usual way to put the form's buttons on the right:


```ts title="grid.ts"

export default [
  gui.layouts.grid([
    gui.actions.button({
      variant: 'outlined',
      label: 'Cancel',
      uid: 'cancel',
    }),
    gui.actions.button({
      label: 'Save',
      uid: 'save',
    }),
  ], {
    direction: 'row',
    justify: 'end',
    uid: 'grid_justify',
  }),
];
```

_Live demo on the docs page. Form definition: [grid-justify.ts](https://golemui.com/template/widgets-dx/grid/grid-justify.ts)._

## How fields line up

In a row or in columns, each child spans three tracks shared with the rest of its row: the label
and hint, then the control, then the errors. So the controls stay level whatever the length of
each label, and an error only grows the last track. Checkboxes, toggles and buttons sit at the
vertical center of the controls. No props are needed for any of this.


```ts

export default [
  gui.layouts.grid([
    gui.inputs.textInput('cardName', {
      hint: 'As it is printed',
      label: 'Name on the card',
      uid: 'cardName',
    }),
    gui.inputs.textInput('cardNumber', {
      label: 'Card number, sixteen digits without spaces',
      uid: 'cardNumber',
    }),
    gui.inputs.checkbox('saveCard', {
      label: 'Save this card',
      uid: 'saveCard',
    }),
  ], {
    direction: 'row',
    uid: 'grid_alignment',
  }),
];
```

_Live demo on the docs page. Form definition: [grid-alignment.ts](https://golemui.com/template/widgets-dx/grid/grid-alignment.ts)._

Fields that need height of their own, such as a textarea, a list or a calendar, line up the same
way, but the row's control track takes the height of the tallest control. Give them a row of
their own, or a full row in a `columns` grid.

## Nested grids

A grid can be the child of another grid. Here, a stack holds two rows, and each row lines up its
own fields:


```ts

export default [
  gui.layouts.grid([
    gui.layouts.grid([
      gui.inputs.textInput('n1', {
        label: 'First Name',
        uid: 'n1',
      }),
      gui.inputs.textInput('n2', {
        label: 'Last Name',
        uid: 'n2',
      }),
    ], {
      direction: 'row',
      uid: 'grid_row_1',
    }),
    gui.layouts.grid([
      gui.inputs.textInput('n3', {
        label: 'Email',
        uid: 'n3',
      }),
      gui.inputs.textInput('n4', {
        label: 'Phone',
        size: 2,
        uid: 'n4',
      }),
    ], {
      direction: 'row',
      uid: 'grid_row_2',
    }),
  ], {
    uid: 'grid_outer',
  }),
];
```

_Live demo on the docs page. Form definition: [grid-nested.ts](https://golemui.com/template/widgets-dx/grid/grid-nested.ts)._

## Narrow containers

Every grid stacks its children when the form is narrower than 480px. It measures the form, not
the window, so a form in a narrow sidebar stacks on a wide screen too.

## Deprecated props

These props from GolemUI 1.x still work, and map to the props above. v3.0.0 removes them, so move to the new ones:

| Deprecated            | Use instead                                                                       |
| --------------------- | --------------------------------------------------------------------------------- |
| `autoFit`             | `columns`. `autoFit: false` is `columns: 12`; `true` is a row                     |
| `columnGap`, `rowGap` | `gap`. Pixels become the nearest step                                             |
| `align`               | `justify`. `space-around` and `space-evenly` become `space-between`               |

The [Flex](https://golemui.com/dx/widgets-reference/layout-fields/flex.md) layout is deprecated too, renders as a grid,
and v3.0.0 removes it. The [migration guide](https://golemui.com/dx/migrating/v1-to-v2.md#layouts) has the details.

## Styling

Grid layouts can be styled as explained in the [Styling Guide](https://golemui.com/dx/styling/overview.md). The grid is
the `gui-grid` classes of GolemUI Components, documented on its
[Grid page](https://golemui.com/components/docs/layout/grid.md).

### CSS Variables

| CSS Variable                                             | Description                                         |
| -------------------------------------------------------- | --------------------------------------------------- |
| `--gui-layout-gap-none` … `--gui-layout-gap-xl`          | The space of each `gap` step                        |
| `--gui-layout-column-min`                                | The narrowest column of a `columns: "auto"` grid    |

### Anatomy

This is the anatomy of the Grid Widget in case you want to use your CSS styles.


```html title="grid-anatomy.html"
<gui-grid-layout class="gui-field">
  <div class="gui-grid gui-grid--row" id="grid_uid">
    <div class="gui-grid__cell">
      <!-- Child 1 -->
    </div>

    <div class="gui-grid__cell gui-grid__cell--span-2">
      <!-- Child 2 (size: 2) -->
    </div>
  </div>
</gui-grid-layout>
```
