# Pagination

Pages through a long list: how many are showing, an optional rows-per-page choice, then previous, the pages around the current one with an ellipsis for each gap, and next. Built on ui-sdk's Pagination: the current page has aria-current, and previous and next stay focusable but disabled at the ends.

- Beta: ready to use; its API can still change in a minor release.
- Category: Navigation

```tsx
import { Pagination } from "@leap-crm/design-system";
```

## Best practices

- Do: Use it under a long list or table where people jump to a place, like page 4 of a firm's contacts.
- Do: Set `itemName` so the count reads "Showing 1–25 of 312 contacts", and give each pagination on a page its own `aria-label`, like "Contacts pages".
- Do: With `pageSizeOptions`, hold the size in state: pass it back as `pageSize` from `onPageSizeChange` and go back to page 1.
- Do: Pass `getHref` when each page has its own URL, so a lawyer can reopen or share the page they were on.
- Don't: Disable previous or next yourself at the ends. They already stay focusable but disabled, so keyboard users don't lose their place.

## Keyboard

| Keys | What they do |
| --- | --- |
| Enter, Space | Go to the focused page. |

## Related

- [Table](./table.md)
- [Tabs](./tabs.md)
- [Select](./select.md)

## Examples

What the examples import:

```tsx
import { useState } from "react";
import { Pagination } from "@leap-crm/design-system";
```

Scaffolding the examples call. It lives in the docs page, not in the library:

```tsx
function WithPageSize() {
  const [pageSize, setPageSize] = useState(25);
  const [page, setPage] = useState(1);
  return (
    <Pagination
      total={312}
      itemName="contacts"
      page={page}
      onPageChange={setPage}
      pageSize={pageSize}
      pageSizeOptions={[25, 50, 100]}
      onPageSizeChange={size => {
        setPageSize(size);
        setPage(1);
      }}
      aria-label="Contacts pages"
    />
  );
}
```

### Pages

312 contacts at 25 a page, on page 4. Arrows step, numbers jump; the ellipses mark the pages in between.

```tsx
<div className="w-full max-w-2xl">
  <Pagination
    total={312}
    defaultPage={4}
    itemName="contacts"
    aria-label="Contacts pages"
  />
</div>
```

### Rows per page

pageSizeOptions adds the choice; changing it goes back to page 1.

```tsx
<div className="w-full max-w-2xl">
  <WithPageSize />
</div>
```

### Few pages, and links

Three pages show without ellipses. getHref makes every page a link, for lists whose pages have their own URL.

```tsx
<div className="w-full max-w-2xl">
  <Pagination
    total={61}
    itemName="tasks"
    getHref={page => `/pagination?page=${page}`}
    aria-label="Tasks pages"
  />
</div>
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `page` | `number` |  | The current page, from 1. Pass it to control the pagination. |
| `defaultPage` | `number` |  | The page it starts on, when uncontrolled. |
| `onPageChange` | `(page: number) => void` |  | Runs with the page to show next, from 1. |
| `total` (required) | `number` |  | How many items there are in all. |
| `pageSize` | `number` | `25` | Items on a page. |
| `pageSizeOptions` | `number[]` |  | Offers a page-size choice with these sizes, like [25, 50, 100]. |
| `onPageSizeChange` | `(pageSize: number) => void` |  | Runs with the page size chosen. Go back to page 1 when it changes. |
| `itemName` | `string` |  | What the items are, for "Showing 1–25 of 312 contacts". |
| `getHref` | `(page: number) => string` |  | Makes each page a link, for pages with their own URL. |
| `siblings` | `number` | `1` | Pages shown either side of the current one before an ellipsis. |
| `disabled` | `boolean` |  | Shows it as unavailable and stops it responding. |
| `aria-label` | `string` | `"Pagination"` | Names the navigation, like "Contacts pages". |
| `className` | `string` |  | Classes added to the root element, merged with its own. Tokens only. |
