Skip to content
Niko Table

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 DataTableHeader and DataTableBody. 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 with minSize and maxSize.

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

column-resize.tsx
Order ID
Customer
Email
Product
Amount
Status
Date
Region
City
Payment
Shipping
Notes
Actions
ORD-001John Doejohn.doe@example.comPremium Widget$299.99delivered1/15/2024North AmericaNew YorkCredit CardExpressLeave at front desk
ORD-002Jane Smithjane.smith@example.comBasic Kit$149.50shipped1/18/2024EuropeLondonPayPalStandardGift wrap requested
ORD-003Bob Johnsonbob.johnson@example.comPro Bundle$599.00pending1/20/2024Asia PacificTokyoBank TransferEconomyCall before delivery
ORD-004Alice Williamsalice.williams@example.comStarter Pack$79.99delivered1/22/2024North AmericaChicagoCredit CardStandardNo special instructions
ORD-005Charlie Browncharlie.brown@example.comEnterprise Suite$1,299.00shipped1/25/2024EuropeBerlinInvoiceExpressBusiness address only
ORD-006Diana Princediana.prince@example.comPremium Widget$299.99cancelled1/28/2024Asia PacificSydneyCredit CardStandardCustomer cancelled
ORD-007Ethan Huntethan.hunt@example.comBasic Kit$149.50pending2/1/2024North AmericaLos AngelesPayPalExpressSignature required
ORD-008Fiona Greenfiona.green@example.comPro Bundle$599.00delivered2/5/2024EuropeParisCredit CardStandardApartment 4B
ORD-009George Millergeorge.miller@example.comStarter Pack$79.99shipped2/8/2024Asia PacificSingaporeBank TransferEconomyOffice hours only
ORD-010Hannah Leehannah.lee@example.comEnterprise Suite$1,299.00delivered2/12/2024North AmericaSeattleInvoiceExpressInclude packing slip
Preview with Controlled State
column-resize-state.tsx
Order ID
Customer
Email
Product
Amount
Status
Date
Region
City
Payment
Shipping
Notes
Actions
ORD-001John Doejohn.doe@example.comPremium Widget$299.99delivered1/15/2024North AmericaNew YorkCredit CardExpressLeave at front desk
ORD-002Jane Smithjane.smith@example.comBasic Kit$149.50shipped1/18/2024EuropeLondonPayPalStandardGift wrap requested
ORD-003Bob Johnsonbob.johnson@example.comPro Bundle$599.00pending1/20/2024Asia PacificTokyoBank TransferEconomyCall before delivery
ORD-004Alice Williamsalice.williams@example.comStarter Pack$79.99delivered1/22/2024North AmericaChicagoCredit CardStandardNo special instructions
ORD-005Charlie Browncharlie.brown@example.comEnterprise Suite$1,299.00shipped1/25/2024EuropeBerlinInvoiceExpressBusiness address only
ORD-006Diana Princediana.prince@example.comPremium Widget$299.99cancelled1/28/2024Asia PacificSydneyCredit CardStandardCustomer cancelled
ORD-007Ethan Huntethan.hunt@example.comBasic Kit$149.50pending2/1/2024North AmericaLos AngelesPayPalExpressSignature required
ORD-008Fiona Greenfiona.green@example.comPro Bundle$599.00delivered2/5/2024EuropeParisCredit CardStandardApartment 4B
ORD-009George Millergeorge.miller@example.comStarter Pack$79.99shipped2/8/2024Asia PacificSingaporeBank TransferEconomyOffice hours only
ORD-010Hannah Leehannah.lee@example.comEnterprise Suite$1,299.00delivered2/12/2024North AmericaSeattleInvoiceExpressInclude packing slip
Current Table State
Live view of the current table state for demonstration purposes
Search Query:None
Total Items:10
Sorting:None
Page:1 (Size: 10)
Hidden Columns:0
Resized Columns:None (using default sizes)
View Full State Object
Sorting:
[]
Column Visibility:
{}
Column Sizing:
{}

Resizing helps with dense tables where content length varies (emails, notes, product names).

  • Set size on columns to make the first layout predictable.
  • The resize add-on handles drag and double-click autosize.

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

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

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",
},
// ...
]

Put <DataTableColumnResize /> near the top of DataTableRoot. Its presence turns on enableColumnResizing. You need no config flag.

column-resize.tsx
<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.

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

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):

columns.tsx
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 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:

columns.tsx
// 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:

wide-table.tsx
<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 not meta.label, copy it into meta.label. Header-fit then measures the right text.

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 columnSizing is 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
auto-fit.tsx
<DataTableRoot data={data} columns={columns}>
<DataTableColumnResize />
<DataTableColumnAutoFit />
<DataTable>
<DataTableHeader />
<DataTableBody />
</DataTable>
</DataTableRoot>

These columns have small size values but still fill the width:

column-resize-autofit.tsx
Team
City
Wins
Status
Fountain Valley HornetsHalifax21active
Darefield LightningHalifax9active
South Jordan CougarsHalifax11active
Treutelside PhoenixHalifax18active
West Lila GrizzliesHalifax7inactive
Declared width total:480px
Auto-fit width total:measuring

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:

column-resize-state.tsx
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>
)
}

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:

column-resize-persisted.tsx

Resize a column, then reload. Widths are restored from localStorage. On first load, before anything is saved, the columns fill the container width.

Order ID
Customer
Email
Product
Status
ORD-001John Doejohn.doe@example.comPremium Widgetdelivered
ORD-002Jane Smithjane.smith@example.comBasic Kitshipped
ORD-003Bob Johnsonbob.johnson@example.comPro Bundlepending
ORD-004Alice Williamsalice.williams@example.comStarter Packdelivered
ORD-005Charlie Browncharlie.brown@example.comEnterprise Suiteshipped

No widths saved yet. Showing the default fill layout.

pnpm dlx shadcn@latest add @niko-table/data-table-column-sizing-persistence
orders-table.tsx
import { 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.

✅ 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 /> on DataTableHeader + 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

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.