Skip to the content
human-kit design
40 — Navigation

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 pagination
npx @human-kit/design add pagination
yarn dlx @human-kit/design add pagination
bunx @human-kit/design add pagination

The 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 siblings at 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 bound page, 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, nextLabel and pageLabel: they are the only words it has.

Accessibility

  • It renders a <nav> named "Pagination", in label, with the pages in a list.
  • Each page is a <button> named "Page 4"; the current one has aria-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

PropTypeDefault
pagenumber

The current page, from 1. Bindable.

1
countnumber

How many pages there are.

—
siblingsnumber

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.

—
labelstring

The name of the <nav>.

'Pagination'
previousLabelstring

The name of the left arrow.

'Previous page'
nextLabelstring

The name of the right arrow.

'Next page'
pageLabel(page: number) => string

The name of each page.

(n) => `Page ${n}`
classstring

Classes for the <nav>.

—

Plus every attribute of a <nav>.

paginationRange

PropTypeDefault
pagenumber

The current page.

—
countnumber

How many pages there are.

—
siblingsnumber

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.