{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"pagination","type":"registry:block","title":"Pagination","description":"A pagination bar with a sliding active page, compressed page ranges, disabled edge arrows, a position rail and a range summary.","dependencies":["motion","lucide-react"],"registryDependencies":["utils"],"files":[{"path":"registry/prequel/pagination/pagination.tsx","type":"registry:component","content":"\"use client\";\n\nimport { useId, useMemo, useRef, useState } from \"react\";\nimport {\n  MotionConfig,\n  motion,\n  useInView,\n  useReducedMotion,\n} from \"motion/react\";\nimport { ChevronLeft, ChevronRight } from \"lucide-react\";\nimport { cn } from \"@/lib/utils\";\n\nconst EASE: [number, number, number, number] = [0.22, 1, 0.36, 1];\n\n/* ------------------------------------------------------------------ */\n/* types                                                               */\n/* ------------------------------------------------------------------ */\n\nexport type PaginationProps = {\n  className?: string;\n  /** Controlled current page. */\n  page?: number;\n  defaultPage?: number;\n  totalPages?: number;\n  totalItems?: number;\n  pageSize?: number;\n  onPageChange?: (page: number) => void;\n};\n\n/* ------------------------------------------------------------------ */\n/* helpers                                                             */\n/* ------------------------------------------------------------------ */\n\ntype PageEntry = number | \"gap\";\n\nfunction buildPages(current: number, total: number): PageEntry[] {\n  if (total <= 7) {\n    return Array.from({ length: total }, (_, index) => index + 1);\n  }\n\n  const anchors = [1, total, current - 1, current, current + 1]\n    .filter((value) => value >= 1 && value <= total)\n    .sort((a, b) => a - b);\n\n  const pages: PageEntry[] = [];\n  anchors.forEach((value, index) => {\n    if (index > 0 && value - anchors[index - 1] > 1) pages.push(\"gap\");\n    pages.push(value);\n  });\n  return pages;\n}\n\n/* ------------------------------------------------------------------ */\n/* block                                                               */\n/* ------------------------------------------------------------------ */\n\nexport function Pagination({\n  className,\n  page,\n  defaultPage = 7,\n  totalPages = 143,\n  totalItems = 2847,\n  pageSize = 20,\n  onPageChange,\n}: PaginationProps) {\n  const ref = useRef<HTMLElement>(null);\n  const inView = useInView(ref, { once: true, margin: \"-10% 0px\" });\n  const reduce = Boolean(useReducedMotion());\n  const pillId = useId();\n\n  const [internalPage, setInternalPage] = useState(defaultPage);\n  const current = Math.min(totalPages, Math.max(1, page ?? internalPage));\n  const pages = useMemo(() => buildPages(current, totalPages), [current, totalPages]);\n\n  const start = totalItems === 0 ? 0 : (current - 1) * pageSize + 1;\n  const end = Math.min(totalItems, current * pageSize);\n  const progress = totalPages > 1 ? current / totalPages : 1;\n\n  const goTo = (next: number) => {\n    const target = Math.min(totalPages, Math.max(1, next));\n    if (target === current) return;\n    if (page === undefined) setInternalPage(target);\n    onPageChange?.(target);\n  };\n\n  return (\n    <MotionConfig reducedMotion=\"user\">\n      <motion.nav\n        ref={ref}\n        aria-label=\"Pagination\"\n        initial={{ opacity: 0, y: reduce ? 0 : 14 }}\n        animate={{ opacity: inView ? 1 : 0, y: 0 }}\n        transition={{ duration: reduce ? 0 : 0.55, ease: EASE }}\n        className={cn(\n          \"w-full rounded-xl border border-border bg-card p-4 text-card-foreground shadow-sm sm:p-5\",\n          className\n        )}\n      >\n        <div className=\"flex flex-wrap items-baseline justify-between gap-x-4 gap-y-1\">\n          <p className=\"text-[12.5px] text-muted-foreground\">\n            Showing{\" \"}\n            <span className=\"font-medium tabular-nums text-foreground\">\n              {start}–{end}\n            </span>{\" \"}\n            of <span className=\"tabular-nums text-foreground\">{totalItems.toLocaleString(\"en-US\")}</span>\n          </p>\n          <p className=\"hidden text-[11.5px] tabular-nums text-muted-foreground sm:block\">\n            Page {current} of {totalPages} · {pageSize} per page\n          </p>\n        </div>\n\n        <div className=\"mt-3 grid grid-cols-[auto_1fr_auto] items-center gap-2\">\n          <button\n            type=\"button\"\n            onClick={() => goTo(current - 1)}\n            disabled={current <= 1}\n            aria-label=\"Previous page\"\n            className=\"inline-flex h-8 w-8 items-center justify-center rounded-md border border-border bg-background text-muted-foreground transition-colors hover:bg-muted hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-card disabled:pointer-events-none disabled:opacity-40\"\n          >\n            <ChevronLeft className=\"h-4 w-4\" aria-hidden=\"true\" />\n          </button>\n\n          <div className=\"flex min-w-0 items-center justify-center gap-0.5\">\n            <div className=\"hidden items-center gap-0.5 sm:flex\">\n              {pages.map((entry, index) =>\n                entry === \"gap\" ? (\n                  <span\n                    key={`gap-${index}`}\n                    aria-hidden=\"true\"\n                    className=\"flex h-8 w-7 items-center justify-center text-[12px] text-muted-foreground/70\"\n                  >\n                    …\n                  </span>\n                ) : (\n                  <button\n                    key={entry}\n                    type=\"button\"\n                    onClick={() => goTo(entry)}\n                    aria-label={`Go to page ${entry}`}\n                    aria-current={entry === current ? \"page\" : undefined}\n                    className={cn(\n                      \"relative inline-flex h-8 min-w-8 items-center justify-center rounded-md px-2 text-[12.5px] font-medium tabular-nums transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-card\",\n                      entry === current\n                        ? \"text-background\"\n                        : \"text-muted-foreground hover:bg-muted hover:text-foreground\"\n                    )}\n                  >\n                    {entry === current ? (\n                      <motion.span\n                        layoutId={pillId}\n                        className=\"absolute inset-0 rounded-md bg-foreground\"\n                        transition={{ duration: reduce ? 0 : 0.32, ease: EASE }}\n                      />\n                    ) : null}\n                    <span className=\"relative z-10\">{entry}</span>\n                  </button>\n                )\n              )}\n            </div>\n\n            <span className=\"text-[12px] tabular-nums text-muted-foreground sm:hidden\">\n              Page {current} of {totalPages}\n            </span>\n          </div>\n\n          <button\n            type=\"button\"\n            onClick={() => goTo(current + 1)}\n            disabled={current >= totalPages}\n            aria-label=\"Next page\"\n            className=\"inline-flex h-8 w-8 items-center justify-center rounded-md border border-border bg-background text-muted-foreground transition-colors hover:bg-muted hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-card disabled:pointer-events-none disabled:opacity-40\"\n          >\n            <ChevronRight className=\"h-4 w-4\" aria-hidden=\"true\" />\n          </button>\n        </div>\n\n        <div className=\"mt-3 h-px w-full bg-border\" />\n\n        <div className=\"mt-3 h-0.5 w-full overflow-hidden rounded-full bg-muted\">\n          <motion.div\n            aria-hidden=\"true\"\n            initial={{ scaleX: reduce ? progress : 0 }}\n            animate={{ scaleX: inView ? progress : 0 }}\n            transition={{ duration: reduce ? 0 : 0.4, ease: EASE }}\n            style={{ transformOrigin: \"left center\" }}\n            className=\"h-full rounded-full bg-foreground/40\"\n          />\n        </div>\n      </motion.nav>\n    </MotionConfig>\n  );\n}\n\nexport default Pagination;\n"}]}