Guidelines
Tables, state and loading
Every table is TanStack Table. What a person is looking at lives in the address, so a reload or a shared link shows the same thing; preferences live in the browser and follow across tabs; changes show at once and the server confirms them. Nothing below the first screen loads before it is needed.
A data table
- Maryam K.
- Status
- Counted
- Total
- 2,100,000
- Gifts
- 1
- Created
- Updated
- Arash
- Status
- Counted
- Total
- 1,965,000
- Gifts
- 2
- Created
- Updated
- Niloufar R.
- Status
- Counted
- Total
- 1,830,000
- Gifts
- 3
- Created
- Updated
- Kaveh
- Status
- Held
- Total
- 1,575,000
- Gifts
- 4
- Created
- Updated
- Shirin
- Status
- Counted
- Total
- 1,440,000
- Gifts
- 1
- Created
- Updated
| Maryam K. | Counted | 2,100,000 | 1 | ||
| Arash | Counted | 1,965,000 | 2 | ||
| Niloufar R. | Counted | 1,830,000 | 3 | ||
| Kaveh | Held | 1,575,000 | 4 | ||
| Shirin | Counted | 1,440,000 | 1 |
Sort by any header, search, turn the pages. The sort, the search and the page are in the address: reload and nothing moves.
State in the address
The default adds nothing to the address.
import { UrlState, codec } from '#lib/state/url.svelte.js';
const filter = new UrlState('filter', 'all', codec.oneOf(['all', 'board', 'held']));
const search = new UrlState('q', '', codec.string);
filter.current = 'held'; // → ?filter=held, no navigation, no reloadPick one, then look at the address bar.
Preferences in the browser
// src/lib/brand/example.svelte.ts: this book's language, kept per browser and followed by every open tab
import { Persisted } from '#lib/state/persisted.svelte.js';
const lang = new Persisted<'en' | 'fa'>('divan-example-lang', 'en', (v): v is 'en' | 'fa' => v === 'en' || v === 'fa');
// in the layout: read what was saved once the page has hydrated
onMount(() => lang.connect());
lang.current = 'fa'; // saved, and every other tab switches tooThe language switch of this book is one: change it here and every other open tab of the book follows.
Changes that show at once
// entries.remote.ts
import { command, getRequestEvent } from '$app/server';
import { requireAdmin } from '#lib/server/supabase.js';
export const setStatus = command('unchecked', async ({ ids, status }: { ids: number[]; status: Status }) => {
requireAdmin(getRequestEvent().locals); // remote calls run outside the route's guards
return { changed: await setEntriesStatus(client, ids, status) };
});
// +page.svelte: show it at once, let the server confirm
for (const id of ids) optimistic.set(id, status);
try {
await setStatus({ ids, status });
await invalidateAll();
} catch (e) {
toast(problemOf(e)); // said, never swallowed: the change goes back as the server had it
} finally {
for (const id of ids) optimistic.delete(id); // the server's answer wins
}A remote function (SvelteKit’s own) does the change on the server. The page shows the new value first and lets the server’s answer replace it.
Loading only what is needed
<!-- the first big picture -->
<img src="/home/home-sam.webp" fetchpriority="high" decoding="async" alt="" width="800" height="1423" />
<!-- everything further down -->
<Picture src={photo} loading="lazy" decoding="async" alt="" width="96" height="96" />
<!-- a phone and a wide version: only one downloads -->
<picture>
<source media="(min-width: 640px)" srcset={wide.src} width={wide.width} height={wide.height} />
<Picture src={portrait.src} width={portrait.width} height={portrait.height} alt="" />
</picture>
<!-- a long section below the first screen -->
<section class="page-container defer-render">…</section>
<!-- code only an interaction needs -->
const { createBrowserClient } = await import('@supabase/ssr');Rules
- Every table is <DataTable>: sortable headers, search when it is long, pages or rows drawn as they scroll into view, numbers aligned at the end.
- Every table ends with Created and Updated: when each row was made and when it last changed, as a date and a time (day, month, year, hour, minute; Tehran time, Persian digits and calendar in Persian). Add ...stampColumns({ created, updated, lang }) to its columns; a row nothing has changed since shows its creation as its update. pnpm divan:lint flags a table without them (table-stamps).
- Every date and time, in a table or not, is in Tehran time, the one zone of Studio, the backoffice and the site: formatTime or timeFormat from #lib/time.js, so the page the server draws and the one the browser draws say the same. A schedule field says beside it that its time is Tehran’s. pnpm divan:lint flags a date or time format with no timeZone (time-without-zone).
- Lists that are not tables still use TanStack’s model when they filter, sort or tick rows.
- What a person is looking at (filter, search, sort, page) goes in the address with UrlState.
- Preferences go in the browser with Persisted; they are read after the page hydrates and follow across tabs.
- Changes made in place go through a remote function, show at once, and the server’s answer wins. Each remote function checks the user itself.
- Below the first screen, images, charts, heavy code and long sections load only when they are needed; slow, secondary data streams in behind a Skeleton.
- A choice the server draws (the brand book’s language, the studio composer’s kind) is kept in a cookie too, so the first paint already shows it; Persisted.connected says when the stored value has been read.
- A redirect to another site names where it may go: redirect(303, url, { external: ['https://openrouter.ai'] }), or SvelteKit answers with a 500 page (tests/redirects.test.ts).
Code
DataTable · Every table of data, on TanStack Table, ending with Created and Updated (stampColumns: date and time, Tehran time). Sortable headers, optional search, pages (pageSize) or rows drawn as they scroll into view, and sort/search/page kept in the address with urlKey. Rows alternate tones; no grid lines. Tables of text pass stack, so phones show each row as a card. Persian pages pass labels={DATA_TABLE_FA} and dir="rtl". Filters kept outside the table (a bar, a Sheet) pass pageKey={view} so it returns to page 1 when they change.
import { DataTable, renderSnippet, stampColumns, type ColumnDef } from '#lib/components/ui/data-table/index.js';
<script lang="ts">
import { DataTable, renderSnippet, stampColumns, type ColumnDef } from '#lib/components/ui/data-table/index.js';
import { Status } from '#lib/components/ui/status/index.js';
type Donor = { id: string; name: string; total: number; status: 'Counted' | 'Held'; createdAt: string; updatedAt: string };
let { donors }: { donors: Donor[] } = $props();
const columns: ColumnDef<Donor>[] = [
{ accessorKey: 'name', header: 'Donor' },
{ accessorKey: 'status', header: 'Status', cell: ({ row }) => renderSnippet(status, row.original) },
{ accessorKey: 'total', header: 'Total', cell: ({ row }) => row.original.total.toLocaleString('en-GB'), meta: { align: 'end' } },
...stampColumns<Donor>({ created: (d) => d.createdAt, updated: (d) => d.updatedAt, lang: 'en' })
];
</script>
{#snippet status(d: Donor)}<Status tone={d.status === 'Counted' ? 'success' : 'attention'}>{d.status}</Status>{/snippet}
<DataTable data={donors} {columns} getRowId={(d) => d.id} sorting={[{ id: 'total', desc: true }]} search urlKey="donors" minWidth="32rem" />