# PageHeader

Header of a page: an optional link back, the title as a heading (`h1` by default, `headingLevel` to change it), a subtitle, a description and the page actions on the right, which wrap under the text when there is no room. With `backHref` the back control is a native link (it can be opened in a new tab and SvelteKit preloads it); with only `back` it is a button that goes back in the browser history. `onback` runs before either and can stop it with `event.preventDefault()`, for example to save first. `size="sm"` is for headers inside a section or a panel. Once the root layout calls `setTitleTemplate`, a header with `headingLevel` 1 also writes the title of the browser tab in the format of the app (`documentTitle` changes or turns it off). It has no space below it, like every component (the layout decides, for example with `gap`); `--page-header-margin-bottom` adds it, and setting `--page-header-default-margin-bottom` in the app CSS gives it to every instance. Its size is exposed as `data-size` for app CSS.

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

Category: simple. Source: `src/lib/components/simple/common/PageHeader.svelte`.

## Examples

### List page

A title, one line of subtitle and the page action on the right. The title is the `h1` of the page.

```svelte
<script lang="ts">
	import { mdiPlus } from '@mdi/js';
	import { Button, PageHeader } from '@likable-hair/svelte';
</script>

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

<style>
	.page {
		width: 100%;
	}
</style>
```

### Detail page

`backHref` adds a link back to the list: a native link, so it opens in a new tab and SvelteKit preloads it. `prependSnippet` holds an `Avatar` and `subtitleSnippet` adds a status `Chip`; the primary action goes last.

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

<div class="page">
	<PageHeader title="Giulia Marini" backHref="#clients" backLabel="Clients">
		{#snippet prependSnippet()}
			<Avatar name="Giulia Marini" size="lg" />
		{/snippet}
		{#snippet subtitleSnippet()}
			<span class="meta">Client since March 2023 <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>
</div>

<style>
	.page {
		width: 100%;
	}

	.meta {
		display: inline-flex;
		flex-wrap: wrap;
		align-items: center;
		gap: 8px;
	}
</style>
```

### Form page

`back` alone goes back in the browser history. `onback` runs first: calling `event.preventDefault()` keeps the user on the page, here to confirm before leaving unsaved changes.

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

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

<div class="page">
	<PageHeader
		back
		title="New client"
		subtitle="The client gets an email to confirm the account."
		description="Fields marked with * are required."
		onback={(event) => {
			event.preventDefault();
			leaving = true;
		}}
	>
		{#snippet actionsSnippet()}
			<ConfirmOrCancelButtons confirmText="Save" />
		{/snippet}
	</PageHeader>
	{#if leaving}
		<p class="note">Back stopped by <code>onback</code>: ask before leaving.</p>
	{/if}
</div>

<style>
	.page {
		width: 100%;
	}

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

### Section header

`size="sm"` and `headingLevel={2}` for a header inside a card, a panel or a dialog, where the page already has its `h1`.

```svelte
<script lang="ts">
	import { mdiDownload } from '@mdi/js';
	import { Button, PageHeader } from '@likable-hair/svelte';
</script>

<section class="card">
	<PageHeader size="sm" headingLevel={2} title="Invoices" subtitle="Last 12 months">
		{#snippet actionsSnippet()}
			<Button size="sm" variant="secondary" icon={mdiDownload}>Export</Button>
		{/snippet}
	</PageHeader>
</section>

<style>
	.card {
		box-sizing: border-box;
		width: min(100%, 560px);
		padding: 20px;
		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);
	}
</style>
```

### Editable title

`titleSnippet` replaces the content of the heading, here with a field that renames a dashboard. The heading level and the layout stay the same.

```svelte
<script lang="ts">
	import { mdiContentSave } from '@mdi/js';
	import { Button, PageHeader } from '@likable-hair/svelte';

	let name = $state('Sales overview');
</script>

<div class="page">
	<PageHeader back subtitle="Drag the widgets to arrange them.">
		{#snippet titleSnippet()}
			<input class="name" aria-label="Dashboard name" bind:value={name} />
		{/snippet}
		{#snippet actionsSnippet()}
			<Button icon={mdiContentSave}>Save</Button>
		{/snippet}
	</PageHeader>
</div>

<style>
	.page {
		width: 100%;
	}

	.name {
		box-sizing: border-box;
		width: 100%;
		padding: 2px 6px;
		margin-inline-start: -6px;
		border: var(--global-border-width) solid transparent;
		border-radius: var(--global-radius-sm);
		background: transparent;
		color: inherit;
		font: inherit;
	}

	@media (hover: hover) {
		.name:hover {
			border-color: var(--global-color-border);
		}
	}

	.name:focus-visible {
		outline: var(--global-focus-ring-width) solid var(--global-focus-ring-color);
		outline-offset: var(--global-focus-ring-offset);
	}
</style>
```

## Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `title` | `string` |  | Title of the page, rendered as a heading. Without it (and without `titleSnippet`) no heading is rendered. |
| `subtitle` | `string` |  | Line under the title. |
| `description` | `string` |  | Smaller text under the subtitle. |
| `headingLevel` | `1 \| 2 \| 3 \| 4 \| 5 \| 6` | `1` | Level of the title heading: `1` for the page, a deeper level inside a section, a panel or a dialog. |
| `size` | `'sm' \| 'md'` | `'md'` | `md` for a page, `sm` for a section or a panel. |
| `back` | `boolean` | `false` | Shows the back control. Without `backHref` it goes back in the browser history. |
| `backHref` | `string` |  | URL the back control links to. It also shows the control, without `back`. |
| `backLabel` | `string` |  | Text of the back control. Defaults to "Back", in the language of `locale`. |
| `backIcon` | `string` | `ARROW_LEFT_ICON` | SVG path of the back icon. |
| `documentTitle` | `string \| false` |  | Title of the browser tab for this page, in the format given to `setTitleTemplate`: another text than `title`, or `false` to leave the tab title alone. Without `setTitleTemplate` in a layout, or with a `headingLevel` other than 1, the tab title is never written. Defaults to `title`. |
| `locale` | `string` |  | BCP 47 locale of the texts: `it` and `en` are built in, other languages fall back to English. Defaults to the one set with `setDefaultLocale`, else `en`. |
| `class` | `{ container?: string; back?: string; title?: string; subtitle?: string; description?: string; actions?: string; }` | `{}` | Extra classes for each part. |

## Snippets

| Name | Parameters | Description |
| --- | --- | --- |
| `titleSnippet` | `{ title: string \| undefined }` | Replaces the content of the title heading, for example with an editable name. |
| `subtitleSnippet` | `{ subtitle: string \| undefined }` | Replaces the subtitle. |
| `descriptionSnippet` | `{ description: string \| undefined }` | Replaces the description. |
| `backSnippet` | `{ backLabel: string; backIcon: string }` | Replaces the icon and the text inside the back control. |
| `prependSnippet` |  | Before the title, for example an `Avatar`. |
| `actionsSnippet` |  | Page actions, on the right of the title: the primary action last. |

## Events

| Name | Type | Description |
| --- | --- | --- |
| `onback` | `(event: MouseEvent) => void` | Called when the back control is clicked, before it navigates. `event.preventDefault()` stops the navigation. |

## CSS variables

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

| Per-instance variable | Default |
| --- | --- |
| `--page-header-gap` | `--page-header-default-gap`: `10px` |
| `--page-header-margin-bottom` | `--page-header-default-margin-bottom`: `0px` |
| `--page-header-back-icon-size` | `--page-header-default-back-icon-size`: `18px` |
| `--page-header-back-gap` | `--page-header-default-back-gap`: `6px` |
| `--page-header-back-border-radius` | `--page-header-default-back-border-radius`: `var(--global-radius-xs)` |
| `--page-header-back-color` | `--page-header-default-back-color`: `var(--global-color-text-2)` |
| `--page-header-back-font-size` | `--page-header-default-back-font-size`: `13px` |
| `--page-header-back-font-weight` | `--page-header-default-back-font-weight`: `500` |
| `--page-header-back-hover-color` | `--page-header-default-back-hover-color`: `var(--global-color-text)` |
| `--page-header-focus-ring-width` | `--page-header-default-focus-ring-width`: `var(--global-focus-ring-width)` |
| `--page-header-focus-ring-color` | `--page-header-default-focus-ring-color`: `var(--global-focus-ring-color)` |
| `--page-header-focus-ring-offset` | `--page-header-default-focus-ring-offset`: `var(--global-focus-ring-offset)` |
| `--page-header-back-icon-shift` | `--page-header-default-back-icon-shift`: `-3px` |
| `--page-header-main-gap` | `--page-header-default-main-gap`: `12px 16px` |
| `--page-header-text-min-width` | `--page-header-default-text-min-width`: `16rem` |
| `--page-header-text-gap` | `--page-header-default-text-gap`: `4px` |
| `--page-header-title-color` | `--page-header-default-title-color`: `var(--global-color-text)` |
| `--page-header-title-font-family` | `--page-header-default-title-font-family`: `var(--global-font-family-display)` |
| `--page-header-title-font-size` | `--page-header-default-title-font-size`: `26px`<br>`--page-header-default-sm-title-font-size`: `19px` |
| `--page-header-title-font-weight` | `--page-header-default-title-font-weight`: `600` |
| `--page-header-title-line-height` | `--page-header-default-title-line-height`: `1.2` |
| `--page-header-subtitle-color` | `--page-header-default-subtitle-color`: `var(--global-color-text-2)` |
| `--page-header-subtitle-font-size` | `--page-header-default-subtitle-font-size`: `14px`<br>`--page-header-default-sm-subtitle-font-size`: `13px` |
| `--page-header-description-color` | `--page-header-default-description-color`: `var(--global-color-text-2)` |
| `--page-header-description-font-size` | `--page-header-default-description-font-size`: `13px`<br>`--page-header-default-sm-description-font-size`: `12px` |
| `--page-header-actions-gap` | `--page-header-default-actions-gap`: `8px` |
