# Grid

A CSS grid of equal columns, or a custom column template. Grid is a Box, so it takes every Box prop and renders on the server.

```tsx
import { Grid } from '@blocks/design-system'
```

## Usage

`columns` takes a count from 1 to 12. Columns share the width equally and never grow past it because of long content.

**Example: Four equal columns** (`examples/grid/fixed-columns.tsx`)

```tsx
import { Box, Grid } from '@blocks/design-system'

export default function FixedColumns() {
  return (
    <Grid columns={4} gap="sm">
      {['Depth', 'New snow', 'Wind', 'Temp'].map((label) => (
        <Box key={label} padding="sm" border="default" textAlign="center">
          {label}
        </Box>
      ))}
    </Grid>
  )
}
```

## Responsive columns

Pass a breakpoint object to change the count with the screen width. The station catalog is a single readable list below `md` (48rem) and three columns from `md` up, with the same markup and reading order.

**Example: Station catalog** (`examples/grid/station-catalog.tsx`)

```tsx
import { Box, Grid } from '@blocks/design-system'

const stations = [
  { name: 'Mount Hood', state: 'Oregon', depth: '84 in' },
  { name: 'Stevens Pass', state: 'Washington', depth: '61 in' },
  { name: 'Alta', state: 'Utah', depth: '102 in' },
  { name: 'Mammoth', state: 'California', depth: '77 in' },
  { name: 'Big Sky', state: 'Montana', depth: '55 in' },
  { name: 'Taos', state: 'New Mexico', depth: '38 in' },
]

export default function StationCatalog() {
  return (
    <Box as="section" aria-label="Station catalog">
      <Grid as="ul" columns={{ base: 1, md: 3 }} gap="md" margin="none" padding="none">
        {stations.map((station) => (
          <Box as="li" key={station.name} padding="md" border="default" dangerouslySetStyle={{ listStyle: 'none' }}>
            <Box as="a" href={`#${station.name.toLowerCase().replaceAll(' ', '-')}`} display="block">
              {station.name}
            </Box>
            <Box as="span" color="text-muted">
              {station.state} · {station.depth}
            </Box>
          </Box>
        ))}
      </Grid>
      <Box as="a" href="#all-stations" display="inline-block" marginTop="md">
        Browse all stations
      </Box>
    </Box>
  )
}
```

## Custom templates

A string becomes `grid-template-columns`. Custom templates are not responsive; reach for a count when the layout should change with the width.

**Example: Sidebar and results** (`examples/grid/custom-template.tsx`)

```tsx
import { Box, Grid } from '@blocks/design-system'

export default function CustomTemplate() {
  return (
    <Grid columns="12rem minmax(0, 1fr)" gap="lg">
      <Box as="aside" aria-label="Filters" padding="md" border="default">
        Filters
      </Box>
      <Box padding="md" border="default">
        Results
      </Box>
    </Grid>
  )
}
```

## Props

**`Grid` props**

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `as` | `ElementType` | — | The element or component to render, e.g. `as={NextLink}`. |
| `columns` | `string \| ResponsiveValue<GridColumns>` | `1` | Equal columns, from 1 to 12, or a `grid-template-columns` value. A count is responsive, e.g. `{ base: 1, md: 3 }`. Defaults to `1`. |
| `gap` | `ResponsiveValue<"sm" \| "md" \| "lg" \| "xl">` | `md` | `sm`, `md`, `lg` or `xl`. Defaults to `"md"`. Responsive. |

Also accepts [Box layout props](https://design-system.blocks-ui.com/components/box.md#props), [styling escape hatches](https://design-system.blocks-ui.com/components/box.md#escape-hatches), [margin props](https://design-system.blocks-ui.com/components/box.md#spacing) and [padding props](https://design-system.blocks-ui.com/components/box.md#spacing), and the native attributes of the element it renders.

## Accessibility

- Render lists of cards `as="ul"` with `as="li"` children so screen readers announce the count.
- A grid places items in source order, row by row. Keep that order meaningful rather than positioning items out of order.
