Guides
Column Resizing Guide
How to use and customize the column resizing features of Shadcn Table
Shadcn Table has a built-in column resizing handle, and it is on by default.
Example
The Column Size features was recently split into its own Guide. View that guide as a prerequisite to this one.
Relevant Table Options
| Prop Name | Type | Description |
|---|---|---|
| columnResizeDirection | 'ltr' | 'rtl' | Determines the direction of column resizing. ltr = left to right, rtl = right to left. |
| columnResizeMode | 'onEnd' | 'onChange' | Determines when the columnSizing state is updated. onChange updates the state when the user is dragging the resize handle. onEnd updates the state when the user releases the resize handle. |
| defaultColumn | Partial<ST_ColumnDef<TData>> | Default column options to use for all column defs supplied to the table. This is useful for providing default cell/header/footer renderers, sorting/filtering/grouping options, etc. |
| defaultDisplayColumn | Partial<ST_DisplayColumnDef<TData>> | Default column options to use for all display column defs supplied to the table. This is useful for providing default cell/header/footer renderers, or turning on or off certain features for all display columns. |
| displayColumnDefOptions | { [key: string]: ST_DisplayColumnDef<TData> } | Customize and override the column definition options for the built-in display columns. (Select, Expand, Row Actions, etc.) |
| enableColumnResizing | boolean | Shows the resize handle on the trailing edge of each header cell. Double-click the handle to reset the width. Set it to false to turn resizing off. |
| layoutMode | 'semantic' | 'grid' | 'grid-no-grow' | The width rule. 'grid' (the default) lets data columns share the spare width in the ratio of their sizes, while display columns and pinned columns keep their set width. 'grid-no-grow' keeps every column at its set width, 'semantic' renders a plain table layout. Virtualization always uses 'grid'. |
| onColumnResizingChange | OnChangeFn<ST_ColumnResizingState> | This optional function will be called when the columnResizing state changes. If you provide this function, you will be responsible for maintaining its state yourself. You can pass this state back to the table via the state.columnResizing table option. |
| onColumnSizingChange | OnChangeFn<ColumnSizingState> | This optional function will be called when the columnSizing state changes. If you provide this function, you will be responsible for maintaining its state yourself. You can pass this state back to the table via the state.columnSizing table option. |
Relevant Column Options
| Column Option | Type | Description |
|---|---|---|
| enableResizing | boolean | |
| grow | boolean | number | If `layoutMode` is `'grid'` (the default) or `'grid-no-grow'`, set the flex grow value for a column, or `false`/0 to keep it at its set width. In the default layout display columns and pinned columns do not grow; a data column grows in the ratio of its size. |
| maxSize | number | |
| minSize | number | |
| size | number |
Relevant State
| State Option | Type | Description |
|---|---|---|
| columnResizing | ST_ColumnResizingState | an object containing information about the column resize in progress |
| columnSizing | Record<string, number> | an object mapping column IDs to their current width in pixels |
Initial Column Sizes
Column sizes will behave differently depending on which layoutMode you have set.
See the Column Size Guide for more information on layout modes and how to set initial column sizes properly for you use case.
Column Resizing Feature
enableColumnResizing is the boolean table option that controls the column resizing feature. It is true by default.
The resize handle is a full-height hairline on the trailing edge of each header cell. It is invisible at rest, lights up while the pointer is over the header cell, turns to the primary color while dragging, and shows the col-resize cursor over the edge. Double-click the handle to reset the column to its default width. While a column's width differs from its default, its column menu also lists a "Reset column size" item.
Set enableColumnResizing: false to turn resizing off for the whole table.
const table = useShadcnTable({
columns,
data,
enableColumnResizing: false,
});
return <ShadcnTable table={table} />;You can disable specific columns from being resizable by setting the enableResizing column option to false in their respective column definition.
Column Resize Mode
The default columnResizeMode is onChange, which means that the column resizing will occur immediately as the user drags the column resize handle. If you are running into performance issues because of many other enabled features, you might want to set the columnResizeMode to onEnd instead. This will make the column resizing only occur after the user has finished dragging the column resize handle and released their mouse.
const table = useShadcnTable({
columns,
data,
columnResizeMode: 'onEnd', //instead of the default "onChange" mode
});Column Growing
Resizing does not change the layout mode. In the default "grid" layout, data columns share the spare width of the table in the ratio of their sizes, while display columns and pinned columns keep their set width. While spare width exists, a dragged edge moves a little faster than the pointer, because the column keeps its share of the spare width. Once the columns are wider than the table, every column renders at its set width and the table scrolls sideways.
If you want every column to keep exactly the width you drag it to, set the layoutMode to "grid-no-grow".
const table = useShadcnTable({
columns,
data,
layoutMode: 'grid-no-grow', //columns keep their set width and do not fill the remaining space
});Alternatively, if you only want certain columns to grow to fill the remaining space, you can set the grow column option to true in their respective column definitions.
const columns = [
//...
{
accessorKey: 'address',
header: 'Address',
size: 250,
grow: true, //allow this column to grow to fill the remaining space
},
];This is discussed in more detail in the Column Size Guide.
Column Resize Direction
If you are displaying your table in a RTL (right-to-left) language, you can set the columnResizeDirection table option to "rtl" to make the column resize handle appear on the left side of the column instead of the right side.
Shadcn Table does not read the document direction, so set this option explicitly when your table is rendered in an RTL layout.
const table = useShadcnTable({
columns,
data,
columnResizeDirection: 'rtl', //instead of the default "ltr" direction
});
return (
<div style={{ direction: 'rtl' }}>
{' '}
{/* app-wide style? */}
<ShadcnTable table={table} />
</div>
);Enable Resizing on Built-in Display Columns
As discussed further in the Display Columns Guide, you can customize the options of the built-in columns that get generated under the hood by ST by enabling certain features.
Here, we can enable column resizing on the built-in row numbers column by setting the enableResizing column option to true in the displayColumnDefOptions table option.
const table = useShadcnTable({
columns,
data,
displayColumnDefOptions: {
'st-row-numbers': {
enableResizing: true, //allow the row numbers column to be resized
size: 40,
grow: false, //display columns do not fill the remaining space (the default)
},
},
enableRowNumbers: true,
});
return <ShadcnTable table={table} />;View Extra Storybook Examples