# Grid

## Usage

`Grid` arranges its children into rows and columns with CSS grid. It solves two-dimensional layout: card galleries, dashboard panels, and any region where several columns have to line up. One-dimensional arrangement belongs to the `Stack` family; reach for `Grid` when rows and columns both matter.

Give it a column configuration and it is ready:

```vue
<script setup lang="ts">
import { Grid } from '@astryx-vue/core/Grid'
</script>

<template>
  <Grid :columns="3" :gap="4">
    <div>First</div>
    <div>Second</div>
    <div>Third</div>
  </Grid>
</template>
```

A number for `columns` means a fixed number of equal-width columns. An object means responsive columns: `minWidth` is the smallest a column may get, in pixels, and the grid decides how many columns fit in the available width on its own, dropping to fewer columns on a narrow screen.

```vue
<template>
  <Grid :columns="{ minWidth: 280 }" :gap="4">
    <div>First</div>
    <div>Second</div>
  </Grid>
</template>
```

The responsive configuration has two more options. `repeat` decides what happens to the gap a partly filled last row leaves behind: `'fill'` (the default) keeps the empty tracks, so every column always has the same width, while `'fit'` collapses them so the columns that are present stretch across the whole row. `max` caps how many columns may appear, which keeps columns from growing uncomfortably wide on a large screen. The cap sits on the minimum track size rather than the maximum, so the count never exceeds `max` while the columns that are present still fill the row — no dead space on the right.

`gap` sets the spacing between rows and columns, with `rowGap` and `columnGap` overriding it on a single axis, and all three take a step from the Astryx spacing scale. `rowHeight` fixes the height of implicit rows in pixels and, together with `GridSpan rows`, produces items that span several rows.

Wrap an item in `GridSpan` when it has to span more than one column or row. Its `columns` takes a number for the number of columns, or `'full'` to span the entire row, and `rows` takes the number of rows.

```vue
<template>
  <Grid :columns="4" :gap="3">
    <GridSpan :columns="2">
      Spans two columns
    </GridSpan>
    <div>Regular item</div>
    <GridSpan columns="full">
      Spans the full row
    </GridSpan>
  </Grid>
</template>
```

`width`, `height`, `maxWidth`, and `minHeight` size the container itself: numbers are pixels and strings are used as they are. `align` controls how items sit vertically inside their row (`align-items`) and `justify` controls the horizontal axis (`justify-items`); both default to `stretch`, which makes every item fill its cell.

The column template is written to the container as the CSS variable `--astryx-grid-template-columns` rather than as an inline `grid-template-columns` declaration. That way your own stylesheet, including a rule inside a media query, can still override the variable or the declaration itself.

### Anatomy

- **Grid container**: Two-dimensional layout container that arranges caller-supplied items in rows and columns.
- **Spanning item**: Optional GridSpan wrapper that changes one item's column or row participation.

## Best practices

| Guidance | Practices |
| --- | --- |
| Do | Use responsive columns (`:columns="{ minWidth: 280 }"`) whenever the layout should adapt to the screen. Let the grid work out the column count instead of writing media queries. |
| Do | Cap the count with `max` on wide screens. Without a cap each column grows very wide and the contents of a row drift far apart. |
| Do | Keep the default `repeat: 'fill'` when every column should have the same width, and switch to `'fit'` when the items of a partly filled row should stretch into the leftover space. |
| Do | Leave spacing to `gap` and the container size to `width`, `maxWidth`, and friends rather than adding margins to the children. |
| Don't | Hand-write `display: grid` and `grid-template-columns` and compute the gaps yourself. `Grid` already does that work. |
| Don't | Fake a grid with a wrapping `HStack`. Use `Stack` for one-dimensional arrangement and `Grid` when both axes have to line up. |
| Don't | Express a fixed column count through the object form of `columns`. Pass a number, which says what it means. |

## Examples

### Dashboard layout

A dashboard mixes a large traffic card, four metrics, and a full-width activity row.

```vue
<script setup lang="ts">
import { Card } from '@astryx-vue/core/Card'
import { Grid, GridSpan } from '@astryx-vue/core/Grid'
import { Text } from '@astryx-vue/core/Text'

const metrics = [
  { label: 'Revenue', value: '$48,290' },
  { label: 'Active Users', value: '12,841' },
  { label: 'Conversion', value: '3.2%' },
  { label: 'Avg Response', value: '245ms' },
]
</script>

<template>
  <Grid :columns="4" :gap="4" width="100%" style="max-width: 500px">
    <GridSpan :columns="2" :rows="2">
      <Card>
        <Text display="block" type="label">
          Weekly Traffic
        </Text>
        <Text display="block" type="supporting">
          Page views and unique visitors over the last 7 days
        </Text>
      </Card>
    </GridSpan>
    <Card v-for="metric in metrics" :key="metric.label">
      <Text display="block" type="supporting">
        {{ metric.label }}
      </Text>
      <Text display="block" type="label">
        {{ metric.value }}
      </Text>
    </Card>
    <GridSpan columns="full">
      <Card>
        <Text display="block" type="label">
          Recent Activity
        </Text>
        <Text display="block" type="supporting">
          Latest events across all projects
        </Text>
      </Card>
    </GridSpan>
  </Grid>
</template>
```

### Card gallery

Cards keep a consistent minimum width and reflow as the gallery gets narrower.

```vue
<script setup lang="ts">
import { Card } from '@astryx-vue/core/Card'
import { Grid } from '@astryx-vue/core/Grid'
import { VStack } from '@astryx-vue/core/Stack'
import { Text } from '@astryx-vue/core/Text'

const cards = [
  { title: 'Getting Started', description: 'Learn the basics of the platform.' },
  { title: 'Components', description: 'Browse the full component library.' },
  { title: 'Design Tokens', description: 'Colors, spacing, and typography.' },
  { title: 'Theming', description: 'Customize the look and feel.' },
  { title: 'Accessibility', description: 'Build inclusive experiences.' },
  { title: 'Patterns', description: 'Common UI composition patterns.' },
]
</script>

<template>
  <Grid :columns="{ minWidth: 180 }" :gap="5" width="100%" style="max-width: 400px">
    <Card v-for="card in cards" :key="card.title">
      <VStack :gap="1">
        <Text display="block" type="label">
          {{ card.title }}
        </Text>
        <Text display="block" type="supporting">
          {{ card.description }}
        </Text>
      </VStack>
    </Card>
  </Grid>
</template>
```

### Responsive auto-fit

Drag the handle. Cards stretch to fill the remaining width of a row that is not full.

```vue
<script setup lang="ts">
import { useResizable } from '@astryx-vue/core'
import { Card } from '@astryx-vue/core/Card'
import { Grid } from '@astryx-vue/core/Grid'
import { Layout, LayoutContent, LayoutPanel } from '@astryx-vue/core/Layout'
import { ResizeHandle } from '@astryx-vue/core/Resizable'
import { VStack } from '@astryx-vue/core/Stack'
import { Text } from '@astryx-vue/core/Text'

const teams = [
  { name: 'Design Systems', members: 8 },
  { name: 'Frontend Platform', members: 12 },
  { name: 'Developer Experience', members: 6 },
  { name: 'Accessibility', members: 4 },
  { name: 'Performance', members: 7 },
  { name: 'Mobile Infrastructure', members: 9 },
]

const gridPanel = useResizable({
  defaultSize: 480,
  minSize: 100,
  maxSize: 480,
})
</script>

<template>
  <Card :height="400" :padding="0" variant="muted" width="100%" style="max-width: 500px">
    <Layout height="fill">
      <template #start>
        <LayoutPanel :has-divider="false" :width="gridPanel.size" style="padding: var(--astryx-spacing-4, 16px)">
          <Grid :columns="{ minWidth: 180, repeat: 'fit' }" :gap="4" width="100%">
            <Card v-for="team in teams" :key="team.name">
              <VStack :gap="1">
                <Text display="block" type="label">
                  {{ team.name }}
                </Text>
                <Text display="block" type="supporting">
                  {{ team.members }} members
                </Text>
              </VStack>
            </Card>
          </Grid>
        </LayoutPanel>
        <ResizeHandle
          direction="horizontal"
          has-divider
          is-always-visible
          label="Resize grid"
          :resizable="gridPanel.props"
        />
      </template>
      <LayoutContent />
    </Layout>
  </Card>
</template>
```

### Column spanning

Featured cards span columns and rows beside a set of smaller stats.

```vue
<script setup lang="ts">
import { Card } from '@astryx-vue/core/Card'
import { Grid, GridSpan } from '@astryx-vue/core/Grid'
import { VStack } from '@astryx-vue/core/Stack'
import { Text } from '@astryx-vue/core/Text'

const stats = [
  { label: 'Components', value: '54 available' },
  { label: 'Templates', value: '28 available' },
  { label: 'Tokens', value: '120 defined' },
  { label: 'Themes', value: '6 published' },
  { label: 'Icons', value: '312 available' },
  { label: 'Patterns', value: '18 documented' },
  { label: 'Contributors', value: '42 active' },
]
</script>

<template>
  <Grid :columns="3" :gap="4" width="100%" style="max-width: 500px">
    <GridSpan :rows="2">
      <Card variant="cyan">
        <VStack :gap="1">
          <Text display="block" type="label">
            Featured Release
          </Text>
          <Text display="block" type="supporting">
            Astryx 4.0 is now available with new layout primitives, refreshed tokens, and improved theming support across the system.
          </Text>
        </VStack>
      </Card>
    </GridSpan>
    <Card v-for="stat in stats" :key="stat.label">
      <Text display="block" type="label">
        {{ stat.label }}
      </Text>
      <Text display="block" type="supporting">
        {{ stat.value }}
      </Text>
    </Card>
    <GridSpan columns="full">
      <Card variant="cyan">
        <VStack :gap="1">
          <Text display="block" type="label">
            Community Showcase
          </Text>
          <Text display="block" type="supporting">
            See how teams are building with Astryx across the organization
          </Text>
        </VStack>
      </Card>
    </GridSpan>
  </Grid>
</template>
```

## Grid Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `columns` | `number \| GridResponsiveColumns` | - | Column configuration. A number is a fixed count of equal-width columns; an object `{ minWidth, max?, repeat? }` creates responsive columns that reflow with the available width. A missing, zero, or negative value falls back to a single column. |
| `width` | `number \| string` | - | Width of the container. Numbers are pixels, strings are used as they are. |
| `height` | `number \| string` | - | Height of the container. Numbers are pixels, strings are used as they are. |
| `maxWidth` | `number \| string` | - | Maximum width of the container. Numbers are pixels, strings are used as they are. |
| `minHeight` | `number \| string` | - | Minimum height of the container. Numbers are pixels, strings are used as they are. |
| `gap` | `SpacingStep` | - | Spacing between items on both axes, using the Astryx spacing scale. |
| `rowGap` | `SpacingStep` | - | Spacing between rows; overrides `gap` on that axis. |
| `columnGap` | `SpacingStep` | - | Spacing between columns; overrides `gap` on that axis. It also takes precedence over `gap` when the responsive column count is capped. |
| `rowHeight` | `number` | - | Height in pixels of each implicit row (`grid-auto-rows`). Use it together with `GridSpan`'s `rows`. |
| `align` | `GridAlignment` | `'stretch'` | Vertical alignment of items inside their row (`align-items`): start, center, end, or stretch. |
| `justify` | `GridAlignment` | `'stretch'` | Horizontal alignment of items within their row (`justify-items`): start, center, end, or stretch. |

`GridResponsiveColumns` has this shape:

```ts
interface GridResponsiveColumns {
  minWidth: number
  max?: number
  repeat?: 'fill' | 'fit'
}
```

In a container narrower than `minWidth`, the single column shrinks to the container width instead of overflowing it.

`SpacingStep` accepts these values:

```ts
type SpacingStep = 0 | 0.5 | 1 | 1.5 | 2 | 3 | 4 | 5 | 6 | 8 | 10
```

## Grid Span Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `columns` | `number \| 'full'` | - | Columns to span. `'full'` renders `grid-column: 1 / -1`, which spans the row whatever the column count is. |
| `rows` | `number` | - | Rows to span, rendered as `grid-row: span N`. |

## Slots

| Name | Description |
| --- | --- |
| `default` | The items to lay out. Place children directly, or wrap one in `GridSpan` to change how many columns or rows it occupies. |

`GridSpan` has a single default slot as well:

| Name | Description |
| --- | --- |
| `default` | The content of the spanning item. |

## CSS variables

`Grid` writes these variables to its root element. They are an implementation detail, but a theme or an application may override them:

| Name | Description |
| --- | --- |
| `--astryx-grid-template-columns` | The computed column template. It lives in a variable rather than an inline `grid-template-columns` declaration, so a media query can still override it. |
| `--astryx-grid-auto-rows` | The implicit row height derived from `rowHeight`. |
| `--astryx-grid-gap` | The spacing token for `gap`, shared by both axes. |
| `--astryx-grid-row-gap` | The spacing token for `rowGap`, which overrides `--astryx-grid-gap` on the row axis. |
| `--astryx-grid-column-gap` | The spacing token for `columnGap`, which overrides `--astryx-grid-gap` on the column axis. |
| `--astryx-grid-default-gap` | A theme token used as the spacing when no gap prop is given. The neutral theme sets it to `--astryx-spacing-0`, which leaves no space between items. |

## Native attributes

`Grid` and `GridSpan` both render a `<div>` at the root.

The following go to the root element:

- HTML attributes.
- `aria-*` attributes.
- `data-*` attributes.
- `class`.
- `style`.

`class` is merged with the component's own classes. When <code>style</code> and the size props (<code>width</code>, <code>height</code>, <code>maxWidth</code>, <code>minHeight</code>) are both given, the props win; the same holds for <code>GridSpan</code>'s <code>columns</code> and <code>rows</code>.

`Grid` also writes `data-columns` (only when `columns` is a number), `data-gap`, `data-row-gap`, `data-column-gap`, `data-align`, and `data-justify`, so a theme can target it by attribute.
