1"use client";2
3import React, { useEffect, useId, useRef, useState } from "react";4import {5 AnimatePresence,6 animate,7 motion,8 useMotionValue,9 useReducedMotion,10 useTransform,11} from "motion/react";12import { cn } from "@/lib/utils";13
14export type ProgressRingProps = {15 16 value?: number;17 size?: number;18 strokeWidth?: number;19 20 label?: string;21 22 sublabel?: string;23 24 indeterminate?: boolean;25 26 showComplete?: boolean;27 28 color?: string;29 className?: string;30 "aria-label"?: string;31};32
33const SPRING = { type: "spring" as const, bounce: 0.1, duration: 0.8 };34
35export function ProgressRing({36 value = 0,37 size: sizeProp = 96,38 strokeWidth: strokeWidthProp = 8,39 label,40 sublabel,41 indeterminate = false,42 showComplete = true,43 color = "var(--color-ink)",44 className,45 "aria-label": ariaLabel,46}: ProgressRingProps) {47 const reduce = !!useReducedMotion();48 const uid = useId().replace(/:/g, "");49 const size = Number.isFinite(sizeProp) ? Math.max(24, sizeProp) : 96;50 const stroke = Number.isFinite(strokeWidthProp) ? Math.max(1, Math.min(size / 4, strokeWidthProp)) : 8;51 const clamped = Number.isFinite(value) ? Math.min(100, Math.max(0, value)) : 0;52 const complete = showComplete && !indeterminate && clamped >= 100;53 const r = (size - stroke) / 2;54 const cx = size / 2;55
56 const progress = useMotionValue(0);57 const first = useRef(true);58 useEffect(() => {59 if (reduce) {60 progress.set(clamped);61 return;62 }63 64 const controls = animate(progress, clamped, first.current ? { ...SPRING, duration: 1.2 } : SPRING);65 first.current = false;66 return () => controls.stop();67 }, [clamped, progress, reduce]);68
69 const length = useTransform(progress, (v) => Math.max(0, v / 100));70 const rounded = useTransform(progress, (v) => Math.round(v));71 72 const angle = useTransform(progress, (v) => (v / 100) * 360 - 90);73 const knobX = useTransform(angle, (a) => cx + r * Math.cos((a * Math.PI) / 180));74 const knobY = useTransform(angle, (a) => cx + r * Math.sin((a * Math.PI) / 180));75 const knobOpacity = useTransform(progress, [0, 2, 99, 100], [0, 1, 1, 0]);76
77 78 const [pulse, setPulse] = useState(0);79 const wasComplete = useRef(complete);80 useEffect(() => {81 if (complete && !wasComplete.current) setPulse((n) => n + 1);82 wasComplete.current = complete;83 }, [complete]);84 const arcOpacity = useTransform(progress, (v) => (v < 0.5 ? 0 : 1));85
86 const ringColor = complete ? "#10b981" : color;87 const fontSize = Math.max(11, size * 0.2);88
89 return (90 <div91 className={cn("relative inline-flex shrink-0 items-center justify-center", className)}92 style={{ width: size, height: size }}93 role="progressbar"94 aria-label={ariaLabel ?? label ?? "Progress"}95 aria-valuemin={indeterminate ? undefined : 0}96 aria-valuemax={indeterminate ? undefined : 100}97 aria-valuenow={indeterminate ? undefined : Math.round(clamped)}98 aria-valuetext={indeterminate ? "Loading" : complete ? "Complete" : `${Math.round(clamped)}%`}99 >100 <svg width={size} height={size} className="overflow-visible">101 <defs>102 <linearGradient id={`${uid}-g`} x1="0" y1="0" x2="1" y2="1">103 <stop offset="0" stopColor={ringColor} stopOpacity="0.55" />104 <stop offset="1" stopColor={ringColor} />105 </linearGradient>106 </defs>107 <circle cx={cx} cy={cx} r={r} fill="none" stroke="var(--color-rule)" strokeOpacity="0.8" strokeWidth={stroke} />108
109 {indeterminate ? (110 <motion.circle111 cx={cx}112 cy={cx}113 r={r}114 fill="none"115 stroke={`url(#${uid}-g)`}116 strokeWidth={stroke}117 strokeLinecap="round"118 pathLength={1}119 style={{ originX: "50%", originY: "50%" }}120 animate={reduce ? { opacity: [1, 0.4, 1] } : { rotate: 360, strokeDasharray: ["0.1 1", "0.45 1", "0.1 1"] }}121 transition={122 reduce123 ? { duration: 1.6, repeat: Infinity }124 : { rotate: { duration: 1.2, repeat: Infinity, ease: "linear" }, strokeDasharray: { duration: 1.6, repeat: Infinity, ease: "easeInOut" } }125 }126 />127 ) : (128 <>129 <motion.circle130 cx={cx}131 cy={cx}132 r={r}133 fill="none"134 stroke={`url(#${uid}-g)`}135 strokeWidth={stroke}136 strokeLinecap="round"137 transform={`rotate(-90 ${cx} ${cx})`}138 style={{ pathLength: length, opacity: arcOpacity }}139 className="transition-[stroke] duration-500"140 />141 <motion.circle142 r={stroke * 0.32}143 fill="var(--color-paper)"144 style={{ cx: knobX, cy: knobY, opacity: knobOpacity }}145 />146 <AnimatePresence>147 {pulse > 0 && !reduce ? (148 <motion.circle149 key={pulse}150 cx={cx}151 cy={cx}152 r={r}153 fill="none"154 stroke={ringColor}155 strokeWidth={stroke}156 initial={{ opacity: 0.3, scale: 1 }}157 animate={{ opacity: 0, scale: 1.12 }}158 transition={{ duration: 0.6, ease: "easeOut" }}159 style={{ originX: "50%", originY: "50%" }}160 />161 ) : null}162 </AnimatePresence>163 </>164 )}165 </svg>166
167 <div className="absolute inset-0 flex flex-col items-center justify-center text-center">168 <AnimatePresence initial={false} mode="popLayout">169 {complete ? (170 <motion.svg171 key="done"172 viewBox="0 0 24 24"173 className="text-emerald-500"174 style={{ width: size * 0.32, height: size * 0.32 }}175 fill="none"176 stroke="currentColor"177 strokeWidth="2.6"178 strokeLinecap="round"179 strokeLinejoin="round"180 initial={reduce ? { opacity: 0 } : { opacity: 0, scale: 0.5 }}181 animate={{ opacity: 1, scale: 1 }}182 exit={{ opacity: 0, scale: 0.5 }}183 transition={{ type: "spring", bounce: 0.4, duration: 0.5 }}184 >185 <motion.path d="M5 12.5l4.5 4.5L19 7.5" initial={{ pathLength: reduce ? 1 : 0 }} animate={{ pathLength: 1 }} transition={{ duration: 0.35, delay: 0.1 }} />186 </motion.svg>187 ) : (188 <motion.div189 key="value"190 initial={{ opacity: 0, scale: 0.8, filter: "blur(4px)" }}191 animate={{ opacity: 1, scale: 1, filter: "blur(0px)" }}192 exit={{ opacity: 0, scale: 0.8, filter: "blur(4px)" }}193 transition={{ duration: 0.25 }}194 className="font-(family-name:--font-display) leading-none font-semibold tracking-[-0.02em] text-(--color-ink) tabular-nums"195 style={{ fontSize }}196 aria-hidden197 >198 {label ?? (indeterminate ? null : (199 <>200 <motion.span>{rounded}</motion.span>201 <span className="text-[0.6em] font-medium text-(--color-neutral)">%</span>202 </>203 ))}204 </motion.div>205 )}206 </AnimatePresence>207 {sublabel ? (208 <div className="mt-1 text-(--color-neutral)" style={{ fontSize: Math.max(10, size * 0.1) }} aria-hidden>209 {sublabel}210 </div>211 ) : null}212 </div>213 </div>214 );215}