# Paginator

Navigation between the pages of a paged list or table: first, previous, the page numbers, next and last. `page` starts from 1 and is bindable; `onchange({ page })` is called when the user picks another page. With `pageCount` it shows the first and the last page, the current one with `siblingCount` pages on each side and "…" for the hidden ones, always the same number of items so the buttons do not move while paging; the arrows at the ends are disabled (`aria-disabled`, so they keep the focus). Without `pageCount` the total is unknown: it shows the previous, the current and the next page, and "next" is always enabled. The paginator never changes `page` on its own: when `page` is past `pageCount` no number is marked and "previous" goes to the last page. With `pageHref` the controls are links, so a page can live in the URL and open in a new tab. `compact` shows "7 / 24" between the arrows instead of the numbers. It is a `<nav>` named "Pagination" in the language of `locale`, the current page has `aria-current="page"`. Its state is exposed as `data-compact` on the root and `data-current` on the current page for app CSS.

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

Category: simple. Source: `src/lib/components/simple/navigation/Paginator.svelte`.

## Examples

### Pages of a list

`bind:page` holds the current page and `pageCount` the number of pages. The first and the last page are always there, the current one has a page on each side and "…" stands for the hidden ones, so the paginator keeps the same number of buttons while paging. Load the page in `onchange`.

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

	let page = $state(7);
	let loaded = $state(7);
</script>

<div class="list">
	<Paginator bind:page pageCount={24} onchange={(event) => (loaded = event.page)} />
	<p>Showing page {loaded} of 24</p>
</div>

<style>
	.list {
		display: flex;
		flex-direction: column;
		gap: 12px;
	}

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

### Unknown number of pages

Without `pageCount` the paginator shows the previous, the current and the next page, "next" is always enabled and there is no "last" button. Use it when the backend does not count the results.

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

	let page = $state(3);
</script>

<Paginator bind:page />
```

### Pages as links

With `pageHref` every control is a link: the page can live in the URL (`?page=3`), a middle click opens it in a new tab and SvelteKit can preload it. A plain click still updates `page` and calls `onchange`. In an app, read the page from `page.url.searchParams` in `load`.

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

	let page = $state(1);
</script>

<Paginator bind:page pageCount={8} pageHref={(target) => `#page-${target}`} />
```

### Compact

`compact` shows "3 / 12" between the arrows instead of the page numbers, for a card footer or a phone screen. Screen readers hear "Page 3 of 12" when the page changes.

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

	let page = $state(3);
</script>

<Paginator bind:page pageCount={12} compact />
```

### More or fewer numbers

`siblingCount` sets how many pages appear on each side of the current one; `showFirstLast={false}` hides the buttons to the first and the last page, which are already reachable through the numbers.

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

	let page = $state(10);
</script>

<div class="rows">
	<Paginator bind:page pageCount={40} siblingCount={0} showFirstLast={false} />
	<Paginator bind:page pageCount={40} siblingCount={2} />
</div>

<style>
	.rows {
		display: flex;
		flex-direction: column;
		gap: 12px;
	}
</style>
```

### Custom style

Every part has a CSS variable: here round buttons, the body font and a soft current page.

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

	let page = $state(2);
</script>

<Paginator
	bind:page
	pageCount={6}
	showFirstLast={false}
	--paginator-font-family="inherit"
	--paginator-button-size="34px"
	--paginator-button-border-radius="999px"
	--paginator-current-background="var(--global-color-primary-soft)"
	--paginator-current-color="var(--global-color-primary)"
	--paginator-current-box-shadow="none"
/>
```

## Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `page` (bindable) | `number` |  | Current page, from 1. Empty means 1. |
| `pageCount` | `number` |  | Number of pages. Without it the total is unknown: the previous, the current and the next page are shown, "next" is always enabled and there is no "last". Less than 1 counts as 1. |
| `siblingCount` | `number` | `1` | Pages shown on each side of the current one, between the first and the last page. |
| `showFirstLast` | `boolean` | `true` | Shows the buttons to the first and to the last page. |
| `compact` | `boolean` | `false` | Shows "7 / 24" between the arrows instead of the page numbers, for narrow spaces. |
| `pageHref` | `(page: number) => string` |  | URL of a page. With it every control is a link, so the page can live in the URL and open in a new tab; a plain click still updates `page` and calls `onchange`. |
| `locale` | `string` |  | BCP 47 locale of the accessible names: `it` and `en` are built in, other languages fall back to English. Defaults to the one set with `setDefaultLocale`, else `en`. |
| `aria-label` | `string \| null` |  | Accessible name of the `<nav>`. Defaults to "Pagination", in the language of `locale`. |
| `firstLabel` | `string` |  | Accessible name of the first page button. Defaults to "First page", in the language of `locale`. |
| `previousLabel` | `string` |  | Accessible name of the previous page button. Defaults to "Previous page", in the language of `locale`. |
| `nextLabel` | `string` |  | Accessible name of the next page button. Defaults to "Next page", in the language of `locale`. |
| `lastLabel` | `string` |  | Accessible name of the last page button. Defaults to "Last page", in the language of `locale`. |
| `pageLabel` | `string` |  | Accessible name of every page number, where `{page}` is replaced by the number. Defaults to "Page {page}", in the language of `locale`. |
| `statusLabel` | `string` |  | Text read by screen readers in the `compact` look, where `{page}` and `{count}` are replaced by the current page and the number of pages. Defaults to "Page {page} of {count}", in the language of `locale`. |
| `firstIcon` | `string` | `FIRST_ICON` | SVG path of the first page icon. |
| `previousIcon` | `string` | `PREVIOUS_ICON` | SVG path of the previous page icon. |
| `nextIcon` | `string` | `NEXT_ICON` | SVG path of the next page icon. |
| `lastIcon` | `string` | `LAST_ICON` | SVG path of the last page icon. |
| `class` | `{ container?: ClassValue; button?: ClassValue; current?: ClassValue; }` | `{}` | Classes of the parts: `container` the root, `button` every arrow and page number, `current` the current page. |
| `paginatorElement` (bindable) | `HTMLElement` |  | The root element. |

## Snippets

| Name | Parameters | Description |
| --- | --- | --- |
| `pageSnippet` | `{ page: number; current: boolean }` | Replaces the number inside every page button. |

## Events

| Name | Type | Description |
| --- | --- | --- |
| `onchange` | `(event: { page: number }) => void` | Called when the user picks another page, after `page` has changed. Not called for the current page. |

## CSS variables

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

| Per-instance variable | Default |
| --- | --- |
| `--paginator-gap` | `--paginator-default-gap`: `4px` |
| `--paginator-font-family` | `--paginator-default-font-family`: `var(--global-font-family-mono)` |
| `--paginator-font-size` | `--paginator-default-font-size`: `13px` |
| `--paginator-icon-size` | `--paginator-default-icon-size`: `16px` |
| `--paginator-button-background` | `--paginator-default-button-background`: `transparent` |
| `--paginator-button-size` | `--paginator-default-button-size`: `30px` |
| `--paginator-button-padding` | `--paginator-default-button-padding`: `0 8px` |
| `--paginator-button-border-radius` | `--paginator-default-button-border-radius`: `var(--global-radius-sm)` |
| `--paginator-button-color` | `--paginator-default-button-color`: `var(--global-color-text-2)` |
| `--paginator-button-font-weight` | `--paginator-default-button-font-weight`: `400` |
| `--paginator-duration` | `--paginator-default-duration`: `var(--global-duration)` |
| `--paginator-easing` | `--paginator-default-easing`: `var(--global-ease)` |
| `--paginator-button-hover-background` | `--paginator-default-button-hover-background`: `var(--global-color-surface-2)` |
| `--paginator-button-hover-color` | `--paginator-default-button-hover-color`: `var(--global-color-text)` |
| `--paginator-current-background` | `--paginator-default-current-background`: `var(--global-fill-primary)` |
| `--paginator-current-box-shadow` | `--paginator-default-current-box-shadow`: `var(--global-shadow-fill)` |
| `--paginator-current-color` | `--paginator-default-current-color`: `var(--global-color-on-primary)` |
| `--paginator-current-font-weight` | `--paginator-default-current-font-weight`: `600` |
| `--paginator-focus-ring-width` | `--paginator-default-focus-ring-width`: `var(--global-focus-ring-width)` |
| `--paginator-focus-ring-color` | `--paginator-default-focus-ring-color`: `var(--global-focus-ring-color)` |
| `--paginator-focus-ring-offset` | `--paginator-default-focus-ring-offset`: `var(--global-focus-ring-offset)` |
| `--paginator-disabled-opacity` | `--paginator-default-disabled-opacity`: `0.4` |
| `--paginator-ellipsis-padding` | `--paginator-default-ellipsis-padding`: `0 4px` |
| `--paginator-ellipsis-color` | `--paginator-default-ellipsis-color`: `var(--global-color-text-3)` |
| `--paginator-status-padding` | `--paginator-default-status-padding`: `0 8px` |
| `--paginator-status-color` | `--paginator-default-status-color`: `var(--global-color-text-2)` |
