Pagination
Page links or buttons with previous and next, ellipses for long ranges, and a compact form.
Demo
Usage
import { Pagination } from '@tuttn/slant'
// Uncontrolled: starts at defaultValue (page 1 when omitted). Pages are 1-based.
<Pagination count={20} defaultValue={3} onValueChange={load} />
// Controlled: you hold the page.
<Pagination count={20} value={page} onValueChange={setPage} />
// Links: real URLs that work without JavaScript, and still report the page.
<Pagination count={20} value={page} onValueChange={setPage} href={n => `?page=${n}`} />
// More pages around the current one and at the ends; or the compact form.
<Pagination count={50} value={page} onValueChange={setPage} siblings={2} boundaries={2} />
<Pagination count={50} value={page} onValueChange={setPage} variant="compact" size="sm" />Props
| Prop | Type | Default | Description |
|---|---|---|---|
count | number | — | The number of pages. 0 or 1 leaves previous and next both disabled. |
value | number | — | The current page (1-based), when controlled. Clamped to 1…count. |
defaultValue | number | 1 | The page shown first (1-based), when uncontrolled. |
onValueChange | (page: number) => void | — | Called with the page (1-based) the user moves to. |
siblings | number | 1 | How many pages to show on either side of the current one. |
boundaries | number | 1 | How many pages to always show at each end. |
href | (page: number) => string | — | Each page's URL. Given, every control is a real link that works without JavaScript, and a click still calls `onValueChange`; otherwise they're buttons. |
variant | 'pages' | 'compact' | 'pages' | `pages`: previous, the page numbers with ellipses for gaps, and next. `compact`: previous, "Page 3 of 10", and next. |
size | 'sm' | 'md' | 'md' | Control height: 28 or 34px at regular density. |
label | string | messages.pagination ("Pagination") | The nav's accessible name. |
…rest | HTMLAttributes | — | className, style, other attributes and the ref go on the <nav>. |
A <nav> landmark named label (“Pagination” by default) around a list: previous, the pages, next. Every page control is named “Page 4” and shows the number; the current one has aria-current="page". Previous and next are disabled at the ends: disabled on a button, aria-disabled (and no href) on a link. When one of them disables itself under the keyboard, focus moves to the current page.
There are no arrow keys: Tab walks the controls and Enter or Space activates one. Past 2 × boundaries + 2 × siblings + 3 pages, the row shows the first and last boundaries pages, the current one with siblings either side, and an ellipsis for each gap. The number of slots stays the same wherever the current page is, so nothing jumps under the pointer; the ellipses are hidden from screen readers. value is clamped to 1…count, and a count of 0 or 1 leaves both arrows disabled.
With href, a plain click also calls onValueChange, while a modified click (a new tab, say) leaves the page alone. The compact form reads “Page 3 of 10” between the arrows and announces the change politely. The chevrons mirror under right-to-left text.