跳至正文
获取 Pro
动效组件M.15

动态分页器

FreeRegistry
components/linyi/pagination.tsx
motionlucide-react
"use client";
 
import * as React from "react";
import { motion } from "motion/react";
import { ChevronLeft, ChevronRight, MoreHorizontal } from "lucide-react";
import { cn } from "@/lib/cn";
import { SPRING_SNAPPY } from "@/lib/motion";
 
type PageToken = number | "ellipsis-left" | "ellipsis-right";
 
function buildPaginationRange(totalPages: number, currentPage: number, siblingCount: number): PageToken[] {
const totalSlots = siblingCount * 2 + 5;
if (totalPages <= totalSlots) {
return Array.from({ length: totalPages }, (_, i) => i + 1);
}
 
const leftSibling = Math.max(currentPage - siblingCount, 1);
const rightSibling = Math.min(currentPage + siblingCount, totalPages);
 
const showLeftEllipsis = leftSibling > 2;
const showRightEllipsis = rightSibling < totalPages - 1;
 
if (!showLeftEllipsis && showRightEllipsis) {
const leftCount = 3 + siblingCount * 2;
const leftRange = Array.from({ length: leftCount }, (_, i) => i + 1);
return [...leftRange, "ellipsis-right", totalPages];
}
 
if (showLeftEllipsis && !showRightEllipsis) {
const rightCount = 3 + siblingCount * 2;
const start = totalPages - rightCount + 1;
const rightRange = Array.from({ length: rightCount }, (_, i) => start + i);
return [1, "ellipsis-left", ...rightRange];
}
 
const middleRange = Array.from(
{ length: rightSibling - leftSibling + 1 },
(_, i) => leftSibling + i
);
return [1, "ellipsis-left", ...middleRange, "ellipsis-right", totalPages];
}
 
export interface PaginationProps {
totalPages: number;
page?: number;
defaultPage?: number;
onPageChange?: (page: number) => void;
siblingCount?: number;
size?: "sm" | "md";
className?: string;
}
 
export function Pagination({
totalPages,
page,
defaultPage = 1,
onPageChange,
siblingCount = 1,
size = "md",
className,
}: PaginationProps) {
const safeTotal = Math.max(1, Math.floor(totalPages));
const [internalPage, setInternalPage] = React.useState(
Math.min(safeTotal, Math.max(1, defaultPage))
);
const uid = React.useId();
 
const currentPage = Math.min(safeTotal, Math.max(1, page ?? internalPage));
const tokens = React.useMemo(
() => buildPaginationRange(safeTotal, currentPage, Math.max(0, siblingCount)),
[safeTotal, currentPage, siblingCount]
);
 
const goTo = (next: number) => {
const clamped = Math.min(safeTotal, Math.max(1, next));
if (clamped === currentPage) return;
if (page === undefined) {
setInternalPage(clamped);
}
onPageChange?.(clamped);
};
 
// md 尺寸在手机窄屏下收敛为 32px(仍满足触控热区),避免两端翻页按钮被容器裁切
const btnSize = size === "sm" ? "h-7 w-7 text-[11px]" : "h-8 w-8 sm:h-9 sm:w-9 text-xs";
const ellipsisSize = size === "sm" ? "h-7 w-5" : "h-8 w-5 sm:h-9 sm:w-9";
const iconSize = size === "sm" ? "h-3.5 w-3.5" : "h-4 w-4";
 
return (
<nav aria-label="分页导航" className={cn("inline-flex max-w-full items-center gap-0.5 sm:gap-1", className)}>
<button
type="button"
onClick={() => goTo(currentPage - 1)}
disabled={currentPage <= 1}
aria-label="上一页"
className={cn(
"inline-flex items-center justify-center rounded-pill border border-line bg-surface text-fg-secondary transition-colors cursor-pointer hover:border-line-strong hover:text-fg disabled:opacity-40 disabled:cursor-not-allowed",
btnSize
)}
>
<ChevronLeft className={iconSize} />
</button>
 
<div className="inline-flex items-center gap-0.5 sm:gap-1">
{tokens.map((token) => {
if (typeof token !== "number") {
return (
<span
key={token}
aria-hidden="true"
className={cn(
"inline-flex items-center justify-center text-fg-muted select-none",
ellipsisSize
)}
>
<MoreHorizontal className={iconSize} />
</span>
);
}
 
const active = token === currentPage;
return (
<button
key={token}
type="button"
onClick={() => goTo(token)}
aria-label={`第 ${token} 页`}
aria-current={active ? "page" : undefined}
className={cn(
"relative inline-flex items-center justify-center rounded-pill font-mono-num font-medium cursor-pointer transition-colors",
btnSize,
active ? "text-white" : "text-fg-secondary hover:text-fg"
)}
>
{active ? (
<motion.span
layoutId={`pagination-pill-${uid}`}
transition={SPRING_SNAPPY}
aria-hidden="true"
className="absolute inset-0 rounded-pill bg-accent shadow-[0_6px_16px_-4px_rgba(var(--accent-rgb),0.50)]"
/>
) : (
<span
aria-hidden="true"
className="absolute inset-0 rounded-pill border border-line bg-surface transition-colors hover:border-line-strong"
/>
)}
<span className="relative z-10">{token}</span>
</button>
);
})}
</div>
 
<button
type="button"
onClick={() => goTo(currentPage + 1)}
disabled={currentPage >= safeTotal}
aria-label="下一页"
className={cn(
"inline-flex items-center justify-center rounded-pill border border-line bg-surface text-fg-secondary transition-colors cursor-pointer hover:border-line-strong hover:text-fg disabled:opacity-40 disabled:cursor-not-allowed",
btnSize
)}
>
<ChevronRight className={iconSize} />
</button>
</nav>
);
}