{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "heatmap-calendar",
  "type": "registry:component",
  "title": "Heatmap Calendar",
  "description": "A GitHub-style activity grid with month and weekday labels, an intensity legend and a hover tooltip with date and count. Ships a deterministic sampleActivity helper.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "charts",
    "heatmap",
    "calendar",
    "contributions",
    "activity",
    "grid",
    "streak"
  ],
  "docs": "https://hyperblur.dev/effects/heatmap-calendar",
  "files": [
    {
      "path": "registry/hyperblur/heatmap-calendar.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/heatmap-calendar.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface HeatmapCalendarProps extends React.HTMLAttributes<HTMLDivElement> {\n  values?: Record<string, number> | number[];\n  endDate?: string;\n  weeks?: number;\n  colors?: string[];\n  unit?: string;\n  showTotal?: boolean;\n  showLegend?: boolean;\n  animate?: boolean;\n}\n\nconst DAY = 86400000;\nconst MONTHS = [\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\", \"Jul\", \"Aug\", \"Sep\", \"Oct\", \"Nov\", \"Dec\"];\nconst WEEKDAYS = [\"Sun\", \"Mon\", \"Tue\", \"Wed\", \"Thu\", \"Fri\", \"Sat\"];\nconst LEVELS = [\"#171717\", \"#112a52\", \"#1a4a94\", \"#2f7bff\", \"#8dbcff\"];\n\nconst css = `\n.hb-hmc { position: relative; display: block; width: 100%; min-width: 0; box-sizing: border-box; color: #ededed; font-family: inherit; }\n.hb-hmc *, .hb-hmc *::before, .hb-hmc *::after { box-sizing: border-box; }\n.hb-hmc-svg { display: block; max-width: 100%; overflow: visible; outline: none; }\n.hb-hmc-lab {\n  fill: #8f8f8f;\n  font-size: 10px;\n  font-family: inherit;\n}\n.hb-hmc-col {\n  animation: hb-hmc-in .5s cubic-bezier(.22, .8, .24, 1) backwards;\n  animation-delay: calc(var(--hb-hmc-c, 0) * 14ms);\n}\n.hb-hmc-cell { outline: none; cursor: pointer; stroke: transparent; stroke-width: 1; transition: stroke .12s ease; }\n.hb-hmc-cell[data-on=\"true\"] { stroke: #ededed; }\n.hb-hmc-cell:focus-visible { stroke: #7fb3ff; stroke-width: 1.5; }\n.hb-hmc-tip {\n  position: absolute;\n  z-index: 2;\n  padding: 6px 9px;\n  border-radius: 8px;\n  border: 1px solid #262626;\n  background: #171717;\n  box-shadow: 0 10px 24px -8px rgba(0, 0, 0, .8);\n  font-size: 11px;\n  line-height: 1.4;\n  white-space: nowrap;\n  pointer-events: none;\n  transform: translate(-50%, calc(-100% - 8px));\n}\n.hb-hmc-tip-v { color: #ededed; font-weight: 600; font-variant-numeric: tabular-nums; }\n.hb-hmc-tip-d { color: #8f8f8f; }\n.hb-hmc-foot { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-top: 8px; font-size: 11px; color: #8f8f8f; }\n.hb-hmc-total { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-variant-numeric: tabular-nums; }\n.hb-hmc-total b { color: #ededed; font-weight: 600; }\n.hb-hmc-legend { display: flex; align-items: center; gap: 4px; margin-left: auto; flex: none; }\n.hb-hmc-sw { width: 10px; height: 10px; border-radius: 3px; }\n.hb-hmc-sr {\n  position: absolute;\n  width: 1px;\n  height: 1px;\n  margin: -1px;\n  padding: 0;\n  overflow: hidden;\n  clip: rect(0 0 0 0);\n  white-space: nowrap;\n  border: 0;\n}\n.hb-hmc[data-animate=\"false\"] .hb-hmc-col { animation: none; }\n@keyframes hb-hmc-in {\n  from { opacity: 0; transform: translateY(6px); }\n}\n@media (prefers-reduced-motion: reduce) {\n  .hb-hmc-col { animation: none !important; }\n  .hb-hmc-cell { transition: none !important; }\n}\n`;\n\nfunction hash(n: number) {\n  let x = (n + 0x9e3779b9) | 0;\n  x = Math.imul(x ^ (x >>> 16), 0x85ebca6b);\n  x = Math.imul(x ^ (x >>> 13), 0xc2b2ae35);\n  return (x ^ (x >>> 16)) >>> 0;\n}\n\nfunction pad2(n: number) {\n  return n < 10 ? `0${n}` : String(n);\n}\n\nfunction parseDate(s: string) {\n  const m = /^(\\d{4})-(\\d{2})-(\\d{2})/.exec(s);\n  if (!m) return Date.UTC(2026, 9, 31);\n  return Date.UTC(Number(m[1]), Number(m[2]) - 1, Number(m[3]));\n}\n\nfunction iso(ms: number) {\n  const d = new Date(ms);\n  return `${d.getUTCFullYear()}-${pad2(d.getUTCMonth() + 1)}-${pad2(d.getUTCDate())}`;\n}\n\nfunction group(n: number) {\n  return String(Math.round(n)).replace(/\\B(?=(\\d{3})+(?!\\d))/g, \",\");\n}\n\nexport function sampleActivity(endDate = \"2026-10-31\", weeks = 52): Record<string, number> {\n  const endMs = parseDate(endDate);\n  const days = Math.max(1, Math.round(weeks)) * 7;\n  const out: Record<string, number> = {};\n  for (let i = 0; i < days; i++) {\n    const ms = endMs - (days - 1 - i) * DAY;\n    const dow = new Date(ms).getUTCDay();\n    const burst = hash(Math.floor(i / 6) + 101) % 5;\n    const roll = hash(i * 7 + 3) % 100;\n    let v = 0;\n    if (dow === 0 || dow === 6) {\n      if (roll < 14) v = 1 + (hash(i + 17) % 3);\n    } else if (burst > 0 && roll < 78) {\n      v = 1 + (hash(i + 29) % (4 * burst));\n    } else if (roll < 22) {\n      v = 1 + (hash(i + 41) % 3);\n    }\n    out[iso(ms)] = v;\n  }\n  return out;\n}\n\nfunction useWidth(fallback: number) {\n  const ref = React.useRef<HTMLDivElement>(null);\n  const [width, setWidth] = React.useState(fallback);\n  React.useEffect(() => {\n    const el = ref.current;\n    if (!el || typeof ResizeObserver === \"undefined\") return;\n    const ro = new ResizeObserver((entries) => {\n      const w = Math.round(entries[0].contentRect.width);\n      if (w > 0) setWidth(w);\n    });\n    ro.observe(el);\n    return () => ro.disconnect();\n  }, []);\n  return [ref, width] as const;\n}\n\nexport function HeatmapCalendar({\n  values,\n  endDate = \"2026-10-31\",\n  weeks = 52,\n  colors = LEVELS,\n  unit = \"contribution\",\n  showTotal = true,\n  showLegend = true,\n  animate = true,\n  className = \"\",\n  ...rest\n}: HeatmapCalendarProps) {\n  const [ref, width] = useWidth(720);\n  const svgRef = React.useRef<SVGSVGElement>(null);\n  const [active, setActive] = React.useState<number | null>(null);\n  const [tab, setTab] = React.useState<number | null>(null);\n\n  const cols = Math.max(1, Math.round(weeks));\n  const endMs = parseDate(endDate);\n  const endDow = new Date(endMs).getUTCDay();\n  const startMs = endMs - endDow * DAY - (cols - 1) * 7 * DAY;\n  const lastIdx = (cols - 1) * 7 + endDow;\n\n  const map = React.useMemo(() => {\n    if (!values) return sampleActivity(endDate, cols);\n    if (Array.isArray(values)) {\n      const m: Record<string, number> = {};\n      values.forEach((v, j) => {\n        m[iso(endMs - (values.length - 1 - j) * DAY)] = v;\n      });\n      return m;\n    }\n    return values;\n  }, [values, endDate, endMs, cols]);\n\n  const cells = React.useMemo(() => {\n    const out: { idx: number; ms: number; v: number }[] = [];\n    for (let idx = 0; idx <= lastIdx; idx++) {\n      const ms = startMs + idx * DAY;\n      const raw = map[iso(ms)];\n      out.push({ idx, ms, v: Number.isFinite(raw) && raw > 0 ? raw : 0 });\n    }\n    return out;\n  }, [map, startMs, lastIdx]);\n\n  const peak = Math.max(1, ...cells.map((c) => c.v));\n  const total = cells.reduce((s, c) => s + c.v, 0);\n  const level = (v: number) => (v <= 0 ? 0 : Math.min(colors.length - 1, Math.ceil((v / peak) * (colors.length - 1))));\n\n  const labelW = 28;\n  const monthH = 16;\n  const pitch = Math.max(5, Math.min(16, (width - labelW) / cols));\n  const gap = Math.min(3, pitch * 0.22);\n  const cell = pitch - gap;\n  const gridW = labelW + cols * pitch - gap;\n  const gridH = monthH + 7 * pitch - gap;\n  const showWeekdays = pitch >= 9;\n\n  const months: { c: number; name: string }[] = [];\n  let lastCol = -10;\n  for (let c = 0; c < cols; c++) {\n    const d = new Date(startMs + c * 7 * DAY);\n    if (d.getUTCDate() <= 7 && c - lastCol >= 3) {\n      months.push({ c, name: MONTHS[d.getUTCMonth()] });\n      lastCol = c;\n    }\n  }\n\n  const tabIdx = tab ?? lastIdx;\n  const word = unit.endsWith(\"s\") ? unit : `${unit}s`;\n\n  const fmtDate = (ms: number) => {\n    const d = new Date(ms);\n    return `${WEEKDAYS[d.getUTCDay()]}, ${MONTHS[d.getUTCMonth()]} ${d.getUTCDate()}, ${d.getUTCFullYear()}`;\n  };\n\n  const move = (to: number) => {\n    if (to < 0 || to > lastIdx) return;\n    setTab(to);\n    setActive(to);\n    const el = svgRef.current?.querySelector<SVGRectElement>(`[data-i=\"${to}\"]`);\n    el?.focus();\n  };\n\n  const onKeyDown = (e: React.KeyboardEvent) => {\n    const base = active ?? tabIdx;\n    const next: Record<string, number> = {\n      ArrowLeft: base - 7,\n      ArrowRight: base + 7,\n      ArrowUp: base - 1,\n      ArrowDown: base + 1,\n      Home: 0,\n      End: lastIdx,\n    };\n    if (e.key in next) {\n      e.preventDefault();\n      move(next[e.key]);\n    } else if (e.key === \"Escape\") {\n      setActive(null);\n    }\n  };\n\n  const act = active === null ? null : cells[active];\n  let tip: React.ReactNode = null;\n  if (act) {\n    const col = Math.floor(act.idx / 7);\n    const row = act.idx % 7;\n    const left = labelW + col * pitch + cell / 2;\n    const top = monthH + row * pitch;\n    tip = (\n      <div\n        className=\"hb-hmc-tip\"\n        aria-hidden=\"true\"\n        style={{ left: Math.min(Math.max(left, 74), Math.max(74, gridW - 74)), top }}\n      >\n        <div className=\"hb-hmc-tip-v\">\n          {group(act.v)} {act.v === 1 ? unit : word}\n        </div>\n        <div className=\"hb-hmc-tip-d\">{fmtDate(act.ms)}</div>\n      </div>\n    );\n  }\n\n  return (\n    <div\n      ref={ref}\n      className={`hb-hmc ${className}`}\n      data-animate={animate ? \"true\" : \"false\"}\n      {...rest}\n    >\n      <style href=\"hb-heatmap-calendar\" precedence=\"default\">{css}</style>\n      <div style={{ position: \"relative\", width: gridW, maxWidth: \"100%\" }} onMouseLeave={() => setActive(null)}>\n        <svg\n          ref={svgRef}\n          className=\"hb-hmc-svg\"\n          width={gridW}\n          height={gridH}\n          viewBox={`0 0 ${gridW.toFixed(2)} ${gridH.toFixed(2)}`}\n          role=\"img\"\n          aria-label={`Activity heatmap for ${cols} weeks ending ${endDate}: ${group(total)} ${word} in total, busiest day ${group(peak)}.`}\n          onKeyDown={onKeyDown}\n        >\n          {months.map((m) => (\n            <text key={m.c} className=\"hb-hmc-lab\" x={(labelW + m.c * pitch).toFixed(2)} y={10}>\n              {m.name}\n            </text>\n          ))}\n          {showWeekdays\n            ? [1, 3, 5].map((r) => (\n                <text\n                  key={r}\n                  className=\"hb-hmc-lab\"\n                  x={0}\n                  y={(monthH + r * pitch + cell / 2).toFixed(2)}\n                  dominantBaseline=\"middle\"\n                >\n                  {WEEKDAYS[r]}\n                </text>\n              ))\n            : null}\n          {Array.from({ length: cols }, (_, c) => (\n            <g key={c} className=\"hb-hmc-col\" style={{ [\"--hb-hmc-c\" as string]: c }}>\n              {cells.slice(c * 7, c * 7 + 7).map((d) => (\n                <rect\n                  key={d.idx}\n                  className=\"hb-hmc-cell\"\n                  data-i={d.idx}\n                  data-on={active === d.idx ? \"true\" : \"false\"}\n                  x={(labelW + c * pitch).toFixed(2)}\n                  y={(monthH + (d.idx % 7) * pitch).toFixed(2)}\n                  width={cell.toFixed(2)}\n                  height={cell.toFixed(2)}\n                  rx={Math.min(2.5, cell / 4).toFixed(2)}\n                  fill={colors[level(d.v)]}\n                  tabIndex={d.idx === tabIdx ? 0 : -1}\n                  aria-label={`${group(d.v)} ${d.v === 1 ? unit : word} on ${fmtDate(d.ms)}`}\n                  onMouseEnter={() => setActive(d.idx)}\n                  onFocus={() => {\n                    setActive(d.idx);\n                    setTab(d.idx);\n                  }}\n                  onBlur={() => setActive(null)}\n                />\n              ))}\n            </g>\n          ))}\n        </svg>\n        {tip}\n      </div>\n      {showTotal || showLegend ? (\n        <div className=\"hb-hmc-foot\" style={{ width: gridW, maxWidth: \"100%\" }}>\n          {showTotal ? (\n            <span className=\"hb-hmc-total\">\n              <b>{group(total)}</b> {word} in {cols} weeks\n            </span>\n          ) : null}\n          {showLegend ? (\n            <span className=\"hb-hmc-legend\" aria-hidden=\"true\">\n              <span>Less</span>\n              {colors.map((c, i) => (\n                <span key={i} className=\"hb-hmc-sw\" style={{ background: c }} />\n              ))}\n              <span>More</span>\n            </span>\n          ) : null}\n        </div>\n      ) : null}\n    </div>\n  );\n}\n"
    }
  ]
}