gridcn2.0.0
Recipes and custom code

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.

Name
Email
Role
Active
Silent Tiger
user0@example.com
Viewer
Quick Lion
user1@example.com
Viewer
Swift Wolf
user2@example.com
Manager
Eager Lion
user3@example.com
User
Eager Tiger
user4@example.com
Viewer
Silent Wolf
user5@example.com
User
Swift Lion
user6@example.com
Viewer
Eager Lion
user7@example.com
Manager
Eager Eagle
user8@example.com
Editor
Bold Tiger
user9@example.com
Editor
Bold Bear
user10@example.com
User
Swift Wolf
user11@example.com
Manager
Swift Bear
user12@example.com
Viewer
Eager Wolf
user13@example.com
Editor
Swift Eagle
user14@example.com
Viewer
Quick Lion
user15@example.com
Manager
Bold Wolf
user16@example.com
Editor
Swift Eagle
user17@example.com
Admin
Eager Eagle
user18@example.com
Manager
Eager Bear
user19@example.com
Editor
Silent Lion
user20@example.com
Manager
Silent Tiger
user21@example.com
Manager
Eager Tiger
user22@example.com
Manager
Silent Lion
user23@example.com
Admin
Bold Eagle
user24@example.com
Admin
Bold Lion
user25@example.com
Viewer
Quick Tiger
user26@example.com
Viewer
Bold Eagle
user27@example.com
Editor
Swift Tiger
user28@example.com
Editor
Silent Lion
user29@example.com
Viewer

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

  1. Hold the panel state next to the grid: an open flag and the rowId of the active row.
  2. Render the panel as an aside beside the grid, with role="complementary" and an aria-label. While open is false, set inert on it. The panel then leaves the tab order and the accessibility tree.
  3. Open the panel from onCellClick. Compare ctx.columnId to skip a checkbox column.
  4. Set rowId from onActiveCellChange. With no active cell, the event passes null.
  5. Look the row up by id in your own data. The event carries the cell reference and not the row.
  6. Define an openRow command in commands with its keys.
  7. Close the panel on Escape inside the aside, then call focus() 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.

On this page