Pro unlocks unlimited templates48:00:00Get Pro →

Data Table Grid

Advanced grid chrome for TanStack Table v9 — sticky pinned columns, resize handles, and sort/pin headers. Pair with the basic Data Table docs when you only need sort, filter, and pagination.

Gallery

Filter, sort, pin, resize, and paginate in one grid — pin menus on headers, drag edges to resize.

Loading…

Installation

CLI
npx @sonojs/ui@latest add data-table-grid
Also needs
npx @sonojs/ui@latest add button dropdown-menu table
npm i @tanstack/react-table@^9.3.1 lucide-react

Usage

data-table-grid.tsx
import {
  columnPinningFeature,
  columnResizingFeature,
  columnSizingFeature,
  createColumnHelper,
  tableFeatures,
  useTable,
} from "@tanstack/react-table"

import {
  DataTableColumnHeader,
  DataTableGrid,
  DataTableGridBody,
  DataTableGridCell,
  DataTableGridHead,
  DataTableGridHeader,
  DataTableGridRow,
  DataTableGridTable,
  DataTableResizeHandle,
} from "@/components/data-table-grid"

const features = tableFeatures({
  columnPinningFeature,
  columnSizingFeature,
  columnResizingFeature,
})

const columnHelper = createColumnHelper<typeof features, { id: string; name: string }>()

const columns = columnHelper.columns([
  columnHelper.accessor("id", {
    header: ({ column }) => (
      <DataTableColumnHeader title="ID" column={column} />
    ),
    size: 100,
  }),
  columnHelper.accessor("name", {
    header: ({ column }) => (
      <DataTableColumnHeader title="Name" column={column} />
    ),
    size: 180,
  }),
])

export function Example({ data }: { data: { id: string; name: string }[] }) {
  const table = useTable({
    features,
    data,
    columns,
    columnResizeMode: "onChange",
    initialState: { columnPinning: { start: ["id"], end: [] } },
  })

  return (
    <DataTableGrid>
      <DataTableGridTable totalSize={table.getTotalSize()}>
        <DataTableGridHeader>
          {table.getHeaderGroups().map((headerGroup) => (
            <DataTableGridRow key={headerGroup.id}>
              {headerGroup.headers.map((header) => (
                <DataTableGridHead
                  key={header.id}
                  column={header.column}
                  table={table}
                >
                  {header.isPlaceholder ? null : (
                    <>
                      <table.FlexRender header={header} />
                      <DataTableResizeHandle header={header} />
                    </>
                  )}
                </DataTableGridHead>
              ))}
            </DataTableGridRow>
          ))}
        </DataTableGridHeader>
        <DataTableGridBody>
          {table.getRowModel().rows.map((row) => (
            <DataTableGridRow key={row.id}>
              {row.getAllCells().map((cell) => (
                <DataTableGridCell
                  key={cell.id}
                  column={cell.column}
                  table={table}
                >
                  <table.FlexRender cell={cell} />
                </DataTableGridCell>
              ))}
            </DataTableGridRow>
          ))}
        </DataTableGridBody>
      </DataTableGridTable>
    </DataTableGrid>
  )
}

Props

PropTypeDefaultDescription
DataTableGrid maxHeightnumber · string"min(28rem, 70vh)"Viewport max height so sticky headers can scroll.
DataTableGridTable totalSizenumber-Pass `table.getTotalSize()` for fixed layout + resize.
DataTableGridHead / Cell column + tablePinableColumn + PinTable-Applies sticky pin offsets and edge shadows.
DataTableColumnHeader title + columnstring + column-Sort toggle and pin start/end/unpin menu.
DataTableResizeHandle headerHeader-Column edge handle via `header.getResizeHandler()`.

Related