{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "product-comparison-table",
  "title": "Product Comparison Table",
  "description": "A side-by-side product comparison with a sticky header of photos, prices, ratings and Add to cart buttons, collapsible spec groups, yes/partly/no marks, bars and Best badges for measured specs, a Show differences only switch, column hover, removable and re-addable products, and a sticky first column when it scrolls sideways on phones.",
  "dependencies": [
    "motion",
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/product-comparison-table/product-comparison-table.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { AnimatePresence, motion, useReducedMotion } from \"motion/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 interface CompareProduct {\n  id: string;\n  name: string;\n  price: number;\n  rating: number;\n  image: string;\n  zoom?: number;\n}\n/** true / false / \"partial\" for yes-no features, a number for measured values, or text. */\nexport type CompareValue = boolean | \"partial\" | number | string | null;\nexport interface CompareRow {\n  label: string;\n  values: Record<string, CompareValue>;\n  /** For numbers: which direction wins, and the unit to show. */\n  better?: \"higher\" | \"lower\";\n  unit?: string;\n  hint?: string;\n}\nexport interface CompareGroup {\n  title: string;\n  rows: CompareRow[];\n}\nexport interface ProductComparisonTableProps {\n  products?: CompareProduct[];\n  groups?: CompareGroup[];\n  currency?: string;\n  title?: string;\n  onAdd?: (product: CompareProduct) => void;\n  theme?: \"dark\" | \"light\";\n  className?: string;\n}\n\nconst PALETTES = {\n  dark: {\n    bg: \"#0A0A0A\",\n    card: \"#141414\",\n    head: \"rgba(10,10,10,0.92)\",\n    title: \"#FFFFFF\",\n    muted: \"rgba(255,255,255,0.45)\",\n    body: \"rgba(255,255,255,0.62)\",\n    line: \"rgba(255,255,255,0.1)\",\n    tile: \"#F1F1F1\",\n    accent: \"#7CDE6A\",\n    accentText: \"#0B1A08\",\n    accentSoft: \"rgba(124,222,106,0.09)\",\n    star: \"#F5C451\",\n    buttonBg: \"#FFFFFF\",\n    buttonText: \"#111111\",\n    ghost: \"rgba(255,255,255,0.06)\",\n    col: \"rgba(255,255,255,0.035)\",\n  },\n  light: {\n    bg: \"#FFFFFF\",\n    card: \"#F7F7F7\",\n    head: \"rgba(255,255,255,0.94)\",\n    title: \"#111111\",\n    muted: \"rgba(0,0,0,0.45)\",\n    body: \"rgba(0,0,0,0.62)\",\n    line: \"rgba(0,0,0,0.08)\",\n    tile: \"#F1F1F1\",\n    accent: \"#2F9E44\",\n    accentText: \"#FFFFFF\",\n    accentSoft: \"rgba(47,158,68,0.07)\",\n    star: \"#E0A100\",\n    buttonBg: \"#111111\",\n    buttonText: \"#FFFFFF\",\n    ghost: \"rgba(0,0,0,0.045)\",\n    col: \"rgba(0,0,0,0.025)\",\n  },\n};\ntype Palette = (typeof PALETTES)[\"dark\"];\n\nconst PRODUCTS: CompareProduct[] = [\n  { id: \"studio\", name: \"Studio Headphones\", price: 199, rating: 4.8, image: \"/demo/blocks/product-list/headphones.webp\" },\n  { id: \"pro\", name: \"Wireless Earbuds Pro\", price: 129, rating: 4.7, image: \"/demo/wireless-earbuds.webp\", zoom: 1.35 },\n  { id: \"lite\", name: \"Earbuds Lite\", price: 79, rating: 4.4, image: \"/demo/blocks/product-list/earbuds.webp\" },\n];\nconst v = (studio: CompareValue, pro: CompareValue, lite: CompareValue) => ({ studio, pro, lite });\nconst GROUPS: CompareGroup[] = [\n  {\n    title: \"Sound\",\n    rows: [\n      { label: \"Active noise cancelling\", values: v(true, true, false) },\n      { label: \"Transparency mode\", values: v(true, true, \"partial\"), hint: \"Lite has a basic ambient mode\" },\n      { label: \"Spatial audio\", values: v(true, true, false) },\n      { label: \"Driver size\", values: v(40, 11, 6), better: \"higher\", unit: \"mm\" },\n    ],\n  },\n  {\n    title: \"Battery\",\n    rows: [\n      { label: \"Playtime, one charge\", values: v(30, 7, 8), better: \"higher\", unit: \"h\" },\n      { label: \"Fast charge\", values: v(\"10 min → 5 h\", \"5 min → 1 h\", \"10 min → 2 h\") },\n      { label: \"Wireless charging\", values: v(false, true, false) },\n    ],\n  },\n  {\n    title: \"Comfort & build\",\n    rows: [\n      { label: \"Weight\", values: v(250, 5.4, 4.1), better: \"lower\", unit: \"g\" },\n      { label: \"Water resistance\", values: v(null, \"IPX4\", \"IPX5\") },\n      { label: \"Colours\", values: v(2, 3, 4) },\n    ],\n  },\n  {\n    title: \"Connectivity\",\n    rows: [\n      { label: \"Bluetooth\", values: v(\"5.3\", \"5.3\", \"5.3\") },\n      { label: \"Multipoint\", values: v(true, true, false), hint: \"Connect to two devices at once\" },\n      { label: \"Codecs\", values: v(\"AAC, LDAC\", \"AAC\", \"AAC, SBC\") },\n      { label: \"Warranty\", values: v(\"2 years\", \"2 years\", \"2 years\") },\n    ],\n  },\n];\n\nconst money = (n: number, c: string) => `${c}${n.toFixed(2)}`;\nconst same = (row: CompareRow, ids: string[]) => ids.every((id) => JSON.stringify(row.values[id] ?? null) === JSON.stringify(row.values[ids[0]] ?? null));\n\nfunction Cell({ value, row, ids, p }: { value: CompareValue; row: CompareRow; ids: string[]; p: Palette }) {\n  const reduce = useReducedMotion();\n  if (value === true || value === false || value === \"partial\") {\n    const label = value === true ? \"Yes\" : value === false ? \"No\" : \"Partly\";\n    return (\n      <span className=\"inline-flex items-center gap-2 text-[13px]\" style={{ color: value === false ? p.muted : p.title }}>\n        <span className=\"grid size-5 place-items-center rounded-full\" style={{ background: value === true ? p.accent : value === \"partial\" ? p.ghost : \"transparent\", boxShadow: value === false ? `inset 0 0 0 1.5px ${p.line}` : undefined }} aria-hidden=\"true\">\n          {value === true ? (\n            <svg width=\"10\" height=\"10\" viewBox=\"0 0 24 24\" fill=\"none\" stroke={p.accentText} strokeWidth=\"3.6\" strokeLinecap=\"round\" strokeLinejoin=\"round\">\n              <path d=\"M5 12.5 10 17l9-10\" />\n            </svg>\n          ) : value === \"partial\" ? (\n            <svg width=\"10\" height=\"10\" viewBox=\"0 0 24 24\" fill=\"none\" stroke={p.title} strokeWidth=\"3.4\" strokeLinecap=\"round\">\n              <path d=\"M6 12h12\" />\n            </svg>\n          ) : null}\n        </span>\n        {label}\n      </span>\n    );\n  }\n  if (value === null) return <span style={{ color: p.muted }}>—</span>;\n  if (typeof value === \"number\" && row.better) {\n    const nums = ids.map((id) => row.values[id]).filter((x): x is number => typeof x === \"number\");\n    const best = row.better === \"higher\" ? Math.max(...nums) : Math.min(...nums);\n    const max = Math.max(...nums);\n    const isBest = value === best && nums.length > 1;\n    const width = row.better === \"higher\" ? value / max : Math.min(...nums) / value;\n    return (\n      <span className=\"block\">\n        <span className=\"flex items-center gap-2 text-[13px] tabular-nums\" style={{ color: p.title }}>\n          {value}\n          {row.unit ? ` ${row.unit}` : \"\"}\n          {isBest ? (\n            <span className=\"rounded-full px-1.5 py-px text-[10px] font-semibold uppercase\" style={{ background: p.accent, color: p.accentText }}>\n              Best\n            </span>\n          ) : null}\n        </span>\n        <span className=\"mt-1.5 block h-1 w-full max-w-[140px] overflow-hidden rounded-full\" style={{ background: p.ghost }} aria-hidden=\"true\">\n          <motion.span\n            className=\"block h-full rounded-full\"\n            style={{ background: isBest ? p.accent : p.muted }}\n            initial={reduce ? false : { width: 0 }}\n            animate={{ width: `${Math.max(6, width * 100)}%` }}\n            transition={{ duration: 0.7, ease: [0.22, 1, 0.36, 1] }}\n          />\n        </span>\n      </span>\n    );\n  }\n  return (\n    <span className=\"text-[13px] tabular-nums\" style={{ color: p.title }}>\n      {value}\n      {typeof value === \"number\" && row.unit ? ` ${row.unit}` : \"\"}\n    </span>\n  );\n}\n\nexport function ProductComparisonTable({ products = PRODUCTS, groups = GROUPS, currency = \"$\", title = \"Compare headphones\", onAdd, theme = \"dark\", className }: ProductComparisonTableProps) {\n  const p = PALETTES[theme];\n  const reduce = !!useReducedMotion();\n  const uid = React.useId();\n  const [shown, setShown] = React.useState(products.map((x) => x.id));\n  const [diffOnly, setDiffOnly] = React.useState(false);\n  const [closed, setClosed] = React.useState<string[]>([]);\n  const [added, setAdded] = React.useState<string[]>([]);\n  const [hoverCol, setHoverCol] = React.useState<string | null>(null);\n\n  const cols = products.filter((x) => shown.includes(x.id));\n  const ids = cols.map((x) => x.id);\n  const hidden = products.filter((x) => !shown.includes(x.id));\n  const template = `minmax(108px, 1fr) repeat(${cols.length + (hidden.length ? 1 : 0)}, minmax(132px, 1fr))`;\n  const identical = groups.reduce((n, g) => n + g.rows.filter((r) => same(r, ids)).length, 0);\n\n  const colBg = (id: string) => (hoverCol === id ? p.col : undefined);\n\n  return (\n    <section className={cn(\"w-full overflow-clip px-4 py-12 sm:px-8 sm:py-16\", className)} style={{ background: p.bg, color: p.body }}>\n      <div className=\"mx-auto w-full max-w-[1040px]\">\n        <div className=\"flex flex-wrap items-end justify-between gap-4\">\n          <div>\n            <h2 className=\"text-[26px] font-semibold tracking-tight sm:text-[30px]\" style={{ color: p.title }}>\n              {title}\n            </h2>\n            <p className=\"mt-1 text-[14px]\">\n              {cols.length} products · {identical ? `${identical} specs are the same` : \"every spec differs\"}\n            </p>\n          </div>\n          <label className=\"flex cursor-pointer items-center gap-2.5 text-[13px] font-medium\" style={{ color: p.title }}>\n            <input type=\"checkbox\" role=\"switch\" checked={diffOnly} onChange={(e) => setDiffOnly(e.target.checked)} className=\"peer sr-only\" />\n            <span className=\"relative h-6 w-10 shrink-0 rounded-full transition-colors peer-focus-visible:ring-2\" style={{ background: diffOnly ? p.accent : p.ghost, [\"--tw-ring-color\" as string]: p.muted }} aria-hidden=\"true\">\n              <motion.span className=\"absolute top-0.5 left-0.5 size-5 rounded-full bg-white shadow\" animate={{ x: diffOnly ? 16 : 0 }} transition={{ type: \"spring\", stiffness: 600, damping: 34 }} />\n            </span>\n            Show differences only\n          </label>\n        </div>\n\n        <div className=\"mt-6 overflow-x-auto [scrollbar-width:thin] md:overflow-visible\" role=\"table\" aria-label={title} onMouseLeave={() => setHoverCol(null)}>\n          <div className=\"w-[var(--table-w)] md:w-auto\" style={{ [\"--table-w\" as string]: `${108 + 140 * (cols.length + (hidden.length ? 1 : 0))}px` }}>\n            {/* Header (sticks to the top while you scroll the specs, from md) */}\n            <div role=\"row\" className=\"z-10 grid border-b backdrop-blur-md md:sticky md:top-0\" style={{ gridTemplateColumns: template, background: p.head, borderColor: p.line }}>\n              <div role=\"columnheader\" className=\"sticky left-0 z-10 flex items-end pr-3 pb-4 text-[12px] md:static\" style={{ background: p.bg, color: p.muted }}>\n                <span className=\"sr-only\">Feature</span>\n              </div>\n              <AnimatePresence initial={false} mode=\"popLayout\">\n                {cols.map((c) => {\n                  const isAdded = added.includes(c.id);\n                  return (\n                    <motion.div\n                      key={c.id}\n                      role=\"columnheader\"\n                      layout={!reduce}\n                      initial={reduce ? false : { opacity: 0, y: 10 }}\n                      animate={{ opacity: 1, y: 0 }}\n                      exit={{ opacity: 0, scale: 0.95 }}\n                      onMouseEnter={() => setHoverCol(c.id)}\n                      className=\"relative px-2 pb-4 sm:px-3\"\n                      style={{ background: colBg(c.id) }}\n                    >\n                      <div className=\"relative aspect-[4/3] overflow-hidden rounded-xl\" style={{ background: p.tile }}>\n                        {/* eslint-disable-next-line @next/next/no-img-element -- plain img keeps the block framework-agnostic */}\n                        <img src={c.image} alt=\"\" width={400} height={300} className=\"h-full w-full object-cover\" style={{ transform: `scale(${c.zoom ?? 1})` }} />\n                        {cols.length > 2 ? (\n                          <button\n                            type=\"button\"\n                            onClick={() => setShown((s) => s.filter((x) => x !== c.id))}\n                            aria-label={`Remove ${c.name} from comparison`}\n                            className=\"absolute top-1.5 right-1.5 grid size-6 place-items-center rounded-full bg-black/60 text-white backdrop-blur\"\n                          >\n                            <svg width=\"9\" height=\"9\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"3.4\" strokeLinecap=\"round\" aria-hidden=\"true\">\n                              <path d=\"M6 6l12 12M18 6 6 18\" />\n                            </svg>\n                          </button>\n                        ) : null}\n                      </div>\n                      <p className=\"mt-2.5 text-[14px] leading-snug font-medium\" style={{ color: p.title }}>\n                        {c.name}\n                      </p>\n                      <p className=\"mt-0.5 flex items-center gap-2 text-[13px] tabular-nums\">\n                        <span style={{ color: p.title }}>{money(c.price, currency)}</span>\n                        <span className=\"inline-flex items-center gap-0.5 text-[12px]\" style={{ color: p.muted }}>\n                          <svg width=\"11\" height=\"11\" viewBox=\"0 0 24 24\" fill={p.star} aria-hidden=\"true\">\n                            <path d=\"m12 2.8 2.8 5.8 6.3.9-4.6 4.4 1.1 6.3L12 17.2l-5.6 3 1.1-6.3-4.6-4.4 6.3-.9z\" />\n                          </svg>\n                          {c.rating}\n                        </span>\n                      </p>\n                      <motion.button\n                        type=\"button\"\n                        onClick={() => (setAdded((a) => (a.includes(c.id) ? a : [...a, c.id])), onAdd?.(c))}\n                        whileTap={reduce ? undefined : { scale: 0.96 }}\n                        animate={{ background: isAdded ? p.accent : p.buttonBg, color: isAdded ? p.accentText : p.buttonText }}\n                        className=\"mt-3 h-9 w-full rounded-full text-[13px] font-semibold whitespace-nowrap\"\n                        aria-label={isAdded ? `${c.name} added to cart` : `Add ${c.name} to cart`}\n                      >\n                        {isAdded ? \"Added ✓\" : \"Add to cart\"}\n                      </motion.button>\n                    </motion.div>\n                  );\n                })}\n                {hidden.length ? (\n                  <motion.div key=\"add\" role=\"columnheader\" layout={!reduce} initial={reduce ? false : { opacity: 0 }} animate={{ opacity: 1 }} exit={{ opacity: 0 }} className=\"px-2 pb-4 sm:px-3\">\n                    <div className=\"flex aspect-[4/3] flex-col items-center justify-center gap-2 rounded-xl border border-dashed p-2\" style={{ borderColor: p.line }}>\n                      <span className=\"text-[12px]\" style={{ color: p.muted }}>\n                        Add back\n                      </span>\n                      {hidden.map((h) => (\n                        <button\n                          key={h.id}\n                          type=\"button\"\n                          onClick={() => setShown((s) => products.filter((x) => s.includes(x.id) || x.id === h.id).map((x) => x.id))}\n                          className=\"max-w-full truncate rounded-full px-3 py-1.5 text-[12px] font-medium\"\n                          style={{ background: p.ghost, color: p.title }}\n                        >\n                          + {h.name}\n                        </button>\n                      ))}\n                    </div>\n                  </motion.div>\n                ) : null}\n              </AnimatePresence>\n            </div>\n\n            {/* Spec groups */}\n            {groups.map((g) => {\n              const isClosed = closed.includes(g.title);\n              const rows = g.rows.filter((r) => !diffOnly || !same(r, ids));\n              const gid = `${uid}-${g.title.replace(/\\W+/g, \"\")}`;\n              return (\n                <div key={g.title} role=\"rowgroup\">\n                  <div role=\"row\" className=\"border-b\" style={{ borderColor: p.line }}>\n                    <div role=\"cell\" className=\"sticky left-0 w-fit md:static\">\n                      <button\n                        type=\"button\"\n                        onClick={() => setClosed((c) => (isClosed ? c.filter((x) => x !== g.title) : [...c, g.title]))}\n                        aria-expanded={!isClosed}\n                        aria-controls={gid}\n                        className=\"flex items-center gap-2 py-3.5 text-[13px] font-semibold tracking-[0.08em] uppercase\"\n                        style={{ color: p.title }}\n                      >\n                        <motion.svg width=\"11\" height=\"11\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"3\" strokeLinecap=\"round\" strokeLinejoin=\"round\" animate={{ rotate: isClosed ? -90 : 0 }} aria-hidden=\"true\">\n                          <path d=\"m6 9 6 6 6-6\" />\n                        </motion.svg>\n                        {g.title}\n                        <span className=\"text-[11px] font-medium tracking-normal normal-case\" style={{ color: p.muted }}>\n                          {rows.length} of {g.rows.length}\n                        </span>\n                      </button>\n                    </div>\n                  </div>\n                  <AnimatePresence initial={false}>\n                    {!isClosed ? (\n                      <motion.div\n                        key=\"rows\"\n                        id={gid}\n                        initial={{ height: 0, opacity: 0 }}\n                        animate={{ height: \"auto\", opacity: 1 }}\n                        exit={{ height: 0, opacity: 0 }}\n                        transition={{ duration: reduce ? 0 : 0.28, ease: [0.22, 1, 0.36, 1] }}\n                        className=\"overflow-clip\"\n                      >\n                        <AnimatePresence initial={false}>\n                          {rows.map((r) => (\n                            <motion.div\n                              key={r.label}\n                              role=\"row\"\n                              initial={reduce ? false : { opacity: 0, height: 0 }}\n                              animate={{ opacity: 1, height: \"auto\" }}\n                              exit={{ opacity: 0, height: 0 }}\n                              transition={{ duration: reduce ? 0 : 0.25 }}\n                              className=\"overflow-clip\"\n                            >\n                              <div className=\"grid border-b\" style={{ gridTemplateColumns: template, borderColor: p.line }}>\n                                <div role=\"rowheader\" className=\"sticky left-0 z-[1] py-3 pr-3 md:static\" style={{ background: p.bg }}>\n                                  <span className=\"block text-[13px]\" style={{ color: p.body }}>\n                                    {r.label}\n                                  </span>\n                                  {r.hint ? (\n                                    <span className=\"mt-0.5 block text-[11px] leading-snug\" style={{ color: p.muted }}>\n                                      {r.hint}\n                                    </span>\n                                  ) : null}\n                                </div>\n                                {cols.map((c) => (\n                                  <div key={c.id} role=\"cell\" onMouseEnter={() => setHoverCol(c.id)} className=\"flex items-center px-2 py-3 transition-colors sm:px-3\" style={{ background: colBg(c.id) }}>\n                                    <Cell value={r.values[c.id] ?? null} row={r} ids={ids} p={p} />\n                                  </div>\n                                ))}\n                                {hidden.length ? <div role=\"cell\" aria-hidden=\"true\" /> : null}\n                              </div>\n                            </motion.div>\n                          ))}\n                        </AnimatePresence>\n                        {!rows.length ? (\n                          <p className=\"border-b py-3 text-[12px]\" style={{ borderColor: p.line, color: p.muted }}>\n                            All {g.title.toLowerCase()} specs are the same.\n                          </p>\n                        ) : null}\n                      </motion.div>\n                    ) : null}\n                  </AnimatePresence>\n                </div>\n              );\n            })}\n          </div>\n        </div>\n      </div>\n    </section>\n  );\n}\n\nexport default ProductComparisonTable;\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}