gridcn2.0.0
Theme, style, and language

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
Name
Role
Score
Silent Tiger
Viewer
4
Quick Lion
Viewer
89
Swift Wolf
Manager
80
Eager Lion
User
51
Eager Tiger
Viewer
83
Silent Wolf
User
15
Swift Lion
Viewer
62
Eager Lion
Manager
42
Eager Eagle
Editor
35
Bold Tiger
Editor
52
Bold Bear
User
28
Swift Wolf
Manager
9
Swift Bear
Viewer
45
Eager Wolf
Editor
44
Swift Eagle
Viewer
51
Quick Lion
Manager
28
Bold Wolf
Editor
76
Swift Eagle
Admin
26
Eager Eagle
Manager
70
Eager Bear
Editor
67
Silent Lion
Manager
89
Silent Tiger
Manager
40
Eager Tiger
Manager
82
Silent Lion
Admin
16
Bold Eagle
Admin
38
Bold Lion
Viewer
3
Quick Tiger
Viewer
41
Bold Eagle
Editor
10
Swift Tiger
Editor
6
Silent Lion
Viewer
42

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}
      />
    </>
  );
}
PropTypeDefault
density"compact" | "default" | "comfortable""default"
rowHeightnumbernone
headerHeightnumber32, 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:

VariableEffectDefault
--grid-scroll-shadowcolor of the edge shadows--grid-pin-shadow
--grid-pin-shadowcolor of the shadows at pinned zonesoklch(0 0 0 / 10%). The CLI install sets 10% for light mode and 40% for dark mode
--grid-shadow-sizewidth of the side shadows and height of the top and bottom shadows8px

--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.

On this page