{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "pagination",
  "type": "registry:ui",
  "title": "Pagination",
  "description": "Page navigation as buttons or plain links, with the current page marked and Previous and Next kept as disabled buttons at the ends.",
  "dependencies": [
    "lucide-react@^1.49.0"
  ],
  "registryDependencies": [
    "https://ui.klarhimmel.com/r/button.json",
    "https://ui.klarhimmel.com/r/theme.json",
    "https://ui.klarhimmel.com/r/utils.json"
  ],
  "files": [
    {
      "path": "src/ui/pagination.tsx",
      "type": "registry:file",
      "target": "components/klarhimmel/ui/pagination.tsx",
      "content": "import { ChevronLeft, ChevronRight } from 'lucide-react';\nimport { cn } from '../lib/utils';\nimport { safeHref } from './button';\n\nfunction pageNumber(value: number): number {\n  return Number.isFinite(value) ? Math.max(1, Math.floor(value)) : 1;\n}\n\nfunction currentPage(page: number, total: number): number {\n  return Math.min(pageNumber(page), total);\n}\n\n/** The page numbers to show: always the first and last, `siblings` either side of the current one, \"…\" for the gaps. */\nexport function pageRange(page: number, count: number, siblings = 1): (number | 'gap')[] {\n  const total = pageNumber(count);\n  const cur = currentPage(page, total);\n  siblings = Number.isFinite(siblings) ? Math.max(0, Math.floor(siblings)) : 1;\n  if (total <= siblings * 2 + 5) return Array.from({ length: total }, (_, i) => i + 1);\n  const keep = new Set([1, total]);\n  for (let i = Math.max(1, cur - siblings); i <= Math.min(total, cur + siblings); i++) keep.add(i);\n  // A single hidden page is shown rather than replaced by \"…\".\n  const sorted = [...keep].sort((a, b) => a - b);\n  const out: (number | 'gap')[] = [];\n  sorted.forEach((n, i) => {\n    const prev = sorted[i - 1];\n    if (prev !== undefined && n - prev === 2) out.push(prev + 1);\n    else if (prev !== undefined && n - prev > 2) out.push('gap');\n    out.push(n);\n  });\n  return out;\n}\n\nexport interface PaginationProps {\n  /** Rounded down and clamped to the available pages; non-finite values use page 1. */\n  page: number;\n  /** Rounded down with a minimum of 1; non-finite values use 1. */\n  pageCount: number;\n  /** Buttons mode: called with the new page. */\n  onPageChange?: (page: number) => void;\n  /** Links mode: the URL of a page. Use this when each page is its own address (it works without script). */\n  href?: (page: number) => string;\n  /** Whole, non-negative adjacent pages; non-finite values use the default of 1. */\n  siblings?: number;\n  label?: string;\n  className?: string;\n}\n\nconst cell =\n  'inline-flex min-h-11 min-w-11 items-center justify-center gap-1 rounded-kh px-3 text-kh-small font-medium text-kh-ink-2 no-underline transition-colors duration-(--kh-dur) ' +\n  'hover:bg-kh-surface-2 hover:text-kh-ink focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-(--kh-focus)';\n\n/** Page navigation. The current page carries aria-current=\"page\"; Previous and Next stay as disabled buttons at the ends. */\nexport function Pagination({ page, pageCount, onPageChange, href, siblings = 1, label = 'Pagination', className }: PaginationProps) {\n  const total = pageNumber(pageCount);\n  const cur = currentPage(page, total);\n  const item = (n: number, content: React.ReactNode, name: string, current = false, extra?: string) => {\n    const cls = cn(cell, current && 'border border-kh-ink bg-kh-surface text-kh-ink', extra);\n    return href ? (\n      <a href={safeHref(href(n))} aria-label={name} aria-current={current ? 'page' : undefined} className={cls}>{content}</a>\n    ) : (\n      <button type=\"button\" aria-label={name} aria-current={current ? 'page' : undefined} onClick={() => onPageChange?.(n)} className={cls}>{content}</button>\n    );\n  };\n  // An unavailable Previous or Next stays in the tree as a disabled button, so assistive tech still knows it exists.\n  const edge = (text: string, icon: React.ReactNode, target: number, enabled: boolean, end: boolean) =>\n    enabled ? (\n      item(target, end ? <>{text}{icon}</> : <>{icon}{text}</>, text)\n    ) : (\n      <button type=\"button\" disabled className={cn(cell, 'cursor-not-allowed text-kh-muted hover:bg-transparent hover:text-kh-muted')}>{end ? <>{text}{icon}</> : <>{icon}{text}</>}</button>\n    );\n  return (\n    <nav aria-label={label} className={className}>\n      <ul className=\"flex flex-wrap items-center justify-center gap-1\">\n        <li>{edge('Previous', <ChevronLeft aria-hidden className=\"size-4\" />, cur - 1, cur > 1, false)}</li>\n        {pageRange(cur, total, siblings).map((n, i) =>\n          n === 'gap' ? (\n            <li key={`gap-${i}`} aria-hidden className=\"inline-flex min-h-11 min-w-8 items-center justify-center text-kh-muted\">…</li>\n          ) : (\n            <li key={n}>{item(n, n, `Page ${n}`, n === cur)}</li>\n          ),\n        )}\n        <li>{edge('Next', <ChevronRight aria-hidden className=\"size-4\" />, cur + 1, cur < total, true)}</li>\n      </ul>\n    </nav>\n  );\n}\n"
    }
  ]
}
