{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "virtualized-table",
  "title": "Virtualized Table",
  "description": "A table that scrolls through 10,000 or 100,000 rows without slowing down: only the rows on screen are rendered, with instant search, sortable columns, a real-size scrollbar and a footer that shows what's visible and how many rows are actually in the DOM.",
  "dependencies": [
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/virtualized-table/virtualized-table.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { clsx, type ClassValue } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nfunction cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n\nexport type TableTheme = \"dark\" | \"light\";\n\nexport interface VirtualColumn<T> {\n  key: keyof T & string;\n  label: string;\n  /** CSS grid track, e.g. \"1.6fr\" or \"96px\". */\n  width: string;\n  align?: \"left\" | \"right\";\n  sortable?: boolean;\n  /** Hidden below the small breakpoint. */\n  hideOnMobile?: boolean;\n  render?: (row: T) => React.ReactNode;\n}\n\nexport interface VirtualUser {\n  id: number;\n  name: string;\n  email: string;\n  country: string;\n  plan: \"Free\" | \"Pro\" | \"Team\" | \"Enterprise\";\n  mrr: number;\n  signedUp: string;\n  status: \"Active\" | \"Trial\" | \"Churned\";\n}\n\nexport interface VirtualizedTableProps extends Omit<\n  React.ComponentPropsWithoutRef<\"div\">,\n  \"title\"\n> {\n  theme?: TableTheme;\n  title?: string;\n  /** How many sample rows to generate when no rows are passed. */\n  rowCount?: number;\n  rows?: VirtualUser[];\n  /** Height of the scroll area, in px. */\n  height?: number;\n  rowHeight?: number;\n  /** Extra rows rendered above and below the visible window. */\n  overscan?: number;\n}\n\nconst PALETTES = {\n  dark: {\n    card: \"#0E0E0E\",\n    border: \"rgba(255,255,255,0.08)\",\n    text: \"#F4F5F7\",\n    sub: \"rgba(255,255,255,0.55)\",\n    faint: \"rgba(255,255,255,0.38)\",\n    head: \"#131315\",\n    chip: \"rgba(255,255,255,0.06)\",\n    hover: \"rgba(255,255,255,0.03)\",\n    stripe: \"rgba(255,255,255,0.012)\",\n    input: \"rgba(255,255,255,0.05)\",\n    accent: \"#8FB8FF\",\n    good: \"#2EE6A8\",\n    warn: \"#F2A841\",\n    bad: \"#FF7A6B\",\n  },\n  light: {\n    card: \"#FFFFFF\",\n    border: \"rgba(0,0,0,0.08)\",\n    text: \"#111317\",\n    sub: \"rgba(17,19,23,0.55)\",\n    faint: \"rgba(17,19,23,0.4)\",\n    head: \"#F7F7F5\",\n    chip: \"rgba(17,19,23,0.05)\",\n    hover: \"rgba(17,19,23,0.025)\",\n    stripe: \"rgba(17,19,23,0.012)\",\n    input: \"rgba(17,19,23,0.04)\",\n    accent: \"#3B6FD8\",\n    good: \"#0E9F6E\",\n    warn: \"#D9822B\",\n    bad: \"#E5484D\",\n  },\n};\n\nconst FIRST = [\n  \"Ava\",\n  \"Noah\",\n  \"Mira\",\n  \"Leo\",\n  \"Zeynep\",\n  \"Kai\",\n  \"Ines\",\n  \"Theo\",\n  \"Priya\",\n  \"Omar\",\n  \"Sofia\",\n  \"Jonah\",\n  \"Ayla\",\n  \"Luca\",\n  \"Nina\",\n  \"Ezra\",\n  \"Hana\",\n  \"Mateo\",\n  \"Elif\",\n  \"Finn\",\n  \"Yara\",\n  \"Arjun\",\n  \"Clara\",\n  \"Ren\",\n];\nconst LAST = [\n  \"Castell\",\n  \"Reyes\",\n  \"Demir\",\n  \"Lindqvist\",\n  \"Nair\",\n  \"Okafor\",\n  \"Moreau\",\n  \"Tanaka\",\n  \"Kowalski\",\n  \"Haddad\",\n  \"Silva\",\n  \"Novak\",\n  \"Fischer\",\n  \"Rossi\",\n  \"Yilmaz\",\n  \"Park\",\n  \"Costa\",\n  \"Dubois\",\n  \"Berg\",\n  \"Ahmed\",\n];\nconst COUNTRIES = [\n  \"Türkiye\",\n  \"Germany\",\n  \"United States\",\n  \"Brazil\",\n  \"Japan\",\n  \"India\",\n  \"France\",\n  \"Sweden\",\n  \"Nigeria\",\n  \"Canada\",\n  \"Spain\",\n  \"Australia\",\n];\nconst PLANS: VirtualUser[\"plan\"][] = [\n  \"Free\",\n  \"Free\",\n  \"Pro\",\n  \"Pro\",\n  \"Pro\",\n  \"Team\",\n  \"Team\",\n  \"Enterprise\",\n];\nconst PRICE: Record<VirtualUser[\"plan\"], number> = {\n  Free: 0,\n  Pro: 19,\n  Team: 79,\n  Enterprise: 499,\n};\nconst MONTHS = [\n  \"Jan\",\n  \"Feb\",\n  \"Mar\",\n  \"Apr\",\n  \"May\",\n  \"Jun\",\n  \"Jul\",\n  \"Aug\",\n  \"Sep\",\n  \"Oct\",\n  \"Nov\",\n  \"Dec\",\n];\n\n/** Sample rows from the index alone, so they're identical on server and client. */\nfunction makeRows(n: number): VirtualUser[] {\n  const out: VirtualUser[] = new Array(n);\n  let s = 12345;\n  const rnd = () => (s = (s * 16807) % 2147483647) / 2147483647;\n  for (let i = 0; i < n; i++) {\n    const first = FIRST[Math.floor(rnd() * FIRST.length)];\n    const last = LAST[Math.floor(rnd() * LAST.length)];\n    const plan = PLANS[Math.floor(rnd() * PLANS.length)];\n    const seats =\n      plan === \"Team\"\n        ? 2 + Math.floor(rnd() * 12)\n        : plan === \"Enterprise\"\n          ? 1 + Math.floor(rnd() * 6)\n          : 1;\n    const r = rnd();\n    const status: VirtualUser[\"status\"] =\n      r < 0.08 ? \"Churned\" : r < 0.2 ? \"Trial\" : \"Active\";\n    const day = 1 + Math.floor(rnd() * 28);\n    const month = Math.floor(rnd() * 12);\n    const year = 2022 + Math.floor(rnd() * 4);\n    out[i] = {\n      id: 100000 + i,\n      name: `${first} ${last}`,\n      email: `${first}.${last}${i % 97}@mail.com`.toLowerCase(),\n      country: COUNTRIES[Math.floor(rnd() * COUNTRIES.length)],\n      plan,\n      mrr: status === \"Churned\" || status === \"Trial\" ? 0 : PRICE[plan] * seats,\n      signedUp: `${year}-${String(month + 1).padStart(2, \"0\")}-${String(day).padStart(2, \"0\")}`,\n      status,\n    };\n  }\n  return out;\n}\n\nfunction useWidth(ref: React.RefObject<HTMLElement | null>) {\n  const [width, setWidth] = React.useState(0);\n  React.useEffect(() => {\n    const el = ref.current;\n    if (!el) return;\n    const ro = new ResizeObserver(([entry]) =>\n      setWidth(entry.contentRect.width),\n    );\n    ro.observe(el);\n    return () => ro.disconnect();\n  }, [ref]);\n  return width;\n}\n\ntype SortKey = keyof VirtualUser;\n\nexport function VirtualizedTable({\n  theme = \"dark\",\n  title = \"Customers\",\n  rowCount = 10000,\n  rows: givenRows,\n  height = 440,\n  rowHeight = 44,\n  overscan = 8,\n  className,\n  style,\n  ...props\n}: VirtualizedTableProps) {\n  const pal = PALETTES[theme];\n  const rootRef = React.useRef<HTMLDivElement>(null);\n  const scrollRef = React.useRef<HTMLDivElement>(null);\n  const width = useWidth(rootRef);\n  const mobile = width > 0 && width < 640;\n  const all = React.useMemo(\n    () => givenRows ?? makeRows(rowCount),\n    [givenRows, rowCount],\n  );\n  const [query, setQuery] = React.useState(\"\");\n  const deferredQuery = React.useDeferredValue(query);\n  const [sort, setSort] = React.useState<{ key: SortKey; dir: 1 | -1 }>({\n    key: \"id\",\n    dir: 1,\n  });\n  const [scrollTop, setScrollTop] = React.useState(0);\n\n  const STATUS: Record<VirtualUser[\"status\"], string> = {\n    Active: pal.good,\n    Trial: pal.warn,\n    Churned: pal.bad,\n  };\n  const columns: VirtualColumn<VirtualUser>[] = [\n    {\n      key: \"id\",\n      label: \"ID\",\n      width: \"84px\",\n      sortable: true,\n      hideOnMobile: true,\n      render: (r) => (\n        <span className=\"font-mono text-[11.5px]\" style={{ color: pal.faint }}>\n          #{r.id}\n        </span>\n      ),\n    },\n    {\n      key: \"name\",\n      label: \"Customer\",\n      width: \"minmax(0,1.7fr)\",\n      sortable: true,\n      render: (r) => (\n        <span className=\"flex min-w-0 items-center gap-2.5\">\n          <span\n            className=\"grid size-6 shrink-0 place-items-center rounded-full text-[9.5px] font-semibold\"\n            style={{ background: pal.chip }}\n          >\n            {r.name\n              .split(\" \")\n              .map((w) => w[0])\n              .join(\"\")}\n          </span>\n          <span className=\"min-w-0\">\n            <span className=\"block truncate leading-tight font-medium\">\n              {r.name}\n            </span>\n            <span\n              className=\"block truncate text-[11px] leading-tight\"\n              style={{ color: pal.sub }}\n            >\n              {r.email}\n            </span>\n          </span>\n        </span>\n      ),\n    },\n    {\n      key: \"country\",\n      label: \"Country\",\n      width: \"minmax(0,1fr)\",\n      sortable: true,\n      hideOnMobile: true,\n      render: (r) => (\n        <span className=\"truncate\" style={{ color: pal.sub }}>\n          {r.country}\n        </span>\n      ),\n    },\n    {\n      key: \"plan\",\n      label: \"Plan\",\n      width: \"96px\",\n      sortable: true,\n      hideOnMobile: true,\n      render: (r) => (\n        <span\n          className=\"rounded-md px-1.5 py-0.5 text-[11.5px]\"\n          style={{ background: pal.chip }}\n        >\n          {r.plan}\n        </span>\n      ),\n    },\n    {\n      key: \"mrr\",\n      label: \"MRR\",\n      width: \"84px\",\n      align: \"right\",\n      sortable: true,\n      render: (r) => (\n        <span\n          className=\"font-medium tabular-nums\"\n          style={{ color: r.mrr ? pal.text : pal.faint }}\n        >\n          {r.mrr ? `$${r.mrr.toLocaleString(\"en-US\")}` : \"–\"}\n        </span>\n      ),\n    },\n    {\n      key: \"signedUp\",\n      label: \"Signed up\",\n      width: \"104px\",\n      align: \"right\",\n      sortable: true,\n      hideOnMobile: true,\n      render: (r) => {\n        const [y, m, d] = r.signedUp.split(\"-\");\n        return (\n          <span\n            className=\"tabular-nums\"\n            style={{ color: pal.sub }}\n          >{`${MONTHS[Number(m) - 1]} ${Number(d)}, ${y}`}</span>\n        );\n      },\n    },\n    {\n      key: \"status\",\n      label: \"Status\",\n      width: \"92px\",\n      sortable: true,\n      render: (r) => (\n        <span\n          className=\"inline-flex items-center gap-1.5 text-[12px]\"\n          style={{ color: STATUS[r.status] }}\n        >\n          <span\n            className=\"size-1.5 rounded-full\"\n            style={{ background: STATUS[r.status] }}\n          />\n          {r.status}\n        </span>\n      ),\n    },\n  ];\n  const shown = columns.filter((c) => !(mobile && c.hideOnMobile));\n  const template = shown.map((c) => c.width).join(\" \");\n\n  // Filter + sort the whole set; only the rows in view get rendered below.\n  const view = React.useMemo(() => {\n    const q = deferredQuery.trim().toLowerCase();\n    const filtered = q\n      ? all.filter(\n          (r) =>\n            r.name.toLowerCase().includes(q) ||\n            r.email.includes(q) ||\n            r.country.toLowerCase().includes(q) ||\n            String(r.id).includes(q),\n        )\n      : all.slice();\n    filtered.sort((a, b) => {\n      const va = a[sort.key];\n      const vb = b[sort.key];\n      return (\n        (typeof va === \"number\"\n          ? va - (vb as number)\n          : String(va).localeCompare(String(vb))) * sort.dir\n      );\n    });\n    return { rows: filtered };\n  }, [all, deferredQuery, sort]);\n\n  const total = view.rows.length;\n  const first = Math.max(0, Math.floor(scrollTop / rowHeight) - overscan);\n  const last = Math.min(\n    total,\n    Math.ceil((scrollTop + height) / rowHeight) + overscan,\n  );\n  const slice = view.rows.slice(first, last);\n  const visibleFrom = Math.min(total, Math.floor(scrollTop / rowHeight) + 1);\n  const visibleTo = Math.min(\n    total,\n    Math.floor((scrollTop + height - 36) / rowHeight),\n  );\n\n  const ticking = React.useRef(false);\n  const onScroll = () => {\n    if (ticking.current) return;\n    ticking.current = true;\n    requestAnimationFrame(() => {\n      ticking.current = false;\n      setScrollTop(scrollRef.current?.scrollTop ?? 0);\n    });\n  };\n  const resetScroll = () => {\n    if (scrollRef.current) scrollRef.current.scrollTop = 0;\n    setScrollTop(0);\n  };\n\n  return (\n    <div\n      ref={rootRef}\n      className={cn(\"w-full overflow-hidden rounded-[20px]\", className)}\n      style={{\n        background: pal.card,\n        border: `1px solid ${pal.border}`,\n        color: pal.text,\n        fontFamily: \"Inter, -apple-system, BlinkMacSystemFont, sans-serif\",\n        ...style,\n      }}\n      {...props}\n    >\n      <div className=\"flex flex-wrap items-center justify-between gap-3 px-5 pt-5 pb-4 sm:px-6\">\n        <div>\n          <p className=\"text-[15px] font-semibold tracking-[-0.01em]\">\n            {title}\n          </p>\n          <p\n            className=\"mt-1 text-[12.5px] tabular-nums\"\n            style={{ color: pal.sub }}\n          >\n            {all.length.toLocaleString(\"en-US\")} rows, only the ones on screen\n            are rendered\n          </p>\n        </div>\n        <label\n          className=\"flex w-full items-center gap-2 rounded-full px-3 py-1.5 text-[12.5px] sm:w-64\"\n          style={{ background: pal.input }}\n        >\n          <svg\n            width=\"14\"\n            height=\"14\"\n            viewBox=\"0 0 14 14\"\n            fill=\"none\"\n            aria-hidden=\"true\"\n            style={{ color: pal.faint }}\n          >\n            <circle\n              cx=\"6\"\n              cy=\"6\"\n              r=\"4.2\"\n              stroke=\"currentColor\"\n              strokeWidth=\"1.4\"\n            />\n            <path\n              d=\"M9.2 9.2L12 12\"\n              stroke=\"currentColor\"\n              strokeWidth=\"1.4\"\n              strokeLinecap=\"round\"\n            />\n          </svg>\n          <input\n            type=\"search\"\n            value={query}\n            onChange={(e) => {\n              setQuery(e.target.value);\n              resetScroll();\n            }}\n            placeholder=\"Search name, email, country…\"\n            aria-label=\"Search customers\"\n            className=\"min-w-0 flex-1 bg-transparent outline-none\"\n            style={{ color: pal.text }}\n          />\n        </label>\n      </div>\n\n      <div\n        role=\"table\"\n        aria-label={title}\n        aria-rowcount={total + 1}\n        className=\"border-t\"\n        style={{ borderColor: pal.border }}\n      >\n        <div role=\"rowgroup\">\n          <div\n            role=\"row\"\n            aria-rowindex={1}\n            className=\"grid items-center gap-3 border-b px-5 sm:px-6\"\n            style={{\n              gridTemplateColumns: template,\n              height: 36,\n              borderColor: pal.border,\n              background: pal.head,\n            }}\n          >\n            {shown.map((c) => (\n              <div\n                key={c.key}\n                role=\"columnheader\"\n                aria-sort={\n                  sort.key === c.key\n                    ? sort.dir === 1\n                      ? \"ascending\"\n                      : \"descending\"\n                    : \"none\"\n                }\n                className={c.align === \"right\" ? \"text-right\" : \"text-left\"}\n              >\n                {c.sortable ? (\n                  <button\n                    type=\"button\"\n                    onClick={() => {\n                      setSort((s) =>\n                        s.key === c.key\n                          ? { key: c.key, dir: s.dir === 1 ? -1 : 1 }\n                          : { key: c.key, dir: c.key === \"mrr\" ? -1 : 1 },\n                      );\n                      resetScroll();\n                    }}\n                    className=\"cursor-pointer text-[11px] font-medium tracking-[0.04em] uppercase\"\n                    style={{ color: sort.key === c.key ? pal.text : pal.faint }}\n                  >\n                    {c.label}{\" \"}\n                    {sort.key === c.key ? (sort.dir === 1 ? \"↑\" : \"↓\") : \"\"}\n                  </button>\n                ) : (\n                  <span\n                    className=\"text-[11px] font-medium tracking-[0.04em] uppercase\"\n                    style={{ color: pal.faint }}\n                  >\n                    {c.label}\n                  </span>\n                )}\n              </div>\n            ))}\n          </div>\n        </div>\n        <div\n          ref={scrollRef}\n          role=\"rowgroup\"\n          tabIndex={0}\n          aria-label=\"Rows, scrollable\"\n          onScroll={onScroll}\n          className=\"relative overflow-y-auto overscroll-contain outline-none focus-visible:ring-2 focus-visible:ring-inset\"\n          style={{ height: height - 36 }}\n        >\n          {/* A spacer as tall as every row together gives the scrollbar its real size. */}\n          <div style={{ height: total * rowHeight, position: \"relative\" }}>\n            {slice.map((r, k) => {\n              const i = first + k;\n              return (\n                <div\n                  key={r.id}\n                  role=\"row\"\n                  aria-rowindex={i + 2}\n                  className=\"absolute inset-x-0 grid items-center gap-3 border-b px-5 text-[13px] sm:px-6\"\n                  style={{\n                    gridTemplateColumns: template,\n                    height: rowHeight,\n                    transform: `translateY(${i * rowHeight}px)`,\n                    borderColor: pal.border,\n                    background: i % 2 ? pal.stripe : undefined,\n                  }}\n                  onMouseEnter={(e) =>\n                    (e.currentTarget.style.background = pal.hover)\n                  }\n                  onMouseLeave={(e) =>\n                    (e.currentTarget.style.background = i % 2 ? pal.stripe : \"\")\n                  }\n                >\n                  {shown.map((c) => (\n                    <div\n                      key={c.key}\n                      role=\"cell\"\n                      className={cn(\n                        \"min-w-0 truncate\",\n                        c.align === \"right\" && \"text-right\",\n                      )}\n                    >\n                      {c.render ? c.render(r) : String(r[c.key])}\n                    </div>\n                  ))}\n                </div>\n              );\n            })}\n            {total === 0 && (\n              <p\n                className=\"absolute inset-x-0 top-10 text-center text-[13px]\"\n                style={{ color: pal.sub }}\n              >\n                No customers match “{deferredQuery}”.\n              </p>\n            )}\n          </div>\n        </div>\n      </div>\n\n      <div\n        className=\"flex flex-wrap items-center justify-between gap-3 border-t px-5 py-2.5 text-[11.5px] tabular-nums sm:px-6\"\n        style={{ borderColor: pal.border, color: pal.sub }}\n      >\n        <span>\n          {total ? (\n            <>\n              Showing{\" \"}\n              <b style={{ color: pal.text }}>\n                {visibleFrom.toLocaleString(\"en-US\")}–\n                {visibleTo.toLocaleString(\"en-US\")}\n              </b>{\" \"}\n              of {total.toLocaleString(\"en-US\")}\n              {total !== all.length &&\n                ` (filtered from ${all.length.toLocaleString(\"en-US\")})`}\n            </>\n          ) : (\n            \"No results\"\n          )}\n        </span>\n        <span>\n          <span style={{ color: pal.accent }}>{slice.length}</span> rows in the\n          DOM\n        </span>\n      </div>\n    </div>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}