All files / src/components/collection Pagination.tsx

91.66% Statements 11/12
90.47% Branches 19/21
80% Functions 4/5
90.9% Lines 10/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99                                            36x   36x   36x           36x               140x 14x 140x                       30x                       4x                               2x                                
import { ChevronLeft, ChevronRight, Loader2 } from "lucide-react";
import React from "react";
 
import { pageNumbers } from "@/util/pagedFeed";
 
export interface PaginationProps {
  /** 1-based. */
  page: number;
  /** Pages known to exist. */
  pageCount: number;
  /** More pages exist past `pageCount` that have not been counted yet — a
   *  feed read a page at a time does not know its length in advance. Keeps
   *  "Next" enabled on the last known page and adds a trailing "…". */
  hasMore?: boolean;
  /** A page is being loaded; "Next" shows a spinner instead of moving. */
  loading?: boolean;
  onPageChange: (page: number) => void;
  /** Names the navigation for assistive tech, e.g. "Activity pages". */
  label?: string;
}
 
const BUTTON =
  "min-w-7 h-7 px-1.5 inline-flex items-center justify-center rounded-md text-xs tabular-nums transition-colors disabled:opacity-40 disabled:cursor-not-allowed";
const IDLE =
  "text-gray-600 dark:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-700";
const CURRENT =
  "bg-gray-900 dark:bg-gray-100 text-white dark:text-gray-900 font-semibold";
 
/**
 * Numbered page controls: previous, `1 … 4 5 6 … 12`, next. Renders nothing
 * when there is only one page and nothing more to load.
 */
const Pagination: React.FC<PaginationProps> = ({
  page,
  pageCount,
  hasMore = false,
  loading = false,
  onPageChange,
  label = "Pages",
}) => {
  if (pageCount <= 1 && !hasMore) return null;
  const atEnd = page >= pageCount && !hasMore;
  return (
    <nav aria-label={label} className="flex items-center gap-0.5">
      <button
        type="button"
        onClick={() => onPageChange(page - 1)}
        disabled={page <= 1}
        aria-label="Previous page"
        className={`${BUTTON} ${IDLE}`}
      >
        <ChevronLeft className="size-3.5" />
      </button>
      {pageNumbers(page, pageCount).map((p, i) =>
        p === "gap" ? (
          <span
            key={`gap-${i}`}
            aria-hidden
            className="min-w-5 text-center text-xs text-gray-400"
          >
            …
          </span>
        ) : (
          <button
            key={p}
            type="button"
            onClick={() => onPageChange(p)}
            aria-label={`Page ${p}`}
            aria-current={p === page ? "page" : undefined}
            className={`${BUTTON} ${p === page ? CURRENT : IDLE}`}
          >
            {p}
          </button>
        ),
      )}
      {hasMore && (
        <span aria-hidden className="min-w-5 text-center text-xs text-gray-400">
          …
        </span>
      )}
      <button
        type="button"
        onClick={() => onPageChange(page + 1)}
        disabled={atEnd || loading}
        aria-label="Next page"
        className={`${BUTTON} ${IDLE}`}
      >
        {loading ? (
          <Loader2 className="size-3.5 animate-spin" />
        ) : (
          <ChevronRight className="size-3.5" />
        )}
      </button>
    </nav>
  );
};
 
export default Pagination;