# Table

Table of rows and columns. `columns` declares what each column shows (`key`, `label`, a `type` that formats numbers, dates and yes/no with `Intl` in the language of `locale`, `format`, or a `cell` snippet) and `rows` are plain objects, identified by `rowKey`. Sortable columns sort the rows on their own (ascending, descending, unsorted), or only report the new `sort` with `sorting="manual"` when the server sorts. `selectable` adds a checkbox per row (shift-click selects a range), `expandedSnippet` a detail row under each row, `actionsSnippet` a column of buttons that stays on the right while scrolling (with `actionsVisibility="hover"` it shows only on hover and focus). `rowHref` turns every row into a link (Ctrl-click and middle click open it in a new tab), `onrowclick` makes rows clickable and focusable. `pinned` columns stay on the left (unless together with the checkboxes and the actions they would take more than half of the table), `resizable` columns can be dragged or resized with the arrow keys, and `columnState` keeps order, visibility, widths and pins as plain data to save. Empty cells show "—". When the table is narrower than `cardBreakpoint` (640px by default) every row becomes a card, with the `cardColumns` (the first 4 by default) and the other columns behind "Show more fields". Above `virtualize` rows (200 by default) only the rows in view are in the page, so thousands of rows stay fast. `onendreached` is called when the last rows come into view, for loading more. While `loading` the table shows a progress bar, or placeholder rows when it has none; with no rows it shows `NoData`. State is exposed as `data-cards`, `data-loading`, `data-disabled`, `data-resizing`, `data-actions-visibility` and `data-pins-off` on the root, and `data-selected`, `data-expanded` on rows for app CSS.

```ts
import { Table } from '@likable-hair/svelte';
```

Category: simple. Source: `src/lib/components/simple/tables/Table.svelte`.

## Examples

### Basic table

`columns` declares what each column shows and `rows` are plain objects with an `id`. A `number` column is right-aligned, with monospaced digits formatted by `Intl.NumberFormat`; a `cell` snippet draws anything else, here a chip. Sortable headers sort the rows: ascending, descending, then back to the original order.

```svelte
<script lang="ts">
	import { Chip, Table, type TableColumn } from '@likable-hair/svelte';

	type Product = { id: number; name: string; stock: number; price: number };

	const rows: Product[] = [
		{ id: 1, name: 'Nourishing shampoo', stock: 42, price: 14.9 },
		{ id: 2, name: 'Keratin mask', stock: 4, price: 22.5 },
		{ id: 3, name: 'Argan oil', stock: 0, price: 18 },
		{ id: 4, name: 'Volume mousse', stock: 17, price: 12.4 }
	];

	const columns: TableColumn<Product>[] = [
		{ key: 'name', label: 'Product', sortable: true },
		{ key: 'stock', label: 'Stock', sortable: true, cell: stock },
		{
			key: 'price',
			label: 'Price',
			type: 'number',
			numberFormat: { style: 'currency', currency: 'EUR' },
			sortable: true
		}
	];
</script>

{#snippet stock({ value }: { value: number })}
	<Chip size="sm" variant={value === 0 ? 'error' : value < 10 ? 'warning' : 'success'}>{value}</Chip>
{/snippet}

<Table {rows} {columns} caption="Products" />
```

### Column types

`type` formats the value with `Intl`, in the language of `locale`: `date` and `datetime` take a `Date`, an ISO string, a timestamp or a luxon `DateTime`, `number` takes the options of `Intl.NumberFormat`, `boolean` shows a check mark for true. `format` writes the text by hand. Empty values show "—", or the `empty` text of the column.

```svelte
<script lang="ts">
	import { Table, type TableColumn } from '@likable-hair/svelte';

	type Visit = {
		id: number;
		client: string;
		day: Date;
		booked: string;
		minutes: number;
		paid: boolean;
		notes: string | null;
	};

	const rows: Visit[] = [
		{
			id: 1,
			client: 'Luca Rossi',
			day: new Date(2026, 8, 24),
			booked: '2026-09-20T09:30:00',
			minutes: 45,
			paid: true,
			notes: 'Prefers the morning'
		},
		{
			id: 2,
			client: 'Giulia Marini',
			day: new Date(2026, 8, 25),
			booked: '2026-09-21T16:05:00',
			minutes: 120,
			paid: false,
			notes: null
		},
		{
			id: 3,
			client: 'Sara Bianchi',
			day: new Date(2026, 8, 26),
			booked: '2026-09-22T11:40:00',
			minutes: 30,
			paid: true,
			notes: ''
		}
	];

	const columns: TableColumn<Visit>[] = [
		{ key: 'client', label: 'Client' },
		{ key: 'day', label: 'Day', type: 'date', dateFormat: { weekday: 'short', day: 'numeric', month: 'short' } },
		{ key: 'booked', label: 'Booked on', type: 'datetime' },
		{ key: 'minutes', label: 'Duration', type: 'number', format: (value) => `${value} min` },
		{ key: 'paid', label: 'Paid', type: 'boolean', align: 'center' },
		{ key: 'notes', label: 'Notes', empty: 'No notes' }
	];
</script>

<Table {rows} {columns} />
```

### Selection and row actions

`selectable="multiple"` adds a checkbox to each row and "select all" in the header; shift-click selects a range. `bind:selected` holds the selected rows. `actionsSnippet` puts buttons in a column that stays on the right while the table scrolls; `actionsVisibility="hover"` shows them only on the row under the pointer or with the focus, for dense tables.

```svelte
<script lang="ts">
	import { mdiDotsVertical, mdiPencilOutline } from '@mdi/js';
	import { Button, Chip, Table, type TableColumn } from '@likable-hair/svelte';

	type Booking = {
		id: number;
		client: string;
		initials: string;
		service: string;
		start: Date;
		status: 'confirmed' | 'pending' | 'cancelled';
		amount: number;
	};

	const rows: Booking[] = [
		{ id: 1, client: 'Luca Rossi', initials: 'LR', service: 'Cut and beard', start: new Date(2026, 8, 24, 9, 30), status: 'confirmed', amount: 42 },
		{ id: 2, client: 'Giulia Marini', initials: 'GM', service: 'Balayage', start: new Date(2026, 8, 24, 11), status: 'pending', amount: 120 },
		{ id: 3, client: 'Sara Bianchi', initials: 'SB', service: 'Blow-dry', start: new Date(2026, 8, 24, 15, 15), status: 'cancelled', amount: 25 },
		{ id: 4, client: 'Marco Neri', initials: 'MN', service: 'Cut', start: new Date(2026, 8, 24, 17), status: 'confirmed', amount: 28 }
	];

	const statuses = {
		confirmed: { label: 'Confirmed', variant: 'success' },
		pending: { label: 'Pending', variant: 'warning' },
		cancelled: { label: 'Cancelled', variant: 'error' }
	} as const;

	const columns: TableColumn<Booking>[] = [
		{ key: 'client', label: 'Client', sortable: true, cell: client },
		{ key: 'service', label: 'Service' },
		{ key: 'start', label: 'Date', type: 'datetime', sortable: true },
		{ key: 'status', label: 'Status', cell: status },
		{ key: 'amount', label: 'Amount', type: 'number', numberFormat: { style: 'currency', currency: 'EUR' } }
	];

	let selected = $state<Booking[]>([rows[0]]);
</script>

{#snippet client({ row }: { row: Booking })}
	<span class="who"><span class="avatar">{row.initials}</span>{row.client}</span>
{/snippet}

{#snippet status({ row }: { row: Booking })}
	<Chip size="sm" variant={statuses[row.status].variant}>{statuses[row.status].label}</Chip>
{/snippet}

{#snippet actions({ row }: { row: Booking })}
	<Button buttonType="icon" variant="secondary" size="sm" icon={mdiPencilOutline} aria-label="Edit {row.client}" />
	<Button buttonType="icon" variant="secondary" size="sm" icon={mdiDotsVertical} aria-label="More for {row.client}" />
{/snippet}

<div class="stack">
	<Table {rows} {columns} selectable="multiple" bind:selected actionsSnippet={actions} />
	<p>{selected.length} selected: {selected.map((row) => row.client).join(', ') || 'none'}</p>
</div>

<style>
	.stack {
		display: flex;
		width: 100%;
		flex-direction: column;
		gap: 12px;
	}

	.who {
		display: inline-flex;
		align-items: center;
		gap: 10px;
	}

	.avatar {
		display: inline-grid;
		place-items: center;
		width: 28px;
		height: 28px;
		border-radius: 50%;
		background: var(--global-color-primary-soft);
		color: var(--global-color-primary);
		font-size: 11px;
		font-weight: 600;
	}

	p {
		margin: 0;
		color: var(--global-color-text-2);
		font-size: 13px;
	}
</style>
```

### Expandable rows

`expandedSnippet` adds a button that opens a detail row under the row; `expandable` chooses which rows have one. `bind:expanded` holds the keys of the open rows. A nested `Table` inside the snippet shows sub-rows.

```svelte
<script lang="ts">
	import { Table, type TableColumn } from '@likable-hair/svelte';

	type Client = { id: number; name: string; visits: number; notes?: string };

	const rows: Client[] = [
		{ id: 1, name: 'Luca Rossi', visits: 12, notes: 'Allergic to PPD: use the ammonia-free line. Last colour 6.3.' },
		{ id: 2, name: 'Giulia Marini', visits: 4 },
		{ id: 3, name: 'Sara Bianchi', visits: 7, notes: 'Likes short appointments, books online.' }
	];

	const columns: TableColumn<Client>[] = [
		{ key: 'name', label: 'Client' },
		{ key: 'visits', label: 'Visits', type: 'number' }
	];

	let expanded = $state<(string | number)[]>([1]);
</script>

{#snippet detail({ row }: { row: Client })}
	{row.notes}
{/snippet}

<Table {rows} {columns} expandedSnippet={detail} expandable={(row) => !!row.notes} bind:expanded />
```

### Rows as links

`rowHref` turns every row into a link: a click opens it, Ctrl-click and middle click open it in a new tab, and a real link inside the row reaches it from the keyboard. Clicks on buttons, links and fields inside the row are left alone. `onrowclick` alone makes the rows clickable and focusable, for actions that are not a page.

```svelte
<script lang="ts">
	import { Table, type TableColumn } from '@likable-hair/svelte';

	type Invoice = { id: number; number: string; client: string; total: number };

	const rows: Invoice[] = [
		{ id: 1, number: 'INV-2026-031', client: 'Luca Rossi', total: 42 },
		{ id: 2, number: 'INV-2026-032', client: 'Giulia Marini', total: 120 },
		{ id: 3, number: 'INV-2026-033', client: 'Sara Bianchi', total: 25 }
	];

	const columns: TableColumn<Invoice>[] = [
		{ key: 'number', label: 'Invoice' },
		{ key: 'client', label: 'Client' },
		{ key: 'total', label: 'Total', type: 'number', numberFormat: { style: 'currency', currency: 'EUR' } }
	];

	let clicked = $state('none');
</script>

<div class="stack">
	<Table {rows} {columns} rowHref={(row) => `#invoice-${row.id}`} />
	<Table {rows} {columns} onrowclick={(row) => (clicked = row.number)} />
	<p>Last clicked: {clicked}</p>
</div>

<style>
	.stack {
		display: flex;
		width: 100%;
		flex-direction: column;
		gap: 16px;
	}

	p {
		margin: 0;
		color: var(--global-color-text-2);
		font-size: 13px;
	}
</style>
```

### Pinned and resizable columns

`pinned` columns stay on the left while the table scrolls sideways, and are moved before the others. With `resizable` the edge of each header can be dragged, or moved with the arrow keys when it has the focus (Shift for bigger steps). `bind:columnState` holds order, visibility, widths and pins as plain data, ready to be saved; `columnsKey` saves it in the browser instead. `cardBreakpoint={false}` keeps the table on narrow screens.

```svelte
<script lang="ts">
	import { Table, type TableColumn, type TableColumnState } from '@likable-hair/svelte';

	type Shift = { id: number; name: string; [day: string]: number | string };

	const days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
	const rows: Shift[] = ['Giulia', 'Luca', 'Sara', 'Marco'].map((name, index) => ({
		id: index + 1,
		name,
		...Object.fromEntries(days.map((day, position) => [day, ((index + 2) * (position + 3)) % 11]))
	}));

	const columns: TableColumn<Shift>[] = [
		{ key: 'name', label: 'Operator', pinned: true, minWidth: '120px' },
		...days.map((day): TableColumn<Shift> => ({ key: day, label: day, type: 'number', minWidth: '110px' }))
	];

	let columnState = $state<TableColumnState[]>();
</script>

<div class="stack">
	<Table {rows} {columns} resizable bind:columnState cardBreakpoint={false} />
	<code>{JSON.stringify(columnState?.filter((column) => column.width) ?? 'not changed yet')}</code>
</div>

<style>
	.stack {
		display: flex;
		width: 100%;
		flex-direction: column;
		gap: 12px;
		max-width: 560px;
	}

	code {
		color: var(--global-color-text-2);
		font-family: var(--global-font-family-mono);
		font-size: 12px;
		overflow-wrap: anywhere;
	}
</style>
```

### Footer and totals

`footer` adds a row at the bottom of the table: a text, or a snippet that receives every row, for example to show a total formatted like the column.

```svelte
<script lang="ts">
	import { Table, type TableColumn } from '@likable-hair/svelte';

	type Line = { id: number; service: string; quantity: number; amount: number };

	const rows: Line[] = [
		{ id: 1, service: 'Cut', quantity: 18, amount: 504 },
		{ id: 2, service: 'Colour', quantity: 9, amount: 765 },
		{ id: 3, service: 'Blow-dry', quantity: 23, amount: 575 }
	];

	const euro = new Intl.NumberFormat('en', { style: 'currency', currency: 'EUR' });

	const columns: TableColumn<Line>[] = [
		{ key: 'service', label: 'Service', footer: 'Total' },
		{ key: 'quantity', label: 'Quantity', type: 'number', footer: quantity },
		{ key: 'amount', label: 'Amount', type: 'number', numberFormat: { style: 'currency', currency: 'EUR' }, footer: amount }
	];
</script>

{#snippet quantity({ rows }: { rows: Line[] })}
	{rows.reduce((sum, row) => sum + row.quantity, 0)}
{/snippet}

{#snippet amount({ rows }: { rows: Line[] })}
	{euro.format(rows.reduce((sum, row) => sum + row.amount, 0))}
{/snippet}

<Table {rows} {columns} />
```

### Loading and no data

While `loading` the table shows a progress bar under the header and dims the rows, or placeholder rows when it has none yet (`loadingRows`). Without rows it shows `NoData`, with `noDataTitle` or `noDataSnippet`.

```svelte
<script lang="ts">
	import { Switch, Table, type TableColumn } from '@likable-hair/svelte';

	type Product = { id: number; name: string; price: number };

	const all: Product[] = [
		{ id: 1, name: 'Nourishing shampoo', price: 14.9 },
		{ id: 2, name: 'Keratin mask', price: 22.5 }
	];

	const columns: TableColumn<Product>[] = [
		{ key: 'name', label: 'Product' },
		{ key: 'price', label: 'Price', type: 'number', numberFormat: { style: 'currency', currency: 'EUR' } }
	];

	let loading = $state(false);
	let empty = $state(false);
</script>

<div class="stack">
	<div class="controls">
		<Switch bind:checked={loading} label="Loading" />
		<Switch bind:checked={empty} label="No rows" />
	</div>
	<Table rows={empty ? [] : all} {columns} {loading} loadingRows={2} noDataTitle="No products yet" />
</div>

<style>
	.stack {
		display: flex;
		width: 100%;
		flex-direction: column;
		gap: 12px;
	}

	.controls {
		display: flex;
		gap: 24px;
	}
</style>
```

### Cards on narrow screens

When the table is narrower than `cardBreakpoint` (640px by default, measured on the table, not on the screen) every row becomes a card: the `cardTitle` column on top with the checkbox and the actions, the other columns as label and value, and the sorting in a select above. `cardBreakpoint={false}` keeps the table, which then scrolls sideways.

```svelte
<script lang="ts">
	import { mdiPencilOutline } from '@mdi/js';
	import { Button, Chip, Table, type TableColumn } from '@likable-hair/svelte';

	type Booking = { id: number; client: string; service: string; start: Date; status: string; amount: number };

	const rows: Booking[] = [
		{ id: 1, client: 'Luca Rossi', service: 'Cut and beard', start: new Date(2026, 8, 24, 9, 30), status: 'Confirmed', amount: 42 },
		{ id: 2, client: 'Giulia Marini', service: 'Balayage', start: new Date(2026, 8, 24, 11), status: 'Pending', amount: 120 }
	];

	const columns: TableColumn<Booking>[] = [
		{ key: 'client', label: 'Client', sortable: true },
		{ key: 'service', label: 'Service' },
		{ key: 'start', label: 'Date', type: 'datetime', sortable: true },
		{ key: 'status', label: 'Status', cell: status },
		{ key: 'amount', label: 'Amount', type: 'number', numberFormat: { style: 'currency', currency: 'EUR' } }
	];

	let selected = $state<Booking[]>([]);
</script>

{#snippet status({ value }: { value: string })}
	<Chip size="sm" variant={value === 'Confirmed' ? 'success' : 'warning'}>{value}</Chip>
{/snippet}

{#snippet actions({ row }: { row: Booking })}
	<Button buttonType="icon" variant="secondary" size="sm" icon={mdiPencilOutline} aria-label="Edit {row.client}" />
{/snippet}

<div class="phone">
	<Table {rows} {columns} selectable="multiple" bind:selected actionsSnippet={actions} />
</div>

<style>
	.phone {
		width: 100%;
		max-width: 360px;
	}
</style>
```

### Many columns in cards

With many columns a card shows the title and a few fields, and the others behind "Show more fields". `cardColumns` picks the fields that stay visible, here the contact details, the last visit and the status; without it the card shows the first 4 columns after the title. In cards a false yes/no reads "No".

```svelte
<script lang="ts">
	import { mdiDotsVertical, mdiPencilOutline } from '@mdi/js';
	import { Button, Chip, Table, type TableColumn } from '@likable-hair/svelte';

	type Client = {
		id: number;
		code: string;
		name: string;
		email: string;
		phone: string;
		city: string;
		province: string;
		country: string;
		vat: string | null;
		fiscalCode: string;
		created: Date;
		lastVisit: Date;
		visits: number;
		spent: number;
		ticket: number;
		points: number;
		newsletter: boolean;
		marketing: boolean;
		status: 'Active' | 'New';
		operator: string;
		notes: string | null;
	};

	const rows: Client[] = [
		{
			id: 1,
			code: 'CL-00231',
			name: 'Luca Rossi',
			email: 'luca.rossi@example.com',
			phone: '+39 333 123 4567',
			city: 'Milan',
			province: 'MI',
			country: 'Italy',
			vat: 'IT01234567890',
			fiscalCode: 'RSSLCU85M01F205X',
			created: new Date(2024, 2, 14),
			lastVisit: new Date(2026, 8, 24, 9, 30),
			visits: 37,
			spent: 1842.5,
			ticket: 49.8,
			points: 1240,
			newsletter: true,
			marketing: false,
			status: 'Active',
			operator: 'Giulia',
			notes: 'Prefers mornings'
		},
		{
			id: 2,
			code: 'CL-00232',
			name: 'Giulia Marini',
			email: 'giulia.marini@example.com',
			phone: '+39 347 765 4321',
			city: 'Turin',
			province: 'TO',
			country: 'Italy',
			vat: null,
			fiscalCode: 'MRNGLI90A41L219K',
			created: new Date(2025, 10, 2),
			lastVisit: new Date(2026, 8, 20, 16),
			visits: 8,
			spent: 960,
			ticket: 120,
			points: 310,
			newsletter: false,
			marketing: true,
			status: 'New',
			operator: 'Luca',
			notes: null
		}
	];

	const euro: Intl.NumberFormatOptions = { style: 'currency', currency: 'EUR' };

	const columns: TableColumn<Client>[] = [
		{ key: 'name', label: 'Client', sortable: true },
		{ key: 'code', label: 'Code' },
		{ key: 'email', label: 'Email' },
		{ key: 'phone', label: 'Phone' },
		{ key: 'city', label: 'City', sortable: true },
		{ key: 'province', label: 'Province' },
		{ key: 'country', label: 'Country' },
		{ key: 'vat', label: 'VAT number' },
		{ key: 'fiscalCode', label: 'Fiscal code' },
		{ key: 'created', label: 'Client since', type: 'date', sortable: true },
		{ key: 'lastVisit', label: 'Last visit', type: 'datetime', sortable: true },
		{ key: 'visits', label: 'Visits', type: 'number', sortable: true },
		{ key: 'spent', label: 'Total spent', type: 'number', numberFormat: euro },
		{ key: 'ticket', label: 'Average ticket', type: 'number', numberFormat: euro },
		{ key: 'points', label: 'Loyalty points', type: 'number' },
		{ key: 'newsletter', label: 'Newsletter', type: 'boolean' },
		{ key: 'marketing', label: 'Marketing consent', type: 'boolean' },
		{ key: 'status', label: 'Status', cell: status },
		{ key: 'operator', label: 'Operator' },
		{ key: 'notes', label: 'Notes' }
	];
</script>

{#snippet status({ value }: { value: Client['status'] })}
	<Chip size="sm" variant={value === 'Active' ? 'success' : 'primary'}>{value}</Chip>
{/snippet}

{#snippet actions({ row }: { row: Client })}
	<Button buttonType="icon" variant="secondary" size="sm" icon={mdiPencilOutline} aria-label="Edit {row.name}" />
	<Button buttonType="icon" variant="secondary" size="sm" icon={mdiDotsVertical} aria-label="More for {row.name}" />
{/snippet}

<div class="phone">
	<Table
		{rows}
		{columns}
		selectable="multiple"
		actionsSnippet={actions}
		cardColumns={['email', 'phone', 'lastVisit', 'status']}
	/>
</div>

<style>
	.phone {
		width: 100%;
		max-width: 360px;
	}
</style>
```

### Sorting on the server

With `sorting="manual"` the table does not reorder the rows: it updates `sort` and calls `onsortchange`, and the app loads the rows in the new order, here with a fake request. `DataTable` does this for you, with the pages, the search and the filters in one query.

```svelte
<script lang="ts">
	import { Table, type TableColumn, type TableSort } from '@likable-hair/svelte';

	type City = { id: number; name: string; population: number };

	const all: City[] = [
		{ id: 1, name: 'Milan', population: 1371000 },
		{ id: 2, name: 'Naples', population: 913000 },
		{ id: 3, name: 'Turin', population: 841000 },
		{ id: 4, name: 'Rome', population: 2749000 }
	];

	const columns: TableColumn<City>[] = [
		{ key: 'name', label: 'City', sortable: true },
		{ key: 'population', label: 'Population', type: 'number', sortable: true }
	];

	let rows = $state(all);
	let sort = $state<TableSort>();
	let loading = $state(false);

	async function load(next: TableSort | undefined) {
		loading = true;
		await new Promise((resolve) => setTimeout(resolve, 600));
		const sign = next?.direction === 'desc' ? -1 : 1;
		rows = next
			? [...all].sort((a, b) =>
					next.key === 'name' ? sign * a.name.localeCompare(b.name) : sign * (a.population - b.population)
				)
			: all;
		loading = false;
	}
</script>

<Table {rows} {columns} sorting="manual" bind:sort {loading} onsortchange={(event) => load(event.sort)} />
```

### Custom style

Every part has a CSS variable: here a compact table with a tinted header, normal-case labels and taller rows on hover. The variables also apply to the cards.

```svelte
<script lang="ts">
	import { Table, type TableColumn } from '@likable-hair/svelte';

	type Product = { id: number; name: string; stock: number; price: number };

	const rows: Product[] = [
		{ id: 1, name: 'Nourishing shampoo', stock: 42, price: 14.9 },
		{ id: 2, name: 'Keratin mask', stock: 4, price: 22.5 },
		{ id: 3, name: 'Argan oil', stock: 0, price: 18 }
	];

	const columns: TableColumn<Product>[] = [
		{ key: 'name', label: 'Product' },
		{ key: 'stock', label: 'Stock', type: 'number' },
		{ key: 'price', label: 'Price', type: 'number', numberFormat: { style: 'currency', currency: 'EUR' } }
	];
</script>

<Table
	{rows}
	{columns}
	--table-header-background="var(--global-color-primary-soft)"
	--table-header-color="var(--global-color-primary)"
	--table-header-text-transform="none"
	--table-header-font-size="13px"
	--table-header-letter-spacing="0"
	--table-cell-padding="6px 12px"
	--table-border-radius="var(--global-radius-sm)"
	--table-row-hover-background="var(--global-color-accent-soft)"
/>
```

### Thousands of rows

Above `virtualize` rows (200 by default) only the rows in view, plus a margin, are in the page: here 10,000 rows scroll inside the table (`--table-max-height`), and they would scroll with the page the same way. The columns keep the widths of the first rows. `virtualize={false}` puts every row in the page, for example to let the browser find text in all of them.

```svelte
<script lang="ts">
	import { Table, type TableColumn } from '@likable-hair/svelte';

	type Movement = { id: number; code: string; warehouse: string; quantity: number; date: Date };

	const warehouses = ['Milan', 'Turin', 'Bologna', 'Naples'];

	const rows: Movement[] = Array.from({ length: 10000 }, (_, index) => ({
		id: index + 1,
		code: `MV-${String(index + 1).padStart(5, '0')}`,
		warehouse: warehouses[index % warehouses.length],
		quantity: ((index * 37) % 500) - 120,
		date: new Date(2026, 8, 1 + (index % 30), 8 + (index % 10))
	}));

	const columns: TableColumn<Movement>[] = [
		{ key: 'code', label: 'Code' },
		{ key: 'warehouse', label: 'Warehouse' },
		{ key: 'quantity', label: 'Quantity', type: 'number' },
		{ key: 'date', label: 'Date', type: 'datetime' }
	];
</script>

<Table --table-max-height="360px" caption="Stock movements" {rows} {columns} selectable="multiple" />
```

## Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `rows` | `Row[]` | `[]` | The rows, plain objects. |
| `columns` (required) | `TableColumn<Row>[]` |  | The columns, in their default order (see `TableColumn`). |
| `rowKey` | `string \| ((row: Row) => string \| number)` | `'id'` | Identity of a row: the name of a property or a function. Used as the key of the list, of the selection and of the expanded rows. |
| `sort` (bindable) | `TableSort` |  | Current sorting, or empty for the order of `rows`. |
| `sorting` | `'local' \| 'manual'` | `'local'` | `local`: the table sorts the rows it has. `manual`: it only updates `sort` and calls `onsortchange`, and the app passes the rows already sorted (for example by the server). |
| `selectable` | `'single' \| 'multiple'` |  | Adds a checkbox to each row: `multiple` also adds "select all" in the header and selects a range with shift-click, `single` keeps at most one row selected. |
| `selected` (bindable) | `Row[]` |  | Selected rows, compared through `rowKey`. |
| `selectedAll` (bindable) | `boolean` |  | Every row is selected, also those not loaded (for example on other pages), except `excluded`. Set by the app or by `DataTable`; the header checkbox of the table selects only the rows it shows. |
| `excluded` (bindable) | `Row[]` |  | Rows left out of `selectedAll`. |
| `expanded` (bindable) | `(string \| number)[]` |  | Keys (`rowKey`) of the expanded rows. |
| `expandable` | `(row: Row) => boolean` |  | Which rows can expand. Defaults to all of them when `expandedSnippet` is set. |
| `rowHref` | `(row: Row) => string \| undefined` |  | URL of a row: the row becomes a link, with a real link inside for the keyboard and screen readers. Ctrl-click and middle click open it in a new tab. |
| `rowClass` | `(row: Row) => ClassValue` |  | Extra classes of a row. |
| `empty` | `string` | `'—'` | Text of empty cells (`null`, `undefined`, empty string) of every column, unless the column sets its own. |
| `loading` | `boolean` | `false` | Shows that the rows are loading: a progress bar under the header, or `loadingRows` placeholder rows when there are no rows yet. |
| `loadingRows` | `number` | `5` | Number of placeholder rows while loading without rows. |
| `noDataTitle` | `string` |  | Title shown when there are no rows. Defaults to the one of `NoData`, in the language of `locale`. |
| `columnState` (bindable) | `TableColumnState[]` |  | Order, visibility, width and pin of the columns, in display order: plain data to save and restore. Columns missing from it keep their defaults, unknown keys are ignored. |
| `columnsKey` | `string` |  | Saves `columnState` in the browser (`localStorage`) under this key and restores it on load, when the app does not pass its own. |
| `resizable` | `boolean` | `false` | Columns can be resized by dragging the edge of their header, or with the arrow keys when it has the focus. |
| `actionsVisibility` | `'always' \| 'hover'` | `'always'` | `always` shows the buttons of `actionsSnippet` on every row; `hover` only on the row under the pointer or with the focus inside, for dense tables (always on touch screens and in the cards). |
| `cardBreakpoint` | `number \| false` | `640` | Below this width of the table, in pixels, every row is shown as a card. `false` keeps the table, which scrolls sideways. |
| `cardTitle` | `string` |  | Key of the column used as the title of the cards and as the name of the rows for screen readers. Defaults to the first visible column. |
| `cardColumns` | `string[]` |  | Keys of the columns always shown in the cards, under the title; the other columns are behind a "Show more fields" button in each card. Defaults to the first 4 visible columns after the title. |
| `caption` | `string` |  | Accessible name of the table, read by screen readers. |
| `locale` | `string` |  | BCP 47 locale of the texts and of the formatting of numbers and dates: `it` and `en` texts are built in, other languages fall back to English. Defaults to the one set with `setDefaultLocale`, else `en`. |
| `labels` | `{ selectAll?: string; selectAllShort?: string; selectRow?: string; row?: string; expand?: string; collapse?: string; actions?: string; resize?: string; info?: string; open?: string; loading?: string; yes?: string; no?: string; sortBy?: string; unsorted?: string; moreFields?: string; fewerFields?: string; ascending?: string; descending?: string; }` |  | Texts to change, on top of those of `locale`. `{row}`, `{index}` and `{column}` are replaced by the name of the row, its position and the label of the column. |
| `class` | `{ container?: ClassValue; table?: ClassValue; header?: ClassValue; row?: ClassValue; cell?: ClassValue; }` | `{}` | Classes of the parts: `container` the root, `table` the `<table>`, `header` the header cells, `row` the rows (and cards), `cell` the body cells. |
| `tableElement` (bindable) | `HTMLTableElement` |  | The `<table>` element, empty while the cards are shown. |
| `virtualize` | `boolean \| number` | `200` | Above this number of rows only the rows in view, plus a margin, are in the page, so thousands of rows stay fast; the others are drawn while scrolling (the page, the app container or the table with `--table-max-height`). `true` always, `false` never. While it is on, the find of the browser and printing see only the drawn rows, and the columns keep the widths they had on the first rows. |
| `disabled` | `boolean` | `false` | Read-only table, for example while the app saves: rows are not clickable and sorting, selection and resizing are off. Expanding rows still works. |
| `selectAll` | `boolean` | `true` | With `selectable="multiple"`, shows the checkbox that selects every row (in the header, or above the cards). `false` keeps only the checkbox of each row. |

It also accepts the native attributes and events of `<div>` (for example `id`, `title`, `aria-label`, `onkeydown`), which are forwarded to the element.

## Snippets

| Name | Parameters | Description |
| --- | --- | --- |
| `cellSnippet` | `{ row: Row; value: any; column: TableColumn<Row> }` | Content of the cells of `custom` columns, for the columns without a `cell` snippet. |
| `expandedSnippet` | `{ row: Row }` | Content of the detail row under an expanded row. |
| `actionsSnippet` | `{ row: Row }` | Buttons of a row, in a column that stays on the right while the table scrolls sideways. |
| `noDataSnippet` |  | Replaces `NoData` when there are no rows. |

## Events

| Name | Type | Description |
| --- | --- | --- |
| `onsortchange` | `(event: { sort: TableSort \| undefined }) => void` | Called when the user changes the sorting, after `sort` has changed. |
| `onselectionchange` | `(event: { selected: Row[]; selectedAll: boolean; excluded: Row[] }) => void` | Called when the user changes the selection, after `selected`, `selectedAll` and `excluded` have changed. |
| `onexpandedchange` | `(event: { expanded: (string \| number)[] }) => void` | Called when the user expands or collapses a row, after `expanded` has changed. |
| `onrowclick` | `(row: Row, event: MouseEvent \| KeyboardEvent) => void` | Called on a click on a row (not on its buttons, links and fields) and on Enter or Space when the row has the focus. With it every row is focusable. Calling `event.preventDefault()` stops the navigation of `rowHref`. |
| `oncolumnstatechange` | `(event: { columnState: TableColumnState[] }) => void` | Called when the user changes the columns (for example by resizing one), after `columnState` has changed. |
| `onendreached` | `() => void` | Called when the last rows come into view (the third from the end), again when the rows change while they are still in view and when this callback changes: for loading the next rows. |

## Types

Exported by the package: `import type { TableColumn, TableSort, TableColumnState } from '@likable-hair/svelte';`

### TableColumn

A column of a `Table`: what it shows, how it formats, sorts and sizes it.

`interface TableColumn<Row = any>`

| Name | Type | Description |
| --- | --- | --- |
| `key` (required) | `string` | Unique key of the column. Without `value` it is also the property of the row that the column shows. |
| `label` | `string` | Text of the header. |
| `type` | `'text' \| 'number' \| 'date' \| 'datetime' \| 'boolean' \| 'custom'` | How the value is formatted and sorted: `text` as is, `number` with `Intl.NumberFormat` (right-aligned, monospaced digits), `date` and `datetime` with `Intl.DateTimeFormat` (a `Date`, an ISO string, a timestamp or an object with `toJSDate()` such as a luxon `DateTime`), `boolean` as a check mark when true, `custom` with the `cellSnippet` of the table (sorted as text). Defaults to `text`. |
| `value` | `(row: Row) => unknown` | Reads the value of the column from a row, for nested or computed values. Defaults to `row[key]`. |
| `format` | `(value: any, row: Row) => string` | Turns the value into the text of the cell, instead of the formatting of `type`. Not called for empty values. |
| `numberFormat` | `Intl.NumberFormatOptions` | Options of `Intl.NumberFormat` for `number` columns, for example `{ style: 'currency', currency: 'EUR' }`. |
| `dateFormat` | `Intl.DateTimeFormatOptions` | Options of `Intl.DateTimeFormat` for `date` and `datetime` columns. Defaults to the day, the month and the year in digits, plus hours and minutes for `datetime`. |
| `empty` | `string` | Text of an empty cell (`null`, `undefined`, an empty string). Defaults to the `empty` of the table. |
| `align` | `'start' \| 'center' \| 'end'` | Horizontal alignment of the header and the cells. Defaults to `end` for numbers, else `start`. |
| `width` | `string` | Preferred width of the column, any CSS length (`120px`, `20%`): columns still shrink to fit the table, `minWidth` keeps them wider and makes the table scroll. A width set by resizing wins. |
| `minWidth` | `string` | Minimum width of the column, any CSS length. |
| `maxWidth` | `string` | Maximum width of the column, any CSS length: longer text is cut with an ellipsis. |
| `sortable` | `boolean` | The header becomes a button that sorts by this column: ascending, descending, then unsorted. |
| `compare` | `(a: Row, b: Row) => number` | Compares two rows when the table sorts by this column on its own, instead of comparing the values. Return a negative number when `a` comes first in ascending order. |
| `sort` | `(params: { builder: FilterBuilder; direction: 'asc' \| 'desc' }) => FilterBuilder` | Adds the sorting by this column to the query sent to the backend, instead of `orderBy(key, direction)`. Used when the table does not sort on its own (`sorting="manual"`, `DataTable`). |
| `pinned` | `boolean` | Keeps the column visible on the left while the table scrolls sideways. Pinned columns are moved before the others; they scroll like the others while, together with the checkboxes and the actions, they would take more than half of the table. |
| `hidden` | `boolean` | Hides the column until it is shown through `columnState` (for example with a `ColumnManager`). |
| `required` | `boolean` | The column is always shown: `columnState` cannot hide it and the `ColumnManager` has no button to hide it. |
| `icon` | `string` | SVG path of the icon of the column in the `ColumnManager`. Defaults to an icon of its `type`. |
| `description` | `string` | Description of the column in the `ColumnManager`, also matched by its search. Defaults to `info`. |
| `resizable` | `boolean` | With `resizable` on the table, `false` keeps this column at its size. |
| `info` | `string` | Explanation of the column, shown in a tooltip next to the header. |
| `footer` | `string \| Snippet<[{ rows: Row[]; column: TableColumn<Row> }]>` | Content of the column in the footer of the table: a text, or a snippet that receives every row, for example to show a total. |
| `cell` | `Snippet<[{ row: Row; value: any; column: TableColumn<Row> }]>` | Content of the cells of this column, instead of the formatted value. |
| `header` | `Snippet<[{ column: TableColumn<Row> }]>` | Content of the header of this column, instead of `label`. The sort arrow and the info icon stay. |

### TableSort

The sorting of a table: the key of the column and the direction.

`interface TableSort`

| Name | Type | Description |
| --- | --- | --- |
| `key` (required) | `string` | Key of the sorted column. |
| `direction` (required) | `'asc' \| 'desc'` | `asc` from the smallest value, `desc` from the largest. |

### TableColumnState

What the user changed about a column, in display order: plain data, ready to be saved.

`interface TableColumnState`

| Name | Type | Description |
| --- | --- | --- |
| `key` (required) | `string` | Key of the column. |
| `visible` (required) | `boolean` | Whether the column is shown. |
| `width` | `number` | Width in pixels set by resizing. |
| `pinned` | `boolean` | Whether the column is pinned on the left. |

## CSS variables

For a single instance, pass them as style props: `<Table --table-background="tomato">`. The `--table-default-*` defaults apply to the whole app: override them in `:root` or in a theme.

| Per-instance variable | Default |
| --- | --- |
| `--table-focus-ring-width` | `--table-default-focus-ring-width`: `var(--global-focus-ring-width)` |
| `--table-focus-ring-color` | `--table-default-focus-ring-color`: `var(--global-focus-ring-color)` |
| `--table-background` | `--table-default-background`: `var(--global-color-surface-solid)` |
| `--table-separator-color` | `--table-default-separator-color`: `var(--global-color-border)` |
| `--table-duration` | `--table-default-duration`: `var(--global-duration-fast)` |
| `--table-easing` | `--table-default-easing`: `var(--global-ease)` |
| `--table-width` | `--table-default-width`: `100%` |
| `--table-color` | `--table-default-color`: `var(--global-color-text)` |
| `--table-font-size` | `--table-default-font-size`: `13.5px` |
| `--table-border` | `--table-default-border`: `var(--global-border-width) solid var(--global-color-border)` |
| `--table-border-radius` | `--table-default-border-radius`: `var(--global-radius-lg)` |
| `--table-max-height` | `--table-default-max-height`: `none` |
| `--table-header-background` | `--table-default-header-background`: `var(--global-color-surface-2)` |
| `--table-header-padding` | `--table-default-header-padding`: `8px 12px` |
| `--table-header-color` | `--table-default-header-color`: `var(--global-color-text-3)` |
| `--table-header-font-size` | `--table-default-header-font-size`: `11px` |
| `--table-header-font-weight` | `--table-default-header-font-weight`: `500` |
| `--table-header-letter-spacing` | `--table-default-header-letter-spacing`: `0.06em` |
| `--table-header-text-transform` | `--table-default-header-text-transform`: `uppercase` |
| `--table-header-sorted-color` | `--table-default-header-sorted-color`: `var(--global-color-primary)` |
| `--table-sort-icon-size` | `--table-default-sort-icon-size`: `14px` |
| `--table-header-hover-color` | `--table-default-header-hover-color`: `var(--global-color-text)` |
| `--table-info-icon-size` | `--table-default-info-icon-size`: `14px` |
| `--table-resizer-width` | `--table-default-resizer-width`: `2px` |
| `--table-resizer-color` | `--table-default-resizer-color`: `var(--global-color-primary)` |
| `--table-cell-padding` | `--table-default-cell-padding`: `9px 12px` |
| `--table-row-background` | `--table-default-row-background`: `transparent` |
| `--table-row-hover-background` | `--table-default-row-hover-background`: `var(--global-color-surface-2)` |
| `--table-row-selected-hover-background` | `--table-default-row-selected-hover-background`: `color-mix(in oklab, var(--global-color-primary-soft), var(--global-color-primary) 6%)` |
| `--table-row-selected-background` | `--table-default-row-selected-background`: `var(--global-color-primary-soft)` |
| `--table-loading-opacity` | `--table-default-loading-opacity`: `0.55` |
| `--table-number-font-family` | `--table-default-number-font-family`: `var(--global-font-family-mono)` |
| `--table-number-font-size` | `--table-default-number-font-size`: `13px` |
| `--table-date-color` | `--table-default-date-color`: `var(--global-color-text-2)` |
| `--table-date-font-family` | `--table-default-date-font-family`: `var(--global-font-family-mono)` |
| `--table-date-font-size` | `--table-default-date-font-size`: `13px` |
| `--table-empty-color` | `--table-default-empty-color`: `var(--global-color-text-3)` |
| `--table-boolean-icon-size` | `--table-default-boolean-icon-size`: `18px` |
| `--table-boolean-color` | `--table-default-boolean-color`: `var(--global-color-success)` |
| `--table-pinned-background` | `--table-default-pinned-background`: `var(--global-color-surface-solid)` |
| `--table-pinned-shadow` | `--table-default-pinned-shadow`: `1px 0 0 var(--global-color-border)` |
| `--table-control-size` | `--table-default-control-size`: `40px` |
| `--table-actions-shadow` | `--table-default-actions-shadow`: `-1px 0 0 var(--global-color-border)` |
| `--table-actions-gap` | `--table-default-actions-gap`: `4px` |
| `--table-expand-icon-size` | `--table-default-expand-icon-size`: `20px` |
| `--table-detail-background` | `--table-default-detail-background`: `var(--global-color-bg-2)` |
| `--table-detail-padding` | `--table-default-detail-padding`: `16px 16px 16px 52px` |
| `--table-detail-color` | `--table-default-detail-color`: `var(--global-color-text-2)` |
| `--table-progress-height` | `--table-default-progress-height`: `2px` |
| `--table-progress-color` | `--table-default-progress-color`: `var(--global-color-primary)` |
| `--table-footer-background` | `--table-default-footer-background`: `var(--global-color-surface-2)` |
| `--table-footer-color` | `--table-default-footer-color`: `var(--global-color-text)` |
| `--table-footer-font-weight` | `--table-default-footer-font-weight`: `600` |
| `--table-card-gap` | `--table-default-card-gap`: `8px` |
| `--table-card-background` | `--table-default-card-background`: `var(--global-color-surface-solid)` |
| `--table-card-padding` | `--table-default-card-padding`: `12px 14px` |
| `--table-card-border` | `--table-default-card-border`: `var(--global-border-width) solid var(--global-color-border)` |
| `--table-card-border-radius` | `--table-default-card-border-radius`: `var(--global-radius-md)` |
| `--table-card-title-font-weight` | `--table-default-card-title-font-weight`: `600` |
| `--table-card-label-color` | `--table-default-card-label-color`: `var(--global-color-text-3)` |
| `--table-card-label-font-size` | `--table-default-card-label-font-size`: `12px` |
