{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "github-activity-chart",
  "title": "GitHub activity chart",
  "description": "A watercolour contribution calendar with painted cells, subtle splatters, day inspection and a five-level activity legend.",
  "dependencies": [
    "motion"
  ],
  "registryDependencies": [],
  "files": [
    {
      "path": "registry/items/github-activity-chart/github-activity-chart.tsx",
      "content": "\"use client\";\n\nimport { useId, useState, type KeyboardEvent } from \"react\";\nimport { useReducedMotion, useReducedMotionConfig } from \"motion/react\";\nimport { contributionCalendar, type ContributionDay } from \"./contribution-calendar\";\nimport { cn } from \"@/lib/utils\";\n\nexport type { ContributionDay } from \"./contribution-calendar\";\nconst PALETTE = [\"var(--activity-0)\", \"var(--activity-1)\", \"var(--activity-2)\", \"var(--activity-3)\", \"var(--activity-4)\"];\n\nexport function GithubActivityChart({ data, label = \"Contributions in the last year\", watercolor = true, showTotal = true, showLegend = true, onDaySelect, className }: {\n  data: ContributionDay[];\n  label?: string;\n  watercolor?: boolean;\n  showTotal?: boolean;\n  showLegend?: boolean;\n  onDaySelect?: (day: ContributionDay) => void;\n  className?: string;\n}) {\n  const id = useId();\n  const preference = useReducedMotion();\n  const configured = useReducedMotionConfig();\n  const reduced = preference || configured;\n  const [hover, setHover] = useState<string | null>(null);\n  const { cells, months, weeks, total } = contributionCalendar(data);\n  const selected = cells.find((day) => day.date === hover);\n  const cell = 12;\n  const step = 15;\n  const width = Math.max(180, 40 + weeks * step);\n  const height = 139;\n\n  function moveFocus(event: KeyboardEvent<SVGRectElement>, index: number) {\n    const delta = ({ ArrowRight: 7, ArrowLeft: -7, ArrowDown: 1, ArrowUp: -1 } as Record<string, number>)[event.key];\n    if (delta === undefined && event.key !== \"Home\" && event.key !== \"End\") return;\n    event.preventDefault();\n    const next = event.key === \"Home\" ? 0 : event.key === \"End\" ? cells.length - 1 : Math.max(0, Math.min(cells.length - 1, index + delta));\n    event.currentTarget.ownerSVGElement?.querySelector<SVGRectElement>(`[data-day-index=\"${next}\"]`)?.focus();\n  }\n\n  return (\n    <div className={cn(\"w-full space-y-3 font-body text-ink\", className)}>\n      <p className=\"text-[14px] font-semibold\">{showTotal ? `${total.toLocaleString(\"en-GB\")} ` : \"\"}{label}</p>\n      {cells.length ? <div className=\"w-full overflow-x-auto pb-1\">\n        <svg viewBox={`0 0 ${width} ${height}`} className=\"block h-auto min-w-[540px] w-full\" aria-label={`${label}: ${total.toLocaleString(\"en-GB\")} contributions`}>\n          <defs>\n            <filter id={`${id}-paint`} x=\"-15%\" y=\"-15%\" width=\"130%\" height=\"130%\">\n              <feTurbulence type=\"fractalNoise\" baseFrequency=\"0.9\" numOctaves=\"2\" seed=\"7\" result=\"noise\" />\n              <feDisplacementMap in=\"SourceGraphic\" in2=\"noise\" scale=\"2.2\" xChannelSelector=\"R\" yChannelSelector=\"G\" />\n            </filter>\n          </defs>\n          {watercolor ? <g aria-hidden=\"true\">\n            {Array.from({ length: 38 }, (_, index) => <circle key={index} cx={(index * 93 + 17) % width} cy={(index * 31 + 9) % height} r={0.5 + index % 4 * 0.5} fill={PALETTE[index % 4 + 1]} opacity={0.1} />)}\n          </g> : null}\n          <g className=\"fill-dim font-body text-[9px]\" aria-hidden=\"true\">\n            {months.map((month) => <text key={month.column} x={32 + month.column * step} y={14}>{month.label}</text>)}\n            {[\"\", \"Mon\", \"\", \"Wed\", \"\", \"Fri\", \"\"].map((day, index) => day ? <text key={day} x={26} y={22 + index * step + 10} textAnchor=\"end\">{day}</text> : null)}\n          </g>\n          <g filter={watercolor ? `url(#${id}-paint)` : undefined}>\n            {cells.map((day, index) => {\n              const x = 32 + day.column * step;\n              const y = 22 + day.row * step;\n              const text = `${day.count} contribution${day.count === 1 ? \"\" : \"s\"} on ${day.date}`;\n              return <rect key={day.date} x={x} y={y} width={cell} height={cell} rx={2}\n                fill={PALETTE[day.level]} opacity={day.level === 0 ? 0.55 : 0.9}\n                transform={watercolor && !reduced ? `rotate(${index * 13 % 7 - 3} ${x + 6} ${y + 6})` : undefined}\n                role=\"button\" tabIndex={hover === day.date || (!hover && index === 0) ? 0 : -1} data-day-index={index} aria-label={text}\n                aria-describedby={hover === day.date ? `${id}-description` : undefined}\n                className=\"cursor-pointer focus:outline-2 focus:outline-offset-2 focus:outline-ring\"\n                onPointerEnter={() => setHover(day.date)} onPointerLeave={() => setHover(null)} onFocus={() => setHover(day.date)} onBlur={() => setHover(null)}\n                onClick={() => { setHover(day.date); onDaySelect?.(day); }}\n                onKeyDown={(event) => { if (event.key === \"Enter\" || event.key === \" \") { event.preventDefault(); onDaySelect?.(day); } else if (event.key === \"Escape\") setHover(null); else moveFocus(event, index); }}>\n                <title>{text}</title>\n              </rect>;\n            })}\n          </g>\n          {selected ? (() => {\n            const text = `${selected.count} contribution${selected.count === 1 ? \"\" : \"s\"} on ${selected.date}`;\n            const tooltipWidth = Math.min(width - 4, text.length * 4.6 + 16);\n            const x = Math.max(2, Math.min(32 + selected.column * step - tooltipWidth / 2, width - tooltipWidth - 2));\n            const y = selected.row < 2 ? 22 + selected.row * step + 18 : 22 + selected.row * step - 26;\n            return <g pointerEvents=\"none\" aria-hidden=\"true\"><rect x={x} y={y} width={tooltipWidth} height={22} rx={4} className=\"fill-ink\" /><text x={x + tooltipWidth / 2} y={y + 14} textAnchor=\"middle\" className=\"fill-on-solid font-body text-[9px]\">{text}</text></g>;\n          })() : null}\n        </svg>\n      </div> : <p className=\"py-6 text-[13px] text-dim\">No activity to show.</p>}\n      <div className=\"flex flex-wrap items-center justify-between gap-3 text-[11px] text-dim\">\n        <span id={`${id}-description`} role=\"status\">{selected ? `${selected.date}: ${selected.count} contribution${selected.count === 1 ? \"\" : \"s\"}` : \"Select a day to inspect its activity.\"}</span>\n        {showLegend ? <span className=\"inline-flex items-center gap-1.5\"><span>Less</span>{PALETTE.map((colour, index) => <span key={colour} aria-hidden=\"true\" className=\"size-3 rounded-[2px]\" style={{ background: colour, opacity: index === 0 ? 0.55 : 0.9 }} />)}<span>More</span></span> : null}\n      </div>\n    </div>\n  );\n}\n",
      "type": "registry:block"
    },
    {
      "path": "registry/items/github-activity-chart/contribution-calendar.ts",
      "content": "export type ContributionDay = { date: string; count: number; level: 0 | 1 | 2 | 3 | 4 };\nconst DAY = 86_400_000;\n\n/** UTC calendar positions preserve real gaps and do not shift with local timezones. */\nexport function contributionCalendar(data: ContributionDay[]) {\n  const dates = new Map<string, ContributionDay>();\n  for (const day of data) {\n    const timestamp = Date.parse(`${day.date}T00:00:00Z`);\n    if (!Number.isFinite(timestamp) || new Date(timestamp).toISOString().slice(0, 10) !== day.date) continue;\n    dates.set(day.date, { ...day, count: Math.max(0, day.count), level: Math.max(0, Math.min(4, day.level)) as ContributionDay[\"level\"] });\n  }\n  const days = [...dates.values()].sort((a, b) => a.date.localeCompare(b.date));\n  if (!days.length) return { cells: [], months: [], weeks: 0, total: 0 };\n  const first = Date.parse(`${days[0].date}T00:00:00Z`);\n  const last = Date.parse(`${days[days.length - 1].date}T00:00:00Z`);\n  const start = first - new Date(first).getUTCDay() * DAY;\n  const weeks = Math.floor((last - start) / DAY / 7) + 1;\n  const cells = days.map((day) => {\n    const offset = Math.round((Date.parse(`${day.date}T00:00:00Z`) - start) / DAY);\n    return { ...day, column: Math.floor(offset / 7), row: offset % 7 };\n  });\n  const months: { column: number; label: string }[] = [];\n  let previousMonth = -1;\n  for (let column = 0; column < weeks; column++) {\n    const date = new Date(start + column * 7 * DAY);\n    if (date.getUTCMonth() !== previousMonth) {\n      months.push({ column, label: date.toLocaleDateString(\"en-GB\", { month: \"short\", timeZone: \"UTC\" }) });\n      previousMonth = date.getUTCMonth();\n    }\n  }\n  return { cells, months, weeks, total: days.reduce((sum, day) => sum + day.count, 0) };\n}\n",
      "type": "registry:block"
    }
  ],
  "meta": {
    "kind": "pattern",
    "whenToUse": "Use for dated activity counts. Supply contribution data from any source; this component performs no requests. The preview uses deterministic example data, not a live GitHub account.",
    "origin": {
      "project": "Portfolio",
      "path": "src/components/github-chart.tsx"
    },
    "credits": []
  },
  "type": "registry:block"
}
