# Pagination

> Previous and next controls with numbered page links and ellipsis gaps, and the current page drawn as a raised key.

- Kind: ui · Category: Navigation · Interactivity: native
- Install: `npx shadcn@latest add @haze/pagination`
- File: `src/components/haze/ui/pagination.astro`
- npm dependencies: @lucide/astro
- Registry dependencies: @haze/utils
- Docs page: https://hazeui.com/ui/pagination

## Use when
- Search results, tables or blog archives split over server-rendered pages
- Letting readers jump straight to a page number
- A page list that must shrink to icon-only arrows on a 320 px screen

## Avoid when
- Feeds that load more on scroll
- Two or three steps in a wizard; use stepper

## Alternatives
- [stepper](https://hazeui.com/ui/stepper.md)
- [breadcrumb](https://hazeui.com/ui/breadcrumb.md)

## Props

| Name | Type | Default | Required | Description |
|---|---|---|---|---|
| current | `number` |  | yes | Current page, 1-based; clamped to the range 1 to total. |
| total | `number` |  | yes | Number of pages. |
| hrefPrefix | `string` | "?page=" | no | Page n links to the prefix followed by n. |
| siblings | `number` | 1 | no | Pages shown on each side of the current page; the first and last page always show. |
| label | `string` | "Pagination" | no | Accessible name of the nav landmark. Give each pagination on one page a different name. |
| previousLabel | `string` | "Previous" | no | Text of the previous control; below sm it is visually hidden but stays in the accessibility tree. |
| nextLabel | `string` | "Next" | no | Text of the next control; below sm it is visually hidden but stays in the accessibility tree. |
| pageLabel | `string` | "Page" | no | Prefix of each page link's accessible name, followed by the number ("Page 4"). Translate it with the other labels. |
| moreLabel | `string` | "More pages" | no | Screen-reader text for the "…" gaps. |
| class | `string` |  | no | Extra classes on the nav. |

## Accessibility
- The current page carries aria-current="page" and is drawn as a raised key with a border and a fill, so it is told apart without color alone.
- On the first and last page the previous or next control is a span with aria-disabled, not a dead link.
- Number links are named "Page 4" (the prefix is the pageLabel prop) and are 36 by 36 px. The "…" gaps are aria-hidden and carry hidden text from moreLabel.
- The list has role="list" so Safari keeps its list semantics. With total below 1 nothing is rendered.
- Two paginations on one page need different label values, so their landmarks can be told apart.
- Key Tab: Move between the page links
- Key Enter: Open the focused page

## Agent prompts

### Install and use

Install the Haze UI component "Pagination" with `npx shadcn@latest add @haze/pagination`. If components.json has no "@haze" registry yet, add "registries": { "@haze": "https://hazeui.com/r/{name}.json" }. If src/styles/haze.css does not exist yet, run `npx shadcn@latest add @haze/theme` once and import it from the global stylesheet after Tailwind. The CLI stops at an overwrite question when a Haze file already exists and differs, even with --yes, and then installs nothing after it: read its output. If the user has not edited their Haze files, re-run with --overwrite to take the registry versions; if they have, ask them before overwriting. Then import Pagination from "@/components/haze/ui/pagination.astro" (or the relative path) and render it where it fits the page. Keep Haze token classes (bg-background, text-muted-foreground, border-border...) and never hardcode colors. Docs: https://hazeui.com/ui/pagination.md

### Customize with real content

Adapt the Haze pagination ui for our product. Replace the demo values of these props with our real content: current, total, hrefPrefix, siblings, label, previousLabel, nextLabel, pageLabel, moreLabel, class. Keep the layout, the heading level rules and these accessibility details: The current page carries aria-current="page" and is drawn as a raised key with a border and a fill, so it is told apart without color alone. On the first and last page the previous or next control is a span with aria-disabled, not a dead link. Number links are named "Page 4" (the prefix is the pageLabel prop) and are 36 by 36 px. The "…" gaps are aria-hidden and carry hidden text from moreLabel. The list has role="list" so Safari keeps its list semantics. With total below 1 nothing is rendered. Two paginations on one page need different label values, so their landmarks can be told apart. Do not add raw color classes; use the Haze tokens.

### Choose the right block

Before using pagination, check it fits: use it when search results, tables or blog archives split over server-rendered pages; letting readers jump straight to a page number; a page list that must shrink to icon-only arrows on a 320 px screen. Avoid it when feeds that load more on scroll; two or three steps in a wizard; use stepper. Alternatives: @haze/stepper, @haze/breadcrumb.

## Example requests that should lead here
- Add pagination for 24 pages with page 7 selected
- Use ?p= as the link prefix and show two siblings

## Source

```astro
---
import { ChevronLeft, ChevronRight } from "@lucide/astro";
import { cn } from "../../../lib/haze/utils";

interface Props {
  /** Current page, 1-based. */
  current: number;
  total: number;
  hrefPrefix?: string;
  /** Pages shown on each side of the current one. */
  siblings?: number;
  /** Accessible name of the nav landmark. Give each pagination on a page a different one. */
  label?: string;
  previousLabel?: string;
  nextLabel?: string;
  /** Prefix of each page link's accessible name, followed by the number ("Page 4"). */
  pageLabel?: string;
  /** Screen-reader text for the "…" gaps. */
  moreLabel?: string;
  class?: string;
}

const {
  current,
  total,
  hrefPrefix = "?page=",
  siblings = 1,
  label = "Pagination",
  previousLabel = "Previous",
  nextLabel = "Next",
  pageLabel = "Page",
  moreLabel = "More pages",
  class: className,
} = Astro.props;

const page = Math.min(Math.max(Math.round(current), 1), Math.max(total, 1));
const wanted = new Set<number>([1, total]);
for (let n = page - siblings; n <= page + siblings; n++) {
  if (n >= 1 && n <= total) wanted.add(n);
}
const sorted = [...wanted].sort((a, b) => a - b);
const entries: (number | "gap")[] = [];
sorted.forEach((n, i) => {
  const prev = sorted[i - 1];
  if (prev !== undefined && n - prev === 2) entries.push(prev + 1);
  else if (prev !== undefined && n - prev > 2) entries.push("gap");
  entries.push(n);
});

const focus = "focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-ring";
const pageBase =
  "inline-flex size-8 items-center justify-center rounded-lg border border-transparent text-[0.8125rem] font-medium tabular-nums text-muted-foreground transition-[background-color,border-color,color,scale] duration-150 ease-snappy hover:bg-accent hover:text-foreground active:scale-[0.97] active:duration-75 motion-reduce:transition-none motion-reduce:active:scale-100";
const currentKey = "border-frame bg-field text-foreground shadow-key hover:bg-field";
const stepBase =
  "inline-flex h-8 min-w-8 items-center justify-center gap-1 rounded-lg border border-transparent px-2 text-[0.8125rem] font-medium text-muted-foreground transition-[background-color,border-color,color,scale] duration-150 ease-snappy sm:px-3";
const stepLink = "hover:bg-accent hover:text-foreground active:scale-[0.97] active:duration-75 motion-reduce:active:scale-100";
const stepOff = "opacity-50";
---

{
  total >= 1 && (
    <nav aria-label={label} class={className}>
      <ul role="list" class="flex flex-wrap items-center gap-1">
        <li>
          {
            page > 1 ? (
              <a href={`${hrefPrefix}${page - 1}`} aria-label={previousLabel} class={cn(stepBase, stepLink, focus)}>
                <ChevronLeft class="size-4" aria-hidden="true" />
                <span class="sr-only sm:not-sr-only">{previousLabel}</span>
              </a>
            ) : (
              <span aria-disabled="true" class={cn(stepBase, stepOff)}>
                <ChevronLeft class="size-4" aria-hidden="true" />
                <span class="sr-only sm:not-sr-only">{previousLabel}</span>
              </span>
            )
          }
        </li>
        {
          entries.map((entry) => (
            <li>
              {entry === "gap" ? (
                <span class="inline-flex size-8 items-center justify-center text-[0.8125rem] text-muted-foreground">
                  <span aria-hidden="true">…</span>
                  <span class="sr-only">{moreLabel}</span>
                </span>
              ) : entry === page ? (
                <a href={`${hrefPrefix}${entry}`} aria-current="page" aria-label={`${pageLabel} ${entry}`} class={cn(pageBase, currentKey, focus)}>{entry}</a>
              ) : (
                <a href={`${hrefPrefix}${entry}`} aria-label={`${pageLabel} ${entry}`} class={cn(pageBase, focus)}>{entry}</a>
              )}
            </li>
          ))
        }
        <li>
          {
            page < total ? (
              <a href={`${hrefPrefix}${page + 1}`} aria-label={nextLabel} class={cn(stepBase, stepLink, focus)}>
                <span class="sr-only sm:not-sr-only">{nextLabel}</span>
                <ChevronRight class="size-4" aria-hidden="true" />
              </a>
            ) : (
              <span aria-disabled="true" class={cn(stepBase, stepOff)}>
                <span class="sr-only sm:not-sr-only">{nextLabel}</span>
                <ChevronRight class="size-4" aria-hidden="true" />
              </span>
            )
          }
        </li>
      </ul>
    </nav>
  )
}
```
