# Pagination

Moves through pages of results with page numbers, previous/next and a page-size choice.

- Family: navigation
- Import: `import { Pagination } from "@senvenli/react/navigation";`
- Page: https://design.senvenli.com/docs/components/pagination
- JSON: https://design.senvenli.com/docs/components/pagination.json

## Example: Pages of results

The range, the page numbers and previous/next. The app owns the page.

```tsx
import { Pagination } from "@senvenli/react/navigation";
import { useState } from "react";

export default function Example() {
  const [page, setPage] = useState(3);
  return <Pagination page={page} pageSize={20} total={1204} onPageChange={setPage} />;
}
```

## Examples

### With a page-size choice

Changing the size goes back to the first page.

```tsx
import { Pagination } from "@senvenli/react/navigation";
import { useState } from "react";

export default function Example() {
  const [page, setPage] = useState(1);
  const [pageSize, setPageSize] = useState(25);
  return (
    <Pagination
      page={page}
      pageSize={pageSize}
      total={312}
      pageSizeOptions={[10, 25, 50]}
      onPageChange={setPage}
      onPageSizeChange={(size) => {
        setPageSize(size);
        setPage(1);
      }}
    />
  );
}
```

### Naming the items

A formatRange function writes the range sentence; format its numbers for the locale yourself.

```tsx
import { Pagination } from "@senvenli/react/navigation";
import { useState } from "react";

// Name the locale: a bare `toLocaleString()` would differ between the server and the browser.
const number = new Intl.NumberFormat("en-US");

export default function Example() {
  const [page, setPage] = useState(3);
  return (
    <Pagination
      page={page}
      pageSize={20}
      total={1204}
      onPageChange={setPage}
      formatRange={(from, to, total) => `${number.format(from)}–${number.format(to)} of ${number.format(total)} orders`}
    />
  );
}
```

## Guidelines

### When to use it

- A list or table with more results than fit one page.

### Use something else when

- [data-table](https://design.senvenli.com/docs/components/data-table.md): The rows already live in a table with paging built in.
- [button](https://design.senvenli.com/docs/components/button.md): A single Load more continues a feed.

## Accessibility

Built in:

- The current page is marked; the range is announced ("41–60 of 1,204").

## API reference

### Pagination

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| page | `number` |  | yes | 1-based. Keep it in the URL so back and refresh return to it. |
| pageSize | `number` |  | yes |  |
| total | `number` |  | yes | All items across pages. |
| onPageChange | `(page: number) => void` |  | yes |  |
| onPageSizeChange | `(pageSize: number) => void` |  | no |  |
| pageSizeOptions | `readonly number[]` | `[20, 50, 100]` | no | 20, 50 and 100 by default; the current size joins the list when it is not in it. |
| formatRange | `(from: number, to: number, total: number) => string` |  | no | The range sentence naming the items: (41, 60, 1204) → "41–60 of 1,204 orders". It gets the raw numbers; format them for the locale yourself (Intl.NumberFormat). |

### Tokens it reads

`--ds-background-neutral-hovered`, `--ds-background-neutral-pressed`, `--ds-background-neutral-subtle-hovered`, `--ds-border-focused`, `--ds-radius-control-medium`, `--ds-space-050`, `--ds-space-075`, `--ds-space-100`, `--ds-space-200`, `--ds-text`, `--ds-text-disabled`, `--ds-text-subtle`

## Related

- [toolbar](https://design.senvenli.com/docs/components/toolbar.md)
