Data table
Searchable, sortable client table with pagination, a loading skeleton and row arrows.
| Atelier Morel | Paris | 6 | €27,100 | |
| Maison Duval | Lille | 5 | €21,900 | |
| Brightwell Legal | Lyon | 4 | €18,400 | |
| Harbor Dental | Bordeaux | 3 | €12,800 |
Installation
pnpm add @heyramzi/uiUsage
import {
Pagination,
RowArrow,
SortableHead,
TableSkeleton,
formatCount,
normalizeQuery,
anyContainsQuery,
paginateRows,
sortRows,
toggleSort,
type SortState,
} from "@heyramzi/ui/components/data-table";<table>
<thead>
<tr>
<SortableHead key="name" {sort} onSort={(key) => (sort = toggleSort(sort, key))}>Client</SortableHead>
<SortableHead key="revenue" {sort} onSort={(key) => (sort = toggleSort(sort, key))} numeric>Revenue</SortableHead>
</tr>
</thead>
<tbody>
{#each paginateRows(sortRows(clients, sort), page, pageSize) as row (row.id)}
<tr><td>{row.name}</td><td>{row.revenue}</td></tr>
{/each}
</tbody>
</table>
<Pagination {page} {pageSize} total={clients.length} onPageChange={(p) => (page = p)} />Keyboard and accessibility
| Key | Does |
|---|---|
| Tab | Moves focus to the next column's sort button, then into the rows. |
| Space Enter | On a sort button, sorts by that column. Pressing again flips the order. |
| Shift F10 ContextMenu | On a row in a RowContextMenu, or anything focused inside it, opens the row menu. |
- A sortable head holds a real button, and the head reports ascending or descending to a screen reader.
- The pager's previous and next buttons are named by previousLabel and nextLabel.
- The row menu also opens on right-click or a long press. Give the row a link or button so the keyboard can reach it.
API reference
SortableHead
keyrequiredkeyof T & stringsortrequiredSortState<T>onSortrequired(key: keyof T & string) => void- Called on click and on Enter/Space. Choosing the next direction is the caller's.
numericboolean- Default
false - Numbers right-align to their digits, so their caret leads the label.
titlestring- The long form of an abbreviated head, as the browser tooltip.
classstringstylestringchildrenrequiredSnippet
| Prop | Type | Default |
|---|---|---|
key required | keyof T & string | |
sort required | SortState<T> | |
onSort required Called on click and on Enter/Space. Choosing the next direction is the caller's. | (key: keyof T & string) => void | |
numeric Numbers right-align to their digits, so their caret leads the label. | boolean | false |
title The long form of an abbreviated head, as the browser tooltip. | string | |
class | string | |
style | string | |
children required | Snippet |
TableSkeleton
rowsnumber- Default
5 - Number of skeleton rows to render
columnsColumnDef[]- Column definitions control skeleton widths and alignment
columnCountnumber- Default
6 - Simple mode: just specify column count
rowClassstring- Merged onto each row, for a table whose own rows draw a different line.
classstring
| Prop | Type | Default |
|---|---|---|
rows Number of skeleton rows to render | number | 5 |
columns Column definitions control skeleton widths and alignment | ColumnDef[] | |
columnCount Simple mode: just specify column count | number | 6 |
rowClass Merged onto each row, for a table whose own rows draw a different line. | string | |
class | string |
Pagination
pagerequirednumberpageSizerequirednumbertotalrequirednumberonPageChange(page: number) => voidbuildHref(page: number) => string- If provided, renders <a> tags instead of buttons (for server-side pagination)
previousLabelstring- Default
"Previous page" - The accessible name of the icon-only previous and next buttons.
nextLabelstring- Default
"Next page"
| Prop | Type | Default |
|---|---|---|
page required | number | |
pageSize required | number | |
total required | number | |
onPageChange | (page: number) => void | |
buildHref If provided, renders <a> tags instead of buttons (for server-side pagination) | (page: number) => string | |
previousLabel The accessible name of the icon-only previous and next buttons. | string | "Previous page" |
nextLabel | string | "Next page" |
InfiniteScroll
itemsrequiredT[]isDonerequiredbooleanisLoadingrequiredbooleanchildrenrequiredSnippet<[{ items: T[] }]>skeletonSnippet- Skeleton snippet rendered while loading more
onLoadMorerequired() => voidthresholdnumber- Default
200 loadingClassstring
| Prop | Type | Default |
|---|---|---|
items required | T[] | |
isDone required | boolean | |
isLoading required | boolean | |
children required | Snippet<[{ items: T[] }]> | |
skeleton Skeleton snippet rendered while loading more | Snippet | |
onLoadMore required | () => void | |
threshold | number | 200 |
loadingClass | string |
RowContextMenu
onDeleterequired() => voidhrefstringidstringdeleteLabelstring- Default
"Delete" copyLinkLabelstring- Default
"Copy link" copyIdLabelstring- Default
"Copy ID" onCopied() => void- Called after a successful clipboard copy so consumers can show their own toast
childrenrequiredSnippet<[Record<string, unknown>]>
| Prop | Type | Default |
|---|---|---|
onDelete required | () => void | |
href | string | |
id | string | |
deleteLabel | string | "Delete" |
copyLinkLabel | string | "Copy link" |
copyIdLabel | string | "Copy ID" |
onCopied Called after a successful clipboard copy so consumers can show their own toast | () => void | |
children required | Snippet<[Record<string, unknown>]> |