Column sizing

DataGrid column resize, min/max sizes

Basic column resizing

Pass withColumnResizing to useDataGrid to let users resize columns by dragging column boundaries. Set the initial column width with the size property of a column definition:

First Name
Last Name
Email
Department
Salary
AliceJohnsonalice.johnson@example.comEngineering95000
BobSmithbob.smith@example.comMarketing72000
CarolWilliamscarol.williams@example.comSales68000
DavidBrowndavid.brown@example.comHR65000
EvaDaviseva.davis@example.comFinance88000
FrankMillerfrank.miller@example.comEngineering102000
GraceWilsongrace.wilson@example.comMarketing76000
HenryTaylorhenry.taylor@example.comSales71000
IreneAndersonirene.anderson@example.comEngineering97000
JamesThomasjames.thomas@example.comFinance91000
KarenJacksonkaren.jackson@example.comHR63000
LeoWhiteleo.white@example.comEngineering110000
MiaHarrismia.harris@example.comSales74000
NathanMartinnathan.martin@example.comMarketing69000
OliviaGarciaolivia.garcia@example.comFinance85000
PatrickMartinezpatrick.martinez@example.comEngineering99000
QuinnRobinsonquinn.robinson@example.comHR67000
RachelClarkrachel.clark@example.comSales73000
import { DataGrid, useDataGrid } from '@mantine/data-grid';
import { employees } from './data';

function Demo() {
  const store = useDataGrid({
    data: employees,
    columns: [
      { accessorKey: 'firstName', header: 'First Name', size: 150 },
      { accessorKey: 'lastName', header: 'Last Name', size: 150 },
      { accessorKey: 'email', header: 'Email', size: 250 },
      { accessorKey: 'department', header: 'Department', size: 150 },
      { accessorKey: 'salary', header: 'Salary', size: 120 },
    ],
    withColumnResizing: true,
  });

  return <DataGrid store={store} />;
}

Resize handles

Set withLastColumnResizing on DataGrid to add a resize handle to the last column. Double-click a handle to fit the column width to its content. To resize columns from your code, pass the table and grid element to autosizeColumns:

import { useRef } from 'react';
import { Button } from '@mantine/core';
import { autosizeColumns, DataGrid, useDataGrid } from '@mantine/data-grid';
import { columns, data } from './data';

function Demo() {
  const ref = useRef<HTMLDivElement>(null);
  const store = useDataGrid({ data, columns, withColumnResizing: true });

  return (
    <>
      <Button
        onClick={() => {
          if (ref.current) {
            autosizeColumns(store.table, ref.current);
          }
        }}
      >
        Fit columns to content
      </Button>
      <DataGrid ref={ref} store={store} withLastColumnResizing />
    </>
  );
}

Resize mode

Set columnResizeMode to change when column widths are updated:

  • onEnd (default) – when the user releases the handle, a guide line previews the new width while dragging
  • onChange – while the user drags the handle
First Name
Last Name
Email
Department
Salary
AliceJohnsonalice.johnson@example.comEngineering95000
BobSmithbob.smith@example.comMarketing72000
CarolWilliamscarol.williams@example.comSales68000
DavidBrowndavid.brown@example.comHR65000
EvaDaviseva.davis@example.comFinance88000
FrankMillerfrank.miller@example.comEngineering102000
GraceWilsongrace.wilson@example.comMarketing76000
HenryTaylorhenry.taylor@example.comSales71000
IreneAndersonirene.anderson@example.comEngineering97000
JamesThomasjames.thomas@example.comFinance91000
KarenJacksonkaren.jackson@example.comHR63000
LeoWhiteleo.white@example.comEngineering110000
MiaHarrismia.harris@example.comSales74000
NathanMartinnathan.martin@example.comMarketing69000
OliviaGarciaolivia.garcia@example.comFinance85000
PatrickMartinezpatrick.martinez@example.comEngineering99000
QuinnRobinsonquinn.robinson@example.comHR67000
RachelClarkrachel.clark@example.comSales73000
import { useState } from 'react';
import { ColumnResizeMode } from '@tanstack/react-table';
import { SegmentedControl } from '@mantine/core';
import { DataGrid, useDataGrid } from '@mantine/data-grid';
import { employees } from './data';

function Demo() {
  const [resizeMode, setResizeMode] = useState<ColumnResizeMode>('onEnd');

  const store = useDataGrid({
    data: employees,
    columns: [
      { accessorKey: 'firstName', header: 'First Name', size: 150 },
      { accessorKey: 'lastName', header: 'Last Name', size: 150 },
      { accessorKey: 'email', header: 'Email', size: 250 },
      { accessorKey: 'department', header: 'Department', size: 150 },
      { accessorKey: 'salary', header: 'Salary', size: 120 },
    ],
    withColumnResizing: true,
    columnResizeMode: resizeMode,
  });

  return (
    <>
      <SegmentedControl
        value={resizeMode}
        onChange={(value) => setResizeMode(value as ColumnResizeMode)}
        data={['onChange', 'onEnd']}
        mb="md"
      />
      <DataGrid store={store} />
    </>
  );
}

Column size constraints

Use minSize and maxSize in column definitions to limit the width of a resized column:

First Name
Last Name
Email
Department
Salary
AliceJohnsonalice.johnson@example.comEngineering95000
BobSmithbob.smith@example.comMarketing72000
CarolWilliamscarol.williams@example.comSales68000
DavidBrowndavid.brown@example.comHR65000
EvaDaviseva.davis@example.comFinance88000
FrankMillerfrank.miller@example.comEngineering102000
GraceWilsongrace.wilson@example.comMarketing76000
HenryTaylorhenry.taylor@example.comSales71000
IreneAndersonirene.anderson@example.comEngineering97000
JamesThomasjames.thomas@example.comFinance91000
KarenJacksonkaren.jackson@example.comHR63000
LeoWhiteleo.white@example.comEngineering110000
MiaHarrismia.harris@example.comSales74000
NathanMartinnathan.martin@example.comMarketing69000
OliviaGarciaolivia.garcia@example.comFinance85000
PatrickMartinezpatrick.martinez@example.comEngineering99000
QuinnRobinsonquinn.robinson@example.comHR67000
RachelClarkrachel.clark@example.comSales73000
import { DataGrid, useDataGrid } from '@mantine/data-grid';
import { employees } from './data';

function Demo() {
  const store = useDataGrid({
    data: employees,
    columns: [
      { accessorKey: 'firstName', header: 'First Name', size: 150, minSize: 100, maxSize: 300 },
      { accessorKey: 'lastName', header: 'Last Name', size: 150, minSize: 100, maxSize: 300 },
      { accessorKey: 'email', header: 'Email', size: 250, minSize: 200 },
      { accessorKey: 'department', header: 'Department', size: 120, minSize: 80, maxSize: 150 },
      { accessorKey: 'salary', header: 'Salary', size: 100 },
    ],
    withColumnResizing: true,
  });

  return <DataGrid store={store} />;
}

Controlled column sizing

Pass columnSizing and onColumnSizingChange to useDataGrid for controlled column sizing state:

First Name
Last Name
Email
Department
Salary
AliceJohnsonalice.johnson@example.comEngineering95000
BobSmithbob.smith@example.comMarketing72000
CarolWilliamscarol.williams@example.comSales68000
DavidBrowndavid.brown@example.comHR65000
EvaDaviseva.davis@example.comFinance88000
FrankMillerfrank.miller@example.comEngineering102000
GraceWilsongrace.wilson@example.comMarketing76000
HenryTaylorhenry.taylor@example.comSales71000
IreneAndersonirene.anderson@example.comEngineering97000
JamesThomasjames.thomas@example.comFinance91000
KarenJacksonkaren.jackson@example.comHR63000
LeoWhiteleo.white@example.comEngineering110000
MiaHarrismia.harris@example.comSales74000
NathanMartinnathan.martin@example.comMarketing69000
OliviaGarciaolivia.garcia@example.comFinance85000
PatrickMartinezpatrick.martinez@example.comEngineering99000
QuinnRobinsonquinn.robinson@example.comHR67000
RachelClarkrachel.clark@example.comSales73000

Current column sizing state:

{}
import { useState } from 'react';
import { ColumnSizingState } from '@tanstack/react-table';
import { Button, Code, Text } from '@mantine/core';
import { DataGrid, useDataGrid } from '@mantine/data-grid';
import { employees } from './data';

function Demo() {
  const [columnSizing, setColumnSizing] = useState<ColumnSizingState>({});

  const store = useDataGrid({
    data: employees,
    columns: [
      { accessorKey: 'firstName', header: 'First Name', size: 150 },
      { accessorKey: 'lastName', header: 'Last Name', size: 150 },
      { accessorKey: 'email', header: 'Email', size: 250 },
      { accessorKey: 'department', header: 'Department', size: 150 },
      { accessorKey: 'salary', header: 'Salary', size: 120 },
    ],
    withColumnResizing: true,
    columnSizing,
    onColumnSizingChange: setColumnSizing,
  });

  return (
    <>
      <DataGrid store={store} />
      <Text mt="md" mb="xs">
        Current column sizing state:
      </Text>
      <Code block>{JSON.stringify(columnSizing, null, 2)}</Code>
      <Button mt="md" onClick={() => setColumnSizing({})}>
        Reset sizes
      </Button>
    </>
  );
}

Disable resizing per column

Set enableResizing: false on a column definition to disable resizing of that column:

const columns = [
  { accessorKey: 'id', header: 'ID', size: 60, enableResizing: false },
  { accessorKey: 'name', header: 'Name', size: 200 },
  { accessorKey: 'email', header: 'Email', size: 250 },
];

const store = useDataGrid({ data, columns, withColumnResizing: true });

Utility columns and actions columns cannot be resized by the user. Their width depends on the grid size, use columnSizing to override it.

Column size defaults

TanStack Table uses these defaults when size, minSize or maxSize are not set:

PropertyDefault
size150
minSize20
maxSizeNumber.MAX_SAFE_INTEGER

Override them with tableOptions.defaultColumn:

const store = useDataGrid({
  data,
  columns,
  withColumnResizing: true,
  tableOptions: {
    defaultColumn: { size: 200, minSize: 50, maxSize: 500 },
  },
});

Table layout

By default, columns use their configured width and the last column fills the remaining space. Set tableLayout="auto" to let the browser distribute column widths based on content. Use textOverflow="wrap" to wrap text that does not fit:

<DataGrid store={store} tableLayout="auto" textOverflow="wrap" />

Header groups

Group headers have resize handles too: dragging resizes all columns of the group proportionally, double-click autosizes them:

const store = useDataGrid({
  data,
  withColumnResizing: true,
  columns: [
    {
      header: 'Name',
      columns: [
        { accessorKey: 'firstName', header: 'First name' },
        { accessorKey: 'lastName', header: 'Last name' },
      ],
    },
    { accessorKey: 'email', header: 'Email' },
  ],
});

RTL support

The resize direction is taken from the closest DirectionProvider. Set columnResizeDirection to override it:

const store = useDataGrid({
  data,
  columns,
  withColumnResizing: true,
  columnResizeDirection: 'rtl',
});