opensourceui
GitHubTwitter/X
  1. Home
  2. Components
  3. Github
  4. Github Contribution
Back to Github

Components / Github

Github Contribution

Free Github React component — GithubContributionCard. MIT licensed, copy-paste ready for Next.js and Tailwind CSS.

GitHub-style contribution heatmap with year selector, total count, and Less/More legend. Pulls real data when you pass a username.

Preview

570 contributions in 2026

DecJanFebMarAprMayJunJulAugSepOctNovDec
MonWedFri
No contributions on Dec 28, 2025No contributions on Dec 29, 2025No contributions on Dec 30, 2025No contributions on Dec 31, 2025No contributions on Jan 1, 2026No contributions on Jan 2, 2026No contributions on Jan 3, 2026
No contributions on Jan 4, 2026No contributions on Jan 5, 20262 contributions on Jan 6, 2026 — bidyut10No contributions on Jan 7, 20264 contributions on Jan 8, 2026 — bidyut10No contributions on Jan 9, 2026No contributions on Jan 10, 2026
No contributions on Jan 11, 2026No contributions on Jan 12, 2026No contributions on Jan 13, 2026No contributions on Jan 14, 2026No contributions on Jan 15, 2026No contributions on Jan 16, 20263 contributions on Jan 17, 2026 — bidyut10
No contributions on Jan 18, 20265 contributions on Jan 19, 2026 — bidyut10No contributions on Jan 20, 2026No contributions on Jan 21, 2026No contributions on Jan 22, 2026No contributions on Jan 23, 2026No contributions on Jan 24, 2026
No contributions on Jan 25, 20262 contributions on Jan 26, 2026 — bidyut10No contributions on Jan 27, 2026No contributions on Jan 28, 2026No contributions on Jan 29, 2026No contributions on Jan 30, 2026No contributions on Jan 31, 2026
No contributions on Feb 1, 2026No contributions on Feb 2, 2026No contributions on Feb 3, 2026No contributions on Feb 4, 2026No contributions on Feb 5, 2026No contributions on Feb 6, 20263 contributions on Feb 7, 2026 — bidyut10
No contributions on Feb 8, 2026No contributions on Feb 9, 20261 contribution on Feb 10, 2026 — bidyut10No contributions on Feb 11, 2026No contributions on Feb 12, 2026No contributions on Feb 13, 2026No contributions on Feb 14, 2026
No contributions on Feb 15, 20262 contributions on Feb 16, 2026 — bidyut10No contributions on Feb 17, 2026No contributions on Feb 18, 2026No contributions on Feb 19, 2026No contributions on Feb 20, 2026No contributions on Feb 21, 2026
No contributions on Feb 22, 20264 contributions on Feb 23, 2026 — bidyut10No contributions on Feb 24, 20261 contribution on Feb 25, 2026 — bidyut10No contributions on Feb 26, 2026No contributions on Feb 27, 2026No contributions on Feb 28, 2026
2 contributions on Mar 1, 2026 — bidyut10No contributions on Mar 2, 2026No contributions on Mar 3, 2026No contributions on Mar 4, 2026No contributions on Mar 5, 20262 contributions on Mar 6, 2026 — bidyut10No contributions on Mar 7, 2026
No contributions on Mar 8, 2026No contributions on Mar 9, 2026No contributions on Mar 10, 2026No contributions on Mar 11, 2026No contributions on Mar 12, 2026No contributions on Mar 13, 20265 contributions on Mar 14, 2026 — bidyut10
1 contribution on Mar 15, 2026 — bidyut10No contributions on Mar 16, 2026No contributions on Mar 17, 2026No contributions on Mar 18, 2026No contributions on Mar 19, 2026No contributions on Mar 20, 2026No contributions on Mar 21, 2026
No contributions on Mar 22, 2026No contributions on Mar 23, 20265 contributions on Mar 24, 2026 — bidyut10No contributions on Mar 25, 2026No contributions on Mar 26, 20263 contributions on Mar 27, 2026 — bidyut10No contributions on Mar 28, 2026
No contributions on Mar 29, 2026No contributions on Mar 30, 2026No contributions on Mar 31, 20262 contributions on Apr 1, 2026 — bidyut103 contributions on Apr 2, 2026 — bidyut104 contributions on Apr 3, 2026 — bidyut105 contributions on Apr 4, 2026 — bidyut10
6 contributions on Apr 5, 2026 — bidyut10No contributions on Apr 6, 20268 contributions on Apr 7, 2026 — bidyut109 contributions on Apr 8, 2026 — bidyut1010 contributions on Apr 9, 2026 — bidyut101 contribution on Apr 10, 2026 — bidyut102 contributions on Apr 11, 2026 — bidyut10
3 contributions on Apr 12, 2026 — bidyut104 contributions on Apr 13, 2026 — bidyut105 contributions on Apr 14, 2026 — bidyut106 contributions on Apr 15, 2026 — bidyut107 contributions on Apr 16, 2026 — bidyut10No contributions on Apr 17, 20269 contributions on Apr 18, 2026 — bidyut10
10 contributions on Apr 19, 2026 — bidyut101 contribution on Apr 20, 2026 — bidyut102 contributions on Apr 21, 2026 — bidyut103 contributions on Apr 22, 2026 — bidyut104 contributions on Apr 23, 2026 — bidyut105 contributions on Apr 24, 2026 — bidyut106 contributions on Apr 25, 2026 — bidyut10
7 contributions on Apr 26, 2026 — bidyut108 contributions on Apr 27, 2026 — bidyut10No contributions on Apr 28, 202610 contributions on Apr 29, 2026 — bidyut101 contribution on Apr 30, 2026 — bidyut102 contributions on May 1, 2026 — bidyut103 contributions on May 2, 2026 — bidyut10
4 contributions on May 3, 2026 — bidyut105 contributions on May 4, 2026 — bidyut10No contributions on May 5, 20267 contributions on May 6, 2026 — bidyut108 contributions on May 7, 2026 — bidyut109 contributions on May 8, 2026 — bidyut1010 contributions on May 9, 2026 — bidyut10
1 contribution on May 10, 2026 — bidyut102 contributions on May 11, 2026 — bidyut103 contributions on May 12, 2026 — bidyut104 contributions on May 13, 2026 — bidyut105 contributions on May 14, 2026 — bidyut106 contributions on May 15, 2026 — bidyut10No contributions on May 16, 2026
8 contributions on May 17, 2026 — bidyut109 contributions on May 18, 2026 — bidyut1010 contributions on May 19, 2026 — bidyut101 contribution on May 20, 2026 — bidyut102 contributions on May 21, 2026 — bidyut103 contributions on May 22, 2026 — bidyut104 contributions on May 23, 2026 — bidyut10
5 contributions on May 24, 2026 — bidyut106 contributions on May 25, 2026 — bidyut107 contributions on May 26, 2026 — bidyut10No contributions on May 27, 20269 contributions on May 28, 2026 — bidyut1010 contributions on May 29, 2026 — bidyut101 contribution on May 30, 2026 — bidyut10
2 contributions on May 31, 2026 — bidyut102 contributions on Jun 1, 2026 — bidyut103 contributions on Jun 2, 2026 — bidyut104 contributions on Jun 3, 2026 — bidyut10No contributions on Jun 4, 20266 contributions on Jun 5, 2026 — bidyut107 contributions on Jun 6, 2026 — bidyut10
8 contributions on Jun 7, 2026 — bidyut109 contributions on Jun 8, 2026 — bidyut1010 contributions on Jun 9, 2026 — bidyut101 contribution on Jun 10, 2026 — bidyut102 contributions on Jun 11, 2026 — bidyut103 contributions on Jun 12, 2026 — bidyut104 contributions on Jun 13, 2026 — bidyut10
5 contributions on Jun 14, 2026 — bidyut10No contributions on Jun 15, 20267 contributions on Jun 16, 2026 — bidyut108 contributions on Jun 17, 2026 — bidyut109 contributions on Jun 18, 2026 — bidyut1010 contributions on Jun 19, 2026 — bidyut101 contribution on Jun 20, 2026 — bidyut10
2 contributions on Jun 21, 2026 — bidyut103 contributions on Jun 22, 2026 — bidyut104 contributions on Jun 23, 2026 — bidyut105 contributions on Jun 24, 2026 — bidyut106 contributions on Jun 25, 2026 — bidyut10No contributions on Jun 26, 20268 contributions on Jun 27, 2026 — bidyut10
9 contributions on Jun 28, 2026 — bidyut1010 contributions on Jun 29, 2026 — bidyut101 contribution on Jun 30, 2026 — bidyut10No contributions on Jul 1, 20263 contributions on Jul 2, 2026 — bidyut10No contributions on Jul 3, 20265 contributions on Jul 4, 2026 — bidyut10
No contributions on Jul 5, 2026No contributions on Jul 6, 2026No contributions on Jul 7, 2026No contributions on Jul 8, 2026No contributions on Jul 9, 2026No contributions on Jul 10, 20262 contributions on Jul 11, 2026 — bidyut10
No contributions on Jul 12, 2026No contributions on Jul 13, 2026No contributions on Jul 14, 2026No contributions on Jul 15, 2026No contributions on Jul 16, 20263 contributions on Jul 17, 2026 — bidyut10No contributions on Jul 18, 2026
No contributions on Jul 19, 20261 contribution on Jul 20, 2026 — bidyut10No contributions on Jul 21, 2026No contributions on Jul 22, 2026No contributions on Jul 23, 2026No contributions on Jul 24, 2026No contributions on Jul 25, 2026
No contributions on Jul 26, 2026No contributions on Jul 27, 2026No contributions on Jul 28, 20265 contributions on Jul 29, 2026 — bidyut101 contribution on Jul 30, 2026 — bidyut10No contributions on Jul 31, 20262 contributions on Aug 1, 2026 — bidyut10
No contributions on Aug 2, 2026No contributions on Aug 3, 2026No contributions on Aug 4, 2026No contributions on Aug 5, 2026No contributions on Aug 6, 2026No contributions on Aug 7, 20264 contributions on Aug 8, 2026 — bidyut10
No contributions on Aug 9, 20261 contribution on Aug 10, 2026 — bidyut10No contributions on Aug 11, 2026No contributions on Aug 12, 2026No contributions on Aug 13, 2026No contributions on Aug 14, 2026No contributions on Aug 15, 2026
No contributions on Aug 16, 2026No contributions on Aug 17, 2026No contributions on Aug 18, 20265 contributions on Aug 19, 2026 — bidyut10No contributions on Aug 20, 20262 contributions on Aug 21, 2026 — bidyut10No contributions on Aug 22, 2026
No contributions on Aug 23, 2026No contributions on Aug 24, 2026No contributions on Aug 25, 2026No contributions on Aug 26, 2026No contributions on Aug 27, 20264 contributions on Aug 28, 2026 — bidyut10No contributions on Aug 29, 2026
No contributions on Aug 30, 2026No contributions on Aug 31, 2026No contributions on Sep 1, 2026No contributions on Sep 2, 2026No contributions on Sep 3, 2026No contributions on Sep 4, 2026No contributions on Sep 5, 2026
No contributions on Sep 6, 2026No contributions on Sep 7, 2026No contributions on Sep 8, 20265 contributions on Sep 9, 2026 — bidyut10No contributions on Sep 10, 2026No contributions on Sep 11, 20263 contributions on Sep 12, 2026 — bidyut10
No contributions on Sep 13, 2026No contributions on Sep 14, 2026No contributions on Sep 15, 2026No contributions on Sep 16, 2026No contributions on Sep 17, 20264 contributions on Sep 18, 2026 — bidyut10No contributions on Sep 19, 2026
No contributions on Sep 20, 2026No contributions on Sep 21, 2026No contributions on Sep 22, 2026No contributions on Sep 23, 2026No contributions on Sep 24, 20261 contribution on Sep 25, 2026 — bidyut10No contributions on Sep 26, 2026
3 contributions on Sep 27, 2026 — bidyut10No contributions on Sep 28, 2026No contributions on Sep 29, 2026No contributions on Sep 30, 2026No contributions on Oct 1, 2026No contributions on Oct 2, 20264 contributions on Oct 3, 2026 — bidyut10
No contributions on Oct 4, 2026No contributions on Oct 5, 2026No contributions on Oct 6, 2026No contributions on Oct 7, 20264 contributions on Oct 8, 2026 — bidyut10No contributions on Oct 9, 2026No contributions on Oct 10, 2026
No contributions on Oct 11, 2026No contributions on Oct 12, 2026No contributions on Oct 13, 2026No contributions on Oct 14, 2026No contributions on Oct 15, 20262 contributions on Oct 16, 2026 — bidyut103 contributions on Oct 17, 2026 — bidyut10
No contributions on Oct 18, 2026No contributions on Oct 19, 2026No contributions on Oct 20, 2026No contributions on Oct 21, 2026No contributions on Oct 22, 2026No contributions on Oct 23, 2026No contributions on Oct 24, 2026
No contributions on Oct 25, 20262 contributions on Oct 26, 2026 — bidyut10No contributions on Oct 27, 2026No contributions on Oct 28, 20265 contributions on Oct 29, 2026 — bidyut10No contributions on Oct 30, 2026No contributions on Oct 31, 2026
No contributions on Nov 1, 2026No contributions on Nov 2, 2026No contributions on Nov 3, 2026No contributions on Nov 4, 2026No contributions on Nov 5, 2026No contributions on Nov 6, 20263 contributions on Nov 7, 2026 — bidyut10
No contributions on Nov 8, 2026No contributions on Nov 9, 2026No contributions on Nov 10, 2026No contributions on Nov 11, 2026No contributions on Nov 12, 2026No contributions on Nov 13, 2026No contributions on Nov 14, 2026
No contributions on Nov 15, 20262 contributions on Nov 16, 2026 — bidyut10No contributions on Nov 17, 2026No contributions on Nov 18, 2026No contributions on Nov 19, 20261 contribution on Nov 20, 2026 — bidyut10No contributions on Nov 21, 2026
No contributions on Nov 22, 2026No contributions on Nov 23, 2026No contributions on Nov 24, 20261 contribution on Nov 25, 2026 — bidyut10No contributions on Nov 26, 2026No contributions on Nov 27, 2026No contributions on Nov 28, 2026
No contributions on Nov 29, 2026No contributions on Nov 30, 2026No contributions on Dec 1, 2026No contributions on Dec 2, 2026No contributions on Dec 3, 2026No contributions on Dec 4, 2026No contributions on Dec 5, 2026
2 contributions on Dec 6, 2026 — bidyut10No contributions on Dec 7, 2026No contributions on Dec 8, 2026No contributions on Dec 9, 2026No contributions on Dec 10, 20262 contributions on Dec 11, 2026 — bidyut10No contributions on Dec 12, 2026
No contributions on Dec 13, 2026No contributions on Dec 14, 20261 contribution on Dec 15, 2026 — bidyut10No contributions on Dec 16, 2026No contributions on Dec 17, 2026No contributions on Dec 18, 2026No contributions on Dec 19, 2026
No contributions on Dec 20, 2026No contributions on Dec 21, 2026No contributions on Dec 22, 2026No contributions on Dec 23, 20265 contributions on Dec 24, 2026 — bidyut10No contributions on Dec 25, 2026No contributions on Dec 26, 2026
No contributions on Dec 27, 2026No contributions on Dec 28, 2026No contributions on Dec 29, 2026No contributions on Dec 30, 2026No contributions on Dec 31, 2026No contributions on Jan 1, 2027No contributions on Jan 2, 2027
Less
More

Setup

How to use

Free for personal and commercial use. No UI attribution required. Include the MIT copyright notice when copying component source into your project. Read the MIT license.

  1. 1

    Run in your terminal:

    $npm install clsx tailwind-merge lucide-react
  2. 2

    Copy lib/cn.ts below. Skip if you already have cn().

  3. 3

    Copy the code below and create components/github/github-contribution.tsx in your project.

  4. 4

    Import and render:

    Example

    import { GithubContributionCard } from "@/components/github/github-contribution";

    <GithubContributionCard username="your-handle" year={2026} />

lib/cn.ts

1
2
3
4
5
6
import { type ClassValue, clsx } from "clsx";
import { twMerge } from "tailwind-merge";
 
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}

components/github/github-contribution.tsx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
"use client";
 
import {
forwardRef,
useCallback,
useEffect,
useMemo,
useRef,
useState,
type ComponentPropsWithoutRef,
} from "react";
 
import { cn } from "@/lib/cn";
import { ChevronDown, ChevronUp } from "lucide-react";
 
// Tailwind-only scroll area — self-contained, no global CSS.
const HIDE_SCROLLBAR =
"[scrollbar-width:none] [-ms-overflow-style:none] [&::-webkit-scrollbar]:hidden";
 
type ScrollAxis = "y" | "x" | "both";
type ThumbMetrics = { size: number; offset: number; active: boolean };
 
function getScrollViewportClasses(axis: ScrollAxis): Readonly<{
overflowClass: string;
clipClass: string;
}> {
if (axis === "x") {
return {
overflowClass: "overflow-x-scroll overflow-y-hidden",
clipClass: "-mb-4 pb-4",
};
}
 
if (axis === "both") {
return {
overflowClass: "overflow-scroll",
clipClass: "-mr-4 pr-4 -mb-4 pb-4",
};
}
 
return {
overflowClass: "overflow-y-scroll overflow-x-hidden",
clipClass: "-mr-4 pr-4",
};
}
 
function computeVerticalThumb(el: HTMLElement): ThumbMetrics {
const { scrollHeight, clientHeight, scrollTop } = el;
if (scrollHeight <= clientHeight + 1)
return { size: 0, offset: 0, active: false };
const thumbSize = Math.max(24, (clientHeight / scrollHeight) * clientHeight);
const maxOffset = clientHeight - thumbSize;
const offset =
maxOffset <= 0
? 0
: (scrollTop / (scrollHeight - clientHeight)) * maxOffset;
return { size: thumbSize, offset, active: true };
}
 
function computeHorizontalThumb(el: HTMLElement): ThumbMetrics {
const { scrollWidth, clientWidth, scrollLeft } = el;
if (scrollWidth <= clientWidth + 1)
return { size: 0, offset: 0, active: false };
const thumbSize = Math.max(24, (clientWidth / scrollWidth) * clientWidth);
const maxOffset = clientWidth - thumbSize;
const offset =
maxOffset <= 0 ? 0 : (scrollLeft / (scrollWidth - clientWidth)) * maxOffset;
return { size: thumbSize, offset, active: true };
}
 
type LocalScrollHoverAreaProps = Readonly<
{
children: React.ReactNode;
viewportClassName?: string;
axis?: ScrollAxis;
} & React.ComponentPropsWithoutRef<"div">
>;
 
const LocalScrollHoverArea = forwardRef<
HTMLDivElement,
LocalScrollHoverAreaProps
>(({ className, viewportClassName, children, axis = "y", ...props }, ref) => {
const viewportRef = useRef<HTMLDivElement>(null);
const [vertical, setVertical] = useState<ThumbMetrics>({
size: 0,
offset: 0,
active: false,
});
const [horizontal, setHorizontal] = useState<ThumbMetrics>({
size: 0,
offset: 0,
active: false,
});
 
const update = useCallback(() => {
const el = viewportRef.current;
if (!el) return;
if (axis === "y" || axis === "both") setVertical(computeVerticalThumb(el));
if (axis === "x" || axis === "both")
setHorizontal(computeHorizontalThumb(el));
}, [axis]);
 
useEffect(() => {
const el = viewportRef.current;
if (!el) return;
update();
const ro = new ResizeObserver(update);
ro.observe(el);
for (const child of el.children) ro.observe(child);
el.addEventListener("scroll", update, { passive: true });
globalThis.addEventListener("resize", update);
return () => {
ro.disconnect();
el.removeEventListener("scroll", update);
globalThis.removeEventListener("resize", update);
};
}, [update, children]);
 
const { overflowClass, clipClass } = getScrollViewportClasses(axis);
 
return (
<div
ref={ref}
className={cn("group/scrollarea relative overflow-hidden", className)}
{...props}
>
<div
ref={viewportRef}
className={cn(
"h-full w-full",
HIDE_SCROLLBAR,
overflowClass,
clipClass,
viewportClassName,
)}
>
{children}
</div>
{vertical.active && (axis === "y" || axis === "both") && (
<div
className="pointer-events-none absolute top-0 right-1 bottom-0 z-10 w-1 opacity-0 transition-opacity duration-150 group-hover/scrollarea:opacity-100"
aria-hidden
>
<div
className="absolute right-0 w-1 rounded-full bg-neutral-400"
style={{ height: vertical.size, top: vertical.offset }}
/>
</div>
)}
{horizontal.active && (axis === "x" || axis === "both") && (
<div
className="pointer-events-none absolute right-0 bottom-1 left-0 z-10 h-1 opacity-0 transition-opacity duration-150 group-hover/scrollarea:opacity-100"
aria-hidden
>
<div
className="absolute bottom-0 h-1 rounded-full bg-neutral-400"
style={{ width: horizontal.size, left: horizontal.offset }}
/>
</div>
)}
</div>
);
});
LocalScrollHoverArea.displayName = "LocalScrollHoverArea";
 
//GitHub-style contribution graph — pass a year + daily counts; grid and colors are derived.
export type ContributionLevel = 0 | 1 | 2 | 3 | 4;
 
export type ContributionDay = Readonly<{
// ISO date — `YYYY-MM-DD`
date: string;
count: number;
}>;
 
export type GithubContributionCardProps = Readonly<
{
// Initial / controlled calendar year (defaults to current year).
year?: number;
// Years shown in the dropdown (newest first). Auto-derived from data when omitted.
years?: number[];
username?: string;
/**
* Daily contribution counts — any year; graph filters by the selected year.
* Pass `Record<"YYYY-MM-DD", number>` or `{ date, count }[]`.
* Omit to use built-in demo data for the selected year.
*/
contributions?: Record<string, number> | ContributionDay[];
onYearChange?: (year: number) => void;
} & ComponentPropsWithoutRef<"div">
>;
 
const MONTHS = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
] as const;
 
const DAY_LABELS: { row: number; label: string }[] = [
{ row: 1, label: "Mon" },
{ row: 3, label: "Wed" },
{ row: 5, label: "Fri" },
];
 
const LEVEL_BG: Record<ContributionLevel, string> = {
0: "bg-[#ebedf0]",
1: "bg-[#9be9a8]",
2: "bg-[#40c463]",
3: "bg-[#30a14e]",
4: "bg-[#216e39]",
};
 
const WEEKS = 53;
const CELL = "h-[10px] w-[10px] rounded-[2px]";
const GAP = 3;
const COL = 10 + GAP;
 
type GraphCell = Readonly<{
level: ContributionLevel;
count: number;
date: Date | null;
inYear: boolean;
}>;
 
function pad(n: number) {
return n.toString().padStart(2, "0");
}
 
function toDateKey(year: number, month: number, day: number) {
return `${year}-${pad(month + 1)}-${pad(day)}`;
}
 
function parseYearFromDate(date: string): number | null {
const year = Number.parseInt(date.slice(0, 4), 10);
return Number.isFinite(year) ? year : null;
}
 
function filterByYear(
contributions: Map<string, number>,
year: number,
): Map<string, number> {
const prefix = `${year}-`;
const filtered = new Map<string, number>();
for (const [date, count] of contributions) {
if (date.startsWith(prefix)) filtered.set(date, count);
}
return filtered;
}
 
function deriveAvailableYears(
contributions: Map<string, number>,
selectedYear: number,
years?: number[],
): number[] {
if (years?.length) {
return [...new Set(years)].sort((a, b) => b - a);
}
 
const fromData = new Set<number>();
for (const date of contributions.keys()) {
const y = parseYearFromDate(date);
if (y !== null) fromData.add(y);
}
fromData.add(selectedYear);
 
const sorted = [...fromData].sort((a, b) => b - a);
if (sorted.length > 1) return sorted;
 
const current = new Date().getFullYear();
return Array.from({ length: 5 }, (_, i) => current - i);
}
 
function normalizeContributions(
input?: Record<string, number> | ContributionDay[],
): Map<string, number> {
const map = new Map<string, number>();
if (!input) return map;
 
if (Array.isArray(input)) {
for (const { date, count } of input) {
if (count > 0) map.set(date, count);
}
return map;
}
 
for (const [date, count] of Object.entries(input)) {
if (count > 0) map.set(date, count);
}
return map;
}
 
function getGraphStart(year: number): Date {
const jan1 = new Date(year, 0, 1);
const start = new Date(jan1);
start.setDate(jan1.getDate() - jan1.getDay());
start.setHours(0, 0, 0, 0);
return start;
}
 
function cellToDate(graphStart: Date, week: number, day: number): Date {
const date = new Date(graphStart);
date.setDate(graphStart.getDate() + week * 7 + day);
return date;
}
 
function countToLevel(count: number, max: number): ContributionLevel {
if (count <= 0 || max <= 0) return 0;
const ratio = count / max;
if (ratio <= 0.25) return 1;
if (ratio <= 0.5) return 2;
if (ratio <= 0.75) return 3;
return 4;
}
 
// Deterministic demo — sparse Q1, Apr–Jun cluster, lighter H2.
function generateDemoContributions(year: number): Map<string, number> {
const map = new Map<string, number>();
 
for (let month = 0; month < 12; month++) {
const daysInMonth = new Date(year, month + 1, 0).getDate();
 
for (let day = 1; day <= daysInMonth; day++) {
const seed = year * 10_000 + month * 100 + day;
 
if (month >= 3 && month <= 5) {
if (seed % 11 === 0) continue;
map.set(toDateKey(year, month, day), (seed % 10) + 1);
continue;
}
 
if (seed % 9 === 0 || seed % 13 === 0) {
map.set(toDateKey(year, month, day), (seed % 5) + 1);
}
}
}
 
return map;
}
 
function buildGraph(
year: number,
contributions: Map<string, number>,
): { grid: GraphCell[][]; total: number } {
const graphStart = getGraphStart(year);
const max = Math.max(0, ...contributions.values());
let total = 0;
 
const grid: GraphCell[][] = Array.from({ length: WEEKS }, (_, week) =>
Array.from({ length: 7 }, (_, day) => {
const date = cellToDate(graphStart, week, day);
const inYear = date.getFullYear() === year;
 
if (!inYear) {
return { level: 0, count: 0, date, inYear: false };
}
 
const key = toDateKey(
date.getFullYear(),
date.getMonth(),
date.getDate(),
);
const count = contributions.get(key) ?? 0;
total += count;
 
return {
level: countToLevel(count, max),
count,
date,
inYear: true,
};
}),
);
 
return { grid, total };
}
 
function computeMonthLabels(year: number): { label: string; week: number }[] {
const start = getGraphStart(year);
const labels: { label: string; week: number }[] = [];
let lastMonth = -1;
 
for (let w = 0; w < WEEKS; w++) {
const date = new Date(start);
date.setDate(start.getDate() + w * 7);
const month = date.getMonth();
if (month !== lastMonth) {
labels.push({ label: MONTHS[month], week: w });
lastMonth = month;
}
}
 
return labels;
}
 
const LEVEL_FILL: Record<ContributionLevel, string> = {
0: "#ebedf0",
1: "#9be9a8",
2: "#40c463",
3: "#30a14e",
4: "#216e39",
};
 
function contributionCellSrc(
level: ContributionLevel,
inYear: boolean,
): string {
const fill = inYear ? LEVEL_FILL[level] : "transparent";
const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="10" height="10"><rect width="10" height="10" rx="2" fill="${fill}"/></svg>`;
return `data:image/svg+xml,${encodeURIComponent(svg)}`;
}
 
function formatTooltip(date: Date, count: number, username: string) {
const label = date.toLocaleDateString("en-US", {
month: "short",
day: "numeric",
year: "numeric",
});
if (count === 0) return `No contributions on ${label}`;
return `${count} contribution${count === 1 ? "" : "s"} on ${label} — ${username}`;
}
 
type YearDropdownProps = Readonly<{
years: readonly number[];
value: number;
onChange: (year: number) => void;
}>;
 
function YearDropdown({ years, value, onChange }: YearDropdownProps) {
const [open, setOpen] = useState(false);
const rootRef = useRef<HTMLDivElement>(null);
 
const close = useCallback(() => setOpen(false), []);
 
useEffect(() => {
if (!open) return;
 
const handlePointerDown = (event: MouseEvent) => {
if (!rootRef.current?.contains(event.target as Node)) close();
};
 
const handleKeyDown = (event: KeyboardEvent) => {
if (event.key === "Escape") close();
};
 
document.addEventListener("mousedown", handlePointerDown);
document.addEventListener("keydown", handleKeyDown);
return () => {
document.removeEventListener("mousedown", handlePointerDown);
document.removeEventListener("keydown", handleKeyDown);
};
}, [close, open]);
 
const handleSelect = (year: number) => {
onChange(year);
close();
};
 
return (
<div
ref={rootRef}
data-slot="github-contribution-card-year-selector"
className="relative z-20 min-w-17 shrink-0"
>
<select
aria-label="Contribution years"
value={value}
onChange={(event) => onChange(Number(event.target.value))}
className="sr-only"
tabIndex={-1}
>
{years.map((year) => (
<option key={year} value={year}>
{year}
</option>
))}
</select>
 
<button
type="button"
aria-haspopup="true"
aria-expanded={open}
aria-label={`Contribution year, ${value}`}
onClick={() => setOpen((current) => !current)}
className={cn(
"flex w-full items-center justify-between gap-2 border border-neutral-200 bg-white px-2 py-1 text-[11px] font-medium text-neutral-700 transition-colors hover:bg-neutral-50",
open
? "relative z-10 rounded-t-md rounded-b-none border-b-transparent bg-neutral-50"
: "rounded-md",
)}
>
<span className="tabular-nums">{value}</span>
<span className="text-neutral-400" aria-hidden>
{open ? <ChevronUp size={12} /> : <ChevronDown size={12} />}
</span>
</button>
 
{open ? (
<div className="absolute top-[calc(100%-1px)] right-0 left-0 z-10 overflow-hidden rounded-b-md border border-t-0 border-neutral-200 bg-white shadow-sm">
<ul className="max-h-32 scrollbar-thin overflow-y-auto py-0.5">
{years.map((year) => {
const selected = year === value;
return (
<li key={year}>
<button
type="button"
onClick={() => handleSelect(year)}
className={cn(
"flex w-full px-2 py-1 text-left text-[11px] tabular-nums transition-colors",
selected
? "bg-neutral-100 font-medium text-neutral-900"
: "text-neutral-600 hover:bg-neutral-50",
)}
>
{year}
</button>
</li>
);
})}
</ul>
</div>
) : null}
</div>
);
}
 
export const GithubContributionCard = forwardRef<
HTMLDivElement,
GithubContributionCardProps
>(
(
{
className,
year: yearProp,
years: yearsProp,
username = "bidyut10",
contributions: contributionsInput,
onYearChange,
...props
},
ref,
) => {
const [internalYear, setInternalYear] = useState(
() => yearProp ?? new Date().getFullYear(),
);
const selectedYear = yearProp ?? internalYear;
 
const allContributions = useMemo(
() => normalizeContributions(contributionsInput),
[contributionsInput],
);
 
const hasUserData = contributionsInput !== undefined;
 
const availableYears = useMemo(
() => deriveAvailableYears(allContributions, selectedYear, yearsProp),
[allContributions, selectedYear, yearsProp],
);
 
const yearContributions = useMemo(() => {
if (!hasUserData) return generateDemoContributions(selectedYear);
return filterByYear(allContributions, selectedYear);
}, [allContributions, hasUserData, selectedYear]);
 
const { grid, total } = useMemo(
() => buildGraph(selectedYear, yearContributions),
[selectedYear, yearContributions],
);
 
const monthLabels = useMemo(
() => computeMonthLabels(selectedYear),
[selectedYear],
);
const graphWidth = WEEKS * COL - GAP;
 
const handleYearSelect = (nextYear: number) => {
if (yearProp === undefined) setInternalYear(nextYear);
onYearChange?.(nextYear);
};
 
return (
<div
ref={ref}
data-slot="github-contribution-card"
className={cn(
"max-w-sm rounded-3xl border border-neutral-100 bg-white p-4 font-sans shadow-lg shadow-black/5 select-none",
className,
)}
{...props}
>
<div
data-slot="github-contribution-card-header"
className="relative z-10 mb-3 flex items-center justify-between gap-2"
>
<p className="text-sm text-neutral-700">
<span className="font-semibold text-neutral-900">
{total.toLocaleString("en-US")}
</span>{" "}
contributions in {selectedYear}
</p>
 
<YearDropdown
years={availableYears}
value={selectedYear}
onChange={handleYearSelect}
/>
</div>
 
<LocalScrollHoverArea axis="x" className="w-full">
<div className="inline-flex min-w-max flex-col" style={{ gap: GAP }}>
<div
className="relative h-4"
style={{ width: graphWidth, marginLeft: 27 }}
>
{monthLabels.map(({ label, week }) => (
<span
key={`${label}-${week}`}
className="absolute top-0 text-[10px] text-neutral-500"
style={{ left: week * COL }}
>
{label}
</span>
))}
</div>
 
<div className="flex" style={{ gap: GAP }}>
<div
className="relative shrink-0 text-[10px] text-neutral-500"
style={{ width: 24, height: 7 * COL - GAP }}
>
{DAY_LABELS.map(({ row, label }) => (
<span
key={label}
className="absolute leading-none"
style={{ top: row * COL + 1 }}
>
{label}
</span>
))}
</div>
 
<div className="flex" style={{ gap: GAP }}>
{grid.map((week) => {
const weekStart = week[0].date!;
return (
<div
key={toDateKey(
weekStart.getFullYear(),
weekStart.getMonth(),
weekStart.getDate(),
)}
className="flex flex-col"
style={{ gap: GAP }}
data-slot="github-contribution-card-week"
>
{week.map((cell) => {
const cellDate = cell.date!;
const tooltip = formatTooltip(
cellDate,
cell.count,
username,
);
 
return (
// Data-URI SVG cells — native img+alt for a11y; not a Next.js LCP image.
// eslint-disable-next-line @next/next/no-img-element
<img
key={toDateKey(
cellDate.getFullYear(),
cellDate.getMonth(),
cellDate.getDate(),
)}
alt={tooltip || ""}
aria-hidden={!tooltip}
title={tooltip || undefined}
src={contributionCellSrc(cell.level, cell.inYear)}
width={10}
height={10}
className={cn(CELL, "block")}
/>
);
})}
</div>
);
})}
</div>
</div>
</div>
</LocalScrollHoverArea>
 
<div
data-slot="github-contribution-card-footer"
className="mt-3 flex items-center justify-end gap-1 text-[10px] text-neutral-500"
>
<span>Less</span>
{([0, 1, 2, 3, 4] as ContributionLevel[]).map((level) => (
<div
key={level}
className={cn("h-2.5 w-2.5 rounded-xs", LEVEL_BG[level])}
/>
))}
<span>More</span>
</div>
</div>
);
},
);
 
GithubContributionCard.displayName = "GithubContributionCard";
PreviousNext

On this

page

Jump to a section on this page.

  • lib/cn.ts
  • components/github/github-contribution.tsx

Sponsor spot · demo preview

Logo

Your brand name

Your tagline or category

A short pitch about your product or service goes here. This is a preview of how sponsor cards will look in this sidebar.

Your call to action

This slot is available. Sponsor Opensource UI and reach developers browsing components every day.