{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "github-map",
  "type": "registry:ui",
  "title": "GitHub Heatmap",
  "description": "An interactive 52-week contribution activity graph with date-fns interval calculation, theme-aware level colors, and date tooltips.",
  "author": "Aditya Ojha <https://akoder.xyz>",
  "categories": [
    "visualization",
    "calendar",
    "github",
    "chart"
  ],
  "dependencies": [
    "date-fns",
    "radix-ui"
  ],
  "registryDependencies": [
    "tooltip"
  ],
  "meta": {
    "docs": "https://akoder.xyz/components/github-map",
    "source": "https://github.com/AdityaKodez/adityaojha/blob/main/components/ui/github-map.tsx"
  },
  "files": [
    {
      "path": "components/ui/github-map.tsx",
      "content": "\"use client\";\n\nimport {\n  addDays,\n  eachDayOfInterval,\n  endOfWeek,\n  format,\n  startOfWeek,\n  subMonths,\n} from \"date-fns\";\nimport { useMemo, useCallback } from \"react\";\nimport { Tooltip, TooltipContent, TooltipTrigger } from \"./tooltip\";\n\ninterface ContributionDay {\n  date: string;\n  count: number;\n}\n\ninterface GitHubCalendarProps {\n  data: ContributionDay[];\n  colors?: string[];\n}\n\n/** Parse a bare YYYY-MM-DD date as a local calendar date. */\nexport function parseLocalDate(iso: string): Date {\n  const [year, month, day] = iso.split(\"-\").map(Number);\n  return new Date(year, month - 1, day);\n}\n\n/** Index contributions by ISO date for constant-time calendar cell lookup. */\nexport function indexByDate<T extends { date: string }>(\n  data: T[],\n): Map<string, T> {\n  return new Map(data.map((entry) => [entry.date, entry]));\n}\n\nconst GitHubCalendar = ({\n  data,\n  // Theme vars with literal fallbacks so the component renders outside this\n  // repo too (consumers won't have --heatmap-level-* defined).\n  colors = [\n    \"var(--heatmap-level-0, oklch(0.92 0 0))\",\n    \"var(--heatmap-level-1, oklch(0.882 0.057 254.128))\",\n    \"var(--heatmap-level-2, oklch(0.707 0.165 254.624))\",\n    \"var(--heatmap-level-3, oklch(0.488 0.243 264.376))\",\n    \"var(--heatmap-level-4, oklch(0.379 0.146 265.522))\",\n  ],\n}: GitHubCalendarProps) => {\n  const { startDate, endDate } = useMemo(() => {\n    const end = new Date();\n    return { startDate: subMonths(end, 12), endDate: end };\n  }, []);\n\n  const weeks = Math.ceil(\n    (endDate.getTime() - startDate.getTime()) / (7 * 24 * 60 * 60 * 1000),\n  );\n  const contributionsByDate = useMemo(() => indexByDate(data), [data]);\n\n  const getColor = useCallback(\n    (count: number) => {\n      if (count === 0) return colors[0];\n      if (count === 1) return colors[1];\n      if (count === 2) return colors[2];\n      if (count === 3) return colors[3];\n      return colors[4] || colors[colors.length - 1];\n    },\n    [colors],\n  );\n\n  const weeksData = useMemo(() => {\n    const weeksList = [];\n    let currentWeekStart = startOfWeek(startDate, { weekStartsOn: 0 });\n\n    for (let i = 0; i < weeks; i++) {\n      const weekDays = eachDayOfInterval({\n        start: currentWeekStart,\n        end: endOfWeek(currentWeekStart, { weekStartsOn: 0 }),\n      });\n\n      const daysList = weekDays.map((day) => {\n        const dateKey = format(day, \"yyyy-MM-dd\");\n        const formattedDate = format(day, \"PPP\");\n        const contribution = contributionsByDate.get(dateKey);\n        const count = contribution?.count ?? 0;\n        return {\n          key: dateKey,\n          count,\n          formattedDate,\n          color: getColor(count),\n        };\n      });\n\n      weeksList.push({ weekIndex: i, days: daysList });\n      currentWeekStart = addDays(currentWeekStart, 7);\n    }\n    return weeksList;\n  }, [startDate, weeks, contributionsByDate, getColor]);\n\n  // Render month labels with proper alignment and solving the 30-day drift\n  const monthLabelsData = useMemo(() => {\n    const labels: { name: string; weekIndex: number }[] = [];\n    let currentWeekStart = startOfWeek(startDate, { weekStartsOn: 0 });\n\n    for (let i = 0; i < weeks; i++) {\n      const monthName = format(currentWeekStart, \"MMM\");\n      if (\n        labels.length === 0 ||\n        (labels[labels.length - 1].name !== monthName &&\n          i - labels[labels.length - 1].weekIndex >= 3)\n      ) {\n        labels.push({ name: monthName, weekIndex: i });\n      }\n      currentWeekStart = addDays(currentWeekStart, 7);\n    }\n    return labels;\n  }, [startDate, weeks]);\n\n  return (\n    <div\n      className=\"no-js-visible p-2 overflow-x-auto overflow-y-hidden no-scrollbar [scrollbar-width:none] [&::-webkit-scrollbar]:hidden\"\n    >\n      <div className=\"flex w-max\">\n        <div className=\"relative\">\n          <div className=\"relative h-4 w-full mb-2\">\n            {monthLabelsData.map((month, index) => (\n              <span\n                key={index}\n                className=\"text-xs text-muted-foreground absolute whitespace-nowrap\"\n                style={{\n                  left: `calc(${month.weekIndex} * (0.75rem + 0.125rem))`,\n                }}\n              >\n                {month.name}\n              </span>\n            ))}\n          </div>\n          <div className=\"flex gap-0.5\">\n            {weeksData.map((week) => (\n              <div key={week.weekIndex} className=\"flex flex-col gap-1\">\n                {week.days.map((day) => (\n                  <Tooltip key={day.key}>\n                    <TooltipTrigger asChild>\n                      <div\n                        className=\"w-3 h-3 rounded-xs\"\n                        style={{ backgroundColor: day.color }}\n                      />\n                    </TooltipTrigger>\n                    <TooltipContent>\n                      <p>\n                        {day.formattedDate}: {day.count} contributions\n                      </p>\n                    </TooltipContent>\n                  </Tooltip>\n                ))}\n              </div>\n            ))}\n          </div>\n        </div>\n      </div>\n      <div className=\"mt-2 justify-center flex gap-1 text-[10px] text-gray-400 items-center\">\n        <span>Less</span>\n\n        {colors.map((color, index) => (\n          <div\n            key={index}\n            className=\"w-2 h-2 rounded-[1px]\"\n            style={{ backgroundColor: color }}\n          />\n        ))}\n        <span>More</span>\n      </div>\n    </div>\n  );\n};\n\nexport { GitHubCalendar };\n",
      "type": "registry:ui"
    }
  ],
  "docs": "## Installation\n\n```bash\nnpx shadcn@latest add @akoder/github-map\n```\n\n## Usage\n\n```tsx\nimport { GitHubCalendar } from \"@/components/ui/github-map\";\n\nexport function ActivityGraph() {\n  return <GitHubCalendar data={[{ date: \"2025-01-01\", count: 4 }]} />;\n}\n```"
}