gridcn2.0.0
Learning path

Style the grid

Step 8 of 8

Change row height, style rows and columns by value, and let your shadcn theme tokens restyle the grid.

Before you start, sort and filter rows.

The grid paints with shadcn tokens. Its root uses bg-background, border-border, and rounded-md. So a change to your theme tokens recolors the grid in light and dark mode. This step uses props for the rest.

Set density and classes

Replace people-grid.tsx with this file. It keeps the toolbar, the header sort, and the undo of the earlier steps. It also sets a taller row, dims inactive rows, and adds a shadow:

people-grid.tsx
"use client";

import {
  DataGridBody,
  DataGridHeader,
  DataGridProvider,
  DataGridRoot,
  type GetRowClassName,
} from "@/components/data-grid/data-grid";
import {
  DataGridColumnsMenu,
  DataGridFilterMenu,
  DataGridSearch,
  DataGridToolbar,
} from "@/components/data-grid-toolbar/data-grid-toolbar";
import {
  useDataGridState,
} from "@/components/data-grid-history/data-grid-history";
import { columns, type Person } from "./columns";

const people: Person[] = [
  { id: "1", name: "Ada Lovelace", age: 28, active: true },
  { id: "2", name: "Grace Hopper", age: 34, active: true },
  { id: "3", name: "Margaret Hamilton", age: 31, active: false },
];

const dimInactive: GetRowClassName<Person> = (person) =>
  person.active ? undefined : "text-muted-foreground";

export function PeopleGrid() {
  const grid = useDataGridState(people);

  return (
    <DataGridProvider
      {...grid}
      columns={columns}
      headerClickBehavior="sort"
    >
      <DataGridToolbar>
        <DataGridSearch />
        <DataGridFilterMenu />
        <DataGridColumnsMenu />
      </DataGridToolbar>
      <DataGridRoot
        aria-label="Players"
        className="h-96 shadow-sm"
        density="comfortable"
        getRowClassName={dimInactive}
      >
        <DataGridHeader />
        <DataGridBody />
      </DataGridRoot>
    </DataGridProvider>
  );
}

Result: rows are 44 px high, Margaret Hamilton shows in the muted text color, and the grid has a shadow.

  • density picks a row height preset, and comfortable gives rows 44 px. See Density and small screens for the other values and rowHeight.
  • className merges after the built-in classes of the root, so your utility wins a conflict.
  • getRowClassName takes a row and its view index, the position of the row in the sorted and filtered list. It can be an inline function. Rows re-render only when it returns different classes for the rendered rows.
  • cellClassName: "font-medium" on a column styles every cell of that column. headerClassName styles its header cell.

Where next:

On this page