Column Resize Table
Let users drag column edges to resize, or double-click to fit the content.
Users drag a header’s right edge to resize a column. Double-clicking the grip fits the column to its widest visible content. Use this when column content varies in length.
Add <DataTableColumnResize /> inside DataTableRoot to turn it on.
-
It works with the regular
DataTableHeaderandDataTableBody. You do not need virtualization. -
Turn it off for one column with
enableResizing: false. -
Minimum width: columns default to a 40px drag floor (
minSize). Override it per column withminSizeandmaxSize. -
Long text: body cells use
truncate, so long text ends in an ellipsis instead of spilling into the next column. The Niko Grid and virtualized bodies do the same.
Fill is on by default. With resize on, the table stretches to its container.
- One flex column (the first non-pinned data column) takes the leftover width. A trailing actions column then sits at the right edge with no dead space.
- Columns you have not resized never get narrower than their header width, so labels do not truncate on load.
See Fill and header-fit to point the fill at another column, opt columns out, or turn filling off.
Want to spread the extra width across every resizable column instead? Add <DataTableColumnAutoFit />. It scales them equally on load and stops once you drag a column or restore a saved layout. See Auto-fit and Persist Widths.
Preview with Controlled State
Introduction
Section titled “Introduction”Resizing helps with dense tables where content length varies (emails, notes, product names).
- Set
sizeon columns to make the first layout predictable. - The resize add-on handles drag and double-click autosize.
Installation
Section titled “Installation”Install the DataTable core and add-ons for this example:
pnpm dlx shadcn@latest add @niko-table/data-table @niko-table/data-table-column-resize @niko-table/data-table-column-sort @niko-table/data-table-column-hide @niko-table/data-table-pagination @niko-table/data-table-search-filter @niko-table/data-table-view-menuFirst time using
@niko-table? See the Installation Guide to set up the registry.
For other add-ons or manual copy-paste, see the Installation Guide.
Prerequisites
Section titled “Prerequisites”You build an orders table. This is the data:
type Order = { id: string customer: string email: string product: string amount: number status: "pending" | "shipped" | "delivered" | "cancelled" date: string region: string city: string payment: string shipping: string notes: string}
const data: Order[] = [ { id: "ORD-001", customer: "John Doe", email: "john.doe@example.com", product: "Premium Widget", amount: 299.99, status: "delivered", date: "2024-01-15", region: "North America", city: "New York", payment: "Credit Card", shipping: "Express", notes: "Leave at front desk", }, // ...]Enable Column Resize
Section titled “Enable Column Resize”Put <DataTableColumnResize /> near the top of DataTableRoot. Its presence turns on enableColumnResizing. You need no config flag.
<DataTableRoot data={data} columns={columns}> <DataTableColumnResize /> <DataTable> <DataTableHeader /> <DataTableBody /> </DataTable></DataTableRoot>While you drag, a guide line follows the cursor. The column takes its new width on release (columnResizeMode: "onEnd"). Widths are not rewritten on every mouse move, so large and virtualized tables stay smooth.
Keyboard
Section titled “Keyboard”You can resize with the keyboard. Tab to the grip (it follows WAI-ARIA separator rules), then use these keys:
| Key | Action |
|---|---|
Arrow Left / Right |
Nudge the width by 8px |
Shift + Arrow |
Nudge by 40px |
Enter |
Autosize to fit content |
Column Definitions
Section titled “Column Definitions”Set size on columns so the first layout makes sense and horizontal scroll appears when needed. Turn resizing off for fixed columns (for example actions):
const columns: DataTableColumnDef<Order>[] = [ { accessorKey: "id", size: 110, header: () => ( <DataTableColumnHeader> <DataTableColumnTitle title="Order ID" /> <DataTableColumnActions> <DataTableColumnSortOptions /> </DataTableColumnActions> </DataTableColumnHeader> ), meta: { label: "Order ID" }, }, { accessorKey: "customer", size: 160, header: () => ( <DataTableColumnHeader> <DataTableColumnTitle title="Customer" /> <DataTableColumnActions> <DataTableColumnSortOptions /> </DataTableColumnActions> </DataTableColumnHeader> ), meta: { label: "Customer" }, }, // ... more columns with explicit sizes { id: "actions", size: 70, enableResizing: false, // no resize grip on this column enableSorting: false, enableHiding: false, // ... },]Fill and header-fit
Section titled “Fill and header-fit”Fill and header-fit come with <DataTableColumnResize />. You need no extra install or config.
- Flex column: one column takes the leftover width. This is layout only. It is never written to
columnSizing. - Still resizable: dragging its grip pins it at a set width and passes the fill to the next eligible column.
- Default target: the first non-pinned data column.
Steer the fill per column with meta:
// Point the fill at a specific column (default: first non-pinned data column){ accessorKey: "description", meta: { flex: true },}
// Opt a column out of being auto-picked{ accessorKey: "notes", meta: { flex: false },}To turn filling off for the whole table (for example a wide table meant to scroll sideways), pass disableFlexFill in the table-level meta option on DataTableRoot. It is a table option, not column meta:
<DataTableRoot data={data} columns={columns} meta={{ disableFlexFill: true }}>disableFlexFill is read when the table options are built. Set it as a fixed value. Do not toggle it at runtime.
Header-fit keeps labels readable.
- Columns you have not resized never go below their header label width.
- A column the user has resized keeps exactly that width.
- If the visible title comes from
<DataTableColumnTitle title="..." />and notmeta.label, copy it intometa.label. Header-fit then measures the right text.
Auto-fit
Section titled “Auto-fit”By default one flex column takes the leftover width. To share it evenly across every resizable column, put <DataTableColumnAutoFit /> next to <DataTableColumnResize />. The resizable columns then scale up to fill the container on load.
- Opt-in: without the marker, the default single-flex-column fill applies, and none of the auto-fit code ships.
- Fixed columns (
enableResizing: false) keep their size. - Re-fits when the container grows.
- Backs off once the user drags a column or a saved
columnSizingis restored. Manual and saved widths win. - Wide tables: columns wider than the container scroll horizontally.
Install it alongside resize:
pnpm dlx shadcn@latest add @niko-table/data-table-column-auto-fit<DataTableRoot data={data} columns={columns}> <DataTableColumnResize /> <DataTableColumnAutoFit /> <DataTable> <DataTableHeader /> <DataTableBody /> </DataTable></DataTableRoot>These columns have small size values but still fill the width:
Controlled Sizing
Section titled “Controlled Sizing”Hold columnSizing yourself to save it or inspect it live. Pass state={{ columnSizing }} and onColumnSizingChange. Restored widths beat automatic sizing (the default fill and auto-fit), so a saved layout is never overwritten on reload:
import { useState } from "react"import { type ColumnSizingState } from "@tanstack/react-table"
export function ControlledResizeTable({ data }: { data: Order[] }) { const [columnSizing, setColumnSizing] = useState<ColumnSizingState>({})
return ( <DataTableRoot data={data} columns={columns} state={{ columnSizing }} onColumnSizingChange={setColumnSizing} > <DataTableColumnResize /> {/* ... */} </DataTableRoot> )}Persist Widths
Section titled “Persist Widths”To keep resized widths after a reload, install the opt-in useColumnSizingPersistence hook. Pass its columnSizing and onColumnSizingChange to DataTableRoot. The hook:
- Uses
localStorage. - Writes right away.
- Syncs across tabs.
- Ignores corrupt storage.
Resize a column below, then reload:
pnpm dlx shadcn@latest add @niko-table/data-table-column-sizing-persistenceimport { useColumnSizingPersistence } from "@/components/niko-table/lib/use-column-sizing-persistence"
export function OrdersTable({ rows, orgId }) { // Scope the key per table (and per tenant/user if widths shouldn't be shared). const { columnSizing, onColumnSizingChange } = useColumnSizingPersistence( `orders-table:${orgId}`, )
return ( <DataTableRoot data={rows} columns={columns} state={{ columnSizing }} onColumnSizingChange={onColumnSizingChange} > <DataTableColumnResize /> <DataTable> <DataTableHeader /> <DataTableBody /> </DataTable> </DataTableRoot> )}The hook also returns resetColumnSizing() to clear the saved widths.
Widths are a per-user preference, so they sync across tabs. Resizing or resetting the table in one tab updates the others through the native storage event. Idle tabs only re-render when a width changed.
Want to own the storage (for example a per-user settings API instead of localStorage)? The hook is a thin wrapper. Copy it and swap the store. DataTableRoot only needs a controlled columnSizing and onColumnSizingChange.
With persistence and <DataTableColumnAutoFit /> together, the fitted widths are saved and restored like any user resize. Auto-fit runs once per user, not on every visit, because restored widths always win.
Persist column width like visibility and order. It is a per-user preference, not URL state. Restored widths beat automatic sizing, so a saved layout is never overwritten.
When to Use
Section titled “When to Use”✅ Use Column Resize when:
- The table has many columns and users need to fit widths to content
- You want drag-to-resize and double-click autosize without a custom header
- Some columns (row number, actions) should stay fixed-width via
enableResizing: false
Works with:
- Row DnD: same
<DataTableColumnResize />onDataTableHeader+DataTableDndBody(opt out on drag-handle columns)
❌ Consider other options when:
- All columns fit and widths never need tuning
- You only need sticky identifier or action columns: see Column Pinning
Virtualization
Section titled “Virtualization”You do not need a separate “virtualized column resize” example. Swap in DataTableVirtualizedHeader and DataTableVirtualizedBody, and give DataTable a fixed height. The same <DataTableColumnResize /> marker applies. See Virtualization Table and Composed Grid.
Next Steps
Section titled “Next Steps”- Column Pinning Table: keep key columns visible while scrolling
- Virtualization Table: same resize marker with virtualized headers and bodies
- Composed Grid: resize plus editable grid cells