# Page patterns

Whole pages built only with the library and the tokens: start from the closest one when you design a page. The app shell around them (sidebar, bar, user menu) is `SidebarLayout`.

## List page

The page of a list of records: a `PageHeader` with the title, a short description and the main action, then a `DataTable` that loads from the server with search, `Filters`, column manager, row actions and selection actions. "Cancel bookings" asks first in a `ConfirmDialog` that names how many and the consequence; it closes by itself when the request ends. "New booking" opens a `Dialog` with a form whose submit button sits in the footer (`form` attribute); the draft lives in the page state because the dialog is unmounted when closed. Spacing between blocks is the app's choice (rem); colors, radii and fonts come from the tokens.

```svelte
<script lang="ts">
	import { mdiDeleteOutline, mdiDownload, mdiEmailOutline, mdiPencilOutline, mdiPlus } from '@mdi/js';
	import {
		Avatar,
		Button,
		Chip,
		ConfirmDialog,
		DataTable,
		DatePickerTextField,
		Dialog,
		PageHeader,
		Select,
		SimpleTextField,
		type DataTableAction,
		type DataTableActionContext,
		type FilterGroup,
		type TableColumn
	} from '@likable-hair/svelte';
	import { bookingFields, fetchBookings, queryBookings, searchColumns, statuses, type Booking } from './data.js';

	const first = queryBookings({ page: 1, pageSize: 10 });
	let filterValue = $state<FilterGroup>();
	let table = $state<ReturnType<typeof DataTable>>();

	const columns: TableColumn<Booking>[] = [
		{ key: 'client', label: 'Client', sortable: true, pinned: 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' }, sortable: true }
	];

	const selectionActions: DataTableAction<Booking>[] = [
		{ label: 'Send reminder', icon: mdiEmailOutline, onclick: () => new Promise((resolve) => setTimeout(resolve, 800)) },
		{ label: 'Export', icon: mdiDownload, onclick: () => undefined },
		{ label: 'Cancel bookings', icon: mdiDeleteOutline, variant: 'danger', onclick: openCancel }
	];

	let cancelOpen = $state(false);
	let cancelling = $state<DataTableActionContext<Booking>>();

	function openCancel(context: DataTableActionContext<Booking>) {
		cancelling = context;
		cancelOpen = true;
	}

	async function cancelBookings() {
		await new Promise((resolve) => setTimeout(resolve, 800));
		cancelling?.clear();
		table?.reload();
	}

	const services = ['Cut', 'Cut and beard', 'Colour', 'Balayage', 'Blow-dry'].map((label) => ({ value: label, label }));

	let open = $state(false);
	let saving = $state(false);
	let draft = $state({ client: '', service: '', date: undefined as Date | undefined });

	function create() {
		draft = { client: '', service: '', date: undefined };
		open = true;
	}

	async function save() {
		saving = true;
		await new Promise((resolve) => setTimeout(resolve, 600));
		saving = false;
		open = false;
		table?.reload();
	}
</script>

{#snippet client({ row }: { row: Booking })}
	<span class="who"><Avatar name={row.client} size="sm" />{row.client}</span>
{/snippet}

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

<main class="page">
	<PageHeader title="Bookings" subtitle="Every appointment of the salon, past and upcoming.">
		{#snippet actionsSnippet()}
			<Button icon={mdiPlus} onclick={create}>New booking</Button>
		{/snippet}
	</PageHeader>

	<DataTable
		bind:this={table}
		caption="Bookings"
		{columns}
		rows={first.rows}
		total={first.total}
		pageSize={10}
		loader={fetchBookings}
		{searchColumns}
		filterFields={bookingFields}
		bind:filterValue
		columnManager
		selectable="multiple"
		{selectionActions}
		actionsVisibility="hover"
	>
		{#snippet actionsSnippet({ row })}
			<Button buttonType="icon" variant="secondary" size="sm" icon={mdiPencilOutline} aria-label="Edit {row.client}" />
		{/snippet}
	</DataTable>
</main>

<ConfirmDialog
	bind:open={cancelOpen}
	title={cancelling?.count === 1 ? 'Cancel 1 booking?' : `Cancel ${cancelling?.count ?? 'the selected'} bookings?`}
	description="Clients get an email that the appointment is cancelled. This cannot be undone."
	icon={mdiDeleteOutline}
	confirmText="Cancel bookings"
	cancelText="Keep bookings"
	onconfirm={cancelBookings}
/>

<Dialog bind:open title="New booking" closable --dialog-max-width="440px">
	<form
		id="new-booking"
		class="form"
		onsubmit={(event) => {
			event.preventDefault();
			save();
		}}
	>
		<SimpleTextField label="Client" bind:value={draft.client} required />
		<Select label="Service" placeholder="Choose a service" items={services} bind:value={draft.service} />
		<DatePickerTextField label="Date" bind:selectedDate={draft.date} />
	</form>
	{#snippet actionsSnippet({ close })}
		<Button buttonType="text" variant="secondary" onclick={close}>Cancel</Button>
		<Button type="submit" form="new-booking" loading={saving}>Create booking</Button>
	{/snippet}
</Dialog>

<style>
	.page {
		display: grid;
		gap: 1.5rem;
		padding: 1.5rem;
	}

	.who {
		display: inline-flex;
		align-items: center;
		gap: 0.5rem;
	}

	.form {
		display: grid;
		gap: 1rem;
	}
</style>
```

## Detail page

The page of one record: a `PageHeader` with the link back to the list, the `Avatar`, the name, a status `Chip` and the actions, an `AlertBanner` for what needs attention, then a `TabSwitcher` with the sections. Each section is a card made with the surface tokens; facts are a description list and the history a `Timeline`.

```svelte
<script lang="ts">
	import { mdiCalendarPlus, mdiPencilOutline } from '@mdi/js';
	import { AlertBanner, Avatar, Button, Chip, PageHeader, TabSwitcher, Timeline, type TimelineItem } from '@likable-hair/svelte';

	const client = {
		name: 'Giulia Marini',
		since: new Date(2023, 2, 14),
		email: 'giulia.marini@example.com',
		phone: '+39 333 123 4567',
		stylist: 'Irene',
		visits: 27,
		spent: 1840,
		balance: 64
	};

	const tabs = [
		{ name: 'overview', label: 'Overview' },
		{ name: 'bookings', label: 'Bookings', badge: 3 },
		{ name: 'notes', label: 'Notes' }
	];
	let selected = $state('overview');

	const history: TimelineItem[] = [
		{ title: 'Colour', description: 'Booked online', from: new Date(2026, 9, 2, 11, 0), status: 'upcoming' },
		{ title: 'Balayage', description: 'With Irene · €120', from: new Date(2026, 8, 20, 15, 0), status: 'done' },
		{ title: 'Cut and blow-dry', description: 'With Marco · €53', from: new Date(2026, 7, 29, 10, 30), status: 'done' }
	];

	const money = new Intl.NumberFormat('en', { style: 'currency', currency: 'EUR' });
	const date = new Intl.DateTimeFormat('en', { dateStyle: 'medium' });
</script>

<main class="page">
	<PageHeader title={client.name} backHref="#clients" backLabel="Clients">
		{#snippet prependSnippet()}
			<Avatar name={client.name} size="xl" />
		{/snippet}
		{#snippet subtitleSnippet()}
			<span class="since">Client since {date.format(client.since)} <Chip size="sm" variant="success">Active</Chip></span>
		{/snippet}
		{#snippet actionsSnippet()}
			<Button variant="secondary" icon={mdiPencilOutline}>Edit</Button>
			<Button icon={mdiCalendarPlus}>New booking</Button>
		{/snippet}
	</PageHeader>

	<AlertBanner
		variant="warning"
		title="Unpaid balance"
		description="{money.format(client.balance)} from the last visit is still to be paid."
	/>

	<TabSwitcher {tabs} bind:selected aria-label="Client sections" />

	{#if selected === 'overview'}
		<div class="grid">
			<section class="card">
				<h2>Contacts</h2>
				<dl>
					<dt>Email</dt>
					<dd>{client.email}</dd>
					<dt>Phone</dt>
					<dd>{client.phone}</dd>
					<dt>Stylist</dt>
					<dd>{client.stylist}</dd>
				</dl>
			</section>
			<section class="card">
				<h2>Numbers</h2>
				<dl>
					<dt>Visits</dt>
					<dd>{client.visits}</dd>
					<dt>Spent</dt>
					<dd>{money.format(client.spent)}</dd>
				</dl>
			</section>
			<section class="card wide">
				<h2>History</h2>
				<Timeline items={history} locale="en" />
			</section>
		</div>
	{:else}
		<section class="card">
			<p class="empty">The {selected} of {client.name} go here.</p>
		</section>
	{/if}
</main>

<style>
	.page {
		display: grid;
		gap: 1.25rem;
		padding: 1.5rem;
	}

	.since {
		display: inline-flex;
		flex-wrap: wrap;
		align-items: center;
		gap: 0.5rem;
	}

	.grid {
		display: grid;
		grid-template-columns: repeat(auto-fit, minmax(min(100%, 24rem), 1fr));
		gap: 1rem;
	}

	.card {
		padding: 1.25rem;
		border: var(--global-border-width) solid var(--global-color-border);
		border-radius: var(--global-radius-lg);
		background: var(--global-color-surface);
		box-shadow: var(--global-shadow-sm);
	}

	.wide {
		grid-column: 1 / -1;
	}

	h2 {
		margin: 0 0 1rem;
		font-size: 0.95rem;
		color: var(--global-color-text);
	}

	dl {
		display: grid;
		grid-template-columns: max-content 1fr;
		gap: 0.5rem 1.5rem;
		margin: 0;
	}

	dt {
		color: var(--global-color-text-3);
	}

	dd {
		margin: 0;
		color: var(--global-color-text);
	}

	.empty {
		margin: 0;
		color: var(--global-color-text-3);
	}
</style>
```

## Form page

A `PageHeader`, then a long form split in sections: each section has a title and a line of explanation on the left and its fields on the right, and stacks on narrow screens. Fields are the library ones with `label`, `hint` and `state`; errors appear after the first submit. `ConfirmOrCancelButtons` closes the form, in a bar that stays at the bottom while scrolling.

```svelte
<script lang="ts">
	import {
		ConfirmOrCancelButtons,
		DatePickerTextField,
		PageHeader,
		Select,
		SimpleTextField,
		Switch,
		Textarea
	} from '@likable-hair/svelte';

	let salon = $state({
		name: 'Likable Milano',
		email: '',
		phone: '+39 02 1234 5678',
		timezone: 'Europe/Rome',
		opening: new Date(2019, 3, 1) as Date | undefined,
		description: '',
		onlineBooking: true,
		reminders: false
	});
	let submitted = $state(false);
	let saving = $state(false);

	const timezones = ['Europe/Rome', 'Europe/London', 'America/New_York'].map((value) => ({ value, label: value }));
	const emailError = $derived(submitted && !/^\S+@\S+\.\S+$/.test(salon.email));

	async function save() {
		submitted = true;
		if (emailError) return;
		saving = true;
		await new Promise((resolve) => setTimeout(resolve, 600));
		saving = false;
	}
</script>

<main class="page">
	<PageHeader title="Salon settings" subtitle="How the salon appears to clients and how bookings work." />

	<form
		onsubmit={(event) => {
			event.preventDefault();
			save();
		}}
	>
		<section>
			<div class="about">
				<h2>Profile</h2>
				<p>Shown on the booking page and in the emails.</p>
			</div>
			<div class="fields">
				<SimpleTextField label="Name" bind:value={salon.name} required />
				<SimpleTextField
					label="Email"
					type="email"
					bind:value={salon.email}
					state={emailError ? 'error' : undefined}
					hint={emailError ? 'Enter a valid email address' : 'Replies to clients come from here'}
				/>
				<SimpleTextField label="Phone" type="tel" bind:value={salon.phone} />
				<Textarea label="Description" bind:value={salon.description} maxlength={280} counter autoGrow />
			</div>
		</section>

		<section>
			<div class="about">
				<h2>Calendar</h2>
				<p>Dates and times of the bookings follow these settings.</p>
			</div>
			<div class="fields">
				<Select label="Time zone" items={timezones} bind:value={salon.timezone} />
				<DatePickerTextField label="Opening date" bind:selectedDate={salon.opening} />
			</div>
		</section>

		<section>
			<div class="about">
				<h2>Bookings</h2>
				<p>What clients can do on their own.</p>
			</div>
			<div class="fields">
				<Switch label="Online booking" bind:checked={salon.onlineBooking} />
				<Switch label="Email reminder the day before" bind:checked={salon.reminders} />
			</div>
		</section>

		<div class="footer">
			<ConfirmOrCancelButtons confirmText="Save changes" confirmType="submit" loading={saving} />
		</div>
	</form>
</main>

<style>
	.page {
		display: grid;
		gap: 1.5rem;
		max-width: 60rem;
		padding: 1.5rem;
	}

	.about p {
		margin: 0.25rem 0 0;
		color: var(--global-color-text-2);
	}

	form {
		border: var(--global-border-width) solid var(--global-color-border);
		border-radius: var(--global-radius-lg);
		background: var(--global-color-surface);
		box-shadow: var(--global-shadow-sm);
	}

	section {
		display: grid;
		grid-template-columns: minmax(0, 1fr) minmax(0, 2fr);
		gap: 1.5rem;
		padding: 1.5rem;
		border-bottom: var(--global-border-width) solid var(--global-color-border);
	}

	h2 {
		margin: 0;
		font-size: 0.95rem;
		color: var(--global-color-text);
	}

	.about p {
		font-size: 0.85rem;
	}

	.fields {
		display: grid;
		gap: 1rem;
	}

	.footer {
		position: sticky;
		bottom: 0;
		padding: 1rem 1.5rem;
		border-radius: 0 0 var(--global-radius-lg) var(--global-radius-lg);
		background: var(--global-color-surface-solid);
	}

	@media (width < 640px) {
		section {
			grid-template-columns: 1fr;
		}
	}
</style>
```

## Dashboard page

Numbers first, then trends: a `PageHeader` with the `PeriodPicker` that drives the page, a row of indicator cards (value, change as a `Chip`), a `BarChart` and a `LineChart` side by side, and a `Table` of the best services. Charts take the data colors of the theme, so the page follows themes and dark mode without extra code.

```svelte
<script lang="ts">
	import { mdiTrendingDown, mdiTrendingUp } from '@mdi/js';
	import {
		BarChart,
		Chip,
		LineChart,
		PageHeader,
		PeriodPicker,
		Table,
		type TableColumn,
		type TimespanSettings
	} from '@likable-hair/svelte';

	let timespanSettings = $state<TimespanSettings>({ method: 'quick', values: 'this-month' });

	const money = new Intl.NumberFormat('en', { style: 'currency', currency: 'EUR', maximumFractionDigits: 0 });
	const indicators = [
		{ label: 'Revenue', value: money.format(35800), change: 8.2 },
		{ label: 'Bookings', value: '412', change: 4.1 },
		{ label: 'New clients', value: '38', change: -6.5 },
		{ label: 'No-shows', value: '2.1%', change: -0.4 }
	];

	const weeks = ['W36', 'W37', 'W38', 'W39', 'W40'];
	const revenue = {
		labels: weeks,
		datasets: [
			{ label: 'Services', data: [6200, 7100, 6800, 7900, 7800] },
			{ label: 'Products', data: [900, 1100, 1250, 1000, 1300] }
		]
	};
	const bookings = {
		labels: weeks,
		datasets: [
			{ label: 'Online', data: [48, 55, 61, 66, 72] },
			{ label: 'At the desk', data: [40, 38, 35, 37, 33] }
		]
	};

	type Service = { name: string; bookings: number; revenue: number };
	const services: Service[] = [
		{ name: 'Balayage', bookings: 64, revenue: 7680 },
		{ name: 'Colour', bookings: 92, revenue: 5888 },
		{ name: 'Cut and beard', bookings: 120, revenue: 5040 },
		{ name: 'Cut', bookings: 136, revenue: 3808 }
	];
	const columns: TableColumn<Service>[] = [
		{ key: 'name', label: 'Service' },
		{ key: 'bookings', label: 'Bookings', type: 'number', sortable: true },
		{ key: 'revenue', label: 'Revenue', type: 'number', numberFormat: { style: 'currency', currency: 'EUR' }, sortable: true }
	];
</script>

<main class="page">
	<PageHeader title="Overview">
		{#snippet actionsSnippet()}
			<PeriodPicker bind:timespanSettings locale="en" />
		{/snippet}
	</PageHeader>

	<div class="indicators">
		{#each indicators as indicator (indicator.label)}
			<section class="card">
				<h2>{indicator.label}</h2>
				<p class="value">{indicator.value}</p>
				<Chip
					size="sm"
					variant={indicator.change >= 0 === (indicator.label !== 'No-shows') ? 'success' : 'error'}
					prependIcon={indicator.change >= 0 ? mdiTrendingUp : mdiTrendingDown}
				>
					{indicator.change > 0 ? '+' : ''}{indicator.change}%
				</Chip>
			</section>
		{/each}
	</div>

	<div class="charts">
		<section class="card">
			<h2>Revenue per week</h2>
			<BarChart data={revenue} stacked label="Revenue per week" format={{ style: 'currency', currency: 'EUR', maximumFractionDigits: 0 }} />
		</section>
		<section class="card">
			<h2>Bookings per channel</h2>
			<LineChart data={bookings} label="Bookings per channel" />
		</section>
	</div>

	<section class="card">
		<h2>Best services</h2>
		<Table caption="Best services" rows={services} {columns} rowKey="name" />
	</section>
</main>

<style>
	.page {
		display: grid;
		gap: 1.25rem;
		padding: 1.5rem;
	}

	.indicators {
		display: grid;
		grid-template-columns: repeat(auto-fit, minmax(min(100%, 9rem), 1fr));
		gap: 1rem;
	}

	.charts {
		display: grid;
		grid-template-columns: repeat(auto-fit, minmax(min(100%, 24rem), 1fr));
		gap: 1rem;
	}

	.card {
		display: grid;
		align-content: start;
		justify-items: start;
		gap: 0.5rem;
		min-width: 0;
		padding: 1.25rem;
		border: var(--global-border-width) solid var(--global-color-border);
		border-radius: var(--global-radius-lg);
		background: var(--global-color-surface);
		box-shadow: var(--global-shadow-sm);
	}

	.card > :global(*) {
		justify-self: stretch;
	}

	.card > :global(.aurora-chip) {
		justify-self: start;
	}

	h2 {
		margin: 0;
		font-size: 0.85rem;
		font-weight: 500;
		color: var(--global-color-text-2);
	}

	.value {
		margin: 0;
		font-family: var(--global-font-family-display);
		font-size: 1.8rem;
		font-weight: 600;
		color: var(--global-color-text);
	}
</style>
```
