# Data table

Searchable, sortable client table with pagination, a loading skeleton and row arrows.

Section: Components / Data display
Page: https://ui.heyramzi.com/components/data-table
Import path: `@heyramzi/ui/components/data-table`

## Import

```ts
import { Pagination, RowArrow, SortableHead, TableSkeleton, formatCount, normalizeQuery, anyContainsQuery, paginateRows, sortRows, toggleSort, type SortState, } from "@heyramzi/ui/components/data-table";
import * as Table from "@heyramzi/ui/shadcn/table";
import { Input } from "@heyramzi/ui/shadcn/input";
import { Button } from "@heyramzi/ui/shadcn/button";
```

## Usage

```svelte
<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)} />
```

## Demo

```svelte
<script lang="ts">
	import {
		Pagination,
		RowArrow,
		SortableHead,
		TableSkeleton,
		formatCount,
		normalizeQuery,
		anyContainsQuery,
		paginateRows,
		sortRows,
		toggleSort,
		type SortState,
	} from "@heyramzi/ui/components/data-table";
	import * as Table from "@heyramzi/ui/shadcn/table";
	import { Input } from "@heyramzi/ui/shadcn/input";
	import { Button } from "@heyramzi/ui/shadcn/button";

	type Client = { id: string; name: string; city: string; projects: number; revenue: number };

	const clients: Client[] = [
		{ id: "c1", name: "Brightwell Legal", city: "Lyon", projects: 4, revenue: 18400 },
		{ id: "c2", name: "Acme Freight", city: "Rotterdam", projects: 2, revenue: 9600 },
		{ id: "c3", name: "Birchwood Logistics", city: "Leeds", projects: 1, revenue: 3200 },
		{ id: "c4", name: "Atelier Morel", city: "Paris", projects: 6, revenue: 27100 },
		{ id: "c5", name: "Harbor Dental", city: "Bordeaux", projects: 3, revenue: 12800 },
		{ id: "c6", name: "Kestrel Surveys", city: "Edinburgh", projects: 2, revenue: 7450 },
		{ id: "c7", name: "Maison Duval", city: "Lille", projects: 5, revenue: 21900 },
		{ id: "c8", name: "Pinewood Plumbing", city: "Bristol", projects: 1, revenue: 2800 },
		{ id: "c9", name: "Studio Verre", city: "Nantes", projects: 3, revenue: 11300 },
		{ id: "c10", name: "Tidewater Marine", city: "Antwerp", projects: 2, revenue: 8900 },
	];

	const pageSize = 4;

	let query = $state("");
	let sort = $state<SortState<Client>>({ key: "revenue", direction: "desc" });
	let page = $state(1);
	let loading = $state(false);

	let matches = $derived(
		clients.filter((c) => anyContainsQuery([c.name, c.city], normalizeQuery(query))),
	);
	let rows = $derived(paginateRows(sortRows(matches, sort), page, pageSize));

	function reload() {
		loading = true;
		setTimeout(() => (loading = false), 1200);
	}
</script>

<div class="flex w-full max-w-2xl flex-col gap-3">
	<div class="flex items-center gap-2">
		<Input
			bind:value={query}
			oninput={() => (page = 1)}
			placeholder="Search clients"
			aria-label="Search clients"
			class="max-w-56"
		/>
		<span class="text-sm tabular-nums text-muted-foreground">
			{formatCount(matches.length, clients.length, "clients")}
		</span>
		<Button variant="outline" class="ms-auto" onclick={reload}>Reload</Button>
	</div>

	<div class="overflow-hidden rounded-xl border border-border bg-card">
		{#if loading}
			<TableSkeleton rows={pageSize} columns={[{}, {}, { align: "right" }, { align: "right" }]} />
		{:else}
			<Table.Root>
				<Table.Header>
					<Table.Row>
						<SortableHead key="name" {sort} onSort={(k) => (sort = toggleSort(sort, k))}
							>Client</SortableHead
						>
						<SortableHead key="city" {sort} onSort={(k) => (sort = toggleSort(sort, k))}
							>City</SortableHead
						>
						<SortableHead key="projects" {sort} onSort={(k) => (sort = toggleSort(sort, k))} numeric
							>Projects</SortableHead
						>
						<SortableHead key="revenue" {sort} onSort={(k) => (sort = toggleSort(sort, k))} numeric
							>Revenue</SortableHead
						>
						<Table.Head class="w-8"></Table.Head>
					</Table.Row>
				</Table.Header>
				<Table.Body>
					{#each rows as row (row.id)}
						<Table.Row class="group/row">
							<Table.Cell class="font-medium">{row.name}</Table.Cell>
							<Table.Cell class="text-muted-foreground">{row.city}</Table.Cell>
							<Table.Cell class="text-right tabular-nums">{row.projects}</Table.Cell>
							<Table.Cell class="text-right tabular-nums"
								>€{row.revenue.toLocaleString("en-US")}</Table.Cell
							>
							<Table.Cell><RowArrow /></Table.Cell>
						</Table.Row>
					{:else}
						<Table.Row>
							<Table.Cell colspan={5} class="h-24 text-center text-muted-foreground"
								>No client matches "{query}"</Table.Cell
							>
						</Table.Row>
					{/each}
				</Table.Body>
			</Table.Root>
			<Pagination {page} {pageSize} total={matches.length} onPageChange={(p) => (page = p)} />
		{/if}
	</div>
</div>
```

## API

### SortableHead

| Prop | Type | Default | Notes |
| --- | --- | --- | --- |
| `key` | `keyof T & string` |  | required |
| `sort` | `SortState<T>` |  | required |
| `onSort` | `(key: keyof T & string) => void` |  | required. Called on click and on Enter/Space. Choosing the next direction is the caller's. |
| `numeric` | `boolean` | `false` | Numbers right-align to their digits, so their caret leads the label. |
| `title` | `string` |  | The long form of an abbreviated head, as the browser tooltip. |
| `class` | `string` |  |  |
| `style` | `string` |  |  |
| `children` | `Snippet` |  | required |

### TableSkeleton

| Prop | Type | Default | Notes |
| --- | --- | --- | --- |
| `rows` | `number` | `5` | Number of skeleton rows to render |
| `columns` | `ColumnDef[]` |  | Column definitions control skeleton widths and alignment |
| `columnCount` | `number` | `6` | Simple mode: just specify column count |
| `rowClass` | `string` |  | Merged onto each row, for a table whose own rows draw a different line. |
| `class` | `string` |  |  |

### RowArrow

| Prop | Type | Default | Notes |
| --- | --- | --- | --- |
| `class` | `string` |  |  |

### Pagination

| Prop | Type | Default | Notes |
| --- | --- | --- | --- |
| `page` | `number` |  | required |
| `pageSize` | `number` |  | required |
| `total` | `number` |  | required |
| `onPageChange` | `(page: number) => void` |  |  |
| `buildHref` | `(page: number) => string` |  | If provided, renders <a> tags instead of buttons (for server-side pagination) |
| `previousLabel` | `string` | `"Previous page"` | The accessible name of the icon-only previous and next buttons. |
| `nextLabel` | `string` | `"Next page"` |  |

### InfiniteScroll

| Prop | Type | Default | Notes |
| --- | --- | --- | --- |
| `items` | `T[]` |  | required |
| `isDone` | `boolean` |  | required |
| `isLoading` | `boolean` |  | required |
| `children` | `Snippet<[{ items: T[] }]>` |  | required |
| `skeleton` | `Snippet` |  | Skeleton snippet rendered while loading more |
| `onLoadMore` | `() => void` |  | required |
| `threshold` | `number` | `200` |  |
| `loadingClass` | `string` |  |  |

### RowContextMenu

| Prop | Type | Default | Notes |
| --- | --- | --- | --- |
| `onDelete` | `() => void` |  | required |
| `href` | `string` |  |  |
| `id` | `string` |  |  |
| `deleteLabel` | `string` | `"Delete"` |  |
| `copyLinkLabel` | `string` | `"Copy link"` |  |
| `copyIdLabel` | `string` | `"Copy ID"` |  |
| `onCopied` | `() => void` |  | Called after a successful clipboard copy so consumers can show their own toast |
| `children` | `Snippet<[Record<string, unknown>]>` |  | required |
