GitHub Contributions
elements/github-contributionsFreeBlock
Display contribution activity grid like GitHub's profile
Install it
npx shadcn@latest add https://www.tryelements.dev/r/github-contributions.jsonSource
1"use client";23import { useEffect, useMemo, useState } from "react";45import { cn } from "@/lib/utils";67interface GitHubContributionsProps8 extends React.HTMLAttributes<HTMLDivElement> {9 owner: string;10 repo: string;11 staticContributions?: number;12 staticData?: number[][];13}1415function generateRandomGrid(rows: number, cols: number): number[][] {16 return Array.from({ length: rows }, () =>17 Array.from({ length: cols }, () => Math.random()),18 );19}2021interface HeatmapGridProps {22 data: number[][];23 className?: string;24}2526const HEATMAP_ROWS = 7;27const HEATMAP_COLS = 12;28const HEATMAP_CELL_SIZE = 14;29const HEATMAP_GAP = 4;30const HEATMAP_VIEWBOX_WIDTH =31 HEATMAP_COLS * HEATMAP_CELL_SIZE + (HEATMAP_COLS - 1) * HEATMAP_GAP;32const HEATMAP_VIEWBOX_HEIGHT =33 HEATMAP_ROWS * HEATMAP_CELL_SIZE + (HEATMAP_ROWS - 1) * HEATMAP_GAP;3435function HeatmapGrid({ data, className }: HeatmapGridProps) {36 const gridData = useMemo(() => {37 if (!data || data.length === 0) {38 return generateRandomGrid(HEATMAP_ROWS, HEATMAP_COLS);39 }40 return data;41 }, [data]);4243 const rows = gridData.length;44 const cols = gridData[0]?.length || 0;4546 const cells = useMemo(() => {47 const result: Array<{48 x: number;49 y: number;50 opacity: number;51 key: string;52 }> = [];5354 for (let row = 0; row < rows; row++) {55 for (let col = 0; col < cols; col++) {56 const value = gridData[row]?.[col] ?? 0;57 result.push({58 x: col * (HEATMAP_CELL_SIZE + HEATMAP_GAP),59 y: row * (HEATMAP_CELL_SIZE + HEATMAP_GAP),60 opacity: Math.max(0.05, Math.min(1, value)),61 key: `${row}-${col}`,62 });63 }64 }6566 return result;67 }, [gridData, rows, cols]);6869 return (70 <svg71 viewBox={`0 0 ${HEATMAP_VIEWBOX_WIDTH} ${HEATMAP_VIEWBOX_HEIGHT}`}72 fill="none"73 preserveAspectRatio="xMidYMid meet"74 className={cn("w-full h-auto text-muted-foreground", className)}75 aria-hidden="true"76 >77 <g opacity={0.5}>78 {cells.map((cell) => (79 <rect80 key={cell.key}81 x={cell.x}82 y={cell.y}83 width={HEATMAP_CELL_SIZE}84 height={HEATMAP_CELL_SIZE}85 fill="currentColor"86 opacity={cell.opacity}87 />88 ))}89 </g>90 </svg>91 );92}9394export function GitHubContributions({95 owner,96 repo,97 staticContributions,98 staticData,99 className,100 ...props101}: GitHubContributionsProps) {102// … truncated
Files it writes
More from elements
All 359 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Agentic Codingagentic-coding | elements | Blocks | Free | no deps | |
| AgentMail Logoagentmail-logo | elements | Blocks | Free | no deps | |
| AI Badgeai-badge | elements | Blocks | Free | 2 deps · 2 files | |
| AI Servicesai-services | elements | Blocks | Free | no deps | |
| Astro Logoastro-logo | elements | Blocks | Free | no deps | |
| AWS All Servicesaws-all | elements | Blocks | Free | no deps | |
| AWS Analyticsaws-analytics | elements | Blocks | Free | no deps | |
| AWS Computeaws-compute | elements | Blocks | Free | no deps |
