Density Toggle Feature Guide
Material React Table includes a density toggle button in the top toolbar by default that lets you toggle between three different density levels. This is a great feature to include to help with accessibility for different user preferences, but it can also easily be disabled if desired.
Relevant Table Options
# | Prop Name | Type | Default Value | More Info Links | |
---|---|---|---|---|---|
1 |
|
| MRT Density Toggle Docs | ||
2 |
| MRT Density Toggle Docs | |||
Relevant State
Default Density
By default, Material React Table will render with a medium comfortable
density.
A density toggle is shown by default to let a user change the density to cycle through spacious
, comfortable
, and compact
densities.
When a
compact
density is set, whitespace is set tonowrap
by default to keep the rows as short in height as possible. This can be overridden in themuiTableBodyCellProps
styles or sx prop.
Change Default Density
If you want to change the default density, you can set it in either the initialState
table option or the state
table option.
const table = useMaterialReactTable({data,columns,initialState: { density: 'compact' },});return <MaterialReactTable table={table} />;
Disable or Hide the Density Toggle
You can change the default density and disable the density toggle itself if you want.
In this example, the density toggle is disabled and a compact
density is set by default in the initialState
table option.
First Name | Last Name | Address | City | State |
---|---|---|---|---|
Dylan | Murray | 261 Erdman Ford | East Daphne | Kentucky |
Raquel | Kohler | 769 Dominic Grove | Columbus | Ohio |
Ervin | Reinger | 566 Brakus Inlet | South Linda | West Virginia |
Brittany | McCullough | 722 Emie Stream | Lincoln | Nebraska |
Branson | Frami | 32188 Larkin Turnpike | Charleston | South Carolina |
1-5 of 5
1import { useMemo } from 'react';2import {3 MaterialReactTable,4 useMaterialReactTable,5 type MRT_ColumnDef,6} from 'material-react-table';7import { data, type Person } from './makeData';89const Example = () => {10 const columns = useMemo(11 //column definitions...37 );3839 const table = useMaterialReactTable({40 columns,41 data,42 enableDensityToggle: false,43 initialState: { density: 'compact' },44 });4546 return <MaterialReactTable table={table} />;47};4849export default Example;50