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.
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)onDataGridorDataGridRootstyles a row.getCellClassName(ctx)on the same components styles a cell.cellClassNameon aColumnDefstyles every cell of the column. It takes a string, or a function with the samectx.headerClassNameon aColumnDefstyles 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:
| Attribute | Set on |
|---|---|
data-active | the active cell |
data-editing | a cell in edit mode |
data-pinned="start" or "end" | header and body cells of a pinned column |
data-row-selected | the marker cell of a selected row |
data-row-changed | a row that the user changed |
data-search-match | a cell that matches the quick search |
data-invalid | a cell with a validation error |
data-filtered | a header cell of a filtered column |
data-hovered | the 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);
}Related
- Colors and radius: theme tokens and the frame radius
- Density and small screens: row height and scroll shadows
- Style contract: every class and data attribute the grid sets
- Styling model: how the layers of style fit together