{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "cohort-retention-heatmap",
  "title": "Cohort Retention Heatmap",
  "description": "A single-hue cohort retention matrix with percentage and user views, weighted retention, hover details and unobserved periods kept apart from zero.",
  "dependencies": [
    "motion"
  ],
  "files": [
    {
      "path": "registry/new-york/cohort-retention-heatmap/cohort-retention-heatmap.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { motion, useReducedMotion } from \"motion/react\";\n\nexport interface RetentionCohort {\n  label: string;\n  size: number;\n  /** Retained users per period, including the initial cohort. Null means not yet observed. */\n  retained: (number | null)[];\n}\n\nexport interface CohortRetentionHeatmapProps extends Omit<\n  React.ComponentPropsWithoutRef<\"section\">,\n  \"title\"\n> {\n  title?: string;\n  subtitle?: string;\n  theme?: \"dark\" | \"light\";\n  cohorts?: RetentionCohort[];\n  periodLabel?: string;\n  /** The single hue the cells build up in. */\n  color?: string;\n  defaultMode?: \"percentage\" | \"users\";\n}\n\nconst PALETTES = {\n  dark: {\n    bg: \"#08090A\",\n    border: \"rgba(255,255,255,0.08)\",\n    text: \"#F7F8F8\",\n    soft: \"#D0D6E0\",\n    sub: \"#8A8F98\",\n    faint: \"#62666D\",\n    chip: \"rgba(255,255,255,0.05)\",\n    chipOn: \"rgba(255,255,255,0.1)\",\n    cell: \"#0F1011\",\n    onStrong: \"#F7F8F8\",\n    /** How strong a cell's colour must be before its text turns light. */\n    flip: 0,\n  },\n  light: {\n    bg: \"#FFFFFF\",\n    border: \"rgba(8,9,10,0.08)\",\n    text: \"#08090A\",\n    soft: \"#3C3F44\",\n    sub: \"#62666D\",\n    faint: \"#8A8F98\",\n    chip: \"rgba(8,9,10,0.04)\",\n    chipOn: \"#FFFFFF\",\n    cell: \"#F7F8F8\",\n    onStrong: \"#FFFFFF\",\n    flip: 0.62,\n  },\n};\n\nconst COHORTS: RetentionCohort[] = [\n  {\n    label: \"Jan 2026\",\n    size: 1240,\n    retained: [1240, 980, 843, 769, 719, 682, 657, 632],\n  },\n  {\n    label: \"Feb 2026\",\n    size: 1480,\n    retained: [1480, 1199, 1051, 962, 918, 873, 844, null],\n  },\n  {\n    label: \"Mar 2026\",\n    size: 1620,\n    retained: [1620, 1361, 1215, 1134, 1069, 1037, null, null],\n  },\n  {\n    label: \"Apr 2026\",\n    size: 1890,\n    retained: [1890, 1625, 1474, 1380, 1323, null, null, null],\n  },\n  {\n    label: \"May 2026\",\n    size: 2140,\n    retained: [2140, 1883, 1755, 1648, null, null, null, null],\n  },\n  {\n    label: \"Jun 2026\",\n    size: 2380,\n    retained: [2380, 2142, 1999, null, null, null, null, null],\n  },\n  {\n    label: \"Jul 2026\",\n    size: 2670,\n    retained: [2670, 2456, null, null, null, null, null, null],\n  },\n  {\n    label: \"Aug 2026\",\n    size: 2940,\n    retained: [2940, null, null, null, null, null, null, null],\n  },\n];\n\nexport function CohortRetentionHeatmap({\n  title = \"Retention by cohort\",\n  subtitle = \"Retention gets stronger with every new cohort.\",\n  theme = \"dark\",\n  cohorts = COHORTS,\n  periodLabel = \"Month\",\n  color = \"#5E6AD2\",\n  defaultMode = \"percentage\",\n  className,\n  style,\n  ...props\n}: CohortRetentionHeatmapProps) {\n  const pal = PALETTES[theme];\n  const [mode, setMode] = React.useState(defaultMode);\n  const [active, setActive] = React.useState<{\n    row: number;\n    col: number;\n  } | null>(null);\n  const reduced = useReducedMotion();\n  const valid = cohorts.filter(\n    (c) =>\n      Number.isFinite(c.size) &&\n      c.size > 0 &&\n      c.retained.every(\n        (v) => v === null || (Number.isFinite(v) && v >= 0 && v <= c.size),\n      ),\n  );\n  const periods = Math.max(0, ...valid.map((c) => c.retained.length));\n  const selected = active && valid[active.row];\n  const value = selected && active ? selected.retained[active.col] : null;\n  const observations = valid.filter((c) => c.retained[1] != null);\n  const base = observations.reduce((n, c) => n + c.size, 0);\n  const retention = base\n    ? (observations.reduce((n, c) => n + (c.retained[1] ?? 0), 0) / base) * 100\n    : null;\n\n  return (\n    <section\n      className={`w-full rounded-2xl p-5 sm:p-7 ${className ?? \"\"}`}\n      style={{\n        background: pal.bg,\n        boxShadow: `inset 0 0 0 1px ${pal.border}`,\n        color: pal.text,\n        fontFamily:\n          '\"Inter Variable\", Inter, \"SF Pro Display\", -apple-system, BlinkMacSystemFont, sans-serif',\n        ...style,\n      }}\n      {...props}\n    >\n      <div className=\"flex flex-wrap items-start justify-between gap-5\">\n        <div>\n          <h3 className=\"m-0 text-[20px] leading-[1.33] font-[590] tracking-[-0.012em]\">\n            {title}\n          </h3>\n          <p\n            className=\"m-0 mt-1 text-[13px] tracking-[-0.011em]\"\n            style={{ color: pal.sub }}\n          >\n            {subtitle}\n          </p>\n        </div>\n        <div className=\"text-left sm:text-right\">\n          <p className=\"m-0 text-[32px] leading-none font-[510] tracking-[-0.022em] tabular-nums\">\n            {retention === null ? \"—\" : `${retention.toFixed(1)}%`}\n          </p>\n          <p className=\"m-0 mt-1.5 text-[13px]\" style={{ color: pal.sub }}>\n            {periodLabel} 1, weighted\n          </p>\n        </div>\n      </div>\n\n      <div className=\"mt-5 flex flex-wrap items-center justify-between gap-3\">\n        <p className=\"m-0 text-[13px] tabular-nums\" style={{ color: pal.sub }}>\n          {valid.length} cohorts,{\" \"}\n          {valid.reduce((n, c) => n + c.size, 0).toLocaleString(\"en-US\")} users\n        </p>\n        <div\n          className=\"flex rounded-full p-0.5\"\n          style={{ background: pal.chip }}\n          role=\"group\"\n          aria-label=\"Cell value display\"\n        >\n          {([\"percentage\", \"users\"] as const).map((m) => (\n            <button\n              key={m}\n              type=\"button\"\n              aria-pressed={mode === m}\n              onClick={() => setMode(m)}\n              className=\"h-7 cursor-pointer rounded-full px-3 text-[13px] font-[510] tracking-[-0.011em] transition-colors outline-none focus-visible:ring-2\"\n              style={{\n                background: mode === m ? pal.chipOn : \"transparent\",\n                color: mode === m ? pal.text : pal.sub,\n              }}\n            >\n              {m === \"percentage\" ? \"Percentage\" : \"Users\"}\n            </button>\n          ))}\n        </div>\n      </div>\n\n      {!periods ? (\n        <p className=\"py-16 text-center text-sm\" style={{ color: pal.sub }}>\n          No cohort data available.\n        </p>\n      ) : (\n        <div className=\"mt-4 overflow-x-auto pb-1\">\n          <table className=\"w-full min-w-[620px] border-separate border-spacing-[3px] text-left text-[12px]\">\n            <caption className=\"sr-only\">\n              Retention by signup cohort and elapsed period. Tab to a cell for\n              retained users and percentage. Unobserved periods are marked with\n              a dash.\n            </caption>\n            <thead>\n              <tr>\n                <th\n                  scope=\"col\"\n                  className=\"px-1 pb-2 font-normal\"\n                  style={{ color: pal.faint }}\n                >\n                  Cohort\n                </th>\n                <th\n                  scope=\"col\"\n                  className=\"pr-3 pb-2 text-right font-normal\"\n                  style={{ color: pal.faint }}\n                >\n                  Users\n                </th>\n                {Array.from({ length: periods }, (_, i) => (\n                  <th\n                    key={i}\n                    scope=\"col\"\n                    className=\"min-w-14 pb-2 text-center font-normal\"\n                    style={{ color: pal.faint }}\n                  >\n                    {periodLabel} {i}\n                  </th>\n                ))}\n              </tr>\n            </thead>\n            <tbody>\n              {valid.map((cohort, row) => (\n                <tr key={`${cohort.label}-${row}`}>\n                  <th\n                    scope=\"row\"\n                    className=\"px-1 font-[510] whitespace-nowrap\"\n                    style={{ color: pal.soft }}\n                  >\n                    {cohort.label}\n                  </th>\n                  <td\n                    className=\"pr-3 text-right tabular-nums\"\n                    style={{ color: pal.sub }}\n                  >\n                    {cohort.size.toLocaleString(\"en-US\")}\n                  </td>\n                  {Array.from({ length: periods }, (_, col) => {\n                    const count = cohort.retained[col];\n                    const percentage =\n                      count == null ? null : (count / cohort.size) * 100;\n                    const label = `${cohort.label}, ${periodLabel} ${col}: ${count == null ? \"not yet observed\" : `${count.toLocaleString(\"en-US\")} users, ${percentage!.toFixed(1)}% retained`}`;\n                    const on = active?.row === row && active?.col === col;\n                    const strength =\n                      percentage === null\n                        ? 0\n                        : 0.06 + (percentage / 100) ** 2.5 * 0.9;\n                    return (\n                      <td key={col} className=\"p-0\">\n                        <motion.button\n                          type=\"button\"\n                          disabled={count == null}\n                          aria-label={label}\n                          onPointerEnter={() => setActive({ row, col })}\n                          onPointerLeave={() => setActive(null)}\n                          onFocus={() => setActive({ row, col })}\n                          onBlur={() => setActive(null)}\n                          initial={{ opacity: reduced ? 1 : 0 }}\n                          whileInView={{ opacity: 1 }}\n                          viewport={{ once: true }}\n                          transition={{\n                            delay: reduced ? 0 : (row + col) * 0.02,\n                          }}\n                          className=\"relative flex h-9 w-full items-center justify-center overflow-hidden rounded text-[12px] font-[510] tabular-nums outline-none enabled:cursor-pointer\"\n                          style={{ background: pal.cell }}\n                        >\n                          <span\n                            className=\"absolute inset-0\"\n                            style={{ background: color, opacity: strength }}\n                          />\n                          <span\n                            className=\"relative\"\n                            style={{\n                              color:\n                                percentage !== null && strength >= pal.flip\n                                  ? pal.onStrong\n                                  : pal.soft,\n                            }}\n                          >\n                            {count == null ? (\n                              <span style={{ color: pal.faint }}>—</span>\n                            ) : mode === \"users\" ? (\n                              count.toLocaleString(\"en-US\")\n                            ) : (\n                              `${Math.round(percentage!)}%`\n                            )}\n                          </span>\n                          {/* An inset ring, so marking a cell never moves anything. */}\n                          <span\n                            className=\"pointer-events-none absolute inset-0 rounded transition-opacity duration-100\"\n                            style={{\n                              boxShadow: `inset 0 0 0 1px ${pal.text}`,\n                              opacity: on ? 1 : 0,\n                            }}\n                          />\n                        </motion.button>\n                      </td>\n                    );\n                  })}\n                </tr>\n              ))}\n            </tbody>\n          </table>\n        </div>\n      )}\n\n      <div\n        className=\"mt-3 flex flex-wrap items-center justify-between gap-3 pt-3 text-[12px]\"\n        style={{ boxShadow: `inset 0 1px 0 ${pal.border}`, color: pal.sub }}\n      >\n        <p aria-live=\"polite\" className=\"m-0 min-h-9 sm:min-h-[18px]\">\n          {selected && active && value != null ? (\n            <>\n              <span style={{ color: pal.text }}>{selected.label}</span>,{\" \"}\n              {periodLabel} {active.col}: {value.toLocaleString(\"en-US\")} of{\" \"}\n              {selected.size.toLocaleString(\"en-US\")} retained (\n              {((value / selected.size) * 100).toFixed(1)}%)\n            </>\n          ) : (\n            \"Hover or focus a cell. A dash means not yet observed.\"\n          )}\n        </p>\n        <div className=\"flex items-center gap-2\">\n          <span>0%</span>\n          <span\n            className=\"h-1 w-20 rounded-full\"\n            style={{\n              background: `linear-gradient(90deg, ${pal.cell}, ${color})`,\n            }}\n          />\n          <span>100%</span>\n        </div>\n      </div>\n    </section>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}