Pagination
Moves through a long list one page at a time: previous, next and numbered pages, with an optional select for the rows on a page. Use it under a table or a list of results. For a short list that fits on one screen, show it whole; for a feed that grows as it is read, load more at the end instead.
Install
npx shadcn@latest add https://thrumui.dev/r/pagination.jsonAdds components/ui/pagination.tsx. Nothing else in your project changes.
import { pageRange } from "@/components/ui/pagination";Automatically installs icons chevron-left, chevron-right, chevrons-left and chevrons-right from the icon set.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
pageCount | number | Required | How many pages there are. Less than 1 is read as 1. |
page | number | – | The current page, counted from 1, when the parent holds it. Use it with onPageChange. A page outside the range is held at the nearest end. |
defaultPage | number | Default 1 | The page shown at first, when the pagination holds it itself. |
onPageChange | (page: number) => void | – | Called with the new page when the reader picks one, and with 1 when the page size changes. |
siblings | number | Default 1 | How many page buttons to keep on each side of the current page. The list has siblings × 2 + 5 entries once there are more pages than that. |
showFirstLast | boolean | Default false | Add buttons that jump to the first and the last page. |
compact | boolean | "auto" | Default "auto" | true always shows Page 3 of 12 instead of the numbered buttons, false never does. auto does so while the pagination is under 448 pixels wide. |
size | "sm" | "md" | "lg" | Default "md" | The size of every button and of the select: 28, 32 or 40 pixels. |
pageSizeOptions | readonly number[] | – | The choices of the Rows per page select. Leave it out to draw no select. |
pageSize | number | – | The selected page size, when the parent holds it. Use it with onPageSizeChange. |
defaultPageSize | number | – | The page size selected at first, when the pagination holds it itself. The default is the first option. |
onPageSizeChange | (pageSize: number) => void | – | Called with the new page size. The page goes back to 1; pass the new pageCount. |
sound | boolean | Default true | Play a soft click when the reader picks a page, if the visitor has sound on. |
className | string | – | Extra classes for the nav element, merged last. It fills the width it is given. |
...props | ComponentProps<"nav"> | – | Every other nav attribute. aria-label is Pagination unless you set another: do so when a page has two. |
pageRange() | (page, pageCount, siblings?) => (number | "ellipsis-start" | "ellipsis-end")[] | – | A second export: the list of buttons the component draws, for a pagination you build yourself, such as one made of links. |
Motion and sound
Motion
- A button's background and text colour change over 150 ms on hover and when it becomes the current page.
- A press scales the button to 97% and back over 150 ms.
- Reduced motion: colours and scale change at once.
Sound
- A soft click when the reader picks another page. Changing the rows per page is silent, like any native select.
Sound is off until the visitor turns it on. Pass sound={false} to keep this component silent. How sound works
Accessibility
- It is a nav landmark named Pagination holding a list of native buttons, each named in words: Previous page, Page 4, Last page.
- The current page carries aria-current="page" and is the only filled button, so it does not rest on colour alone.
- A polite live line reads Page 4 of 12 after every change, because the focus stays on the button that was pressed.
- Previous and Next use aria-disabled at the ends instead of the disabled attribute, so the focus is not lost when the reader reaches the first or last page.
- The select is a native one, named by its visible label Rows per page.
- Every button is at least 28 pixels square. In a narrow container the numbered buttons give way to the text Page 3 of 12, and nothing overflows at 160 pixels.
| Keys | Action |
|---|---|
| Tab | Move through the select and the buttons. A button with nowhere to go, such as Previous on page 1, stays in the order and says it is disabled. |
| Enter / Space | Go to the focused button's page. The focus stays on that button. |
| ↑ / ↓ | On the Rows per page select: change the page size. |
Source
"use client";
import { useState, type ComponentProps } from "react";
import { cn } from "@/lib/utils";
import { play } from "@/lib/thrum-sound";
import { buttonClass } from "@/components/ui/button";
import { NativeSelect, NativeSelectOption } from "@/components/ui/native-select";
import { ChevronLeftIcon } from "@/components/icons/chevron-left";
import { ChevronRightIcon } from "@/components/icons/chevron-right";
import { ChevronsLeftIcon } from "@/components/icons/chevrons-left";
import { ChevronsRightIcon } from "@/components/icons/chevrons-right";
export type PageRangeItem = number | "ellipsis-start" | "ellipsis-end";
/**
* The page buttons to draw: the first page, the last page, the current page with `siblings` pages on each side,
* and an ellipsis for every gap of two pages or more. Once there are more pages than fit, the list always has
* `siblings * 2 + 5` entries, so the buttons do not jump about as the reader moves.
*/
export function pageRange(page: number, pageCount: number, siblings = 1): PageRangeItem[] {
const count = Number.isFinite(pageCount) ? Math.max(0, Math.floor(pageCount)) : 0;
const around = Number.isFinite(siblings) ? Math.max(0, Math.floor(siblings)) : 1;
if (count <= around * 2 + 5) return Array.from({ length: count }, (_, i) => i + 1);
const current = Number.isFinite(page) ? Math.min(count, Math.max(1, Math.floor(page))) : 1;
// The run around the current page, held far enough from both ends that the list keeps its length.
const from = Math.max(Math.min(current - around, count - around * 2 - 2), 3);
const to = Math.min(Math.max(current + around, around * 2 + 3), count - 2);
const run = Array.from({ length: to - from + 1 }, (_, i) => from + i);
// A gap of one page shows that page: an ellipsis would take the same room and say less.
return [1, from > 3 ? "ellipsis-start" : 2, ...run, to < count - 2 ? "ellipsis-end" : count - 1, count];
}
export type PaginationSize = "sm" | "md" | "lg";
export type PaginationProps = ComponentProps<"nav"> & {
/** How many pages there are. */
pageCount: number;
/** The current page, counted from 1, when the parent holds it. Use with `onPageChange`. */
page?: number;
/** The page shown at first, when the pagination holds it itself. */
defaultPage?: number;
/** Called with the new page when the reader picks one. */
onPageChange?: (page: number) => void;
/** How many page buttons to keep on each side of the current page. */
siblings?: number;
/** Add buttons that jump to the first and the last page. */
showFirstLast?: boolean;
/** `true` always shows "Page 3 of 12" instead of the numbered buttons, `false` never does, and `"auto"` does so while the pagination is under 448 pixels wide. */
compact?: boolean | "auto";
/** The size of every button: `sm` is 28 pixels, `md` 32, `lg` 40. */
size?: PaginationSize;
/** The choices of the "Rows per page" select. Leave it out to draw no select. */
pageSizeOptions?: readonly number[];
/** The selected page size, when the parent holds it. Use with `onPageSizeChange`. */
pageSize?: number;
/** The page size selected at first, when the pagination holds it itself. The default is the first option. */
defaultPageSize?: number;
/** Called with the new page size. The page goes back to 1, and you pass the new `pageCount`. */
onPageSizeChange?: (pageSize: number) => void;
/** Play a soft click when the reader picks a page, if the visitor has sound on. */
sound?: boolean;
};
const BOX: Record<PaginationSize, string> = {
sm: "h-7 min-w-7 px-1.5 text-xs",
md: "h-8 min-w-8 px-2 text-sm",
lg: "h-10 min-w-10 px-2.5 text-sm",
};
// A button at either end stays focusable when there is nowhere to go: a disabled one would drop the focus to the page.
const INERT = "aria-disabled:opacity-50 aria-disabled:hover:bg-transparent aria-disabled:hover:text-foreground";
/**
* Page navigation: previous and next, numbered pages with ellipses, and an optional "Rows per page" select.
* It is a `<nav>` named "Pagination". It is its own size container, so a narrow one shows "Page 3 of 12" instead of the numbers.
*/
export function Pagination({
pageCount,
page,
defaultPage = 1,
onPageChange,
siblings = 1,
showFirstLast = false,
compact = "auto",
size = "md",
pageSizeOptions,
pageSize,
defaultPageSize,
onPageSizeChange,
sound = true,
className,
...props
}: PaginationProps) {
const [innerPage, setInnerPage] = useState(defaultPage);
const [innerSize, setInnerSize] = useState(defaultPageSize ?? pageSizeOptions?.[0]);
const count = Number.isFinite(pageCount) ? Math.max(1, Math.floor(pageCount)) : 1;
const clamp = (value: number) => (Number.isFinite(value) ? Math.min(count, Math.max(1, Math.floor(value))) : 1);
const current = clamp(page ?? innerPage);
const rows = pageSize ?? innerSize;
const go = (next: number) => {
const target = clamp(next);
if (target === current) return;
setInnerPage(target);
onPageChange?.(target);
if (sound) play("select");
};
const arrow = (Glyph: typeof ChevronLeftIcon, label: string, target: number) => (
<li>
<button
type="button"
aria-label={label}
aria-disabled={clamp(target) === current || undefined}
onClick={() => go(target)}
className={cn(buttonClass({ variant: "ghost" }), BOX[size], "px-0", INERT)}
>
<Glyph size={16} className="rtl:rotate-180" />
</button>
</li>
);
return (
<nav data-slot="pagination" aria-label="Pagination" className={cn("@container/pagination w-full min-w-0", className)} {...props}>
<div className={cn("flex flex-wrap items-center gap-x-6 gap-y-2", pageSizeOptions?.length ? "justify-between" : "justify-center")}>
{pageSizeOptions && pageSizeOptions.length > 0 && (
<label data-slot="pagination-page-size" className="flex items-center gap-2 text-sm whitespace-nowrap text-muted-foreground">
Rows per page
<NativeSelect
size={size}
className="w-20"
value={rows}
onChange={(event) => {
const next = Number(event.target.value);
setInnerSize(next);
onPageSizeChange?.(next);
// Page 7 of the old size is somewhere else in the new one: start again at the top.
if (current !== 1) {
setInnerPage(1);
onPageChange?.(1);
}
}}
>
{pageSizeOptions.map((option) => (
<NativeSelectOption key={option} value={option}>
{option}
</NativeSelectOption>
))}
</NativeSelect>
</label>
)}
<ul data-slot="pagination-list" className="flex flex-wrap items-center gap-1">
{showFirstLast && arrow(ChevronsLeftIcon, "First page", 1)}
{arrow(ChevronLeftIcon, "Previous page", current - 1)}
{compact !== true &&
pageRange(current, count, siblings).map((item) =>
typeof item === "number" ? (
<li key={item} className={cn(compact === "auto" && "hidden @md/pagination:block")}>
<button
type="button"
data-surface={item === current ? "accent" : undefined}
aria-label={`Page ${item}`}
aria-current={item === current ? "page" : undefined}
onClick={() => go(item)}
className={cn(buttonClass({ variant: item === current ? "primary" : "ghost" }), BOX[size], "tabular-nums")}
>
{item}
</button>
</li>
) : (
<li key={item} aria-hidden="true" className={cn("place-items-center text-muted-foreground", BOX[size], compact === "auto" ? "hidden @md/pagination:grid" : "grid")}>
…
</li>
),
)}
{compact !== false && (
// The same words are in the live line below, so a screen reader hears them once.
<li aria-hidden="true" data-slot="pagination-status" className={cn("px-2 text-sm whitespace-nowrap tabular-nums", compact === "auto" && "@md/pagination:hidden")}>
Page {current} of {count}
</li>
)}
{arrow(ChevronRightIcon, "Next page", current + 1)}
{showFirstLast && arrow(ChevronsRightIcon, "Last page", count)}
</ul>
</div>
{/* Read out when the page changes: the focus stays on the button that was pressed. */}
<p className="sr-only" aria-live="polite">
Page {current} of {count}
</p>
</nav>
);
}Files and dependencies
- Adds
components/ui/pagination.tsx- Packages
- None
- Brings along
- ButtonNative selectSound engine
- Icons
chevron-leftchevron-rightchevrons-leftchevrons-right
Changelog
- 1.0.0 · 2026-12-03First release.