Open a detail panel
Show the active row in a side panel that opens on a click or a key and follows the active cell.
Show the active row in a side panel that opens on a click or a key and follows the active cell. The panel does not block the grid. Focus stays in the grid, so the arrow keys keep moving the active cell. Click a cell, or press the open key on the active cell:
Click a cell, or press Shift+Enter on the active cell, to show its row. The panel follows the active cell while you move with the arrow keys. Escape closes it when focus is inside the panel.
Before you start
Give every row a stable id. If rows have no id field, pass getRowId. A numeric id becomes a string row id. If the task must stop until the user answers, use a dialog instead. A dialog traps focus and blocks the grid.
Steps
- Hold the panel state next to the grid: an
openflag and therowIdof the active row. - Render the panel as an
asidebeside the grid, withrole="complementary"and anaria-label. Whileopenis false, setinerton it. The panel then leaves the tab order and the accessibility tree. - Open the panel from
onCellClick. Comparectx.columnIdto skip a checkbox column. - Set
rowIdfromonActiveCellChange. With no active cell, the event passesnull. - Look the row up by id in your own data. The event carries the cell reference and not the row.
- Define an
openRowcommand incommandswith itskeys. - Close the panel on Escape inside the
aside, then callfocus()on the grid API from a ref. It focuses the active cell and does nothing during an edit.
Wire the panel, the click, and the key
Enter, F2, Space, and a double-click edit the cell, so the open gesture needs its own key. The default keymap binds mod+Enter to scrollActiveIntoView and mod+shift+Enter to toggleAllRows, so the sample binds shift+Enter.
onCellClick fires for a click on any data cell, and a checkbox column fires it too. The sample skips the active column. The data-[open=false] classes slide the closed panel out of view:
"use client";
import { useMemo, useRef, useState } from "react";
import { Button } from "@/components/ui/button";
import {
DataGrid,
defineColumns,
type DataGridApi,
type GridCommand,
} from "@/components/data-grid/data-grid";
type Order = { id: string; customer: string; active: boolean };
const columns = defineColumns<Order>()([
{ id: "customer", header: "Customer", accessorKey: "customer", type: "text" },
{ id: "active", header: "Active", accessorKey: "active", type: "checkbox" },
] as const);
export function OrdersWithPanel({ orders }: { orders: Order[] }) {
const [open, setOpen] = useState(false);
const [rowId, setRowId] = useState<string | null>(null);
const gridRef = useRef<DataGridApi<Order> | null>(null);
const [rows, setRows] = useState<readonly Order[]>(orders);
const order = rows.find((item) => item.id === rowId);
const close = () => {
setOpen(false);
gridRef.current?.focus();
};
const commands = useMemo<Record<string, GridCommand<Order>>>(
() => ({
openRow: {
label: "Open row",
keys: ["shift+Enter"],
when: (ctx) => ctx.row !== undefined,
run: () => setOpen(true),
},
}),
[],
);
return (
<div className="relative overflow-hidden">
<DataGrid
ref={gridRef}
className="h-[400px]"
data={rows}
onDataChange={setRows}
columns={columns}
commands={commands}
onCellClick={(ctx) => {
if (ctx.columnId !== "active") setOpen(true);
}}
onActiveCellChange={(next) => setRowId(next?.rowId ?? null)}
/>
<aside
role="complementary"
aria-label="Row details"
inert={!open}
data-open={open}
onKeyDown={(event) => {
if (event.key === "Escape") close();
}}
className={[
"absolute inset-y-0 end-0 w-64 bg-background p-4",
"transition-transform data-[open=false]:translate-x-full",
"rtl:data-[open=false]:-translate-x-full",
].join(" ")}
>
<Button
type="button"
variant="ghost"
aria-label="Close details"
onClick={close}
>
×
</Button>
<p>{order?.customer ?? "No row"}</p>
</aside>
</div>
);
}Result
A click on a cell, or Shift+Enter on the active cell, opens the panel. The panel shows the active row and changes as you move with the arrow keys. Escape inside the panel, or the close button, closes it and returns focus to the grid.
Related
- Custom commands: how
commands,keys, andkeymapfit together. - Selection and cell events: the
onCellClickandonActiveCellChangecallbacks. - Edit a row with a form: put a form in the panel and save it as one undoable change.