{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "radial-gauge",
  "type": "registry:component",
  "title": "Radial Gauge",
  "description": "A dial with threshold bands and a target notch: the needle springs to each new reading, the fill takes the colour of the band it lands in, and hovering a band names its range.",
  "categories": [
    "component",
    "Numbers"
  ],
  "dependencies": [
    "motion"
  ],
  "registryDependencies": [
    "utils"
  ],
  "files": [
    {
      "path": "registry/devl/radial-gauge.tsx",
      "type": "registry:component",
      "content": "\"use client\";\n\nimport { motion, useReducedMotion, useSpring, useTransform } from \"motion/react\";\nimport { type ReactNode, useEffect, useState } from \"react\";\nimport { cn } from \"@/lib/utils\";\n\nexport type GaugeTone = \"success\" | \"warning\" | \"destructive\" | \"info\";\n\nexport interface GaugeBand {\n  to: number;\n  label: string;\n  tone: GaugeTone;\n}\n\nexport interface RadialGaugeProps {\n  value: number;\n  min?: number;\n  max?: number;\n  target?: number;\n  bands?: GaugeBand[];\n  label?: string;\n  unit?: string;\n  caption?: string;\n  action?: ReactNode;\n  children?: ReactNode;\n  className?: string;\n}\n\nconst W = 260;\nconst H = 178;\nconst CX = W / 2;\nconst CY = 124;\nconst R = 92;\nconst SWEEP = 240;\nconst DIGITS = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];\nconst TRACK = 10;\nconst CAP = ((TRACK / 2 / R) * 180) / Math.PI;\n\nconst TONE: Record<GaugeTone, { stroke: string; text: string; bg: string }> = {\n  success: { stroke: \"stroke-success\", text: \"text-success-foreground\", bg: \"bg-success/10 dark:bg-success/16\" },\n  warning: { stroke: \"stroke-warning\", text: \"text-warning-foreground\", bg: \"bg-warning/10 dark:bg-warning/16\" },\n  destructive: { stroke: \"stroke-destructive\", text: \"text-destructive-foreground\", bg: \"bg-destructive/10 dark:bg-destructive/16\" },\n  info: { stroke: \"stroke-info\", text: \"text-info-foreground\", bg: \"bg-info/10 dark:bg-info/16\" },\n};\n\nconst defaultBands: GaugeBand[] = [\n  { to: 70, label: \"Healthy\", tone: \"success\" },\n  { to: 85, label: \"Busy\", tone: \"warning\" },\n  { to: 100, label: \"Saturated\", tone: \"destructive\" },\n];\n\nfunction RollingNumber({ text, className }: { text: string; className?: string }) {\n  const reduce = useReducedMotion();\n  let d = 0;\n  let o = 0;\n  const glyphs = [...text]\n    .reverse()\n    .map((ch) => {\n      const isDigit = ch >= \"0\" && ch <= \"9\";\n      return { ch, key: isDigit ? `d${d++}` : `s${o++}${ch}`, digit: isDigit ? Number(ch) : null };\n    })\n    .reverse();\n  return (\n    <span className={cn(\"relative inline-flex whitespace-nowrap tabular-nums leading-none\", className)}>\n      <span className=\"sr-only\">{text}</span>\n      <span aria-hidden className=\"inline-flex [--row:1.15em]\">\n        {glyphs.map((g) =>\n          g.digit === null ? (\n            <span className=\"inline-block h-(--row) leading-(--row)\" key={g.key}>\n              {g.ch}\n            </span>\n          ) : (\n            <span\n              className=\"relative inline-block h-(--row) overflow-hidden\"\n              key={g.key}\n              style={{ maskImage: \"linear-gradient(transparent, black 18%, black 82%, transparent)\" }}\n            >\n              <span className=\"invisible block leading-(--row)\">0</span>\n              <motion.span\n                animate={{ y: `${-g.digit * 10}%` }}\n                className=\"absolute inset-x-0 top-0 flex flex-col items-center\"\n                initial={false}\n                transition={reduce ? { duration: 0 } : { type: \"spring\", stiffness: 260, damping: 30 }}\n              >\n                {DIGITS.map((n) => (\n                  <span className=\"block h-(--row) leading-(--row)\" key={n}>\n                    {n}\n                  </span>\n                ))}\n              </motion.span>\n            </span>\n          ),\n        )}\n      </span>\n    </span>\n  );\n}\n\nconst rad = (deg: number) => (deg * Math.PI) / 180;\nconst round = (n: number) => Math.round(n * 100) / 100;\nconst pt = (r: number, deg: number) => [round(CX + r * Math.sin(rad(deg))), round(CY - r * Math.cos(rad(deg)))] as const;\nfunction arcPath(r: number, d0: number, d1: number) {\n  if (d1 - d0 < 0.01) return \"\";\n  const [x0, y0] = pt(r, d0);\n  const [x1, y1] = pt(r, d1);\n  return `M${x0.toFixed(2)} ${y0.toFixed(2)} A${r} ${r} 0 ${d1 - d0 > 180 ? 1 : 0} 1 ${x1.toFixed(2)} ${y1.toFixed(2)}`;\n}\n\nexport function RadialGauge({\n  value,\n  min = 0,\n  max = 100,\n  target,\n  bands = defaultBands,\n  label = \"CPU\",\n  unit = \"%\",\n  caption,\n  action,\n  children,\n  className,\n}: RadialGaugeProps) {\n  const reduce = useReducedMotion();\n  const [hoverBand, setHoverBand] = useState<number | null>(null);\n  const clamp = (v: number) => Math.max(min, Math.min(max, v));\n  const deg = (v: number) => -SWEEP / 2 + ((clamp(v) - min) / (max - min || 1)) * SWEEP;\n\n  const spring = useSpring(min, { stiffness: 120, damping: 19, mass: 1 });\n  useEffect(() => {\n    if (reduce) spring.jump(value);\n    else spring.set(value);\n  }, [value, reduce, spring]);\n\n  const needle = useTransform(spring, (v) => {\n    const a = deg(v);\n    const [tx, ty] = pt(R - 18, a);\n    const [lx, ly] = pt(3.2, a - 90);\n    const [rx, ry] = pt(3.2, a + 90);\n    const [bx, by] = pt(12, a + 180);\n    return `M${bx.toFixed(2)} ${by.toFixed(2)} L${lx.toFixed(2)} ${ly.toFixed(2)} L${tx.toFixed(2)} ${ty.toFixed(2)} L${rx.toFixed(2)} ${ry.toFixed(2)} Z`;\n  });\n  const fill = useTransform(spring, (v) => arcPath(R, -SWEEP / 2 + CAP, Math.max(-SWEEP / 2 + CAP + 0.02, deg(v) - CAP)));\n\n  const bandIndex = Math.max(0, bands.findIndex((b) => clamp(value) <= b.to));\n  const band = bands[bandIndex] ?? bands[bands.length - 1];\n  const tone = TONE[band.tone];\n  const ticks = Array.from({ length: 21 }, (_, i) => min + ((max - min) * i) / 20);\n  const shown = hoverBand === null ? null : bands[hoverBand];\n  const gap = target === undefined ? null : value - target;\n\n  return (\n    <section\n      aria-label={`${label} gauge`}\n      className={cn(\"w-full max-w-[340px] rounded-2xl border bg-card p-5 text-card-foreground shadow-xs/5\", className)}\n    >\n      <header className=\"mb-3 flex items-center justify-between gap-3\">\n        <div className=\"flex min-w-0 flex-col\">\n          <h3 className=\"truncate font-medium text-sm\">{label}</h3>\n          {caption ? <span className=\"truncate text-muted-foreground text-xs\">{caption}</span> : null}\n        </div>\n        {action}\n      </header>\n      <div\n        aria-label={label}\n        aria-valuemax={max}\n        aria-valuemin={min}\n        aria-valuenow={Math.round(value)}\n        aria-valuetext={`${Math.round(value)}${unit}, ${band.label}${target !== undefined ? `, target ${target}${unit}` : \"\"}`}\n        className=\"relative mx-auto\"\n        role=\"meter\"\n        style={{ width: W, maxWidth: \"100%\" }}\n      >\n        <svg aria-hidden className=\"block h-auto w-full overflow-visible\" viewBox={`0 0 ${W} ${H}`}>\n          {bands.map((b, i) => {\n            const from = i === 0 ? min : bands[i - 1].to;\n            const d0 = deg(from) + (i === 0 ? 0 : 1.2);\n            const d1 = deg(b.to) - (i === bands.length - 1 ? 0 : 1.2);\n            return (\n              <g key={b.label} onPointerEnter={() => setHoverBand(i)} onPointerLeave={() => setHoverBand(null)}>\n                <path className=\"fill-none stroke-transparent\" d={arcPath(R + 13, d0, d1)} strokeWidth={14} />\n                <path\n                  className={cn(\"fill-none transition-[opacity,stroke-width] duration-150\", TONE[b.tone].stroke)}\n                  d={arcPath(R + 13, d0, d1)}\n                  opacity={hoverBand === null || hoverBand === i ? 0.9 : 0.35}\n                  strokeLinecap=\"butt\"\n                  strokeWidth={hoverBand === i ? 5 : 3}\n                />\n              </g>\n            );\n          })}\n\n          {ticks.map((t, i) => {\n            const major = i % 5 === 0;\n            const [x0, y0] = pt(R - 12, deg(t));\n            const [x1, y1] = pt(R - (major ? 19 : 15), deg(t));\n            return (\n              <line\n                className={major ? \"stroke-muted-foreground/60\" : \"stroke-muted-foreground/25\"}\n                key={t}\n                strokeLinecap=\"round\"\n                strokeWidth={major ? 1.5 : 1}\n                x1={x0}\n                x2={x1}\n                y1={y0}\n                y2={y1}\n              />\n            );\n          })}\n          {ticks\n            .filter((_, i) => i % 5 === 0)\n            .map((t) => {\n              const [x, y] = pt(R - 30, deg(t));\n              return (\n                <text\n                  className=\"fill-muted-foreground text-[10px] tabular-nums\"\n                  dominantBaseline=\"central\"\n                  key={t}\n                  textAnchor=\"middle\"\n                  x={x}\n                  y={y}\n                >\n                  {Math.round(t)}\n                </text>\n              );\n            })}\n\n          <path\n            className=\"fill-none stroke-muted\"\n            d={arcPath(R, -SWEEP / 2 + CAP, SWEEP / 2 - CAP)}\n            strokeLinecap=\"round\"\n            strokeWidth={TRACK}\n          />\n          <motion.path\n            className={cn(\"fill-none transition-[stroke] duration-300\", tone.stroke)}\n            d={fill}\n            strokeLinecap=\"round\"\n            strokeWidth={TRACK}\n          />\n\n          {target !== undefined ? (\n            <g>\n              {(() => {\n                const a = deg(target);\n                const [x0, y0] = pt(R - 8, a);\n                const [x1, y1] = pt(R + 17, a);\n                return (\n                  <>\n                    <line className=\"stroke-card\" strokeLinecap=\"round\" strokeWidth={5} x1={x0} x2={x1} y1={y0} y2={y1} />\n                    <line className=\"stroke-foreground\" strokeLinecap=\"round\" strokeWidth={2} x1={x0} x2={x1} y1={y0} y2={y1} />\n                  </>\n                );\n              })()}\n            </g>\n          ) : null}\n\n          <motion.path className=\"fill-foreground\" d={needle} />\n          <circle className=\"fill-card stroke-foreground\" cx={CX} cy={CY} r={5.5} strokeWidth={2} />\n        </svg>\n\n        <div className=\"pointer-events-none absolute inset-x-0 bottom-0 flex flex-col items-center\">\n          <div className=\"flex items-baseline gap-0.5\">\n            <RollingNumber className=\"font-semibold text-3xl tracking-tight\" text={String(Math.round(value))} />\n            <span className=\"font-medium text-muted-foreground text-sm\">{unit}</span>\n          </div>\n        </div>\n      </div>\n\n      <div className=\"mt-2 flex items-center justify-center gap-2 text-xs\">\n        <span\n          className={cn(\n            \"inline-flex items-center gap-1.5 rounded-full px-2 py-0.5 font-medium transition-colors duration-300\",\n            (shown ? TONE[shown.tone] : tone).bg,\n            (shown ? TONE[shown.tone] : tone).text,\n          )}\n        >\n          <span className={cn(\"size-1.5 rounded-full bg-current\")} />\n          {shown\n            ? `${shown.label} · ${hoverBand === 0 ? min : bands[(hoverBand ?? 1) - 1].to}–${shown.to}${unit}`\n            : band.label}\n        </span>\n        {gap !== null && !shown ? (\n          <span className=\"text-muted-foreground tabular-nums\">\n            {Math.abs(gap) < 0.5\n              ? \"On target\"\n              : `${Math.abs(Math.round(gap))}${unit === \"%\" ? \" pts\" : unit} ${gap > 0 ? \"over\" : \"under\"} target`}\n          </span>\n        ) : null}\n      </div>\n\n      {children ? <div className=\"mt-4\">{children}</div> : null}\n    </section>\n  );\n}\n"
    }
  ]
}