# Column sizing
Package: @mantine/data-grid
Import: import { DataGridColumnSizing } from '@mantine/data-grid';
Description: 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:

```tsx
// Demo.tsx
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} />;
}

// data.ts
export const employees = [
  { id: '1', firstName: 'Alice', lastName: 'Johnson', email: 'alice.johnson@example.com', department: 'Engineering', salary: 95000, startDate: '2021-03-15', status: 'active' },
  { id: '2', firstName: 'Bob', lastName: 'Smith', email: 'bob.smith@example.com', department: 'Marketing', salary: 72000, startDate: '2020-07-01', status: 'active' },
  { id: '3', firstName: 'Carol', lastName: 'Williams', email: 'carol.williams@example.com', department: 'Sales', salary: 68000, startDate: '2022-01-10', status: 'active' },
  { id: '4', firstName: 'David', lastName: 'Brown', email: 'david.brown@example.com', department: 'HR', salary: 65000, startDate: '2019-11-20', status: 'inactive' },
  { id: '5', firstName: 'Eva', lastName: 'Davis', email: 'eva.davis@example.com', department: 'Finance', salary: 88000, startDate: '2021-06-05', status: 'active' },
  { id: '6', firstName: 'Frank', lastName: 'Miller', email: 'frank.miller@example.com', department: 'Engineering', salary: 102000, startDate: '2018-09-12', status: 'active' },
  { id: '7', firstName: 'Grace', lastName: 'Wilson', email: 'grace.wilson@example.com', department: 'Marketing', salary: 76000, startDate: '2022-04-18', status: 'active' },
  { id: '8', firstName: 'Henry', lastName: 'Taylor', email: 'henry.taylor@example.com', department: 'Sales', salary: 71000, startDate: '2020-02-28', status: 'inactive' },
  { id: '9', firstName: 'Irene', lastName: 'Anderson', email: 'irene.anderson@example.com', department: 'Engineering', salary: 97000, startDate: '2021-08-30', status: 'active' },
  { id: '10', firstName: 'James', lastName: 'Thomas', email: 'james.thomas@example.com', department: 'Finance', salary: 91000, startDate: '2019-05-14', status: 'active' },
  { id: '11', firstName: 'Karen', lastName: 'Jackson', email: 'karen.jackson@example.com', department: 'HR', salary: 63000, startDate: '2023-01-09', status: 'active' },
  { id: '12', firstName: 'Leo', lastName: 'White', email: 'leo.white@example.com', department: 'Engineering', salary: 110000, startDate: '2017-12-01', status: 'active' },
  { id: '13', firstName: 'Mia', lastName: 'Harris', email: 'mia.harris@example.com', department: 'Sales', salary: 74000, startDate: '2022-07-22', status: 'active' },
  { id: '14', firstName: 'Nathan', lastName: 'Martin', email: 'nathan.martin@example.com', department: 'Marketing', salary: 69000, startDate: '2021-10-03', status: 'inactive' },
  { id: '15', firstName: 'Olivia', lastName: 'Garcia', email: 'olivia.garcia@example.com', department: 'Finance', salary: 85000, startDate: '2020-04-11', status: 'active' },
  { id: '16', firstName: 'Patrick', lastName: 'Martinez', email: 'patrick.martinez@example.com', department: 'Engineering', salary: 99000, startDate: '2019-08-25', status: 'active' },
  { id: '17', firstName: 'Quinn', lastName: 'Robinson', email: 'quinn.robinson@example.com', department: 'HR', salary: 67000, startDate: '2022-11-14', status: 'active' },
  { id: '18', firstName: 'Rachel', lastName: 'Clark', email: 'rachel.clark@example.com', department: 'Sales', salary: 73000, startDate: '2021-02-07', status: 'active' },
];
```


## 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`:

```tsx
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

```tsx
// Demo.tsx
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} />
    </>
  );
}

// data.ts
export const employees = [
  { id: '1', firstName: 'Alice', lastName: 'Johnson', email: 'alice.johnson@example.com', department: 'Engineering', salary: 95000, startDate: '2021-03-15', status: 'active' },
  { id: '2', firstName: 'Bob', lastName: 'Smith', email: 'bob.smith@example.com', department: 'Marketing', salary: 72000, startDate: '2020-07-01', status: 'active' },
  { id: '3', firstName: 'Carol', lastName: 'Williams', email: 'carol.williams@example.com', department: 'Sales', salary: 68000, startDate: '2022-01-10', status: 'active' },
  { id: '4', firstName: 'David', lastName: 'Brown', email: 'david.brown@example.com', department: 'HR', salary: 65000, startDate: '2019-11-20', status: 'inactive' },
  { id: '5', firstName: 'Eva', lastName: 'Davis', email: 'eva.davis@example.com', department: 'Finance', salary: 88000, startDate: '2021-06-05', status: 'active' },
  { id: '6', firstName: 'Frank', lastName: 'Miller', email: 'frank.miller@example.com', department: 'Engineering', salary: 102000, startDate: '2018-09-12', status: 'active' },
  { id: '7', firstName: 'Grace', lastName: 'Wilson', email: 'grace.wilson@example.com', department: 'Marketing', salary: 76000, startDate: '2022-04-18', status: 'active' },
  { id: '8', firstName: 'Henry', lastName: 'Taylor', email: 'henry.taylor@example.com', department: 'Sales', salary: 71000, startDate: '2020-02-28', status: 'inactive' },
  { id: '9', firstName: 'Irene', lastName: 'Anderson', email: 'irene.anderson@example.com', department: 'Engineering', salary: 97000, startDate: '2021-08-30', status: 'active' },
  { id: '10', firstName: 'James', lastName: 'Thomas', email: 'james.thomas@example.com', department: 'Finance', salary: 91000, startDate: '2019-05-14', status: 'active' },
  { id: '11', firstName: 'Karen', lastName: 'Jackson', email: 'karen.jackson@example.com', department: 'HR', salary: 63000, startDate: '2023-01-09', status: 'active' },
  { id: '12', firstName: 'Leo', lastName: 'White', email: 'leo.white@example.com', department: 'Engineering', salary: 110000, startDate: '2017-12-01', status: 'active' },
  { id: '13', firstName: 'Mia', lastName: 'Harris', email: 'mia.harris@example.com', department: 'Sales', salary: 74000, startDate: '2022-07-22', status: 'active' },
  { id: '14', firstName: 'Nathan', lastName: 'Martin', email: 'nathan.martin@example.com', department: 'Marketing', salary: 69000, startDate: '2021-10-03', status: 'inactive' },
  { id: '15', firstName: 'Olivia', lastName: 'Garcia', email: 'olivia.garcia@example.com', department: 'Finance', salary: 85000, startDate: '2020-04-11', status: 'active' },
  { id: '16', firstName: 'Patrick', lastName: 'Martinez', email: 'patrick.martinez@example.com', department: 'Engineering', salary: 99000, startDate: '2019-08-25', status: 'active' },
  { id: '17', firstName: 'Quinn', lastName: 'Robinson', email: 'quinn.robinson@example.com', department: 'HR', salary: 67000, startDate: '2022-11-14', status: 'active' },
  { id: '18', firstName: 'Rachel', lastName: 'Clark', email: 'rachel.clark@example.com', department: 'Sales', salary: 73000, startDate: '2021-02-07', status: 'active' },
];
```


## Column size constraints

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

```tsx
// Demo.tsx
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} />;
}

// data.ts
export const employees = [
  { id: '1', firstName: 'Alice', lastName: 'Johnson', email: 'alice.johnson@example.com', department: 'Engineering', salary: 95000, startDate: '2021-03-15', status: 'active' },
  { id: '2', firstName: 'Bob', lastName: 'Smith', email: 'bob.smith@example.com', department: 'Marketing', salary: 72000, startDate: '2020-07-01', status: 'active' },
  { id: '3', firstName: 'Carol', lastName: 'Williams', email: 'carol.williams@example.com', department: 'Sales', salary: 68000, startDate: '2022-01-10', status: 'active' },
  { id: '4', firstName: 'David', lastName: 'Brown', email: 'david.brown@example.com', department: 'HR', salary: 65000, startDate: '2019-11-20', status: 'inactive' },
  { id: '5', firstName: 'Eva', lastName: 'Davis', email: 'eva.davis@example.com', department: 'Finance', salary: 88000, startDate: '2021-06-05', status: 'active' },
  { id: '6', firstName: 'Frank', lastName: 'Miller', email: 'frank.miller@example.com', department: 'Engineering', salary: 102000, startDate: '2018-09-12', status: 'active' },
  { id: '7', firstName: 'Grace', lastName: 'Wilson', email: 'grace.wilson@example.com', department: 'Marketing', salary: 76000, startDate: '2022-04-18', status: 'active' },
  { id: '8', firstName: 'Henry', lastName: 'Taylor', email: 'henry.taylor@example.com', department: 'Sales', salary: 71000, startDate: '2020-02-28', status: 'inactive' },
  { id: '9', firstName: 'Irene', lastName: 'Anderson', email: 'irene.anderson@example.com', department: 'Engineering', salary: 97000, startDate: '2021-08-30', status: 'active' },
  { id: '10', firstName: 'James', lastName: 'Thomas', email: 'james.thomas@example.com', department: 'Finance', salary: 91000, startDate: '2019-05-14', status: 'active' },
  { id: '11', firstName: 'Karen', lastName: 'Jackson', email: 'karen.jackson@example.com', department: 'HR', salary: 63000, startDate: '2023-01-09', status: 'active' },
  { id: '12', firstName: 'Leo', lastName: 'White', email: 'leo.white@example.com', department: 'Engineering', salary: 110000, startDate: '2017-12-01', status: 'active' },
  { id: '13', firstName: 'Mia', lastName: 'Harris', email: 'mia.harris@example.com', department: 'Sales', salary: 74000, startDate: '2022-07-22', status: 'active' },
  { id: '14', firstName: 'Nathan', lastName: 'Martin', email: 'nathan.martin@example.com', department: 'Marketing', salary: 69000, startDate: '2021-10-03', status: 'inactive' },
  { id: '15', firstName: 'Olivia', lastName: 'Garcia', email: 'olivia.garcia@example.com', department: 'Finance', salary: 85000, startDate: '2020-04-11', status: 'active' },
  { id: '16', firstName: 'Patrick', lastName: 'Martinez', email: 'patrick.martinez@example.com', department: 'Engineering', salary: 99000, startDate: '2019-08-25', status: 'active' },
  { id: '17', firstName: 'Quinn', lastName: 'Robinson', email: 'quinn.robinson@example.com', department: 'HR', salary: 67000, startDate: '2022-11-14', status: 'active' },
  { id: '18', firstName: 'Rachel', lastName: 'Clark', email: 'rachel.clark@example.com', department: 'Sales', salary: 73000, startDate: '2021-02-07', status: 'active' },
];
```


## Controlled column sizing

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

```tsx
// Demo.tsx
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>
    </>
  );
}

// data.ts
export const employees = [
  { id: '1', firstName: 'Alice', lastName: 'Johnson', email: 'alice.johnson@example.com', department: 'Engineering', salary: 95000, startDate: '2021-03-15', status: 'active' },
  { id: '2', firstName: 'Bob', lastName: 'Smith', email: 'bob.smith@example.com', department: 'Marketing', salary: 72000, startDate: '2020-07-01', status: 'active' },
  { id: '3', firstName: 'Carol', lastName: 'Williams', email: 'carol.williams@example.com', department: 'Sales', salary: 68000, startDate: '2022-01-10', status: 'active' },
  { id: '4', firstName: 'David', lastName: 'Brown', email: 'david.brown@example.com', department: 'HR', salary: 65000, startDate: '2019-11-20', status: 'inactive' },
  { id: '5', firstName: 'Eva', lastName: 'Davis', email: 'eva.davis@example.com', department: 'Finance', salary: 88000, startDate: '2021-06-05', status: 'active' },
  { id: '6', firstName: 'Frank', lastName: 'Miller', email: 'frank.miller@example.com', department: 'Engineering', salary: 102000, startDate: '2018-09-12', status: 'active' },
  { id: '7', firstName: 'Grace', lastName: 'Wilson', email: 'grace.wilson@example.com', department: 'Marketing', salary: 76000, startDate: '2022-04-18', status: 'active' },
  { id: '8', firstName: 'Henry', lastName: 'Taylor', email: 'henry.taylor@example.com', department: 'Sales', salary: 71000, startDate: '2020-02-28', status: 'inactive' },
  { id: '9', firstName: 'Irene', lastName: 'Anderson', email: 'irene.anderson@example.com', department: 'Engineering', salary: 97000, startDate: '2021-08-30', status: 'active' },
  { id: '10', firstName: 'James', lastName: 'Thomas', email: 'james.thomas@example.com', department: 'Finance', salary: 91000, startDate: '2019-05-14', status: 'active' },
  { id: '11', firstName: 'Karen', lastName: 'Jackson', email: 'karen.jackson@example.com', department: 'HR', salary: 63000, startDate: '2023-01-09', status: 'active' },
  { id: '12', firstName: 'Leo', lastName: 'White', email: 'leo.white@example.com', department: 'Engineering', salary: 110000, startDate: '2017-12-01', status: 'active' },
  { id: '13', firstName: 'Mia', lastName: 'Harris', email: 'mia.harris@example.com', department: 'Sales', salary: 74000, startDate: '2022-07-22', status: 'active' },
  { id: '14', firstName: 'Nathan', lastName: 'Martin', email: 'nathan.martin@example.com', department: 'Marketing', salary: 69000, startDate: '2021-10-03', status: 'inactive' },
  { id: '15', firstName: 'Olivia', lastName: 'Garcia', email: 'olivia.garcia@example.com', department: 'Finance', salary: 85000, startDate: '2020-04-11', status: 'active' },
  { id: '16', firstName: 'Patrick', lastName: 'Martinez', email: 'patrick.martinez@example.com', department: 'Engineering', salary: 99000, startDate: '2019-08-25', status: 'active' },
  { id: '17', firstName: 'Quinn', lastName: 'Robinson', email: 'quinn.robinson@example.com', department: 'HR', salary: 67000, startDate: '2022-11-14', status: 'active' },
  { id: '18', firstName: 'Rachel', lastName: 'Clark', email: 'rachel.clark@example.com', department: 'Sales', salary: 73000, startDate: '2021-02-07', status: 'active' },
];
```


## Disable resizing per column

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

```tsx
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:

Override them with `tableOptions.defaultColumn`:

```tsx
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:

```tsx
<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:

```tsx
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:

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