Pagination
FreePrevious, every page and next as quiet buttons, with one glass marker that glides to the page you pick.
npx shadcn@latest add @pixilui/paginationPreview
One glass marker glides from page to page.
import { IconBack } from "@/components/pixilui/icons/back";
import { IconForward } from "@/components/pixilui/icons/forward";
import { Pagination } from "@/components/pixilui/pagination";
// One finished element, no rows.
export const knobs = {} as const;
export default function PaginationDemo() {
return <Pagination pageCount={3} defaultPage={2} previousIcon={<IconBack />} nextIcon={<IconForward />} />;
}When to use
Use it when
- Paging a short list: run history, an audit log, eval results, a few pages of invoices.
- Under a table whose row count is known up front.
Use something else when
- More than about 7 pages: this shows every page; use a windowed pagination or a page input.
- Feeds that grow as you read: load more on scroll or with a button.
Installation
Terminal
npx shadcn@latest add @pixilui/paginationAdds pagination.tsx with button.tsx, and installs clsx and tailwind-merge. First PixilUI item in this app? Add the registry once, as in Installation.
components/pixilui/pagination.tsx
"use client";
import { forwardRef, useCallback, useLayoutEffect, useRef, useState, type ComponentPropsWithoutRef, type ReactNode } from "react";
import { cn } from "./lib/utils";
import { Button } from "./button";
/*
The locked pagination (exploration 04): Secondary 2 page buttons with one glass marker that glides
to the current page. The marker's look and glide live in foundation.css section 7
(pixilui-btn-pages); this file places it, at once on first paint and after the font loads.
*/
export type PaginationProps = Omit<ComponentPropsWithoutRef<"div">, "onChange" | "children"> & {
/** How many pages there are. Every page shows, so keep it to about 7. */
pageCount: number;
/** The current page (from 1), controlled. Pair with onPageChange. */
page?: number;
/** The starting page when uncontrolled. */
defaultPage?: number;
/** Runs with the page a person picked. */
onPageChange?: (page: number) => void | Promise<void>;
/** The previous icon (Lucide ArrowLeft). */
previousIcon: ReactNode;
/** The next icon (Lucide ArrowRight). */
nextIcon: ReactNode;
/** Accessible name of the previous button. */
previousLabel?: string;
/** Accessible name of the next button. */
nextLabel?: string;
/** Names the navigation for screen readers. */
"aria-label"?: string;
};
/** Previous, every page, next: one glass marker glides to the current page. */
export const Pagination = forwardRef<HTMLDivElement, PaginationProps>(function Pagination(
{ pageCount, page: pageProp, defaultPage = 1, onPageChange, previousIcon, nextIcon, previousLabel = "Previous page", nextLabel = "Next page", "aria-label": ariaLabel = "Pages", className, ...rest },
ref,
) {
const [inner, setInner] = useState(defaultPage);
const page = pageProp ?? inner;
const mark = useRef<HTMLSpanElement | null>(null);
const nav = useRef<HTMLDivElement | null>(null);
const placed = useRef(false);
const setRef = useCallback(
(node: HTMLDivElement | null) => {
nav.current = node;
if (typeof ref === "function") ref(node);
else if (ref) ref.current = node;
},
[ref],
);
useLayoutEffect(() => {
const place = () => {
const current = nav.current?.querySelector<HTMLElement>('[aria-current="page"]');
const el = mark.current;
if (!current || !el) return;
const instant = !placed.current;
if (instant) el.style.transition = "none";
el.style.setProperty("--btn-mx", `${current.offsetLeft}px`);
el.style.setProperty("--btn-mw", `${current.offsetWidth}px`);
if (instant) {
void el.offsetWidth;
el.style.transition = "";
}
};
place();
placed.current = true;
document.fonts?.ready.then(() => {
placed.current = false;
place();
placed.current = true;
});
}, [page]);
const go = (next: number) => {
if (next < 1 || next > pageCount || next === page) return;
if (pageProp === undefined) setInner(next);
void onPageChange?.(next);
};
return (
<div ref={setRef} className={cn("pixilui-btn-pages font-body", className)} data-has-mark="" role="navigation" aria-label={ariaLabel} {...rest}>
<span ref={mark} className="pixilui-btn-pages-mark" aria-hidden="true" />
<Button variant="secondary-2" size="sm" aria-label={previousLabel} icon={previousIcon} disabled={page === 1} onClick={() => go(page - 1)} />
{Array.from({ length: pageCount }, (_, i) => i + 1).map((n) => (
<Button key={n} variant="secondary-2" size="sm" aria-current={n === page ? "page" : undefined} onClick={() => go(n)}>
{String(n)}
</Button>
))}
<Button variant="secondary-2" size="sm" aria-label={nextLabel} icon={nextIcon} disabled={page === pageCount} onClick={() => go(page + 1)} />
</div>
);
});
It uses Button: add it the same way. Then npm i clsx tailwind-merge, and add the foundation once per app, as in Installation.
Usage
example.tsx
import { Pagination } from "@/components/pixilui/pagination";
<Pagination
pageCount={pageCount}
previousIcon={previousIcon}
nextIcon={nextIcon}
onPageChange={onPageChange}
/>As wide as its pages. Three to seven pages fit at 375 wide.
API reference
PropTypeDefault
pageCountRequirednumber–How many pages there are. Every page shows, so keep it to about 7.previousIconRequiredReactNode–The previous icon (Lucide ArrowLeft).nextIconRequiredReactNode–The next icon (Lucide ArrowRight).pagenumber–The current page (from 1), controlled. Pair with onPageChange.defaultPagenumber1The starting page when uncontrolled.onPageChange(page: number) => void | Promise<void>–Runs with the page a person picked.previousLabelstring"Previous page"Accessible name of the previous button.nextLabelstring"Next page"Accessible name of the next button.aria-labelstring–Names the navigation for screen readers.Also takes every native <div> attribute except onChange, and a ref.
Keyboard
TabMoves through previous, each page and next.
EnterGoes to the focused page.
SpaceThe same as Enter, on release.
Accessibility
- A navigation landmark named Pages (aria-label), holding native buttons.
- The current page has aria-current="page".
- Previous and next are named Previous page and Next page, and disable on the first and last page.
- The marker is aria-hidden decoration.
Motion
- The glass marker glides to the new page on the snappy spring, its width following the number.
- On first paint and after the font loads the marker is placed at once, never sliding in from the start.
- Reduced motion: the marker moves at once.