Documentation
Guides

Nested grids

Put a grid inside a cell of another grid. Each keeps its own active cell, keys and focus, and Tab moves between them.

A cell can render anything, including another DataGrid.Root. The two grids stay independent: each has its own model and engine, and each looks only at its own cells. The same holds for a grid in a row's detail: to open a grid below a row rather than inside one of its cells, see Master-detail, whose example nests a grid of each order's items.

Giving the cell room

The outer grid sizes its rows, so make the rows that hold a grid tall enough (rowHeight, a number or a function of the index), and give the inner root a size, as any grid needs.

orders.tsx
const columns: Column<Order>[] = [
    { key: "id", name: "Order", width: 90 },
    { key: "items", name: "Items", width: 320, renderCell: ({ row }) => <Items order={row} /> },
];

<DataGrid.Root columns={columns} rows={orders} rowHeight={124}>…</DataGrid.Root>

What each grid owns

  • Keys. A key pressed in the inner grid is the inner grid's. The outer grid never moves for it, even when the inner grid leaves it unhandled.
  • The active cell. Clicking a cell of the inner grid makes it active there, and makes the outer cell that holds the inner grid active in the outer grid.
  • Focus. Each grid restores focus to its own cells only, even when both have a cell at the same row and column.

Tab

A grid on its own, with navigation() installed (on each grid, the outer and the inner), is one tab stop: its active cell, or the grid itself before one is active. A grid nested in another one is reachable through the cell that holds it: its tab stop is in the page's tab order only while the outer grid's active cell holds it (for a grid in a row's detail, while the active cell is in that row), or while focus is inside it. So Tab moves from the active outer cell into its inner grid, Shift+Tab moves back, and Tab from any other outer cell leaves the outer grid without visiting the grids nested in it.

items.tsx
function Items({ order }: { order: Order }) {
    // nothing to do: the inner grid is reachable while the outer cell holding it is active
    return (
        <DataGrid.Root columns={itemColumns} rows={order.items}>
            <DataGrid.Grid aria-label={`Items of order ${order.id}`}>…</DataGrid.Grid>
        </DataGrid.Root>
    );
}

To give a nested grid a tab stop of its own, as before, pass ownTabStop to its root. A tabIndex you pass to a part still overrides the grid's own.

The wheel

A wheel over a nested grid scrolls it while it can scroll that way. At its end the browser passes the wheel on to the outer grid; when the outer grid's scroll is scaled, the outer grid takes that wheel itself and moves by exactly its delta, as it does over its own cells, instead of the browser's native scroll, which would jump far. The same holds for anything that scrolls inside a cell or a detail (a notes panel, a text area), and each axis is decided apart: a diagonal wheel over a panel that scrolls only sideways scrolls the panel sideways and the outer grid down. Content with overscroll-behavior: contain keeps the wheel at its end too.