# Grid list

A list to move through with arrow keys and select from: rows with a visual, a title and status (`layout="stack"`), or tiles in a responsive grid with actions and drag to reorder (`layout="grid"`).

- Family: collection
- Import: `import { GridList, GridListItem, GridListSection } from "@senvenli/react/collection";`
- Page: https://design.senvenli.com/docs/components/grid-list
- JSON: https://design.senvenli.com/docs/components/grid-list.json

## Example: Rows

The default `stack` layout: a title, a line of context, a visual at the start and a status at the end.

```tsx
import { GridList, GridListItem } from "@senvenli/react/collection";
import { Avatar, Badge } from "@senvenli/react/display";

export default function Example() {
  return (
    <GridList aria-label="Customers">
      <GridListItem id="sofia" leading={<Avatar name="Sofia Marin" src="/avatars/05.webp" />} title="Sofia Marin" description="42 orders · Boston" trailing={<Badge tone="success">Active</Badge>} />
      <GridListItem id="claire" leading={<Avatar name="Claire Dubois" src="/avatars/07.webp" />} title="Claire Dubois" description="18 orders · Chicago" trailing={<Badge tone="success">Active</Badge>} />
      <GridListItem id="jamal" leading={<Avatar name="Jamal Carter" src="/avatars/08.webp" />} title="Jamal Carter" description="64 orders · Denver" trailing={<Badge tone="warning">Payment pending</Badge>} />
      <GridListItem id="lena" leading={<Avatar name="Lena Fischer" src="/avatars/02.webp" />} title="Lena Fischer" description="No orders yet · Portland" trailing={<Badge>Invited</Badge>} />
    </GridList>
  );
}
```

## Examples

### Rows in sections

Rows from data, grouped under small headers.

```tsx
import { GridList } from "@senvenli/react/collection";
import { Icon, Badge } from "@senvenli/react/display";

type Delivery = { id: string; customer: string; detail: string; isDelivered?: boolean };

const DELIVERIES: { title: string; items: Delivery[] }[] = [
  {
    title: "Today",
    items: [
      { id: "1042", customer: "Ada Lovelace", detail: "Order SO-1042 · 9:00 AM · Boston", isDelivered: true },
      { id: "1043", customer: "Grace Hopper", detail: "Order SO-1043 · 11:30 AM · Cambridge" },
      { id: "1044", customer: "Alan Turing", detail: "Order SO-1044 · 2:00 PM · Somerville" },
    ],
  },
  {
    title: "Tomorrow",
    items: [
      { id: "1045", customer: "Katherine Johnson", detail: "Order SO-1045 · 10:00 AM · Boston" },
      { id: "1046", customer: "Margaret Hamilton", detail: "Order SO-1046 · 3:30 PM · Quincy" },
    ],
  },
];

export default function Example() {
  return (
    <GridList
      aria-label="Deliveries"
      sections={DELIVERIES}
      renderItem={(delivery) => ({
        id: delivery.id,
        leading: <Icon name="package" color="subtle" />,
        title: delivery.customer,
        description: delivery.detail,
        trailing: delivery.isDelivered ? <Badge tone="success">Delivered</Badge> : <Badge tone="information">Scheduled</Badge>,
      })}
    />
  );
}
```

### Picking a set

A checkbox per row, for acting on several at once.

```tsx
import { useState } from "react";
import { GridList, type GridListStackProps } from "@senvenli/react/collection";
import { Text } from "@senvenli/react/display";
import { Stack } from "@senvenli/react/layout";

const INVOICES = [
  { id: "inv-301", customer: "Ada Lovelace", detail: "$248.50 · due March 25" },
  { id: "inv-302", customer: "Grace Hopper", detail: "$96.00 · due March 27" },
  { id: "inv-303", customer: "Alan Turing", detail: "$1,290.00 · due April 2" },
];

type Keys = NonNullable<GridListStackProps["selectedKeys"]>;

export default function Example() {
  const [selected, setSelected] = useState<Keys>(new Set(["inv-301"]));
  const count = selected === "all" ? INVOICES.length : [...selected].length;
  return (
    <Stack space="space-150">
      <GridList
        aria-label="Unpaid invoices"
        items={INVOICES}
        selectionMode="multiple"
        selectedKeys={selected}
        onSelectionChange={setSelected}
        renderItem={(invoice) => ({ id: invoice.id, title: invoice.customer, description: invoice.detail })}
      />
      <Text size="body-small" tone="subtle">{count} selected for a reminder</Text>
    </Stack>
  );
}
```

### One open row

`replace` selection with no checkboxes, for a list beside the record it opens.

```tsx
import { GridList, GridListItem } from "@senvenli/react/collection";
import { Avatar } from "@senvenli/react/display";

export default function Example() {
  // The arrow keys move the selection with the focus, as in an inbox.
  return (
    <GridList aria-label="Conversations" selectionMode="single" selectionBehavior="replace" defaultSelectedKeys={["ada"]}>
      <GridListItem id="sofia" leading={<Avatar name="Sofia Marin" src="/avatars/05.webp" size="small" />} title="Sofia Marin" description="Where is my order?" />
      <GridListItem id="claire" leading={<Avatar name="Claire Dubois" src="/avatars/07.webp" size="small" />} title="Claire Dubois" description="Thanks, that fixed it." />
      <GridListItem id="jamal" leading={<Avatar name="Jamal Carter" src="/avatars/08.webp" size="small" />} title="Jamal Carter" description="Can I change the delivery address?" />
    </GridList>
  );
}
```

### Tiles: product photos

`layout="grid"`: tiles people know by their picture, picked as a set.

```tsx
import { GridList } from "@senvenli/react/collection";

// A stand-in picture as an inline SVG data URI, so the example loads nothing from the network.
const picture = (shape: string) =>
  `data:image/svg+xml,${encodeURIComponent(`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 160 120"><rect width="160" height="120" fill="gray" fill-opacity=".2"/>${shape}</svg>`)}`;

const PHOTOS = [
  { id: "front", title: "Front view", size: "1.9 MB", src: picture('<rect x="50" y="30" width="60" height="60" rx="6" fill="gray"/>') },
  { id: "side", title: "Side view", size: "2.3 MB", src: picture('<rect x="65" y="25" width="30" height="70" rx="6" fill="gray"/>') },
  { id: "top", title: "From above", size: "1.4 MB", src: picture('<circle cx="80" cy="60" r="32" fill="gray"/>') },
  { id: "detail", title: "Detail", size: "3.1 MB", src: picture('<path d="M40 90 80 30l40 60z" fill="gray"/>') },
];

export default function Example() {
  return (
    <GridList layout="grid"
      aria-label="Product photos"
      items={PHOTOS}
      selectionMode="multiple"
      defaultSelectedKeys={["front"]}
      // The title names the tile, so the image itself needs no alt text.
      renderItem={(photo) => ({ title: photo.title, meta: photo.size, media: <img src={photo.src} alt="" /> })}
    />
  );
}
```

### Reordering

Drag a tile or use its handle from the keyboard; the tile's own buttons do the same on touch.

```tsx
import { useState } from "react";
import { GridList, type GridListGridProps } from "@senvenli/react/collection";

const picture = (shape: string) =>
  `data:image/svg+xml,${encodeURIComponent(`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 160 120"><rect width="160" height="120" fill="gray" fill-opacity=".2"/>${shape}</svg>`)}`;

type Photo = { id: string; title: string; src: string };
const PHOTOS: Photo[] = [
  { id: "front", title: "Front view", src: picture('<rect x="50" y="30" width="60" height="60" rx="6" fill="gray"/>') },
  { id: "side", title: "Side view", src: picture('<rect x="65" y="25" width="30" height="70" rx="6" fill="gray"/>') },
  { id: "top", title: "From above", src: picture('<circle cx="80" cy="60" r="32" fill="gray"/>') },
  { id: "detail", title: "Detail", src: picture('<path d="M40 90 80 30l40 60z" fill="gray"/>') },
];

type Reorder = NonNullable<GridListGridProps<Photo>["dragAndDrop"]>["onReorder"];

export default function Example() {
  const [photos, setPhotos] = useState(PHOTOS);
  // Takes the moved tiles out, then puts them back before or after the target.
  const reorder: Reorder = (keys, target) =>
    setPhotos((current) => {
      const moved = current.filter((photo) => keys.has(photo.id));
      const rest = current.filter((photo) => !keys.has(photo.id));
      const at = rest.findIndex((photo) => photo.id === target.key) + (target.position === "after" ? 1 : 0);
      return [...rest.slice(0, at), ...moved, ...rest.slice(at)];
    });
  return (
    <GridList layout="grid"
      aria-label="Product photos, in display order"
      items={photos}
      dragAndDrop={{ onReorder: reorder }}
      renderItem={(photo) => {
        const index = photos.indexOf(photo);
        return {
          title: photo.title,
          meta: index === 0 ? "Cover photo" : `Photo ${index + 1}`,
          media: <img src={photo.src} alt="" />,
          actions: [
            {
              id: "earlier",
              label: `Move earlier: ${photo.title}`,
              icon: "arrow-left",
              isDisabled: index === 0,
              onPress: () => reorder(new Set([photo.id]), { key: photos[index - 1]!.id, position: "before" }),
            },
            {
              id: "later",
              label: `Move later: ${photo.title}`,
              icon: "arrow-right",
              isDisabled: index === photos.length - 1,
              onPress: () => reorder(new Set([photo.id]), { key: photos[index + 1]!.id, position: "after" }),
            },
          ],
        };
      }}
    />
  );
}
```

### Tiles that open

An icon as the media, and a press or Enter that opens the tile.

```tsx
import { useState } from "react";
import { GridList } from "@senvenli/react/collection";
import { Icon, Text } from "@senvenli/react/display";
import { Stack } from "@senvenli/react/layout";

const DOCUMENTS = [
  { id: "invoice", title: "Invoice 2026-0318", meta: "PDF · 84 KB", icon: "file-text" },
  { id: "report", title: "Q1 sales report", meta: "Spreadsheet · 1.2 MB", icon: "file-spreadsheet" },
  { id: "contract", title: "Supplier contract", meta: "PDF · 310 KB", icon: "file-signature" },
] as const;

export default function Example() {
  const [opened, setOpened] = useState<string | null>(null);
  return (
    <Stack space="space-150">
      <GridList layout="grid"
        aria-label="Documents"
        items={DOCUMENTS}
        onAction={(key) => setOpened(DOCUMENTS.find((document) => document.id === key)?.title ?? null)}
        renderItem={(document) => ({ title: document.title, meta: document.meta, media: <Icon name={document.icon} size="large" /> })}
      />
      <Text size="body-small" tone="subtle">{opened ? `Opened: ${opened}` : "Press a tile, or focus it and press Enter."}</Text>
    </Stack>
  );
}
```

## Guidelines

### When to use it

- Records read as one line each, and tables on narrow screens (`layout="stack"`).
- Items people recognize by an image: photos, documents, cards (`layout="grid"`).

### Use something else when

- [table](https://design.senvenli.com/docs/components/table.md): People compare several fields across records.
- [list-box](https://design.senvenli.com/docs/components/list-box.md): A plain choice of options in a form, not records to open.

## Accessibility

Keyboard:

- Arrow keys: Move between rows, or between tiles in two dimensions.
- Enter: Opens the focused row or tile.
- Space: Selects the focused item.

Built in:

- Sections are named for screen readers.
- Tile actions are always visible, so they need no hover and work on touch.
- A drag handle gives the keyboard a way to reorder tiles.

You provide:

- An aria-label.
- A name for each tile action that names the tile.

## API reference

### GridList

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| aria-label | `string` |  | yes | Names the list ("Today's deliveries"). |
| children | `React.ReactNode` |  | no | GridListItem rows, or ListSections of them; or give `items` (or `sections`) and `renderItem`. |
| items | `readonly T[]` |  | no |  |
| sections | `readonly GridListSectionData<T>[]` |  | no | Grouped rows under small headers ("Today", "Tomorrow"). |
| renderItem | `(item: T) => GridListItemProps \| (item: T) => GridTileContent` |  | no | One row's content for `items` / `sections`. |
| selectionMode | `"none" \| "single" \| "multiple"` |  | no |  |
| selectionBehavior | `"toggle" \| "replace"` |  | no | How a press selects. `toggle` (default): each row has a checkbox and a press adds or removes it, for picking a set to act on. `replace`: no checkboxes; a press selects that row in place of the last, and with `single` the arrow keys move the selection, for a list that shows one open record beside it (an inbox's conversation). The selected row keeps its marker either way. With `onAction` as well, a press runs the action and selection needs the keyboard (React Aria's replace rule), so a replace list usually leaves `onAction` out. |
| selectedKeys | `"all" \| Iterable<Key>` |  | no |  |
| defaultSelectedKeys | `"all" \| Iterable<Key>` |  | no |  |
| onSelectionChange | `(keys: Selection) => void` |  | no |  |
| onAction | `(key: Key) => void` |  | no | Opens the row's record. |
| emptyState | `React.ReactNode` |  | no | Shown when there are no rows: an `EmptyState` or a sentence. |
| layout | `"stack" \| "grid"` |  | no |  |
| itemKey | `(item: T) => Key` |  | no | The item's stable id; `item.id` by default. |
| dragAndDrop | `{ onReorder: (keys: Set<Key>, target: { key: Key; position: "before" \| "after" }) => void }` |  | no | Reordering (photos): drag a tile, or focus its handle and use the keyboard. `onReorder` gets the moved keys and the tile they go before or after. |
| ref | `React.Ref<HTMLDivElement>` |  | no | The grid element. Focusing it focuses the tile React Aria keeps as focused (the next one after a removed tile), so it is the `returnFocusRef` for a confirm whose action removes the tile it was opened from. |

### GridListItem

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| id | `Key` |  | yes |  |
| leading | `React.ReactNode` |  | no | A visual at the start: an Avatar, an icon or a thumbnail. |
| title | `string` |  | yes | The one primary line ("Ada Lovelace"); also the row's text for typeahead. |
| description | `React.ReactNode` |  | no | The context the reader scans for: item, time, place ("Order 1042 · 10:00 · Berlin"). |
| trailing | `React.ReactNode` |  | no | At the end: a Badge for status, then a subtle icon-button menu. |
| href | `string` |  | no | Makes the row a link to the record. |

### GridListSection

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| title | `string` |  | yes |  |
| children | `React.ReactNode` |  | yes |  |

### Tokens it reads

`--ds-background-danger`, `--ds-background-selected`, `--ds-border`, `--ds-border-focused`, `--ds-border-selected`, `--ds-icon-danger`, `--ds-radius-control-small`, `--ds-radius-large`, `--ds-radius-small`, `--ds-shadow-raised`, `--ds-space-025`, `--ds-space-050`, `--ds-space-075`, `--ds-space-100`, `--ds-space-150`, `--ds-space-200`, `--ds-space-300`, `--ds-space-500`, `--ds-surface-hovered`, `--ds-surface-raised`, `--ds-surface-sunken`, `--ds-text`, `--ds-text-disabled`, `--ds-text-subtle`

## Related

- [table](https://design.senvenli.com/docs/components/table.md)
- [list-box](https://design.senvenli.com/docs/components/list-box.md)
