DocsGuides

Guides

Density Toggle Guide

How to use, customize, or disable the density toggle button feature of Shadcn Table

Shadcn Table lets users switch between three density levels. By default the density control lives in the view settings panel as a select, and it is pinned to the toolbar as a button that cycles through the densities. This is a great feature to include to help with accessibility for different user preferences, but it can also easily be disabled if desired.

Example

Relevant Table Options

Prop NameTypeDescription
enableDensityToggleboolean
onDensityChangeOnChangeFn<ST_DensityState>Receives a function updater, as the TanStack Table on*Change handlers do. Resolve it with functionalUpdate, or pass a React state setter.

Relevant State

State OptionTypeDescription
density'comfortable' | 'compact' | 'spacious'

Default Density

By default, Shadcn Table will render with a medium comfortable density.

Each density gives body rows one fixed height: compact 40 px, comfortable 48 px and spacious 56 px. The header row is 40 px at every density. The horizontal cell padding grows with the density too.

The Density row of the view settings panel picks one of the three from a select. The same control is pinned to the toolbar by default as an icon button ("Toggle density") that cycles from compact to comfortable to spacious; unpin it in the panel to remove it from the toolbar.

Cell text stays on one line at every density and is cut with an ellipsis, with a tooltip showing the full value when it is cut off. Turn on Wrap cell text in the view settings panel to show the full text instead. See the Overflow Tooltip Guide.

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. With enableDensityToggle: false, the Density row leaves the view settings panel and the pinned density button leaves the toolbar.

In this example, the density toggle is disabled and a compact density is set by default in the initialState table option.

Storybook

On this page