Table
Rows and columns of data that sort, select, pin and scroll both ways. The parts draw the table; TanStack Table supplies the rows, state and column sizes.
Usage
import {
Table,
TableBody,
TableCell,
TableContainer,
TableHead,
TableHeader,
TableRow,
TableViewport,
} from 'nyctoui/table'
<TableContainer>
<TableViewport>
<Table>
<TableHeader>
<TableRow>
<TableHead>Company</TableHead>
<TableHead align="end">Seats</TableHead>
</TableRow>
</TableHeader>
<TableBody>
<TableRow>
<TableCell>Northwind</TableCell>
<TableCell align="end">48</TableCell>
</TableRow>
</TableBody>
</Table>
</TableViewport>
</TableContainer>The parts render real table elements and own the look: density, dividers, row states, sticky header and pinned edges. They hold no data and no state, so any source works. The examples use @tanstack/react-table (v9) for sorting, filtering, selection, pagination and column sizes, and pass its values to the parts as props.
Every row is exactly one row height, 32, 40 or 48px by density, and cells stay on one line. That keeps a long table easy to scan, lets rows pin at known offsets, and lets a virtualizer place rows without measuring them. Text that doesn’t fit truncates in a table with a width, and widens the column in one without.
Good for
- A list of customers in a CRM, sortable by name, plan and last seen.
- Transactions in a banking or a finance app.
- Orders in a store admin, with selection for bulk actions.
- Usage per project in a cloud console, with pinned totals.
Anatomy
<TableContainer>
<TableToolbar /> // optional, above or below
<TableViewport>
<Table>
<TableHeader>
<TableRow>
<TableHead>
<TableHeadButton />
</TableHead>
</TableRow>
</TableHeader>
<TableBody>
<TableRow>
<TableCell>
<TableActions />
</TableCell>
</TableRow>
<TableEmpty /> // in place of the rows
</TableBody>
<TableFooter />
</Table>
</TableViewport>
</TableContainer>With TanStack Table
import {
columnSizingFeature,
createColumnHelper,
tableFeatures,
useTable,
} from '@tanstack/react-table'
import { columnProps } from 'nyctoui/table'
const features = tableFeatures({ columnSizingFeature })
const helper = createColumnHelper<typeof features, Customer>()
const columns = helper.columns([
helper.accessor('company', { header: 'Company', size: 200, meta: { grow: true } }),
helper.accessor('seats', { header: 'Seats', size: 90, meta: { align: 'end' } }),
])
const table = useTable({ features, columns, data })
<Table width={table.getTotalSize()}>
…
<TableCell {...columnProps(cell.column)}>
<table.FlexRender cell={cell} />
</TableCell>
</Table>columnProps(column) turns a TanStack column into cell props: its width, its pinned side and offset, whether it is the edge of the pinned ones, and the align from its meta. Spread it on the column’s TableHead and every TableCell, so header, cells and pinned offsets agree to the pixel.
Pass table.getTotalSize() as the table’s width for a fixed layout. Where the container is wider than the columns, the one column with meta: { grow: true } takes the rest; where it’s narrower, the table scrolls sideways. Keep features and columns at module level, as TanStack asks, so its row models don’t rebuild on every render.
Scrolling
TableViewport scrolls both ways with thin overlay scrollbars that show on hover. It grows with its rows until you cap it: give it a maxHeight, or a height in a layout that sizes it, through sx. The header sticks to the top while the body scrolls; set sticky={false} on TableHeader to let it scroll away.
On a narrow screen the table keeps its columns and scrolls sideways inside the card, rather than squeezing text or stacking cells. Pin the column that names each row so it stays in view, and the toolbar wraps onto a second line when it runs out of room.
Examples
Pinned columns
columnPinningFeature with the company pinned to the start and the row menu to the end. Scroll sideways: the pinned columns stay, and a divider and shadow appear on the side content passes under. Open a column’s header to pin it to either side; TableHeadButton doubles as the menu trigger.
Pinned rows
rowPinningFeature splits rows into top, center and bottom. Render each group in order and pass pinned and pinOffset to its rows: top rows stack under the header, bottom rows at the foot of the viewport. Pin and unpin from the button at the end of a row.
Selection
rowSelectionFeature with a checkbox column. The header box selects all and shows a dash for a partial selection. Selected rows tint through selected, and the toolbar below turns into actions for the whole selection.
Pagination
rowPaginationFeature with a page size select and previous and next buttons in a bottom toolbar. The range is announced as it changes.
Density and columns
density switches the row height for the whole table. columnVisibilityFeature backs a menu of columns to show; render row.getVisibleCells() so hidden ones drop out.
Dashboard
A report table under a row of stats: a title and period menu in the toolbar, sortable numeric columns with an inline meter, info tooltips on the columns that need explaining, change badges, and a TableFooter of totals.
Virtualized
Ten thousand log lines, of which only the visible ones and a few either side are in the DOM. viewportRef hands the scrolling element to @tanstack/react-virtual; empty rows above and below the rendered ones hold the scroll height. Fixed row heights mean nothing has to be measured.
Accessibility
The parts render a native <table>, so screen readers announce rows, columns and headers without extra roles. TableHead sets scope="col", and aria-sort when given a sort. Name icon-only buttons in rows after their row, such as “Actions for NYC-204”, so a list of them reads apart. TableActions hides its controls only visually: they stay in the tab order, and show as soon as the row holds focus.
Best practices
- Pick the
densityfrom how people read the data: compact for scanning many rows, comfortable for a few rich ones. - Make a row open with a link or a button in its main cell. A click on the whole row can’t be reached by keyboard.
- Show a
TableEmptythat says why there are no rows and how to clear filters. - Keep IDs, emails and other long values on one line, and give their columns enough width.
- Show the data as a list on phones when it’s read mostly there.
- Use a table for items that don’t share the same columns. Use a list or cards.
- Put a status badge in every row. Use plain text and badge only the exceptions.
- Put a button or a filter inside the header of a sortable column; it ends up inside the sort button.
- Turn on every feature at once. Add sorting, pinning or selection when the task needs it.
API reference
TableContainer
The card: surface, ring and rounded corners. Renders a <div>.
| Prop | Type | Default |
|---|---|---|
renderElement to render instead, keeping the styles. | ReactElement | (props, state) => ReactElement | — |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
TableViewport
Scrolls the table both ways with overlay scrollbars. Renders a <div>; cap its height with sx.
| Prop | Type | Default |
|---|---|---|
viewportRefThe scrolling element, for a virtualizer. ref reaches the outer frame. | Ref<HTMLDivElement> | — |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
TableToolbar
A row of controls above or below the table, with a divider on the side touching it. Renders a <div>.
| Prop | Type | Default |
|---|---|---|
renderElement to render instead, keeping the styles. | ReactElement | (props, state) => ReactElement | — |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
TableToolbarText
Quiet 12px text for a toolbar, such as a count. Renders a <span>.
| Prop | Type | Default |
|---|---|---|
renderElement to render instead, keeping the styles. | ReactElement | (props, state) => ReactElement | — |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
Table
Renders a <table>.
| Prop | Type | Default |
|---|---|---|
densityRow height: 32, 40 or 48px. | 'compact' | 'regular' | 'comfortable' | 'regular' |
widthTotal width in px, such as table.getTotalSize(). Switches to a fixed layout; still fills the container. | number | — |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
TableHeader
Renders a <thead>.
| Prop | Type | Default |
|---|---|---|
stickyKeeps the header in view while the viewport scrolls. | boolean | true |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
TableBody
Renders a <tbody>.
| Prop | Type | Default |
|---|---|---|
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
TableFooter
Renders a <tfoot>, in medium weight, for totals.
| Prop | Type | Default |
|---|---|---|
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
TableRow
Renders a <tr>. Body rows highlight on hover.
| Prop | Type | Default |
|---|---|---|
selectedTints the row and sets data-selected. | boolean | false |
pinnedSticks the row under the header or to the foot of the viewport. | 'top' | 'bottom' | false | — |
pinOffsetPinned rows between this one and its edge, so stacked rows don’t overlap. | number | 0 |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
TableHead
Renders a <th> with scope col.
| Prop | Type | Default |
|---|---|---|
alignText alignment. Use end for numbers, so their digits line up. | 'start' | 'center' | 'end' | 'start' |
widthWidth in px. Holds exactly in a table with a width. | number | — |
pinnedSticks the cell to that side while the table scrolls sideways. | 'start' | 'end' | false | — |
offsetDistance in px from the pinned side: the widths of the pinned columns before (start) or after (end) it. | number | 0 |
edgeMarks the pinned column next to the scrolling ones, which shows a divider and shadow while content scrolls under it. | boolean | false |
sortThe column’s sort. Sets aria-sort and the arrow of a TableHeadButton inside. Leave it out on columns that don’t sort. | 'asc' | 'desc' | false | — |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
TableHeadButton
A column label as a button, for sorting or as a menu trigger. Renders a <button>.
| Prop | Type | Default |
|---|---|---|
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
TableCell
Renders a <td>. Stays on one line.
| Prop | Type | Default |
|---|---|---|
alignText alignment. Use end for numbers, so their digits line up. | 'start' | 'center' | 'end' | 'start' |
widthWidth in px. Holds exactly in a table with a width. | number | — |
pinnedSticks the cell to that side while the table scrolls sideways. | 'start' | 'end' | false | — |
offsetDistance in px from the pinned side: the widths of the pinned columns before (start) or after (end) it. | number | 0 |
edgeMarks the pinned column next to the scrolling ones, which shows a divider and shadow while content scrolls under it. | boolean | false |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
TableActions
Row controls, hidden until the row is hovered or focused, or while their menu is open. Renders a <div>.
| Prop | Type | Default |
|---|---|---|
renderElement to render instead, keeping the styles. | ReactElement | (props, state) => ReactElement | — |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
TableEmpty
One full-width row of centered, muted text in place of the rows. Renders a <tr>.
| Prop | Type | Default |
|---|---|---|
colSpanThe number of columns, so the cell spans them all. | number | — |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
columnProps
A function, not a part: reads a TanStack column into TableHead and TableCell props.
| Prop | Type | Default |
|---|---|---|
columnA TanStack Table column. Reads getSize, getIsPinned, getStart and getAfter when their features are registered, and meta.align and meta.grow. | Column | — |