Number 85: Page numbers with a gliding current page
Specification
- Catalogue
- No. 85 of 110
- Section
- Navigation
- Interaction
- Layout Animation
- Element
- Navigation
- Preview
- Live component
- Imports
- react 19.3.1, @number-flow/react 0.6.2, framer-motion 12.40.0
- Added
Notes
A pill glides to the current page, the page numbers slide as the visible window moves, and each gap turns into a double chevron when pointed at and jumps five pages.
Blogs, shops and listing pages all paginate. The first and last pages are always there, the row never changes width, and the jump says what it will do before it is pressed.
One page at each end, one either side of the current page, and a gap for any run of two or more hidden pages. The pill and the numbers move on one spring (stiffness 360, damping 32, mass 0.6); numbers entering or leaving fade over 160ms cubic-bezier(.16, 1, .3, 1) and scale from 0.8. The gap's dots and chevrons trade places in 150ms on hover or keyboard focus. Every control is 44px with its own label (previous, next, the page number, back or forward five pages), and a press scales to 0.94. The demo uses buttons; a client build renders links so each page stays shareable. Crossfades only under reduced motion.
A plain sliding five-page window has no first and last page and no jump, so a visitor on page 6 of 20 cannot reach the end in one press; this one keeps both.
Code
React and plain CSS"use client";
import { useId, useState, type ReactNode } from "react";
import { AnimatePresence, LayoutGroup, motion, useIsPresent } from "framer-motion";
import type { LivePreviewProps } from "@/components/live/types";
import { useSafeReducedMotion } from "@/components/pieces/kit/use-safe-reduced-motion";
import { Num } from "@/components/pieces/shared";
import { PieceStage } from "@/components/pieces/stage/PieceStage";
import type { Tour } from "@/components/pieces/stage/tour";
import "./page-numbers.css";
type Item = number | "gap-start" | "gap-end";
const TOTAL = 20;
const PER_PAGE = 12;
const JUMP = 5;
const SPRING = { type: "spring" as const, stiffness: 360, damping: 32, mass: 0.6 };
const FADE = { duration: 0.16, ease: [0.16, 1, 0.3, 1] as [number, number, number, number] };
/**
* One page at each end, one either side of the current page, and a gap for any
* run of two or more hidden pages. The list keeps one length as the page moves,
* so the row never changes width.
*/
function pages(page: number, total: number): Item[] {
const range = (a: number, b: number) => Array.from({ length: Math.max(0, b - a + 1) }, (_, i) => a + i);
if (total <= 7) return range(1, total);
const left = Math.max(page - 1, 1);
const right = Math.min(page + 1, total - 1);
const gapStart = left > 3;
const gapEnd = right < total - 2;
if (!gapStart && gapEnd) return [...range(1, 5), "gap-end", total];
if (gapStart && !gapEnd) return [1, "gap-start", ...range(total - 4, total)];
return [1, "gap-start", ...range(left, right), "gap-end", total];
}
function Chevron({ dir, double = false }: { dir: 1 | -1; double?: boolean }) {
const d = dir === 1 ? "m9 6 6 6-6 6" : "m15 6-6 6 6 6";
return (
<svg viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
{double ? <path d={dir === 1 ? "m6 6 6 6-6 6M12 6l6 6-6 6" : "m18 6-6 6 6 6M12 6l-6 6 6 6"} /> : <path d={d} />}
</svg>
);
}
/** A control that is leaving the row stops being a control at once: no focus, no clicks, no current page. */
function Leaving({ children }: { children: (present: boolean) => ReactNode }) {
const present = useIsPresent();
return <span className="pn-slot" inert={!present || undefined}>{children(present)}</span>;
}
function PageNumbersDemo() {
const reduce = useSafeReducedMotion();
const id = useId();
const [page, setPage] = useState(6);
const [hot, setHot] = useState<string | null>(null);
const go = (p: number) => setPage(Math.min(TOTAL, Math.max(1, p)));
const items = pages(page, TOTAL);
const fade = reduce ? { duration: 0 } : FADE;
const from = (page - 1) * PER_PAGE + 1;
return (
<div className="dm pn">
<p className="pn-caption" aria-live="polite">
Recipes <Num value={from} />
{" to "}
<Num value={Math.min(page * PER_PAGE, TOTAL * PER_PAGE)} /> of {TOTAL * PER_PAGE}
</p>
<nav aria-label="Pages">
<LayoutGroup id={id}>
<ul className="pn-row">
<li>
<button type="button" className="pn-btn pn-step" aria-label="Previous page" disabled={page === 1} onClick={() => go(page - 1)}>
<Chevron dir={-1} />
</button>
</li>
<AnimatePresence mode="popLayout" initial={false}>
{items.map((item) => {
if (typeof item !== "number") {
const back = item === "gap-start";
return (
<motion.li key={item} layout={reduce ? false : "position"} initial={{ opacity: 0 }} animate={{ opacity: 1 }} exit={{ opacity: 0, transition: fade }} transition={{ layout: SPRING, opacity: fade }}>
<Leaving>
{() => (
<button
type="button"
className="pn-btn pn-gap"
data-hot={hot === item || undefined}
aria-label={back ? `Back ${JUMP} pages` : `Forward ${JUMP} pages`}
onPointerEnter={() => setHot(item)}
onPointerLeave={() => setHot(null)}
onClick={() => go(page + (back ? -JUMP : JUMP))}
>
<span className="pn-dots" aria-hidden="true">
…
</span>
<span className="pn-jump">
<Chevron dir={back ? -1 : 1} double />
</span>
</button>
)}
</Leaving>
</motion.li>
);
}
const current = item === page;
return (
<motion.li
key={item}
layout={reduce ? false : "position"}
initial={{ opacity: 0, scale: reduce ? 1 : 0.8 }}
animate={{ opacity: 1, scale: 1 }}
exit={{ opacity: 0, scale: reduce ? 1 : 0.8, transition: fade }}
transition={{ layout: SPRING, opacity: fade, scale: SPRING }}
>
<Leaving>
{(present) => (
<button type="button" className="pn-btn pn-page" aria-label={`Page ${item}`} aria-current={current && present ? "page" : undefined} onClick={() => go(item)}>
{current && <motion.span className="pn-pill" layoutId={reduce ? undefined : "pn-pill"} transition={SPRING} aria-hidden="true" />}
<span className="pn-num">{item}</span>
</button>
)}
</Leaving>
</motion.li>
);
})}
</AnimatePresence>
<li>
<button type="button" className="pn-btn pn-step pn-next" aria-label="Next page" disabled={page === TOTAL} onClick={() => go(page + 1)}>
<Chevron dir={1} />
</button>
</li>
</ul>
</LayoutGroup>
</nav>
</div>
);
}
// What a hovered card shows: the next page, Next, then the gap turning into a jump of five.
const TOUR: Tour = [
{ click: ".pn-page", text: "7", wait: 700 },
{ click: ".pn-next", wait: 700 },
{ hover: ".pn-gap", nth: 1, wait: 600 },
{ click: ".pn-gap", nth: 1, wait: 900 },
];
export default function PageNumbersEntry(props: LivePreviewProps) {
return (
<PieceStage {...props} label="Page numbers with a gliding current page" tour={TOUR}>
<PageNumbersDemo />
</PieceStage>
);
}