{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "grouped-rows-table",
  "title": "Grouped Rows Table",
  "description": "A deals table grouped by stage or owner: sticky group headers with a count, total, weighted value and share bar, groups that fold away smoothly, a Group by switch and collapse all.",
  "dependencies": [
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/grouped-rows-table/grouped-rows-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\";\nexport type DealStage =\n  \"Lead\" | \"Qualified\" | \"Proposal\" | \"Negotiation\" | \"Won\";\n\nexport interface Deal {\n  company: string;\n  owner: string;\n  stage: DealStage;\n  value: number;\n  /** Chance of closing, 0–1. */\n  probability: number;\n  close: string;\n}\n\nexport type GroupKey = \"stage\" | \"owner\" | \"none\";\n\nexport interface GroupedRowsTableProps extends Omit<\n  React.ComponentPropsWithoutRef<\"div\">,\n  \"title\"\n> {\n  theme?: TableTheme;\n  title?: string;\n  subtitle?: string;\n  deals?: Deal[];\n  /** Field to group by at first. */\n  groupBy?: GroupKey;\n  /** Height of the scroll area, in px. */\n  height?: number;\n  currency?: string;\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    group: \"#151619\",\n    hover: \"rgba(255,255,255,0.025)\",\n    chip: \"rgba(255,255,255,0.06)\",\n    track: \"rgba(255,255,255,0.08)\",\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    group: \"#F4F4F2\",\n    hover: \"rgba(17,19,23,0.02)\",\n    chip: \"rgba(17,19,23,0.05)\",\n    track: \"rgba(17,19,23,0.08)\",\n  },\n};\n\nconst STAGES: DealStage[] = [\n  \"Lead\",\n  \"Qualified\",\n  \"Proposal\",\n  \"Negotiation\",\n  \"Won\",\n];\nconst STAGE_COLOR: Record<TableTheme, Record<DealStage, string>> = {\n  dark: {\n    Lead: \"#9AA0AA\",\n    Qualified: \"#8FB8FF\",\n    Proposal: \"#B69CFF\",\n    Negotiation: \"#F2A841\",\n    Won: \"#2EE6A8\",\n  },\n  light: {\n    Lead: \"#6B7280\",\n    Qualified: \"#3B6FD8\",\n    Proposal: \"#7B5CE0\",\n    Negotiation: \"#D9822B\",\n    Won: \"#0E9F6E\",\n  },\n};\n\nconst DEFAULT_DEALS: Deal[] = [\n  {\n    company: \"Northwind Labs\",\n    owner: \"Ayla Demir\",\n    stage: \"Negotiation\",\n    value: 84000,\n    probability: 0.7,\n    close: \"Nov 28\",\n  },\n  {\n    company: \"Brightline Health\",\n    owner: \"Jonah Reyes\",\n    stage: \"Proposal\",\n    value: 126000,\n    probability: 0.45,\n    close: \"Dec 12\",\n  },\n  {\n    company: \"Kanat Air\",\n    owner: \"Ayla Demir\",\n    stage: \"Won\",\n    value: 58000,\n    probability: 1,\n    close: \"Nov 3\",\n  },\n  {\n    company: \"Copperwick\",\n    owner: \"Mira Castell\",\n    stage: \"Qualified\",\n    value: 22000,\n    probability: 0.25,\n    close: \"Jan 9\",\n  },\n  {\n    company: \"Graphline\",\n    owner: \"Jonah Reyes\",\n    stage: \"Negotiation\",\n    value: 64000,\n    probability: 0.65,\n    close: \"Nov 30\",\n  },\n  {\n    company: \"Orbit Freight\",\n    owner: \"Mira Castell\",\n    stage: \"Lead\",\n    value: 18000,\n    probability: 0.1,\n    close: \"Feb 2\",\n  },\n  {\n    company: \"Sora Robotics\",\n    owner: \"Theo Lindqvist\",\n    stage: \"Proposal\",\n    value: 92000,\n    probability: 0.5,\n    close: \"Dec 19\",\n  },\n  {\n    company: \"Lumen Grid\",\n    owner: \"Theo Lindqvist\",\n    stage: \"Won\",\n    value: 41000,\n    probability: 1,\n    close: \"Oct 29\",\n  },\n  {\n    company: \"Paloma Foods\",\n    owner: \"Ayla Demir\",\n    stage: \"Qualified\",\n    value: 15500,\n    probability: 0.3,\n    close: \"Jan 16\",\n  },\n  {\n    company: \"Halcyon Bank\",\n    owner: \"Jonah Reyes\",\n    stage: \"Lead\",\n    value: 210000,\n    probability: 0.08,\n    close: \"Mar 6\",\n  },\n  {\n    company: \"Fjord Analytics\",\n    owner: \"Mira Castell\",\n    stage: \"Proposal\",\n    value: 36000,\n    probability: 0.55,\n    close: \"Dec 5\",\n  },\n  {\n    company: \"Mesa Mobility\",\n    owner: \"Theo Lindqvist\",\n    stage: \"Negotiation\",\n    value: 47000,\n    probability: 0.75,\n    close: \"Nov 21\",\n  },\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\nexport function GroupedRowsTable({\n  theme = \"dark\",\n  title = \"Pipeline\",\n  subtitle = \"Group deals by stage or owner, fold groups you don't need\",\n  deals = DEFAULT_DEALS,\n  groupBy: initialGroup = \"stage\",\n  height = 440,\n  currency = \"$\",\n  className,\n  style,\n  ...props\n}: GroupedRowsTableProps) {\n  const pal = PALETTES[theme];\n  const boxRef = React.useRef<HTMLDivElement>(null);\n  const width = useWidth(boxRef);\n  const compact = width > 0 && width < 620;\n  const [groupBy, setGroupBy] = React.useState<GroupKey>(initialGroup);\n  const [closed, setClosed] = React.useState<Set<string>>(new Set());\n  const money = (v: number) =>\n    `${currency}${v >= 1e6 ? `${(v / 1e6).toFixed(2)}M` : v >= 1000 ? `${(v / 1000).toFixed(v >= 100000 ? 0 : 1)}K` : v}`;\n  const stageColor = STAGE_COLOR[theme];\n\n  const groups = React.useMemo(() => {\n    if (groupBy === \"none\")\n      return [\n        {\n          key: \"All deals\",\n          rows: [...deals].sort((a, b) => b.value - a.value),\n        },\n      ];\n    const keys =\n      groupBy === \"stage\"\n        ? STAGES.filter((s) => deals.some((d) => d.stage === s))\n        : [...new Set(deals.map((d) => d.owner))].sort();\n    return keys.map((k) => ({\n      key: k,\n      rows: deals\n        .filter((d) => d[groupBy] === k)\n        .sort((a, b) => b.value - a.value),\n    }));\n  }, [deals, groupBy]);\n  const total = deals.reduce((s, d) => s + d.value, 0);\n  const weighted = deals.reduce((s, d) => s + d.value * d.probability, 0);\n  const allClosed = groups.every((g) => closed.has(g.key));\n  const cols = compact\n    ? \"grid-cols-[1fr_auto]\"\n    : \"grid-cols-[1.6fr_1.2fr_1fr_100px_90px_80px]\";\n\n  return (\n    <div\n      ref={boxRef}\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-start justify-between gap-3 px-5 pt-5 pb-4 sm:px-6\">\n        <div className=\"min-w-0\">\n          <p className=\"text-[15px] font-semibold tracking-[-0.01em]\">\n            {title}\n          </p>\n          {subtitle && (\n            <p className=\"mt-1 text-[12.5px]\" style={{ color: pal.sub }}>\n              {subtitle}\n            </p>\n          )}\n        </div>\n        <div className=\"flex gap-5 text-right\">\n          <div>\n            <p className=\"text-[19px] leading-tight font-semibold tracking-[-0.02em] tabular-nums\">\n              {money(total)}\n            </p>\n            <p className=\"mt-0.5 text-[11px]\" style={{ color: pal.sub }}>\n              Pipeline\n            </p>\n          </div>\n          <div>\n            <p className=\"text-[19px] leading-tight font-semibold tracking-[-0.02em] tabular-nums\">\n              {money(Math.round(weighted))}\n            </p>\n            <p className=\"mt-0.5 text-[11px]\" style={{ color: pal.sub }}>\n              Weighted\n            </p>\n          </div>\n        </div>\n      </div>\n      <div className=\"flex flex-wrap items-center justify-between gap-3 px-5 pb-3 sm:px-6\">\n        <div\n          role=\"radiogroup\"\n          aria-label=\"Group by\"\n          className=\"flex items-center gap-1 rounded-full p-0.5 text-[12px] font-medium\"\n          style={{ background: pal.chip }}\n        >\n          <span className=\"px-2 text-[11px]\" style={{ color: pal.faint }}>\n            Group by\n          </span>\n          {([\"stage\", \"owner\", \"none\"] as const).map((g) => (\n            <button\n              key={g}\n              type=\"button\"\n              role=\"radio\"\n              aria-checked={groupBy === g}\n              onClick={() => {\n                setGroupBy(g);\n                setClosed(new Set());\n              }}\n              className=\"cursor-pointer rounded-full px-2.5 py-1 capitalize transition-colors\"\n              style={{\n                background: groupBy === g ? pal.card : \"transparent\",\n                color: groupBy === g ? pal.text : pal.sub,\n                boxShadow: groupBy === g ? \"0 1px 2px rgba(0,0,0,.2)\" : \"none\",\n              }}\n            >\n              {g}\n            </button>\n          ))}\n        </div>\n        {groupBy !== \"none\" && (\n          <button\n            type=\"button\"\n            onClick={() =>\n              setClosed(\n                allClosed ? new Set() : new Set(groups.map((g) => g.key)),\n              )\n            }\n            className=\"cursor-pointer text-[12px] font-medium\"\n            style={{ color: pal.sub }}\n          >\n            {allClosed ? \"Expand all\" : \"Collapse all\"}\n          </button>\n        )}\n      </div>\n\n      <div\n        role=\"table\"\n        aria-label={title}\n        className=\"relative overflow-y-auto border-t\"\n        style={{ maxHeight: height, borderColor: pal.border }}\n      >\n        <div role=\"rowgroup\" className=\"sticky top-0 z-20\">\n          <div\n            role=\"row\"\n            className={cn(\n              \"grid items-center gap-3 border-b px-5 py-2.5 text-[11px] font-medium tracking-[0.04em] uppercase sm:px-6\",\n              cols,\n            )}\n            style={{\n              borderColor: pal.border,\n              background: pal.head,\n              color: pal.faint,\n            }}\n          >\n            <span role=\"columnheader\">Company</span>\n            {compact ? (\n              <span role=\"columnheader\" className=\"text-right\">\n                Value\n              </span>\n            ) : (\n              <>\n                <span role=\"columnheader\">Owner</span>\n                <span role=\"columnheader\">Stage</span>\n                <span role=\"columnheader\" className=\"text-right\">\n                  Value\n                </span>\n                <span role=\"columnheader\" className=\"text-right\">\n                  Chance\n                </span>\n                <span role=\"columnheader\" className=\"text-right\">\n                  Close\n                </span>\n              </>\n            )}\n          </div>\n        </div>\n        {groups.map((g) => {\n          const isClosed = closed.has(g.key);\n          const sum = g.rows.reduce((s, d) => s + d.value, 0);\n          const w = g.rows.reduce((s, d) => s + d.value * d.probability, 0);\n          const dot =\n            groupBy === \"stage\" ? stageColor[g.key as DealStage] : undefined;\n          return (\n            <div\n              key={g.key}\n              role=\"rowgroup\"\n              aria-label={`${g.key}, ${g.rows.length} deals`}\n            >\n              {groupBy !== \"none\" && (\n                // The group header sticks under the column header while its rows scroll past.\n                <div\n                  role=\"row\"\n                  className=\"sticky top-[37px] z-10 border-b\"\n                  style={{ borderColor: pal.border, background: pal.group }}\n                >\n                  <div role=\"cell\" className=\"px-3 py-2 sm:px-4\">\n                    <button\n                      type=\"button\"\n                      aria-expanded={!isClosed}\n                      onClick={() =>\n                        setClosed((prev) => {\n                          const next = new Set(prev);\n                          if (next.has(g.key)) next.delete(g.key);\n                          else next.add(g.key);\n                          return next;\n                        })\n                      }\n                      className=\"flex w-full cursor-pointer items-center gap-2.5 rounded-lg px-2 py-1 text-left\"\n                    >\n                      <svg\n                        width=\"12\"\n                        height=\"12\"\n                        viewBox=\"0 0 12 12\"\n                        fill=\"none\"\n                        aria-hidden=\"true\"\n                        className=\"shrink-0 transition-transform duration-200\"\n                        style={{\n                          transform: isClosed ? \"rotate(-90deg)\" : \"none\",\n                          color: pal.sub,\n                        }}\n                      >\n                        <path\n                          d=\"M3 4.5l3 3 3-3\"\n                          stroke=\"currentColor\"\n                          strokeWidth=\"1.6\"\n                          strokeLinecap=\"round\"\n                          strokeLinejoin=\"round\"\n                        />\n                      </svg>\n                      {dot ? (\n                        <span\n                          className=\"size-2 shrink-0 rounded-full\"\n                          style={{ background: dot }}\n                        />\n                      ) : (\n                        <span\n                          className=\"grid size-5 shrink-0 place-items-center rounded-full text-[9px] font-semibold\"\n                          style={{ background: pal.chip }}\n                        >\n                          {g.key\n                            .split(\" \")\n                            .map((x) => x[0])\n                            .join(\"\")}\n                        </span>\n                      )}\n                      <span className=\"text-[13px] font-semibold\">{g.key}</span>\n                      <span\n                        className=\"rounded-full px-1.5 text-[11px] tabular-nums\"\n                        style={{ background: pal.chip, color: pal.sub }}\n                      >\n                        {g.rows.length}\n                      </span>\n                      <span\n                        className=\"ml-auto flex items-center gap-4 text-[12px] tabular-nums\"\n                        style={{ color: pal.sub }}\n                      >\n                        <span>\n                          <b style={{ color: pal.text }}>{money(sum)}</b>\n                        </span>\n                        {!compact && (\n                          <span>weighted {money(Math.round(w))}</span>\n                        )}\n                        {!compact && (\n                          <span\n                            className=\"h-1.5 w-20 overflow-hidden rounded-full\"\n                            style={{ background: pal.track }}\n                            title={`${((sum / total) * 100).toFixed(0)}% of pipeline`}\n                          >\n                            <span\n                              className=\"block h-full rounded-full\"\n                              style={{\n                                width: `${(sum / total) * 100}%`,\n                                background: dot ?? pal.sub,\n                              }}\n                            />\n                          </span>\n                        )}\n                      </span>\n                    </button>\n                  </div>\n                </div>\n              )}\n              <div\n                className=\"grid transition-[grid-template-rows] duration-300 ease-out motion-reduce:transition-none\"\n                style={{ gridTemplateRows: isClosed ? \"0fr\" : \"1fr\" }}\n                inert={isClosed}\n              >\n                <div className=\"overflow-hidden\">\n                  {g.rows.map((d) => (\n                    <div\n                      key={d.company}\n                      role=\"row\"\n                      className={cn(\n                        \"grid items-center gap-3 border-b px-5 py-2.5 text-[13px] transition-colors sm:px-6\",\n                        cols,\n                      )}\n                      style={{ borderColor: pal.border }}\n                      onMouseEnter={(e) =>\n                        (e.currentTarget.style.background = pal.hover)\n                      }\n                      onMouseLeave={(e) =>\n                        (e.currentTarget.style.background = \"\")\n                      }\n                    >\n                      <span\n                        role=\"cell\"\n                        className={cn(\"min-w-0\", groupBy !== \"none\" && \"pl-7\")}\n                      >\n                        <span className=\"block truncate font-medium\">\n                          {d.company}\n                        </span>\n                        {compact && (\n                          <span\n                            className=\"block truncate text-[11.5px]\"\n                            style={{ color: pal.sub }}\n                          >\n                            {groupBy === \"owner\" ? d.stage : d.owner} ·{\" \"}\n                            {d.close}\n                          </span>\n                        )}\n                      </span>\n                      {compact ? (\n                        <span\n                          role=\"cell\"\n                          className=\"text-right font-semibold tabular-nums\"\n                        >\n                          {money(d.value)}\n                        </span>\n                      ) : (\n                        <>\n                          <span\n                            role=\"cell\"\n                            className=\"truncate\"\n                            style={{ color: pal.sub }}\n                          >\n                            {d.owner}\n                          </span>\n                          <span role=\"cell\">\n                            <span\n                              className=\"inline-flex items-center gap-1.5 rounded-full px-2 py-0.5 text-[11.5px]\"\n                              style={{\n                                background: `${stageColor[d.stage]}1c`,\n                                color: stageColor[d.stage],\n                              }}\n                            >\n                              <span\n                                className=\"size-1.5 rounded-full\"\n                                style={{ background: stageColor[d.stage] }}\n                              />\n                              {d.stage}\n                            </span>\n                          </span>\n                          <span\n                            role=\"cell\"\n                            className=\"text-right font-semibold tabular-nums\"\n                          >\n                            {money(d.value)}\n                          </span>\n                          <span\n                            role=\"cell\"\n                            className=\"text-right tabular-nums\"\n                            style={{ color: pal.sub }}\n                          >\n                            {Math.round(d.probability * 100)}%\n                          </span>\n                          <span\n                            role=\"cell\"\n                            className=\"text-right tabular-nums\"\n                            style={{ color: pal.sub }}\n                          >\n                            {d.close}\n                          </span>\n                        </>\n                      )}\n                    </div>\n                  ))}\n                </div>\n              </div>\n            </div>\n          );\n        })}\n      </div>\n    </div>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}