Application
Pagination
Numbered pagination with prev/next and smart ellipsis truncation.
Motionnone
Install
Install Kineto and follow the theme setup once in your app.
bash
npm install kinetotsx
import { Pagination } from "kineto/pagination";Preview
tsx
"use client";
import * as React from "react";
import { ChevronLeft, ChevronRight } from "lucide-react";
import { cn } from "@/lib/cn";
export interface PaginationProps
extends Omit<React.HTMLAttributes<HTMLElement>, "onChange"> {
/** Current page (1-based). */
page: number;
/** Total number of pages. */
total: number;
onPageChange?: (page: number) => void;
siblingCount?: number;
}
function range(start: number, end: number): number[] {
return Array.from({ length: end - start + 1 }, (_, i) => start + i);
}
function getPages(page: number, total: number, siblings: number): (number | "…")[] {
const totalToShow = siblings * 2 + 5;
if (total <= totalToShow) return range(1, total);
const leftSibling = Math.max(page - siblings, 1);
const rightSibling = Math.min(page + siblings, total);
const showLeftDots = leftSibling > 2;
const showRightDots = rightSibling < total - 1;
if (!showLeftDots && showRightDots) {
return [...range(1, siblings * 2 + 3), "…", total];
}
if (showLeftDots && !showRightDots) {
return [1, "…", ...range(total - (siblings * 2 + 2), total)];
}
return [1, "…", ...range(leftSibling, rightSibling), "…", total];
}
const itemClass =
"inline-flex h-9 min-w-9 items-center justify-center rounded-lg px-2 text-sm font-medium transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-40";
const Pagination = React.forwardRef<HTMLElement, PaginationProps>(
({ className, page, total, onPageChange, siblingCount = 1, ...props }, ref) => {
const pages = getPages(page, total, siblingCount);
const go = (p: number) => {
if (p >= 1 && p <= total && p !== page) onPageChange?.(p);
};
return (
<nav
ref={ref}
aria-label="Pagination"
className={cn("flex items-center gap-1", className)}
{...props}
>
<button
type="button"
aria-label="Previous page"
onClick={() => go(page - 1)}
disabled={page <= 1}
className={cn(itemClass, "gap-1 text-muted-foreground hover:bg-accent hover:text-foreground")}
>
<ChevronLeft className="size-4" />
</button>
{pages.map((p, i) =>
p === "…" ? (
<span
key={`dots-${i}`}
className="inline-flex h-9 min-w-9 items-center justify-center text-sm text-muted-foreground"
>
…
</span>
) : (
<button
key={p}
type="button"
aria-current={p === page ? "page" : undefined}
onClick={() => go(p)}
className={cn(
itemClass,
p === page
? "bg-primary text-primary-foreground"
: "text-muted-foreground hover:bg-accent hover:text-foreground"
)}
>
{p}
</button>
)
)}
<button
type="button"
aria-label="Next page"
onClick={() => go(page + 1)}
disabled={page >= total}
className={cn(itemClass, "gap-1 text-muted-foreground hover:bg-accent hover:text-foreground")}
>
<ChevronRight className="size-4" />
</button>
</nav>
);
}
);
Pagination.displayName = "Pagination";
export { Pagination };src/components/ui/pagination.tsx
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| page / total | number | — | Current page (1-based) and page count. |
| onPageChange | (page: number) => void | — | Fires on navigation. |
| siblingCount | number | 1 | Pages shown either side of current. |