DocsGuides

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

#
Prop Name
Type
Default Value
More Info Links
1booleantrueST Density Toggle Docs
2OnChangeFn<ST_DensityState>ST Density Toggle Docs

Relevant State

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

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 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 stTableBodyCellProps style or className 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 = 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.

Loading example

On this page