Guides
Density Toggle Guide
How to use, customize, or disable the density toggle button feature of Shadcn Table
Shadcn 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
Relevant State
Default Density
By default, Shadcn 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
compactdensity is set, whitespace is set tonowrapby default to keep the rows as short in height as possible. This can be overridden in thestTableBodyCellPropsstyleorclassNameprop.
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 = useShadcnTable({
data,
columns,
initialState: { density: 'compact' },
});
return <ShadcnTable 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.