Grid
Arranges children in equal columns that reflow with the width of the grid itself, with no breakpoints.
Rotterdam
12,480 units on handChicago
8,215 units on handSingapore
3,902 units on handLisbon
6,730 units on handOsaka
5,118 units on handDenver
2,044 units on handimport { Badge } from '@slotted/react/badge';import { Card, CardBody } from '@slotted/react/card';import { Grid } from '@slotted/react/grid';import { Inline } from '@slotted/react/inline';import { Stack } from '@slotted/react/stack';import { Heading, Text } from '@slotted/react/typography';const WAREHOUSES = [ { name: 'Rotterdam', units: '12,480 units on hand', status: 'Healthy', tone: 'success' }, { name: 'Chicago', units: '8,215 units on hand', status: 'Low stock', tone: 'warning' }, { name: 'Singapore', units: '3,902 units on hand', status: 'Delayed', tone: 'danger' }, { name: 'Lisbon', units: '6,730 units on hand', status: 'Healthy', tone: 'success' }, { name: 'Osaka', units: '5,118 units on hand', status: 'Audit due', tone: 'info' }, { name: 'Denver', units: '2,044 units on hand', status: 'Opening soon', tone: 'secondary' },] as const;export default function GridBasic() { return ( <Grid> {WAREHOUSES.map((warehouse) => ( <Card key={warehouse.name}> <CardBody> <Stack gap={3}> <Heading level={2} size="md"> {warehouse.name} </Heading> <Text size="sm" tone="muted"> {warehouse.units} </Text> <Inline> <Badge tone={warehouse.tone}>{warehouse.status}</Badge> </Inline> </Stack> </CardBody> </Card> ))} </Grid> );}Installation
The packages are a 0.x beta on npm, and their public API changes between beta releases without deprecation. One line installs the binding and the default theme.
pnpm add @slotted/react @slotted/theme @slotted/themesGrid, Stack, Inline and Container
Use Grid for cells of equal width that wrap into rows, such as cards, metrics or form fields. Use Stack to lay children out in one column and Inline to lay them out in one row, each sized by its content. Use Container to bound the width of a whole page or section; a grid fills whatever width it is given. The root is a div; pass render in React, or write slGrid on a ul or section in Angular, to change the element. The order of the children is the reading order and the Tab order.
Minimum cell width
Without a column count, the grid draws as many equal columns as fit while each stays at least minItemWidth wide, and drops a column when it would not. The steps are xs (8rem), sm (12rem), md (16rem, the default), lg (20rem) and xl (24rem). A minimum wider than the grid becomes one full-width column. A column left empty collapses, so two cells fill the row. Each group below holds the same four cells at one step.
import { Card, CardBody } from '@slotted/react/card';import { Grid } from '@slotted/react/grid';import { Stack } from '@slotted/react/stack';import { Text } from '@slotted/react/typography';const WIDTHS = [ { size: 'xs', rem: '8rem' }, { size: 'sm', rem: '12rem' }, { size: 'md', rem: '16rem' }, { size: 'lg', rem: '20rem' }, { size: 'xl', rem: '24rem' },] as const;const BAYS = ['Bay A1', 'Bay A2', 'Bay B1', 'Bay B2'] as const;export default function GridMinItemWidth() { return ( <Stack gap={6}> {WIDTHS.map((width) => ( <Stack key={width.size} gap={3}> <Text size="sm" tone="muted">{`minItemWidth: ${width.size} (${width.rem})`}</Text> <Grid gap={3} minItemWidth={width.size}> {BAYS.map((bay) => ( <Card key={bay}> <CardBody> <Text>{bay}</Text> </CardBody> </Card> ))} </Grid> </Stack> ))} </Stack> );}Column count
columns sets the most columns a row may hold, from 1 to 12. It is a ceiling, not a promise: a row never holds a cell narrower than minItemWidth, so the grid uses fewer columns when it narrows. A count above the number of children leaves the remaining tracks empty. Four metrics below fill two, three and four columns; with three, the fourth starts a second row.
$48,200
$1,940
7
$44,310
$48,200
$1,940
7
$44,310
$48,200
$1,940
7
$44,310
import { Card, CardBody } from '@slotted/react/card';import { Grid } from '@slotted/react/grid';import { Stack } from '@slotted/react/stack';import { Heading, Text } from '@slotted/react/typography';const COUNTS = [2, 3, 4] as const;const METRICS = [ { label: 'Revenue', value: '$48,200' }, { label: 'Refunds', value: '$1,940' }, { label: 'Disputes', value: '7' }, { label: 'Payouts', value: '$44,310' },] as const;export default function GridColumns() { return ( <Stack gap={6}> {COUNTS.map((count) => ( <Stack key={count} gap={3}> <Text size="sm" tone="muted">{`columns: ${count}`}</Text> <Grid columns={count} gap={4} minItemWidth="xs"> {METRICS.map((metric) => ( <Card key={metric.label}> <CardBody> <Stack gap={1}> <Text size="sm" tone="muted"> {metric.label} </Text> <Heading level={2} size="md"> {metric.value} </Heading> </Stack> </CardBody> </Card> ))} </Grid> </Stack> ))} </Stack> );}Gap
gap is the separation between cells, a step on the space scale from 0 to 12, 6 by default. It applies to rows and columns alike. At 0 the cells touch.
import { Card, CardBody } from '@slotted/react/card';import { Grid } from '@slotted/react/grid';import { Stack } from '@slotted/react/stack';import { Text } from '@slotted/react/typography';const GAPS = [0, 2, 6, 12] as const;const ORDERS = ['SO-10482', 'SO-10483', 'SO-10484'] as const;export default function GridGap() { return ( <Stack gap={6}> {GAPS.map((gap) => ( <Stack key={gap} gap={3}> <Text size="sm" tone="muted">{`gap: ${gap}`}</Text> <Grid columns={3} gap={gap} minItemWidth="xs"> {ORDERS.map((order) => ( <Card key={order}> <CardBody> <Text numeric="tabular">{order}</Text> </CardBody> </Card> ))} </Grid> </Stack> ))} </Stack> );}Alignment across the row
align positions each child along the block axis inside its own cell. By default, and with stretch, every child takes the height of its row. start, center and end give a child its own height and place it at the top, middle or bottom of the row.
import { Card, CardBody } from '@slotted/react/card';import { Grid } from '@slotted/react/grid';import { Stack } from '@slotted/react/stack';import { Text } from '@slotted/react/typography';const ALIGNS = ['start', 'center', 'end', 'stretch'] as const;const SHIPMENTS = [ 'Ships today.', 'Waits for the carrier pickup window on Thursday afternoon.', 'Held until payment clears and the customs check for the destination country finishes.',] as const;export default function GridAlign() { return ( <Stack gap={6}> {ALIGNS.map((align) => ( <Stack key={align} gap={3}> <Text size="sm" tone="muted">{`align: ${align}`}</Text> <Grid align={align} columns={3} gap={4} minItemWidth="xs"> {SHIPMENTS.map((shipment) => ( <Card key={shipment}> <CardBody> <Text>{shipment}</Text> </CardBody> </Card> ))} </Grid> </Stack> ))} </Stack> );}Justification along the row
justify positions each child along the inline axis inside its own cell. With stretch a child fills its cell. start, center and end size the child to its content and place it at the start, middle or end of the cell.
import { Card, CardBody } from '@slotted/react/card';import { Grid } from '@slotted/react/grid';import { Stack } from '@slotted/react/stack';import { Text } from '@slotted/react/typography';const JUSTIFIES = ['start', 'center', 'end', 'stretch'] as const;const ACTIONS = ['Ship', 'Hold for review', 'Return to supplier'] as const;export default function GridJustify() { return ( <Stack gap={6}> {JUSTIFIES.map((justify) => ( <Stack key={justify} gap={3}> <Text size="sm" tone="muted">{`justify: ${justify}`}</Text> <Grid columns={3} gap={4} justify={justify} minItemWidth="xs"> {ACTIONS.map((action) => ( <Card key={action}> <CardBody> <Text>{action}</Text> </CardBody> </Card> ))} </Grid> </Stack> ))} </Stack> );}Spanning every column
Grid has no prop that places a child. A child that crosses the row states it on itself with grid-column: 1 / -1, through style: it spans the columns the row holds at that width, and stays one cell when the grid is one column. span 2 would add a second column to a grid that had reflowed to one. In this form the company field spans both columns, and the other four fields fill the two rows below it.
import { Field, FieldLabel } from '@slotted/react/field';import { Grid } from '@slotted/react/grid';import { TextField } from '@slotted/react/text-field';export default function GridSpan() { return ( <Grid columns={2} gap={6} minItemWidth="sm"> <Field style={{ gridColumn: '1 / -1' }}> <FieldLabel>Company</FieldLabel> <TextField defaultValue="Northwind Traders" /> </Field> <Field> <FieldLabel>Contact</FieldLabel> <TextField defaultValue="Priya Raman" /> </Field> <Field> <FieldLabel>Phone</FieldLabel> <TextField defaultValue="+31 10 555 0142" /> </Field> <Field> <FieldLabel>City</FieldLabel> <TextField defaultValue="Rotterdam" /> </Field> <Field> <FieldLabel>Postal code</FieldLabel> <TextField defaultValue="3011 AB" /> </Field> </Grid> );}Reflow follows the grid
The grid measures its own width, not the window, so no breakpoint is involved. The same grid with columns={4} and minItemWidth="xs" holds four columns across the preview and fewer inside a card that is half as wide.
Zone A
Zone B
import { Badge } from '@slotted/react/badge';import { Card, CardBody } from '@slotted/react/card';import { Grid } from '@slotted/react/grid';import { Stack } from '@slotted/react/stack';import { Heading, Text } from '@slotted/react/typography';const BAYS = [ { name: 'Bay 1', status: 'Full', tone: 'success' }, { name: 'Bay 2', status: 'Half', tone: 'info' }, { name: 'Bay 3', status: 'Low', tone: 'warning' }, { name: 'Bay 4', status: 'Empty', tone: 'danger' },] as const;const ZONES = ['Zone A', 'Zone B'] as const;export default function GridOwnWidth() { return ( <Stack gap={6}> <Stack gap={3}> <Text size="sm" tone="muted"> Across the whole preview </Text> <Grid columns={4} gap={4} justify="start" minItemWidth="xs"> {BAYS.map((bay) => ( <Badge key={bay.name} tone={bay.tone}>{`${bay.name}: ${bay.status}`}</Badge> ))} </Grid> </Stack> <Stack gap={3}> <Text size="sm" tone="muted"> The same grid inside two half-width cards </Text> <Grid columns={2} gap={6}> {ZONES.map((zone) => ( <Card key={zone}> <CardBody> <Stack gap={3}> <Heading level={2} size="md"> {zone} </Heading> <Grid columns={4} gap={4} justify="start" minItemWidth="xs"> {BAYS.map((bay) => ( <Badge key={bay.name} tone={bay.tone}>{`${bay.name}: ${bay.status}`}</Badge> ))} </Grid> </Stack> </CardBody> </Card> ))} </Grid> </Stack> </Stack> );}Accessibility
- This is not the APG grid widget. There is no role="grid", no cell and no arrow-key navigation: Grid lays children out, and the children keep whatever semantics they already had.
- There is no prop that reorders or places children. Visual order stays DOM order, which is what WCAG 2.2 SC 1.3.2 asks for (technique C27, failure F1) and what keeps the Tab order matching the page.
- A stated count is a ceiling the grid may lower. Twelve columns become six and then one as the grid narrows, so a page reflows to 320 CSS pixels without scrolling in two dimensions, as WCAG 1.4.10 requires.
- The reflow is measured against the grid’s own inline size, not the viewport, so a grid inside a narrow panel behaves the same as one inside a narrow window. Grid uses no breakpoint and no container query.
- minItemWidth is stated in rem, so the point at which a column is dropped moves with the browser zoom WCAG 1.4.4 requires.
API
grid
Props
gap0 | 1 | … | 12Default6- Sets the separation between cells as a step on the space scale.
columns1 | 2 | … | 12Defaultauto- Sets the most columns a row may hold. When absent, the grid fits as many as it can.
minItemWidth'xs' | 'sm' | 'md' | 'lg' | 'xl'Defaultmd- Sets the narrowest a cell may become before a column is dropped.
align'start' | 'center' | 'end' | 'stretch'- Positions a child along the block axis, inside its own cell.
justify'start' | 'center' | 'end' | 'stretch'- Positions a child along the inline axis, inside its own cell.
render(props) => ReactNode- Renders a different root element from the props it receives.
Theming
Each token is a CSS custom property: set it in a theme, or on any element to retune every grid inside it.
Grid tokens
| Token | Default |
|---|---|
--slotted-grid-columns | 1 |
--slotted-grid-min-item-width-lg | 20rem |
--slotted-grid-min-item-width-md | 16rem |
--slotted-grid-min-item-width-sm | 12rem |
--slotted-grid-min-item-width-xl | 24rem |
--slotted-grid-min-item-width-xs | 8rem |
Theme tokens it reads
| Token | Default |
|---|---|
--slotted-space-1 | 0.125rem |
--slotted-space-10 | 2.5rem |
--slotted-space-11 | 3rem |
--slotted-space-12 | 4rem |
--slotted-space-2 | 0.25rem |
--slotted-space-3 | 0.375rem |
--slotted-space-4 | 0.5rem |
--slotted-space-5 | 0.75rem |
--slotted-space-6 | 1rem |
--slotted-space-7 | 1.25rem |
--slotted-space-8 | 1.5rem |
--slotted-space-9 | 2rem |