Skip to content

Contribution Graph

Visualize a year of activity as a calendar of daily cells, with a tooltip and a legend.

Preview

1,175 contributions in 2025
import {
  ContributionGraph,
  ContributionGraphCalendar,
  ContributionGraphFooter,
  ContributionGraphLegend,
  ContributionGraphTotal,
} from "@/components/ui/contribution-graph";
import type { ContributionDay } from "@/components/ui/contribution-graph";

const DAY_MS = 86_400_000;
const START = Date.UTC(2025, 0, 1);

// A seeded generator, so the server and the browser draw the same year.
let seed = 2025;

const random = () => {
  seed = (seed * 16_807) % 2_147_483_647;

  return seed / 2_147_483_647;
};

const BUSY = new Set(
  Array.from({ length: 7 }, () => Math.floor(random() * 50)).flatMap((week) => [
    week,
    week + 1,
    week + 2,
  ])
);

const QUIET = new Set([31, 32, 51]);

const chanceOf = (week: number) => {
  if (QUIET.has(week)) {
    return 0.04;
  }

  return BUSY.has(week) ? 0.92 : 0.6;
};

const DAYS: ContributionDay[] = Array.from({ length: 365 }, (_, index) => {
  const week = Math.floor((index + 3) / 7);
  const weekend = [0, 6].includes((index + 3) % 7);
  const busy = BUSY.has(week);
  const active = random() < chanceOf(week) * (weekend ? 0.35 : 1);
  const level = active ? Math.ceil(random() ** 1.6 * 3) + (busy ? 1 : 0) : 0;

  return {
    count: level === 0 ? 0 : level * 3 - Math.floor(random() * 3),
    date: new Date(START + index * DAY_MS).toISOString().slice(0, 10),
  };
});

export const ContributionGraphExample = () => (
  <ContributionGraph className="w-full" days={DAYS}>
    <ContributionGraphCalendar label="in 2025" />
    <ContributionGraphFooter>
      <span>
        <ContributionGraphTotal /> contributions in 2025
      </span>
      <ContributionGraphLegend />
    </ContributionGraphFooter>
  </ContributionGraph>
);

Features

  • Shares one UI SDK tooltip across every cell instead of mounting one per cell.
  • Takes plain day data, so it works with GitHub, GitLab, or your own numbers.
  • Trims to the latest weeks on phones.
  • Reads its five shades from theme tokens.

Installation

bunx --bun shadcn@latest add https://olekaleksander.dev/r/contribution-graph.json

Usage

import {
  ContributionGraph,
  ContributionGraphCalendar,
  ContributionGraphFooter,
  ContributionGraphTotal,
  ContributionGraphLegend,
} from "@/components/ui/contribution-graph";
<ContributionGraph days={days}>
  <ContributionGraphCalendar label="in 2025" />
  <ContributionGraphFooter>
    <ContributionGraphTotal />
    <ContributionGraphLegend />
  </ContributionGraphFooter>
</ContributionGraph>

API Reference

Every part takes className for styling, and parts that wrap content take children. ContributionGraphFooter, ContributionGraphTotal and ContributionGraphLegend take nothing else.

ContributionGraph

PropTypeDefault
daysContributionDay[]–
totalnumbersum of days

ContributionGraphCalendar

PropTypeDefault
labelstring–