Pagination
The pages of a long list: the ends, the current one with its neighbours, and an ellipsis for the rest.
Installation
pnpm dlx @human-kit/design add paginationnpx @human-kit/design add paginationyarn dlx @human-kit/design add paginationbunx @human-kit/design add paginationThe command copies the files to src/lib/components/ui/pagination. It also
installs the packages that the files import.
Usage
<script lang="ts">
import * as Pagination from '$lib/components/ui/pagination';
let page = $state(1);
</script>
<Pagination.Root bind:page count={24} /> Examples
With the range shown
Say which records the page holds, "41–50 of 236", beside the row. The numbers of the pages alone do not tell how long the list is.
1–10 of 236 invoices
Guidelines
- Use it under a long list or a table that the server returns a page at a time.
- Keep
siblingsat 1. The row then has seven places at most and keeps its width as the page moves, so the arrows stay under the pointer. - Load the new page in
onPageChange, or from the boundpage, and scroll back to the top of the list. - For a feed read from top to bottom, or a list short enough for one screen, load more on scroll or show it all instead.
- In another language, give
label,previousLabel,nextLabelandpageLabel: they are the only words it has.
Accessibility
- It renders a
<nav>named "Pagination", inlabel, with the pages in a list. - Each page is a
<button>named "Page 4"; the current one hasaria-current="page". - The ellipsis is hidden from screen readers: the numbers on each side of it say what is skipped.
- At the first or the last page its arrow stays in place, disabled, and leaves the tab order.
- The arrows turn round when the text runs right to left.
API reference
Pagination.Root
| Prop | Type | Default |
|---|---|---|
page | number The current page, from 1. Bindable. | 1 |
count | number How many pages there are. | — |
siblings | number Pages shown on each side of the current one. | 1 |
onPageChange | (page: number) => void Called with the new page, after a press that changes it. | — |
label | string The name of the | 'Pagination' |
previousLabel | string The name of the left arrow. | 'Previous page' |
nextLabel | string The name of the right arrow. | 'Next page' |
pageLabel | (page: number) => string The name of each page. | (n) => `Page ${n}` |
class | string Classes for the | — |
Plus every attribute of a <nav>.
paginationRange
| Prop | Type | Default |
|---|---|---|
page | number The current page. | — |
count | number How many pages there are. | — |
siblings | number Pages on each side of the current one. | 1 |
The places the row shows, for a pagination of your own: page numbers, and gap-start or gap-end for an ellipsis.