Pagination
Previous and next controls with numbered page links and ellipsis gaps, and the current page drawn as a raised key.
pnpm dlx shadcn@latest add @haze/pagination---
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>
)
}When to use
- 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
When not to use
- Feeds that load more on scroll
- Two or three steps in a wizard; use stepper
Installation
Add the Haze registry to components.json once, then install Pagination with the shadcn CLI. Works in any Astro project with Tailwind CSS v4.
pnpm dlx shadcn@latest add @haze/paginationnpx shadcn@latest add @haze/paginationyarn dlx shadcn@latest add @haze/paginationbunx --bun shadcn@latest add @haze/paginationFirst time? Registry and theme setup
1. Add to components.json:
{
"registries": {
"@haze": "http://localhost:4329/r/{name}.json"
}
}2. Install the theme once: npx shadcn@latest add @haze/theme, then import it after Tailwind in your global CSS:@import "./haze.css";
API reference
| Prop | Type | Default | Description |
|---|---|---|---|
| current* | number | – | Current page, 1-based; clamped to the range 1 to total. |
| total* | number | – | Number of pages. |
| hrefPrefix | string | "?page=" | Page n links to the prefix followed by n. |
| siblings | number | 1 | Pages shown on each side of the current page; the first and last page always show. |
| label | string | "Pagination" | Accessible name of the nav landmark. Give each pagination on one page a different name. |
| previousLabel | string | "Previous" | Text of the previous control; below sm it is visually hidden but stays in the accessibility tree. |
| nextLabel | string | "Next" | Text of the next control; below sm it is visually hidden but stays in the accessibility tree. |
| pageLabel | string | "Page" | Prefix of each page link's accessible name, followed by the number ("Page 4"). Translate it with the other labels. |
| moreLabel | string | "More pages" | Screen-reader text for the "…" gaps. |
| class | string | – | Extra classes on the nav. |
Keyboard interactions
- Tab
- Move between the page links
- Enter
- Open the focused page
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.
Notes for AI
Copy into Claude Code, Cursor, Codex or any coding agent. Agents can also read the Markdown version of this page.
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": "http://localhost:4329/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: http://localhost:4329/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.
Dependencies
- npm: @lucide/astro
- @haze/utils
- @haze/theme (one-time setup)
Source · Original Haze UI work.