Guides
View Settings Guide
How the view settings panel, its toolbar pins and the opt-in view persistence of Shadcn Table work
To remember each user's view across reloads, set persistViewSettings: true; nothing is stored until you do.
Example
const table = useShadcnTable({
columns,
data,
persistViewSettings: true, //save the view settings, pins and column layout to localStorage
});The view settings panel collects the table's display preferences behind one button at the far right of the toolbar. Each setting can also be pinned to the toolbar as its own icon button. The panel is on by default (enableViewSettings).
Relevant Table Options
| Prop Name | Type | Description |
|---|---|---|
| enableDensityToggle | boolean | |
| enableRowNumbers | boolean | |
| enableStickyHeader | boolean | Keeps the header row in view while the body scrolls. The scroll box is capped by the --st-sticky-max-height CSS variable (70vh by default). Row virtualization turns it on. |
| enableViewSettings | boolean | Adds the view settings button to the toolbar. Its panel holds Columns, Density, Wrap cell text, Grid lines, Sticky header, Alternating row colors, Row numbers and Reset view; pinned items show as toolbar buttons. |
| id | string | A stable id for the table. With persistViewSettings: true it names the storage key (shadcn-table:<id>:view) instead of the hash of the column ids. Without it the table generates an id of its own. |
| nonce | string | The Content Security Policy nonce for the inline script a table renders when persistViewSettings is on. Without it, a strict policy blocks the script and the table paints the default view before the stored one. |
| persistViewSettings | boolean | string | Saves the view settings, the pins, column visibility, order, pinning and rows per page to localStorage. true uses a key built from the column ids (or the id option when set); a string is the key. Sorts and filters are not saved. |
Relevant State
| State Option | Type | Description |
|---|---|---|
| columnOrder | Array<string> | an array of strings representing the current order of the table columns |
| columnPinning | { start: Array<string>, end: Array<string> } | an object with start and end properties, each of which is an array of strings representing the columns that are pinned to the start (left in LTR) or end (right in LTR) side of the table |
| columnVisibility | Record<string, boolean> | an object mapping column IDs to a boolean indicating whether the column is currently visible |
| density | 'comfortable' | 'compact' | 'spacious' | |
| viewSettings | ST_ViewSettingsState | The view settings panel values and the items pinned to the toolbar. rowNumbers and stickyHeader start from enableRowNumbers and enableStickyHeader. |
Persist the View
persistViewSettings is false by default: a stock table writes nothing to storage and renders no inline script. With persistViewSettings: true, the table saves the user's view to localStorage on every change and restores it on the next visit. Reset view deletes the stored entry.
What is stored:
- the view settings rows: wrap cell text, grid lines, sticky header, alternating row colors and row numbers
- the toolbar pins
- density
- column visibility, column order and column pinning
- rows per page
What is not stored: sorts, filters, the search text, the page index and column sizes. To persist those, manage them through state yourself, as the State Management Guide shows. A view equal to the table's initialState is not kept; the entry is removed instead.
The Storage Key
The entry is stored under shadcn-table:<key>:view. You do not need to name the table:
- By default the key is a hash of the table's column ids in definition order. Two tables with the same column ids share one stored view.
- Set the
idtable option to use it as the key instead, for example when two tables have the same columns but should keep separate views. - Pass a string as
persistViewSettingsto use that string as the whole storage key.
When a column is added, removed or renamed, the hash changes and the view starts from the defaults. A stored view that names a column the table no longer has is dropped, and in development the console says so once.
const table = useShadcnTable({
columns,
data,
id: 'orders', //stored under shadcn-table:orders:view
persistViewSettings: true,
});Server Rendering Without a Flash
A server-rendered page cannot read localStorage, so the server HTML shows the default view. To keep that default from flashing before the stored view applies, a table with persistViewSettings renders one small inline script with its server HTML. Before the first paint, the script checks storage for this table's key and, when the stored view differs from the defaults, marks the table root with data-st-view-pending and aria-busy. While marked, the body cells hide their content and show skeleton bars. Hydration applies the stored view in one render and clears the mark. A visitor with nothing stored sees the server-rendered table at once.
Apps with a strict Content Security Policy pass the policy's nonce through the nonce table option so the script may run. Without it, the browser blocks the script and the table paints the default view first, then switches to the stored one after hydration.
const table = useShadcnTable({
columns,
data,
persistViewSettings: true,
nonce, //the CSP nonce of the current request
});Client-only apps read storage before the first paint and need none of this.
The View Settings Panel
The view settings button (aria-label "View settings") opens a panel with these rows, each with an icon that follows its current value:
- Columns: opens the columns menu, showing how many columns are shown ("5 shown"). See the Column Hiding Guide.
- Density: a select of Compact, Comfortable and Spacious. See the Density Toggle Guide. Hidden with
enableDensityToggle: false. - Wrap cell text: a switch. Off, cell text stays on one line and is cut with an ellipsis; on, it wraps. See the Overflow Tooltip Guide.
- Grid lines: a select of None, Rows (the default), Columns and All.
- Sticky header: a switch, starting from
enableStickyHeader. See the Sticky Header Guide. - Alternating row colors: a switch for striped rows, using the
--st-stripecolor. See the Tint Tokens Guide. - Row numbers: a switch, starting from
enableRowNumbers. Turning it on adds the row numbers column pinned to the start. - Reset view: restores every row, the pins, density, column visibility, order, pinning and rows per page to the table's
initialState, and deletes the stored view when persistence is on.
Pin Items to the Toolbar
Each row has a pin toggle ("Pin to toolbar", "Unpin from toolbar"). A pinned item shows in the toolbar between the sort button and the full screen toggle, as an icon button whose icon follows its value: Columns opens the columns menu, Density and Grid lines cycle through their values, and the switches toggle. Columns and Density are pinned by default.
The pins are part of the viewSettings state, so you can change the default pins through initialState:
const table = useShadcnTable({
columns,
data,
initialState: {
viewSettings: {
gridLines: 'rows',
pins: ['columns', 'density', 'wrapText'], //also pin Wrap cell text
rowNumbers: false,
stickyHeader: false,
striped: false,
wrapText: false,
},
},
});Search, filter, sort and full screen are fixed toolbar buttons and cannot be pinned or unpinned.
Turn the View Settings Off
Set enableViewSettings: false to remove the view settings button and its pinned items. The separate columns button and density toggle return to the toolbar in their place.
const table = useShadcnTable({
columns,
data,
enableViewSettings: false,
});ST_ViewSettingsButton and ST_ViewSettingsItems (the pinned buttons) are exported and take the table as a prop, for a toolbar of your own. The panel's texts use the viewSettings, columns, columnsShown, density, wrapCellText, singleLineCells, gridLines, stickyHeader, alternatingRowColors, rowNumbers, pinToToolbar, unpinFromToolbar and resetView localization keys.
View Extra Storybook Examples