# Pagination

Links to the pages of a long list of results.

**Our choice:** No standard exists: a labelled `<nav>` with a list of links and `aria-current="page"`, as for breadcrumbs.

_Diagram: Pagination is the row of page links under a long list of results, with the current page marked._

## Live example

### Middle of a long list

```html
<nav aria-label="Pagination" data-pagination>
  <ul>
    <li><a href="?page=4" rel="prev">Previous<span class="visually-hidden"> page</span></a></li>
    <li><a href="?page=1"><span class="visually-hidden">Page </span>1</a></li>
    <li aria-hidden="true">…</li>
    <li><a href="?page=4"><span class="visually-hidden">Page </span>4</a></li>
    <li><a href="?page=5" aria-current="page"><span class="visually-hidden">Page </span>5</a></li>
    <li><a href="?page=6"><span class="visually-hidden">Page </span>6</a></li>
    <li aria-hidden="true">…</li>
    <li><a href="?page=10"><span class="visually-hidden">Page </span>10</a></li>
    <li><a href="?page=6" rel="next">Next<span class="visually-hidden"> page</span></a></li>
  </ul>
</nav>
```

### First page

```html
<nav aria-label="Pagination, search results" data-pagination>
  <ul>
    <li><a>Previous<span class="visually-hidden"> page</span></a></li>
    <li><a href="?page=1" aria-current="page"><span class="visually-hidden">Page </span>1</a></li>
    <li><a href="?page=2"><span class="visually-hidden">Page </span>2</a></li>
    <li><a href="?page=3"><span class="visually-hidden">Page </span>3</a></li>
    <li><a href="?page=2" rel="next">Next<span class="visually-hidden"> page</span></a></li>
  </ul>
</nav>
```

## Attributes

| Attribute or value | Where | Effect |
| --- | --- | --- |
| `data-pagination` | `<nav aria-label="Pagination">` |  |
| `aria-current="page"` | the current page link (native ARIA) |  |
| `<a> without href` |  | a disabled Previous or Next (native placeholder link) |
| `<li aria-hidden="true">…</li>` |  | a gap in the page numbers |

### Keyboard

| Key | Action |
| --- | --- |
| <kbd>Tab</kbd> | moves to the next page link / Shift+Tab: previous |
| <kbd>Enter</kbd> | follows the focused link |

## Accessibility

- A visually hidden "Page" completes each number's link name ([WCAG 2.4.4](https://www.w3.org/WAI/WCAG22/Understanding/link-purpose-in-context.html)).
- At the ends, Previous or Next is an `<a>` with no `href`: not focusable.
- **Our choice:** the current page stays a link with `aria-current="page"`, so the list count stays stable.

## Specifications

- [HTML `<nav>` element](https://html.spec.whatwg.org/multipage/sections.html#the-nav-element)
- [MDN `aria-current`](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Reference/Attributes/aria-current)
- [WHATWG link type next](https://html.spec.whatwg.org/multipage/links.html#link-type-next)
- [WHATWG the a element](https://html.spec.whatwg.org/multipage/text-level-semantics.html#the-a-element)
- [WCAG Target size minimum](https://www.w3.org/WAI/WCAG22/Understanding/target-size-minimum.html)

## See also

- [Molecules](https://domestic-ui.odonckers.dev/molecules/index.md)
- Source: [`css/components/molecules/pagination.css`](https://github.com/odonckers/domestic-ui/blob/main/css/components/molecules/pagination.css), [`examples/molecules/pagination.html`](https://github.com/odonckers/domestic-ui/blob/main/examples/molecules/pagination.html)

## How other systems do it

For comparison only; these are not the standard this system follows.

- Apple Human Interface Guidelines: No equivalent
- Material Design 3: No equivalent
- [Vercel Geist: Pagination](https://vercel.com/geist/pagination)
