{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"donut-chart","type":"registry:component","title":"Donut Chart","description":"Donut whose arcs sweep in on springs; hovering a segment or legend row expands it and swaps the rolling centre total for that segment's share.","author":"GetUrUI <hello@geturui.dev>","dependencies":["motion"],"registryDependencies":["@geturui/number-ticker"],"files":[{"path":"registry/geturui/charts/donut-chart.tsx","type":"registry:component","target":"components/charts/donut-chart.tsx","content":"\"use client\";\n\nimport * as React from \"react\";\nimport { AnimatePresence, motion, useInView, useReducedMotion } from \"motion/react\";\nimport { cn } from \"@/lib/utils\";\nimport { NumberTicker } from \"@/components/ui/number-ticker\";\n\nexport interface DonutDatum {\n  label: string;\n  value: number;\n  color?: string;\n}\n\nexport interface DonutChartProps {\n  data: DonutDatum[];\n  /** Diameter in px. */\n  size?: number;\n  /** Ring thickness in px. */\n  thickness?: number;\n  /** Caption under the centre total. */\n  totalLabel?: string;\n  prefix?: string;\n  suffix?: string;\n  valueFormatter?: (n: number) => string;\n  title?: string;\n  className?: string;\n}\n\nconst PALETTE = [\"var(--volt)\", \"var(--info)\", \"var(--ember)\", \"var(--success)\", \"var(--warning)\", \"var(--ink-subtle)\"];\nconst GAP = 0.006;\n\n/** Donut whose arcs sweep in; hovering a segment or legend item expands it and swaps the centre total for that segment. */\nexport function DonutChart({\n  data,\n  size = 220,\n  thickness = 26,\n  totalLabel = \"Total\",\n  prefix = \"\",\n  suffix = \"\",\n  valueFormatter,\n  title,\n  className,\n}: DonutChartProps) {\n  const reduce = useReducedMotion();\n  const ref = React.useRef<HTMLDivElement>(null);\n  const inView = useInView(ref, { once: true, margin: \"-40px\" });\n  const [hidden, setHidden] = React.useState<Set<string>>(new Set());\n  const [active, setActive] = React.useState<string | null>(null);\n\n  const fmt = valueFormatter ?? ((n: number) => `${prefix}${n.toLocaleString(\"en-US\")}${suffix}`);\n  const colored = data.map((d, i) => ({ ...d, color: d.color ?? PALETTE[i % PALETTE.length] }));\n  const total = colored.reduce((a, d) => a + (hidden.has(d.label) ? 0 : Math.max(0, d.value)), 0);\n  const visibleCount = colored.filter((d) => !hidden.has(d.label) && d.value > 0).length;\n\n  const fracs = colored.map((d) => (hidden.has(d.label) || !total ? 0 : Math.max(0, d.value) / total));\n  const arcs = colored.map((d, i) => {\n    const frac = fracs[i];\n    const start = fracs.slice(0, i).reduce((x, y) => x + y, 0);\n    const gap = visibleCount > 1 && frac > 0 ? Math.min(GAP, frac / 3) : 0;\n    return { ...d, frac, start: start + gap / 2, len: Math.max(0, frac - gap) };\n  });\n\n  const r = (size - thickness - 12) / 2;\n  const c = size / 2;\n  const act = arcs.find((a) => a.label === active && a.frac > 0) ?? null;\n\n  const toggle = (label: string) =>\n    setHidden((prev) => {\n      const next = new Set(prev);\n      if (next.has(label)) next.delete(label);\n      else if (data.length - next.size > 1) next.add(label);\n      return next;\n    });\n\n  return (\n    <figure className={cn(\"flex w-full flex-col items-center gap-5 rounded-lg border border-border bg-surface-raised p-4 shadow-sm sm:flex-row sm:p-6\", className)}>\n      <div ref={ref} className=\"relative shrink-0\" style={{ width: size, height: size }} onPointerLeave={() => setActive(null)}>\n        <svg width={size} height={size} viewBox={`0 0 ${size} ${size}`} role=\"img\" aria-label={`${title ?? \"Donut chart\"}: ${arcs.filter((a) => a.frac > 0).map((a) => `${a.label} ${(a.frac * 100).toFixed(1)}%`).join(\", \")}`}>\n          <circle cx={c} cy={c} r={r} fill=\"none\" className=\"stroke-surface-sunken\" strokeWidth={thickness} />\n          <g transform={`rotate(-90 ${c} ${c})`}>\n            {arcs.map((a, i) => {\n              const on = active === a.label;\n              const dim = active !== null && !on;\n              return (\n                <motion.circle\n                  key={a.label}\n                  cx={c}\n                  cy={c}\n                  r={r}\n                  fill=\"none\"\n                  style={{ stroke: a.color }}\n                  strokeLinecap=\"butt\"\n                  initial={{ pathLength: 0, pathOffset: 0, strokeWidth: thickness, opacity: 1 }}\n                  animate={\n                    inView\n                      ? { pathLength: a.len, pathOffset: a.start, strokeWidth: on ? thickness + 10 : thickness, opacity: dim ? 0.35 : 1 }\n                      : undefined\n                  }\n                  transition={{\n                    pathLength: reduce ? { duration: 0 } : { type: \"spring\", stiffness: 90, damping: 20, delay: hidden.size ? 0 : i * 0.08 },\n                    pathOffset: reduce ? { duration: 0 } : { type: \"spring\", stiffness: 90, damping: 20, delay: hidden.size ? 0 : i * 0.08 },\n                    strokeWidth: { type: \"spring\", stiffness: 500, damping: 28 },\n                    opacity: { duration: 0.2 },\n                  }}\n                  onPointerEnter={() => a.frac > 0 && setActive(a.label)}\n                  className=\"cursor-pointer\"\n                />\n              );\n            })}\n          </g>\n        </svg>\n        <div className=\"pointer-events-none absolute inset-0 grid place-items-center text-center\">\n          <AnimatePresence mode=\"popLayout\" initial={false}>\n            <motion.div\n              key={act ? act.label : \"total\"}\n              initial={{ opacity: 0, filter: \"blur(8px)\", scale: 0.94 }}\n              animate={{ opacity: 1, filter: \"blur(0px)\", scale: 1 }}\n              exit={{ opacity: 0, filter: \"blur(8px)\", scale: 0.94 }}\n              transition={{ type: \"spring\", stiffness: 420, damping: 32 }}\n              className=\"col-start-1 row-start-1 flex flex-col items-center\"\n              aria-live=\"polite\"\n            >\n              {act ? (\n                <>\n                  <span className=\"font-display text-[26px] font-semibold tracking-[-0.5px] text-ink tabular-nums\">{(act.frac * 100).toFixed(1)}%</span>\n                  <span className=\"mt-1 max-w-[120px] truncate text-[12px] text-ink-muted\">{act.label}</span>\n                  <span className=\"text-[11.5px] tabular-nums text-ink-subtle\">{fmt(act.value)}</span>\n                </>\n              ) : (\n                <>\n                  <NumberTicker value={total} prefix={prefix} suffix={suffix} className=\"font-display text-[28px] font-semibold tracking-[-0.6px] text-ink\" />\n                  <span className=\"mt-1.5 text-[12px] text-ink-muted\">{totalLabel}</span>\n                </>\n              )}\n            </motion.div>\n          </AnimatePresence>\n        </div>\n      </div>\n\n      <div className=\"w-full min-w-0 flex-1\">\n        {title && <figcaption className=\"mb-3 text-[14.5px] font-semibold text-ink\">{title}</figcaption>}\n        <ul className=\"space-y-1\" aria-label=\"Segments\">\n          {arcs.map((a) => {\n            const on = !hidden.has(a.label);\n            return (\n              <li key={a.label}>\n                <button\n                  type=\"button\"\n                  aria-pressed={on}\n                  onClick={() => toggle(a.label)}\n                  onPointerEnter={() => on && setActive(a.label)}\n                  onPointerLeave={() => setActive(null)}\n                  onFocus={() => on && setActive(a.label)}\n                  onBlur={() => setActive(null)}\n                  className={cn(\n                    \"flex w-full items-center gap-2.5 rounded-sm px-2.5 py-1.5 text-left text-[13px] outline-none transition-colors focus-visible:shadow-[var(--focus-ring)]\",\n                    active === a.label ? \"bg-surface-hover\" : \"hover:bg-surface-hover\",\n                    !on && \"opacity-55\",\n                  )}\n                >\n                  <span\n                    aria-hidden\n                    className={cn(\"size-2.5 shrink-0 rounded-[3px] transition-transform duration-300 ease-[var(--ease-spring)]\", !on && \"scale-50\")}\n                    style={{ background: a.color }}\n                  />\n                  <span className={cn(\"min-w-0 flex-1 truncate\", on ? \"text-ink\" : \"text-ink-subtle line-through\")}>{a.label}</span>\n                  <span className=\"font-mono text-[12px] tabular-nums text-ink-subtle\">{fmt(a.value)}</span>\n                  <span className=\"w-12 text-right font-mono text-[12px] font-medium tabular-nums text-ink\">{on ? `${(a.frac * 100).toFixed(1)}%` : \"—\"}</span>\n                </button>\n              </li>\n            );\n          })}\n        </ul>\n      </div>\n    </figure>\n  );\n}\n"},{"path":"LICENSE-geturui.md","type":"registry:file","target":"LICENSE-geturui.md","content":"MIT License\n\nCopyright (c) 2026 geturui.com\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n"}],"categories":["charts","chart","donut","pie","share","breakdown","svg"],"meta":{"tier":"free","license":"MIT","docs":"https://geturui.com/docs/donut-chart"}}