Contribution Graph
ncdai/contribution-graphUnverifiedComponent
A GitHub-style contribution graph component that displays activity levels over time.
Install it
npx shadcn@latest add https://raw.githubusercontent.com/Sumeet011/Portfolio/main/public/r/contribution-graph.jsonSource
1// Credit: https://www.kibo-ui.com/components/contribution-graph23"use client"45import {6 createContext,7 Fragment,8 useContext,9 useMemo,10 type CSSProperties,11 type HTMLAttributes,12 type ReactNode,13} from "react"14import type { Day as WeekDay } from "date-fns"15import {16 differenceInCalendarDays,17 eachDayOfInterval,18 formatISO,19 getDay,20 getMonth,21 getYear,22 nextDay,23 parseISO,24 subWeeks,25} from "date-fns"2627import { cn } from "@/lib/utils"2829export type Activity = {30 date: string31 count: number32 level: number33}3435type Week = Array<Activity | undefined>3637export type Labels = {38 months?: string[]39 weekdays?: string[]40 totalCount?: string41 legend?: {42 less?: string43 more?: string44 }45}4647type MonthLabel = {48 weekIndex: number49 label: string50}5152const DEFAULT_MONTH_LABELS = [53 "Jan",54 "Feb",55 "Mar",56 "Apr",57 "May",58 "Jun",59 "Jul",60 "Aug",61 "Sep",62 "Oct",63 "Nov",64 "Dec",65]6667const DEFAULT_LABELS: Labels = {68 months: DEFAULT_MONTH_LABELS,69 weekdays: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],70 totalCount: "{{count}} activities in {{year}}",71 legend: {72 less: "Less",73 more: "More",74 },75}7677const THEME = cn(78 'data-[level="0"]:fill-muted-foreground/5',79 'data-[level="1"]:fill-muted-foreground/20',80 'data-[level="2"]:fill-muted-foreground/40',81 'data-[level="3"]:fill-muted-foreground/60',82 'data-[level="4"]:fill-muted-foreground/80'83)8485type ContributionGraphContextType = {86 data: Activity[]87 weeks: Week[]88 blockMargin: number89 blockRadius: number90 blockSize: number91 fontSize: number92 labels: Labels93 labelHeight: number94 maxLevel: number95 totalCount: number96 weekStart: WeekDay97 year: number98 width: number99 height: number100}101102const ContributionGraphContext =103 createContext<ContributionGraphContextType | null>(null)104105const useContributionGraph = () => {106 const context = useContext(ContributionGraphContext)107108 if (!context) {109 throw new Error(110 "ContributionGraph components must be used within a ContributionGraph"111 )112 }113114 return context115}116117const fillHoles = (activities: Activity[]): Activity[] => {118 if (activities.length === 0) {119 return []120 }121122 // Sort activities by date to ensure correct date range123 const sortedActivities = [...activities].sort((a, b) =>124 a.date.localeCompare(b.date)125 )126127 const calendar = new Map<string, Activity>(activities.map((a) => [a.date, a]))128129 const firstActivity = sortedActivities[0] as Activity130 const lastActivity = sortedActivities.at(-1)131132// … truncated
What it pulls in
npm packages
Files it writes
More from ncdai
All 48 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Apple Hello Effectapple-hello-effect | ncdai | Components | Unverified | 1 dep | |
| Apple Hello Effect - Hindiapple-hello-effect-hindi | ncdai | Components | Unverified | 1 dep | |
| Apple Hello Effect - Spanishapple-hello-effect-spanish | ncdai | Components | Unverified | 1 dep | |
| Apple Hello Effect - Vietnameseapple-hello-effect-vietnamese | ncdai | Components | Unverified | 1 dep | |
| Brand Assets Menubrand-assets-menu | ncdai | Components | Unverified | 1 dep | |
| chevrons-up-down-iconchevrons-up-down-icon | ncdai | Components | Unverified | 1 dep | |
| Code Block Commandcode-block-command | ncdai | Components | Unverified | 4 deps · 2 files | |
| Consent Managerconsent-manager | ncdai | Components | Unverified | 1 dep |
