{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"data-table","type":"registry:block","title":"Data table","description":"A sortable transactions table with animated row reordering, live search, and compact pagination.","dependencies":["motion","lucide-react"],"registryDependencies":["utils"],"files":[{"path":"registry/prequel/data-table/data-table.tsx","type":"registry:component","content":"\"use client\";\n\nimport { useId, useMemo, useRef, useState } from \"react\";\nimport {\n  AnimatePresence,\n  MotionConfig,\n  motion,\n  useInView,\n  useReducedMotion,\n} from \"motion/react\";\nimport {\n  ChevronDown,\n  ChevronLeft,\n  ChevronRight,\n  ChevronsUpDown,\n  ChevronUp,\n  Inbox,\n  Search,\n  X,\n} 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 DataTableStatus = \"succeeded\" | \"pending\" | \"refunded\" | \"failed\";\n\nexport type DataTableRow = {\n  id: string;\n  name: string;\n  email: string;\n  plan: string;\n  /** Transaction amount, in whole currency units. */\n  amount: number;\n  status: DataTableStatus;\n  /** ISO date string, e.g. \"2026-11-09\". Sorts lexicographically. */\n  date: string;\n};\n\nexport type DataTableSortKey = \"name\" | \"plan\" | \"amount\" | \"status\" | \"date\";\n\nexport type DataTableProps = {\n  className?: string;\n  title?: string;\n  description?: string;\n  rows?: DataTableRow[];\n  /** Number of rows per page. */\n  pageSize?: number;\n  formatAmount?: (amount: number) => string;\n  searchPlaceholder?: string;\n};\n\n/* ------------------------------------------------------------------ */\n/* deterministic demo data (no Math.random / Date.now at module load)  */\n/* ------------------------------------------------------------------ */\n\nconst DEFAULT_ROWS: DataTableRow[] = [\n  { id: \"tx-01\", name: \"Amara Osei\", email: \"amara@brightloom.io\", plan: \"Scale\", amount: 4120, status: \"succeeded\", date: \"2026-11-09\" },\n  { id: \"tx-02\", name: \"Jonas Weber\", email: \"jonas@tidewell.com\", plan: \"Scale\", amount: 3380, status: \"succeeded\", date: \"2026-11-08\" },\n  { id: \"tx-03\", name: \"Priya Raman\", email: \"priya@kestrel.ai\", plan: \"Growth\", amount: 1720, status: \"pending\", date: \"2026-11-08\" },\n  { id: \"tx-04\", name: \"Mateo Silva\", email: \"mateo@quillfeather.co\", plan: \"Growth\", amount: 990, status: \"succeeded\", date: \"2026-11-07\" },\n  { id: \"tx-05\", name: \"Ingrid Halvorsen\", email: \"ingrid@northgate.studio\", plan: \"Starter\", amount: 349, status: \"failed\", date: \"2026-11-06\" },\n  { id: \"tx-06\", name: \"Chen Wei\", email: \"chen@emberoak.com\", plan: \"Scale\", amount: 2890, status: \"succeeded\", date: \"2026-11-05\" },\n  { id: \"tx-07\", name: \"Sofia Rossi\", email: \"sofia@verdant.io\", plan: \"Starter\", amount: 290, status: \"refunded\", date: \"2026-11-04\" },\n  { id: \"tx-08\", name: \"Liam O'Connor\", email: \"liam@fernway.app\", plan: \"Growth\", amount: 1180, status: \"succeeded\", date: \"2026-11-03\" },\n  { id: \"tx-09\", name: \"Yuki Tanaka\", email: \"yuki@oakridge.co\", plan: \"Starter\", amount: 290, status: \"pending\", date: \"2026-11-02\" },\n  { id: \"tx-10\", name: \"Elena Petrova\", email: \"elena@mosaic.dev\", plan: \"Scale\", amount: 5120, status: \"succeeded\", date: \"2026-11-01\" },\n  { id: \"tx-11\", name: \"Noah Berg\", email: \"noah@halden.co\", plan: \"Growth\", amount: 840, status: \"failed\", date: \"2026-10-31\" },\n  { id: \"tx-12\", name: \"Amina Diallo\", email: \"amina@nimbus.cloud\", plan: \"Starter\", amount: 290, status: \"succeeded\", date: \"2026-10-30\" },\n];\n\n/* ------------------------------------------------------------------ */\n/* helpers                                                             */\n/* ------------------------------------------------------------------ */\n\nconst MONTHS = [\n  \"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\",\n  \"Jul\", \"Aug\", \"Sep\", \"Oct\", \"Nov\", \"Dec\",\n] as const;\n\nconst STATUS_ORDER: Record<DataTableStatus, number> = {\n  succeeded: 0,\n  pending: 1,\n  refunded: 2,\n  failed: 3,\n};\n\nconst STATUS_META: Record<\n  DataTableStatus,\n  { label: string; classes: string; dot: string }\n> = {\n  succeeded: {\n    label: \"Succeeded\",\n    classes: \"border-success/25 bg-success/10 text-success\",\n    dot: \"bg-success\",\n  },\n  pending: {\n    label: \"Pending\",\n    classes: \"border-border bg-muted text-muted-foreground\",\n    dot: \"bg-muted-foreground\",\n  },\n  refunded: {\n    label: \"Refunded\",\n    classes: \"border-border bg-muted text-muted-foreground\",\n    dot: \"bg-muted-foreground\",\n  },\n  failed: {\n    label: \"Failed\",\n    classes: \"border-destructive/25 bg-destructive/10 text-destructive\",\n    dot: \"bg-destructive\",\n  },\n};\n\nfunction compareStrings(a: string, b: string) {\n  return a < b ? -1 : a > b ? 1 : 0;\n}\n\nfunction compareRows(\n  a: DataTableRow,\n  b: DataTableRow,\n  key: DataTableSortKey\n) {\n  switch (key) {\n    case \"amount\":\n      return a.amount - b.amount;\n    case \"date\":\n      return compareStrings(a.date, b.date);\n    case \"name\":\n      return compareStrings(a.name, b.name);\n    case \"plan\":\n      return compareStrings(a.plan, b.plan);\n    case \"status\":\n      return STATUS_ORDER[a.status] - STATUS_ORDER[b.status];\n  }\n}\n\nfunction defaultDirection(key: DataTableSortKey): \"asc\" | \"desc\" {\n  return key === \"amount\" || key === \"date\" ? \"desc\" : \"asc\";\n}\n\nfunction formatDate(iso: string) {\n  const [y, m, d] = iso.split(\"-\").map(Number);\n  return `${MONTHS[m - 1]} ${d}, ${y}`;\n}\n\nfunction initials(name: string) {\n  return name\n    .split(/\\s+/)\n    .filter(Boolean)\n    .slice(0, 2)\n    .map((word) => word[0])\n    .join(\"\")\n    .toUpperCase();\n}\n\nfunction defaultFormatAmount(amount: number) {\n  return \"$\" + amount.toLocaleString(\"en-US\");\n}\n\nfunction range(start: number, end: number) {\n  return Array.from({ length: end - start + 1 }, (_, i) => start + i);\n}\n\n/** Compact window of page numbers with ellipses for long lists. */\nfunction pageItems(\n  current: number,\n  total: number\n): Array<number | \"ellipsis\"> {\n  if (total <= 7) return range(1, total);\n  const left = Math.max(2, current - 1);\n  const right = Math.min(total - 1, current + 1);\n  const items: Array<number | \"ellipsis\"> = [1];\n  if (left > 2) items.push(\"ellipsis\");\n  for (let i = left; i <= right; i++) items.push(i);\n  if (right < total - 1) items.push(\"ellipsis\");\n  items.push(total);\n  return items;\n}\n\ntype Column = {\n  key: DataTableSortKey;\n  label: string;\n  align: \"left\" | \"right\";\n};\n\nconst COLUMNS: Column[] = [\n  { key: \"name\", label: \"Customer\", align: \"left\" },\n  { key: \"plan\", label: \"Plan\", align: \"left\" },\n  { key: \"amount\", label: \"Amount\", align: \"right\" },\n  { key: \"status\", label: \"Status\", align: \"left\" },\n  { key: \"date\", label: \"Date\", align: \"right\" },\n];\n\n/* ------------------------------------------------------------------ */\n/* block                                                               */\n/* ------------------------------------------------------------------ */\n\nexport function DataTable({\n  className,\n  title = \"Recent transactions\",\n  description = \"Payments and subscriptions across your workspace.\",\n  rows = DEFAULT_ROWS,\n  pageSize = 6,\n  formatAmount = defaultFormatAmount,\n  searchPlaceholder = \"Search customers…\",\n}: DataTableProps) {\n  const ref = useRef<HTMLElement>(null);\n  const inView = useInView(ref, { once: true, margin: \"-10% 0px\" });\n  const reduce = Boolean(useReducedMotion());\n  const show = inView;\n\n  const inputId = useId();\n  const [query, setQuery] = useState(\"\");\n  const [page, setPage] = useState(1);\n  const [sort, setSort] = useState<{\n    key: DataTableSortKey;\n    dir: \"asc\" | \"desc\";\n  }>({ key: \"date\", dir: \"desc\" });\n\n  const filtered = useMemo(() => {\n    const q = query.trim().toLowerCase();\n    if (!q) return rows;\n    return rows.filter(\n      (r) =>\n        r.name.toLowerCase().includes(q) ||\n        r.email.toLowerCase().includes(q) ||\n        r.plan.toLowerCase().includes(q) ||\n        r.status.toLowerCase().includes(q)\n    );\n  }, [rows, query]);\n\n  const sorted = useMemo(() => {\n    const arr = [...filtered];\n    arr.sort((a, b) => {\n      const cmp = compareRows(a, b, sort.key);\n      return sort.dir === \"asc\" ? cmp : -cmp;\n    });\n    return arr;\n  }, [filtered, sort]);\n\n  const totalPages = Math.max(1, Math.ceil(sorted.length / pageSize));\n  const currentPage = Math.min(page, totalPages);\n  const startIndex = (currentPage - 1) * pageSize;\n  const pageRows = sorted.slice(startIndex, startIndex + pageSize);\n\n  const rangeStart = sorted.length === 0 ? 0 : startIndex + 1;\n  const rangeEnd = Math.min(startIndex + pageSize, sorted.length);\n\n  const handleSort = (key: DataTableSortKey) => {\n    setSort((prev) =>\n      prev.key === key\n        ? { key, dir: prev.dir === \"asc\" ? \"desc\" : \"asc\" }\n        : { key, dir: defaultDirection(key) }\n    );\n    setPage(1);\n  };\n\n  return (\n    <MotionConfig reducedMotion=\"user\">\n      <motion.section\n        ref={ref}\n        initial={{ opacity: 0, y: 20 }}\n        animate={show ? { opacity: 1, y: 0 } : { opacity: 0, y: 20 }}\n        transition={{ duration: reduce ? 0 : 0.6, ease: EASE }}\n        className={cn(\n          \"rounded-xl border border-border bg-card text-card-foreground shadow-sm\",\n          className\n        )}\n      >\n        <div className=\"p-5 sm:p-6\">\n          {/* header */}\n          <div className=\"flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between\">\n            <div>\n              <h2 className=\"text-[15px] font-semibold tracking-tight\">\n                {title}\n              </h2>\n              <p className=\"mt-0.5 text-[13px] text-muted-foreground\">\n                {description}\n              </p>\n            </div>\n\n            <div className=\"relative w-full sm:w-72\">\n              <Search className=\"pointer-events-none absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2 text-muted-foreground\" />\n              <label htmlFor={inputId} className=\"sr-only\">\n                Search transactions\n              </label>\n              <input\n                id={inputId}\n                type=\"text\"\n                value={query}\n                onChange={(e) => {\n                  setQuery(e.target.value);\n                  setPage(1);\n                }}\n                placeholder={searchPlaceholder}\n                className=\"h-9 w-full rounded-lg border border-input bg-transparent pl-9 pr-9 text-sm outline-none placeholder:text-muted-foreground focus-visible:ring-2 focus-visible:ring-ring\"\n              />\n              <AnimatePresence>\n                {query && (\n                  <motion.button\n                    key=\"clear\"\n                    type=\"button\"\n                    aria-label=\"Clear search\"\n                    onClick={() => {\n                      setQuery(\"\");\n                      setPage(1);\n                    }}\n                    initial={{ opacity: 0, scale: 0.8 }}\n                    animate={{ opacity: 1, scale: 1 }}\n                    exit={{ opacity: 0, scale: 0.8 }}\n                    transition={{ duration: reduce ? 0 : 0.15 }}\n                    className=\"absolute right-2 top-1/2 grid h-5 w-5 -translate-y-1/2 place-items-center rounded-md text-muted-foreground transition-colors hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring\"\n                  >\n                    <X className=\"h-3.5 w-3.5\" />\n                  </motion.button>\n                )}\n              </AnimatePresence>\n            </div>\n          </div>\n\n          {/* table */}\n          <div className=\"mt-4 -mx-5 overflow-x-auto px-5 sm:-mx-6 sm:px-6\">\n            <table className=\"w-full min-w-[640px] border-collapse\">\n              <thead>\n                <tr>\n                  {COLUMNS.map((col) => {\n                    const active = sort.key === col.key;\n                    return (\n                      <th\n                        key={col.key}\n                        scope=\"col\"\n                        aria-sort={\n                          active\n                            ? sort.dir === \"asc\"\n                              ? \"ascending\"\n                              : \"descending\"\n                            : \"none\"\n                        }\n                        className={cn(\n                          \"border-b border-border px-3 py-2.5 text-xs font-medium first:pl-0 last:pr-0\",\n                          col.align === \"right\" ? \"text-right\" : \"text-left\"\n                        )}\n                      >\n                        <button\n                          type=\"button\"\n                          onClick={() => handleSort(col.key)}\n                          className={cn(\n                            \"group inline-flex items-center gap-1 rounded-md px-1.5 py-1 -my-1 transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring\",\n                            col.align === \"right\" && \"flex-row-reverse\",\n                            active\n                              ? \"text-foreground\"\n                              : \"text-muted-foreground hover:text-foreground\"\n                          )}\n                        >\n                          <span>{col.label}</span>\n                          {active ? (\n                            sort.dir === \"asc\" ? (\n                              <ChevronUp className=\"h-3.5 w-3.5\" />\n                            ) : (\n                              <ChevronDown className=\"h-3.5 w-3.5\" />\n                            )\n                          ) : (\n                            <ChevronsUpDown className=\"h-3.5 w-3.5 opacity-40 transition-opacity group-hover:opacity-70\" />\n                          )}\n                        </button>\n                      </th>\n                    );\n                  })}\n                </tr>\n              </thead>\n              <tbody>\n                {pageRows.length === 0 ? (\n                  <motion.tr\n                    key=\"empty\"\n                    initial={{ opacity: 0 }}\n                    animate={{ opacity: 1 }}\n                    transition={{ duration: reduce ? 0 : 0.3 }}\n                  >\n                    <td colSpan={COLUMNS.length} className=\"px-3 py-14\">\n                      <div className=\"flex flex-col items-center justify-center gap-2 text-center\">\n                        <Inbox className=\"h-6 w-6 text-muted-foreground/60\" />\n                        <p className=\"text-sm font-medium\">No transactions found</p>\n                        <p className=\"text-[13px] text-muted-foreground\">\n                          Nothing matches “{query}”. Try a different search.\n                        </p>\n                        <button\n                          type=\"button\"\n                          onClick={() => {\n                            setQuery(\"\");\n                            setPage(1);\n                          }}\n                          className=\"mt-1 rounded-md px-2 py-1 text-[13px] font-medium text-foreground underline-offset-4 transition-colors hover:underline focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring\"\n                        >\n                          Clear search\n                        </button>\n                      </div>\n                    </td>\n                  </motion.tr>\n                ) : (\n                  <AnimatePresence initial={false}>\n                    {pageRows.map((row, index) => {\n                      const meta = STATUS_META[row.status];\n                      const isLast = index === pageRows.length - 1;\n                      return (\n                        <motion.tr\n                          key={row.id}\n                          layout\n                          initial={{ opacity: 0 }}\n                          animate={{ opacity: show ? 1 : 0 }}\n                          exit={{\n                            opacity: 0,\n                            transition: { duration: reduce ? 0 : 0.15 },\n                          }}\n                          transition={{\n                            opacity: {\n                              duration: reduce ? 0 : 0.35,\n                              delay: reduce ? 0 : index * 0.04,\n                              ease: EASE,\n                            },\n                            layout: { duration: reduce ? 0 : 0.45, ease: EASE },\n                          }}\n                          className=\"group/row transition-colors hover:bg-muted/50\"\n                        >\n                          <td\n                            className={cn(\n                              \"py-3 pr-3 text-left first:pl-0\",\n                              isLast ? \"border-b-0\" : \"border-b border-border\"\n                            )}\n                          >\n                            <div className=\"flex items-center gap-3\">\n                              <span className=\"relative flex h-9 w-9 shrink-0 items-center justify-center rounded-full border border-border bg-muted text-[11px] font-medium text-foreground\">\n                                {initials(row.name)}\n                                {/* the lens glint: a dot that rides the avatar's rim on hover */}\n                                <span\n                                  aria-hidden\n                                  className=\"pointer-events-none absolute inset-0 opacity-0 transition-opacity duration-300 group-hover/row:opacity-100\"\n                                >\n                                  <span className=\"absolute inset-0 transition-transform duration-700 ease-out group-hover/row:rotate-[120deg]\">\n                                    <span className=\"absolute left-1/2 top-0 h-1 w-1 -translate-x-1/2 rounded-full bg-foreground\" />\n                                  </span>\n                                </span>\n                              </span>\n                              <div className=\"min-w-0\">\n                                <div className=\"truncate text-sm font-medium\">\n                                  {row.name}\n                                </div>\n                                <div className=\"truncate text-xs text-muted-foreground\">\n                                  {row.email}\n                                </div>\n                              </div>\n                            </div>\n                          </td>\n                          <td\n                            className={cn(\n                              \"px-3 py-3 text-left\",\n                              isLast ? \"border-b-0\" : \"border-b border-border\"\n                            )}\n                          >\n                            <span className=\"inline-flex rounded-md bg-muted px-2 py-0.5 text-xs font-medium text-muted-foreground\">\n                              {row.plan}\n                            </span>\n                          </td>\n                          <td\n                            className={cn(\n                              \"px-3 py-3 text-right tabular-nums\",\n                              isLast ? \"border-b-0\" : \"border-b border-border\"\n                            )}\n                          >\n                            <span className=\"text-sm font-medium\">\n                              {formatAmount(row.amount)}\n                            </span>\n                          </td>\n                          <td\n                            className={cn(\n                              \"px-3 py-3 text-left\",\n                              isLast ? \"border-b-0\" : \"border-b border-border\"\n                            )}\n                          >\n                            <span\n                              className={cn(\n                                \"inline-flex items-center gap-1.5 rounded-full border px-2 py-0.5 text-[11px] font-medium\",\n                                meta.classes\n                              )}\n                            >\n                              <span\n                                className={cn(\n                                  \"h-1.5 w-1.5 rounded-full\",\n                                  meta.dot\n                                )}\n                              />\n                              {meta.label}\n                            </span>\n                          </td>\n                          <td\n                            className={cn(\n                              \"py-3 pl-3 pr-0 text-right tabular-nums\",\n                              isLast ? \"border-b-0\" : \"border-b border-border\"\n                            )}\n                          >\n                            <span className=\"text-sm text-muted-foreground\">\n                              {formatDate(row.date)}\n                            </span>\n                          </td>\n                        </motion.tr>\n                      );\n                    })}\n                  </AnimatePresence>\n                )}\n              </tbody>\n            </table>\n          </div>\n\n          {/* footer / pagination */}\n          <div className=\"mt-4 flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between\">\n            <p className=\"text-[13px] tabular-nums text-muted-foreground\">\n              {sorted.length === 0\n                ? \"0 results\"\n                : `Showing ${rangeStart}–${rangeEnd} of ${sorted.length}`}\n            </p>\n\n            <nav\n              aria-label=\"Pagination\"\n              className=\"flex items-center gap-1 self-start sm:self-auto\"\n            >\n              <button\n                type=\"button\"\n                aria-label=\"Previous page\"\n                onClick={() => setPage((p) => Math.max(1, p - 1))}\n                disabled={currentPage <= 1}\n                className=\"grid h-8 w-8 place-items-center rounded-md text-muted-foreground transition-colors hover:bg-muted hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-40\"\n              >\n                <ChevronLeft className=\"h-4 w-4\" />\n              </button>\n\n              {pageItems(currentPage, totalPages).map((item, i) =>\n                item === \"ellipsis\" ? (\n                  <span\n                    key={`ellipsis-${i}`}\n                    aria-hidden\n                    className=\"px-1 text-sm text-muted-foreground\"\n                  >\n                    …\n                  </span>\n                ) : (\n                  <button\n                    key={item}\n                    type=\"button\"\n                    aria-current={item === currentPage ? \"page\" : undefined}\n                    onClick={() => setPage(item)}\n                    className={cn(\n                      \"h-8 min-w-8 rounded-md px-2 text-sm tabular-nums transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring\",\n                      item === currentPage\n                        ? \"bg-primary font-medium text-primary-foreground\"\n                        : \"text-muted-foreground hover:bg-muted hover:text-foreground\"\n                    )}\n                  >\n                    {item}\n                  </button>\n                )\n              )}\n\n              <button\n                type=\"button\"\n                aria-label=\"Next page\"\n                onClick={() => setPage((p) => Math.min(totalPages, p + 1))}\n                disabled={currentPage >= totalPages}\n                className=\"grid h-8 w-8 place-items-center rounded-md text-muted-foreground transition-colors hover:bg-muted hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-40\"\n              >\n                <ChevronRight className=\"h-4 w-4\" />\n              </button>\n            </nav>\n          </div>\n        </div>\n      </motion.section>\n    </MotionConfig>\n  );\n}\n\nexport default DataTable;\n"}]}