Contribution Graph
GitHub-style contribution calendar that turns a year of activity into a wall of green — pass a username and it fetches, caches, and animates the data itself.
Contribution Graph
A GitHub-style contribution calendar that turns a year of activity into a wall of green — pass it a username and it fetches, caches, and animates the data itself into a color-coded grid.
Hit the re-trigger button to replay the level-by-level reveal — the muted grid fades in first, then the blocks light up in rounds from lightest to darkest green.
Installation
Install the component with the Craft-UI Blocks CLI — it drops contribution-graph.tsx into your project and wires up its runtime dependencies:
npx mvpblocks add contribution-graphpnpm dlx mvpblocks add contribution-graphyarn dlx mvpblocks add contribution-graphbunx mvpblocks add contribution-graphFile Structure
The CLI saves the component to your project's components/ui folder:
Import it from @/components/ui/contribution-graph.
Usage
ContributionGraph is a client component with a composable API — it provides the shared data, sizing, and animation state through context, while you assemble the grid from smaller pieces:
import {
ContributionGraph,
ContributionGraphBlock,
ContributionGraphCalendar,
ContributionGraphFooter,
ContributionGraphLegend,
ContributionGraphTotalCount,
} from "@/components/ui/contribution-graph";
export function GitHubActivity({ username }: { username: string }) {
return (
<ContributionGraph username={username}>
<ContributionGraphCalendar>
{({ activity, dayIndex, weekIndex }) => (
<ContributionGraphBlock
activity={activity}
dayIndex={dayIndex}
weekIndex={weekIndex}
/>
)}
</ContributionGraphCalendar>
<ContributionGraphFooter>
<ContributionGraphTotalCount />
<ContributionGraphLegend />
</ContributionGraphFooter>
</ContributionGraph>
);
}Pass animated={false} to render the finished grid immediately, or render smaller cells with blockSize={8} to fit a dashboard row.
Server-side data
Passing a username fires a browser-side fetch from the github-contributions API and caches the response in memory per username, so remounts replay the entrance without refetching. When you already have the activity (or want cache-heavy data fetching with Next.js), skip the fetch entirely by passing data — a raw Activity[] with an ISO date, a count, and a level between 0 and maxLevel:
import { unstable_cache } from "next/cache";
import {
ContributionGraph,
ContributionGraphBlock,
ContributionGraphCalendar,
ContributionGraphFooter,
ContributionGraphLegend,
ContributionGraphTotalCount,
type Activity,
} from "@/components/craft-blocks/blocks/contribution-graph";
type GitHubContributionsResponse = {
total: Record<string, number>;
contributions: Activity[];
};
export const getCachedContributions = unstable_cache(
async (username: string) => {
const response = await fetch(
`https://github-contributions-api.jogruber.de/v4/${username}?y=last`,
{ next: { revalidate: 3600 } },
);
return (await response.json()) as GitHubContributionsResponse;
},
["github-contributions"],
{ revalidate: 3600 },
);
export async function Contributions({ username }: { username: string }) {
const user = await getCachedContributions(username);
return (
<ContributionGraph
data={user.contributions}
totalCount={user.total[new Date().getFullYear()]}
>
<ContributionGraphCalendar>
{({ activity, dayIndex, weekIndex }) => (
<ContributionGraphBlock
activity={activity}
dayIndex={dayIndex}
weekIndex={weekIndex}
/>
)}
</ContributionGraphCalendar>
<ContributionGraphFooter>
<ContributionGraphTotalCount />
<ContributionGraphLegend />
</ContributionGraphFooter>
</ContributionGraph>
);
}Missing days inside the series are filled as level 0, and the last day's year drives the ContributionGraphTotalCount display when totalCount isn't passed explicitly.
Props
ContributionGraph
Prop
Type
ContributionGraphCalendar
Renders the scrolling week grid and month labels, calling the render-prop for every day cell.
Prop
Type
ContributionGraphBlock
The single day cell — an SVG rectangle colored by activity.level through the shared level ramp.
Prop
Type
ContributionGraphTotalCount
Shows "N activities in YEAR" (or a custom template via labels.totalCount).
Prop
Type
ContributionGraphLegend
A "Less / More" caption with one swatch per level, pulled from the same context sizing so it always matches the grid.
Prop
Type
Theming
The block follows your exact theme with no configuration. Level 0 uses fill-muted, while levels 1 through 4 walk the app's emerald ramp with dedicated light and dark stops — muted green on white (emerald-200 → emerald-800) that brightens into a GitHub-dark palette (emerald-900 → emerald-300) in dark mode. Month labels inherit the surrounding text color, the footer and legend captions use text-muted-foreground, and the legend swatches carry a subtle stroke-border edge so they read against any surface.
Animations
- Entrance: the level-0 cells fade in together as the canvas, then colored cells pop in level-by-level (
cubic-bezier(0.22, 1, 0.36, 1)) with per-block jitter so the wave reads organically. The keyframes are injected at runtime, and the whole sequence replays whenever the dataset changes or the username swaps. - Loading skeleton: the placeholder grid pulses with scattered negative delays, so it flickers like a traffic light instead of beating in unison.
- Hover: each block scales to 125% on a transform-only transition, kept inside the clip boundary by the calendar's padded scroll container.
- Tooltip: bubbles position above the block through a fixed portal, slide and scale in with a slight overshoot, then dismiss 40ms after the pointer leaves — closing also on scroll and resize.
- Auto-scroll: the calendar scrolls to the latest week on mount and after data lands.
- Reduced motion: all entrance and hover animation honors
prefers-reduced-motion.
Registry Bundle
Install the exact registry entry when you want contribution-graph.tsx and its declared runtime dependencies together. Dependencies: date-fns.
Contact
If you find any bug or issue, feel free to raise an issue.
Last updated on