Guides
Row Pinning Guide
How to pin rows to the top or bottom of the table, while scrolling or always, in Shadcn Table
Shadcn Table has a powerful row pinning feature that allows you to either pin rows to the top and bottom of the table as a user scrolls, or pin rows to the top and bottom of the table regardless of scrolling.
Example
Relevant Table Options
| Prop Name | Type | Description |
|---|---|---|
| enableRowSelection | boolean | (row: ST_Row) => boolean | Enables row selection either for every row, or on a per-row basis using a function. |
| keepPinnedRows | boolean | |
| onRowPinningChange | OnChangeFn<RowSelectionState> | If provided, this function will be called with an updaterFn when state.rowPinning changes. This overrides the default internal state management, so you will need to persist the state change either fully or partially outside of the table. |
| rowPinningDisplayMode | 'sticky' | 'top' | 'bottom' | 'top-and-bottom' | 'select-sticky' | 'select-top' | 'select-bottom' |
Relevant State
| State Option | Type | Description |
|---|---|---|
| rowPinning | { top: string[], bottom: string[] } | an object with top and bottom properties, each of which is an array of strings representing the rows that are pinned to the top or bottom of the table |
Row Pinning Display Modes
You can enable the row pinning feature with the enableRowPinning table option. There are multiple display modes that you can choose from with the rowPinningDisplayMode table option.
const table = useShadcnTable({
columns,
data,
enableRowPinning: true,
rowPinningDisplayMode: 'sticky', // default
});sticky- This is the default display mode. It will make rows stick to the top and bottom of the table as a user scrolls.top- This will statically pin rows to the top of the table upon pinning.bottom- This will statically pin rows to the bottom of the table upon pinning.top-and-bottom- This allows the user to statically pin rows to either the top or bottom of the table.select-sticky- This pins selected rows to the top and bottom of the table as a user scrolls. (alternative pinning UI)select-top- This statically pins selected rows to the top of the table upon pinning. (alternative pinning UI)select-bottom- This statically pins selected rows to the bottom of the table upon pinning. (alternative pinning UI)
Sticky Row Pinning
By default, the row pinning feature will add a position sticky style to the pinned rows. This means that the pinned rows will stay in place if the row is already in view. However, the rows will stay in view as the user scrolls up or down.
Sticky row pinning will work best if you give your rows a fixed height, or at least disable any kind of whitespace wrapping.
const table = useShadcnTable({
columns,
data,
enableRowPinning: true,
stTableBodyRowProps: ({ row, table }) => {
const density = table.atoms.density.get();
return {
style: {
//Set a fixed height for pinned rows
height: row.getIsPinned()
? `${
//The library's row heights per density. Adjust as needed.
density === 'compact' ? 40 : density === 'comfortable' ? 48 : 56
}px`
: undefined,
},
};
},
rowPinningDisplayMode: 'sticky', // default
});Static Row Pinning
Some of the other row pinning modes will show pinned rows at the top or bottom of the the table regardless of scrolling. This is a simpler implementation of the row pinning feature.
Static row pinning does not require set row heights, as they will render in a normal <tbody> element that itself is what is sticky.
Alternative Selection Row Pinning UI
By default, the row pinning feature adds a pinning display column with pinning buttons for each row. However, you can instead combine the row pinning feature with the row selection feature to pin selected rows.
const table = useShadcnTable({
columns,
data,
enableRowPinning: true,
enableRowSelection: true,
rowPinningDisplayMode: 'select-sticky',
});