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
pnpm dlx shadcn@latest add @gridcn/data-grid-styling-patterns-demo
- The Region column carries
cellClassNameandheaderClassName. 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.
Related
- Style overrides: all four class hooks and the state attributes
- Density and small screens: the row height and header height props
- Style contract: the CSS variables and the data attributes
- Right-to-left example: the grid with another language