Density and small screens
Set row and header height, fit the grid and toolbar to narrow screens, and restyle the scroll shadows with CSS variables.
A dense table shows more rows, and a roomy table is easier to tap. density picks a row height preset, rowHeight sets exact pixels, and headerHeight sets the header. On a narrow screen, the grid scrolls inside its own box.
Density and row height
density takes compact, default, or comfortable. The presets give rows a height of 28, 36, and 44 px. rowHeight sets exact pixels and wins over density. The header rows follow density and have a height of 32, 36, and 40 px. headerHeight sets the height of one header row. Column groups add header rows of the same height.
This grid starts compact and switches to comfortable when you click the button:
"use client";
import { useState } from "react";
import {
DataGrid,
defineColumns,
type DensityMode,
} from "@/components/data-grid/data-grid";
type Row = { id: string; name: string };
const rows: Row[] = [{ id: "1", name: "Ada" }];
const columns = defineColumns<Row>()([
{ id: "name", header: "Name", accessorKey: "name" },
]);
export function DenseGrid() {
const [density, setDensity] = useState<DensityMode>("compact");
return (
<>
<button onClick={() => setDensity("comfortable")}>Comfortable</button>
<DataGrid
className="h-80"
defaultData={rows}
columns={columns}
density={density}
/>
</>
);
}| Prop | Type | Default |
|---|---|---|
density | "compact" | "default" | "comfortable" | "default" |
rowHeight | number | none |
headerHeight | number | 32, 36, or 40 by density |
Set the heights with these props. The grid writes --grid-row-height itself and uses the same numbers to place the rows. A CSS rule on the row height does not reach that calculation.
Small screens
The grid scrolls sideways and down inside its own box, so a wide table does not widen the page. Give the grid a height. Columns keep their widths. A column with flex grows to share extra width and never gets narrower than its width.
The toolbar is one row that scrolls sideways. Pass flex-wrap to let its controls wrap onto a second line:
import {
DataGridFilterMenu,
DataGridSearch,
DataGridToolbar,
} from "@/components/data-grid-toolbar/data-grid-toolbar";
export function Toolbar() {
return (
<DataGridToolbar className="flex-wrap">
<DataGridSearch />
<DataGridFilterMenu />
</DataGridToolbar>
);
}On a touch screen, the grid adapts:
- The resize handle of a header is wider.
- With column reorder on, which is the default, each header shows a drag handle.
- The header menu button and the header filter button stay visible. With a mouse or a pen, they show on hover.
Scroll shadows
A soft shadow marks each side where more content lies beyond the visible area. The end shadow shows while more columns follow, and the bottom shadow shows while more rows follow. After the user scrolls, the start and top shadows show too. A pinned column zone or row band moves the shadow to its boundary.
Set these CSS variables on the grid or on any ancestor:
| Variable | Effect | Default |
|---|---|---|
--grid-scroll-shadow | color of the edge shadows | --grid-pin-shadow |
--grid-pin-shadow | color of the shadows at pinned zones | oklch(0 0 0 / 10%). The CLI install sets 10% for light mode and 40% for dark mode |
--grid-shadow-size | width of the side shadows and height of the top and bottom shadows | 8px |
--grid-scroll-shadow: transparent hides the edge shadows and keeps the shadows at pinned zones:
.my-grid {
--grid-scroll-shadow: transparent;
}For a different look, select the shadow elements. Each one has data-grid-pin-shadow with its side. A shadow at a pinned zone also has data-pinned. This rule restyles the end shadow:
[data-grid-pin-shadow="end"]:not([data-pinned]) {
background-image: linear-gradient(
to left,
var(--color-primary),
transparent
);
}In a right-to-left grid, the start shadow sits on the right edge and the end shadow on the left edge. The gradient of the end shadow then runs to right, so a rule for it needs to right too.
Related
- Style overrides: restyle one surface of the grid
- Column sizing: widths, flex, and autosize
- Column order and pinning: the pinned zones that move the shadows
- Colors and radius: theme tokens and the frame radius
- DataGrid props: every prop of the grid