MRT logoMaterial React Table

    Density Toggle Feature Guide

    Material React Table includes a density toggle button in the top toolbar by default where a user can toggle between 3 different density levels. This is a great feature to include to help with accessibility for different user preferences, but it can easily be disabled if it is not wanted.

    Relevant Props

    #
    Prop Name
    Type
    Default Value
    More Info Links
    1
    boolean
    true

    No Description Provided... Yet...

    2
    OnChangeFn<DensityState>

    No Description Provided... Yet...

    Relevant State

    #
    State Option
    Type
    Default Value
    More Info Links
    1
    'comfortable' | 'compact' | 'spacious'
    comfortable

    No Description Provided... Yet...

    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 to nowrap by default to keep the rows as short in height as possible. This can be overridden in the muiTableBodyCellProps styles or sx prop.

    Change Default Density

    If you want to change the default density, you can set that in either the initialState prop or the state prop.

    <MaterialReactTable
    data={data}
    columns={columns}
    initialState={{ density: 'compact' }}
    />

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


    Demo

    Open Code SandboxOpen on GitHub
    First Name
    Last Name
    Address
    City
    State
    DylanMurray261 Erdman FordEast DaphneKentucky
    RaquelKohler769 Dominic GroveColumbusOhio
    ErvinReinger566 Brakus InletSouth LindaWest Virginia
    BrittanyMcCullough722 Emie StreamLincolnNebraska
    BransonFrami32188 Larkin TurnpikeCharlestonSouth Carolina

    Rows per page

    1-5 of 5

    Source Code

    1import React, { FC, useMemo } from 'react';
    2import MaterialReactTable, { MRT_ColumnDef } from 'material-react-table';
    3import { data, Person } from './makeData';
    4
    5const Example: FC = () => {
    6 const columns = useMemo(
    7 //column definitions...
    33 );
    34
    35 return (
    36 <MaterialReactTable
    37 columns={columns}
    38 data={data}
    39 enableDensityToggle={false}
    40 initialState={{ density: 'compact' }}
    41 />
    42 );
    43};
    44
    45export default Example;
    46