gridcn2.0.0
Style examples

Styling example

Style a whole column, style rows by their data, and change the row height with density, all with class names on shadcn tokens.

Class hooks style the grid by value, and every class name can use your shadcn tokens, so a theme change carries over. This demo styles a column, styles at-risk rows, and changes the density of the rows.

Patterns

Account
Region
Owner
Amount
Stage
Northwind Traders
AMER
Priya Nair
84000
Committed
Fabrikam Group
EMEA
Marcus Cole
41500
At risk
Contoso Retail
AMER
Dana Whitfield
120000
Committed
Tailspin Toys
APAC
Ilya Petrov
27800
Prospecting
Wide World Importers
EMEA
Grace Owusu
63200
At risk
Adatum Corp
APAC
Tom Bracewell
95400
Committed
Lucerne Publishing
AMER
Yuki Tanaka
18900
Prospecting
Proseware Inc
EMEA
Leah Fischer
52700
At risk
pnpm dlx shadcn@latest add @gridcn/data-grid-styling-patterns-demo
  • The Region column carries cellClassName and headerClassName. Every cell and the header of the column get the same classes.
  • A row with the stage "at-risk" gets the classes of getRowClassName.
  • The select changes density.

This grid does the same:

import {
  DataGrid,
  defineColumns,
  type GetRowClassName,
} from "@/components/data-grid/data-grid";

type Deal = { id: string; region: string; stage: string };

const getRowClassName: GetRowClassName<Deal> = (row) =>
  row.stage === "at-risk"
    ? "border-s-2 border-s-destructive bg-destructive/5 text-destructive"
    : undefined;

const columns = defineColumns<Deal>()([
  {
    id: "region",
    header: "Region",
    accessorKey: "region",
    cellClassName: "bg-primary/5 border-e border-e-primary/30 font-medium",
    headerClassName: "bg-primary/10 border-e border-e-primary/30 text-primary",
  },
  { id: "stage", header: "Stage", accessorKey: "stage" },
]);

export function Deals({ rows }: { rows: Deal[] }) {
  return (
    <DataGrid
      className="h-80"
      defaultData={rows}
      columns={columns}
      getRowId={(row) => row.id}
      getRowClassName={getRowClassName}
      density="compact"
    />
  );
}

Row height

density is "compact", "default", or "comfortable", which set the row height to 28, 36, or 44 pixels. If you also pass rowHeight, rowHeight wins. Neither changes the header height.

getRowClassName can be an inline function. Rows re-render only when the function returns different classes for the rendered rows. The grid merges the class names after its built-in classes, so a conflicting utility wins.

On this page