gridcn2.0.0
Theme, style, and language

Style overrides

Restyle grid parts with plain CSS, style rows and cells by value with class callbacks, and style by state with data attributes.

The grid paints with shadcn tokens, so your theme restyles it. When a token is not enough, override the classes of one part. Plain CSS changes the built-in parts. Class callbacks and column fields style rows and cells by value. Data attributes style by state.

Name
Dept.
Salary
Perf.
Status
Priya Nair
Engineering
128000
94
Active
Marcus Cole
Sales
76000
41
Active
Dana Whitfield
Support
58000
88
Active
Ilya Petrov
Engineering
141000
96
Active
Grace Owusu
Marketing
69000
47
On leave
Tom Bracewell
Sales
71000
33
Terminated
Yuki Tanaka
Engineering
118000
91
Active
Leah Fischer
Support
54000
62
Active
Omar Haddad
Marketing
73000
78
Active
Sophie Renard
Engineering
135000
45
On leave

Override with CSS

The built-in classes are Tailwind utilities in the utilities cascade layer. A CSS rule that you write outside any @layer beats every layered rule, whatever the specificity or the order. So one selector overrides a built-in class with no !important:

/* selection range: another fill and border */
[data-grid-selection-overlay] {
  background-color: color-mix(in oklch, var(--color-accent) 20%, transparent);
  border-color: var(--color-accent);
}

/* active cell: a dashed outline instead of the ring */
[data-grid-active-cell-overlay] {
  box-shadow: none;
  outline: 2px dashed var(--color-ring);
  outline-offset: -2px;
}

/* invalid cell: a rail instead of the inset ring, the tint stays */
[role="grid"] [role="gridcell"][data-invalid] {
  box-shadow: none;
  border-inline-start: 2px solid var(--color-destructive);
}

/* search match: an underline instead of the background tint */
[role="grid"] [role="gridcell"][data-search-match] {
  background-color: transparent;
  text-decoration: underline solid var(--search-highlight, currentColor) 2px;
}

Style rows and cells by value

Two props and two column fields add classes by value:

  • getRowClassName(row, viewRowIndex) on DataGrid or DataGridRoot styles a row.
  • getCellClassName(ctx) on the same components styles a cell.
  • cellClassName on a ColumnDef styles every cell of the column. It takes a string, or a function with the same ctx.
  • headerClassName on a ColumnDef styles the header cell.

ctx holds value, row, rowId, columnId, viewRowIndex, and column. All four merge through cn() after the built-in classes, so a conflicting utility wins. The column classes apply last, so they win over a grid-wide default. The callbacks skip rows that are not loaded yet.

This grid fades archived rows and colors negative balances:

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

type Row = { id: string; status: string; balance: number };

const getRowClassName: GetRowClassName<Row> = (row) =>
  row.status === "archived" ? "opacity-50" : undefined;

const getCellClassName: GetCellClassName<Row> = ({ value, columnId }) =>
  columnId === "balance" && typeof value === "number" && value < 0
    ? "text-destructive"
    : undefined;

const columns = defineColumns<Row>()([
  {
    id: "status",
    header: "Status",
    accessorKey: "status",
    headerClassName: "text-primary",
  },
  { id: "balance", header: "Balance", accessorKey: "balance", type: "number" },
]);

export function Accounts({ rows }: { rows: Row[] }) {
  return (
    <DataGrid
      className="h-80"
      defaultData={rows}
      columns={columns}
      getRowClassName={getRowClassName}
      getCellClassName={getCellClassName}
    />
  );
}

Result: archived rows fade, and negative balances turn red.

A select cell holds the value of its choice, not the label. To color a status column, compare value with the choice value. A date cell holds an ISO yyyy-mm-dd string, and ISO dates compare correctly as text. So typeof value === "string" && value < today marks an overdue date, where today is the current date in the same format. Compute today once outside the callback, from local date parts. toISOString() returns the UTC date, which differs from the local date near midnight.

getRowClassName and getCellClassName can be inline functions. Rows and cells re-render only when the function returns different classes for the rendered rows.

Style a whole column

When a rule depends on the column only, use the string form of cellClassName. Add headerClassName to style the header with it:

import { defineColumns } from "@/components/data-grid/data-grid";

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

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",
  },
]);

If the same rule also reads value or row, use getCellClassName.

Align a column

The cell type sets the alignment of a cell. number and date cells align to the end, which is the right in a left-to-right grid. checkbox cells center, and the other types align to the start. To align a column, add a text alignment class in cellClassName. The class works for every cell type that shows text. A checkbox cell stays centered:

import { defineColumns } from "@/components/data-grid/data-grid";

type Item = { id: string; code: string; qty: number };

const columns = defineColumns<Item>()([
  {
    id: "code",
    header: "Code",
    accessorKey: "code",
    cellClassName: "text-end",
  },
  {
    id: "qty",
    header: "Qty",
    accessorKey: "qty",
    type: "number",
    cellClassName: "text-center",
  },
]);

Result: the codes move to the end of their cells, and the quantities center.

Mark changed rows

When onSave returns a promise, a row carries data-save-status="saving" while its save is pending, and "failed" after a refusal. useDataGridRowSaveStatus(rowId) reads the same status. The Style contract lists the attribute.

A row that the user changed carries data-row-changed until an undo, a push, or new data writes it again. A class on the grid selects it:

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

type Row = { id: string; name: string; qty: number };

const columns = defineColumns<Row>()([
  { id: "name", header: "Name", accessorKey: "name" },
  { id: "qty", header: "Qty", accessorKey: "qty", type: "number" },
]);

export function ChangedRows({ rows }: { rows: Row[] }) {
  return (
    <DataGrid
      className="h-80 [&_[data-row-changed]]:bg-primary/10"
      defaultData={rows}
      columns={columns}
    />
  );
}

Result: an edited, pasted, filled, cleared, cut, replaced, or imported row gets a tint.

Style by state with CSS

The grid keeps these data attributes up to date. Scope each selector to the grid, because other components also use data-* attributes. CSS selectors on them need no callback:

AttributeSet on
data-activethe active cell
data-editinga cell in edit mode
data-pinned="start" or "end"header and body cells of a pinned column
data-row-selectedthe marker cell of a selected row
data-row-changeda row that the user changed
data-search-matcha cell that matches the quick search
data-invalida cell with a validation error
data-filtereda header cell of a filtered column
data-hoveredthe row under the pointer

These rules outline the active cell and border an end-pinned cell:

[role="grid"] [role="gridcell"][data-active] {
  outline: 2px solid var(--color-primary);
  outline-offset: -2px;
}

[role="grid"] [role="gridcell"][data-pinned="end"] {
  border-inline-start: 1px solid var(--color-border);
}

On this page