{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "gauge",
  "title": "Gauge",
  "description": "Semi-circular gauge with Core-tone bands whose needle springs to the value with a small overshoot (or an arc that fills), a rolling readout and a band label pill, exposed as a meter.",
  "dependencies": [
    "motion"
  ],
  "registryDependencies": [
    "https://goodui.dev/r/locale.json",
    "https://goodui.dev/r/number-flow.json"
  ],
  "files": [
    {
      "path": "registry/components/charts/gauge.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport { useMotionValueEvent, useReducedMotion, useSpring } from \"motion/react\"\nimport { cn } from \"@/lib/utils\"\nimport { NumberFlow } from \"@/components/goodui/number-flow\"\nimport { useLabels } from \"@/lib/goodui-locale\"\n\nexport type GaugeTone = \"primary\" | \"neutral\" | \"success\" | \"warning\" | \"danger\" | \"info\"\n\nexport type GaugeBand = {\n  from: number\n  to: number\n  /** Core tone of the band. */\n  tone?: GaugeTone\n  /** Any CSS color, overrides the tone. */\n  color?: string\n  /** Shown as a pill under the value while the needle is in this band. */\n  label?: string\n}\n\nexport type GaugeLabels = {\n  /** Accessible name when `label` is not a string. */\n  name: string\n  /** aria-valuetext and the live announcement. */\n  valueText: (value: string, band: string | null) => string\n}\nconst DEFAULT_LABELS: GaugeLabels = {\n  name: \"Gauge\",\n  valueText: (value, band) => (band ? `${value}, ${band}` : value),\n}\n\nexport type GaugeProps = Omit<React.ComponentProps<\"div\">, \"children\"> & {\n  value: number\n  min?: number\n  max?: number\n  /** Colored ranges along the arc, e.g. healthy / watch / critical. Drawn with 2px gaps. */\n  bands?: GaugeBand[]\n  /** needle: a needle springs to the value over the bands. arc: the arc fills to the value in the band's color. */\n  variant?: \"needle\" | \"arc\"\n  /** Width in px; the gauge is half as tall plus the readout. */\n  size?: number\n  /** Arc thickness in px. */\n  thickness?: number\n  /** Metric name under the value; also the accessible name when it is a string. */\n  label?: React.ReactNode\n  /** Color of the needle (needle) or of the arc when no band covers the value (arc). */\n  color?: string\n  /** Min and max printed under the arc ends. */\n  showRange?: boolean\n  /** Intl.NumberFormat options for the value, e.g. { style: \"percent\" } with values 0..1. */\n  format?: Intl.NumberFormatOptions\n  locale?: string\n  formatValue?: (value: number) => string\n  /** Text for built-in controls and screen readers; pass translations here. */\n  labels?: Partial<GaugeLabels>\n}\n\nconst CSS = `\n.pc-gauge{--pc-gauge-primary:var(--primary);--pc-gauge-neutral:color-mix(in oklab,var(--foreground) 55%,transparent);--pc-gauge-success:oklch(0.53 0.13 155);--pc-gauge-warning:oklch(0.75 0.16 70);--pc-gauge-danger:oklch(0.56 0.21 27);--pc-gauge-info:oklch(0.55 0.17 250)}\n.dark .pc-gauge{--pc-gauge-success:oklch(0.7 0.16 155);--pc-gauge-danger:oklch(0.68 0.2 25);--pc-gauge-info:oklch(0.7 0.15 245)}\n.pc-gauge-needle{transition:none}\n`\n\nfunction useReduce() {\n  const prefers = useReducedMotion()\n  const [mounted, setMounted] = React.useState(false)\n  React.useEffect(() => setMounted(true), [])\n  return mounted && !!prefers\n}\n\nfunction useInView(ref: React.RefObject<Element | null>, threshold = 0.2) {\n  const [inView, setInView] = React.useState(false)\n  React.useEffect(() => {\n    const el = ref.current\n    if (!el || inView) return\n    if (typeof IntersectionObserver === \"undefined\") {\n      setInView(true)\n      return\n    }\n    const io = new IntersectionObserver(\n      ([e]) => {\n        if (e?.isIntersecting) {\n          setInView(true)\n          io.disconnect()\n        }\n      },\n      { threshold },\n    )\n    io.observe(el)\n    return () => io.disconnect()\n  }, [ref, inView, threshold])\n  return inView\n}\n\nconst polar = (cx: number, cy: number, r: number, deg: number) => ({ x: cx + r * Math.cos((deg * Math.PI) / 180), y: cy + r * Math.sin((deg * Math.PI) / 180) })\n// Arc along the top half, from 180° (start side) clockwise to 360° (end side).\nfunction arcPath(cx: number, cy: number, r: number, a0: number, a1: number) {\n  const p0 = polar(cx, cy, r, a0)\n  const p1 = polar(cx, cy, r, a1)\n  return `M${p0.x.toFixed(2)},${p0.y.toFixed(2)}A${r},${r} 0 ${a1 - a0 > 180 ? 1 : 0} 1 ${p1.x.toFixed(2)},${p1.y.toFixed(2)}`\n}\n\n/** Semi-circular gauge with tone bands, a needle that springs (and overshoots a little) to the value, and a rolling readout. */\nexport function Gauge({\n  value,\n  min = 0,\n  max = 100,\n  bands = [],\n  variant = \"needle\",\n  size = 240,\n  thickness = 14,\n  label,\n  color,\n  showRange = true,\n  format,\n  locale = \"en-US\",\n  formatValue,\n  labels,\n  className,\n  \"aria-label\": ariaLabel,\n  ...props\n}: GaugeProps) {\n  const l = useLabels(\"gauge\", DEFAULT_LABELS, labels)\n  const reduce = useReduce()\n  const rootRef = React.useRef<HTMLDivElement>(null)\n  const inView = useInView(rootRef)\n  const fmt = React.useMemo(() => formatValue ?? ((v: number) => new Intl.NumberFormat(locale, format).format(v)), [formatValue, locale, format])\n\n  const span = max - min || 1\n  const ratio = Math.min(1, Math.max(0, (value - min) / span))\n  const targetDeg = -90 + ratio * 180\n  // The needle angle lives in a spring; React only re-renders with the rounded value.\n  const spring = useSpring(-90, { stiffness: 70, damping: 11, mass: 0.9 })\n  const [deg, setDeg] = React.useState(-90)\n  useMotionValueEvent(spring, \"change\", (v) => setDeg(v))\n  React.useEffect(() => {\n    if (!inView && !reduce) return\n    if (reduce) spring.jump(targetDeg)\n    else spring.set(targetDeg)\n  }, [inView, reduce, targetDeg, spring])\n  const ready = inView || reduce\n\n  const pad = 6\n  const r = (size - thickness) / 2 - pad\n  const cx = size / 2\n  const cy = r + thickness / 2 + pad\n  const height = cy + (showRange ? 22 : 8)\n  const gapDeg = (2 / r) * (180 / Math.PI)\n  const angleOf = (v: number) => 180 + (Math.min(1, Math.max(0, (v - min) / span)) * 180)\n  const toneColor = (b: GaugeBand) => b.color ?? `var(--pc-gauge-${b.tone ?? \"primary\"})`\n  const current = bands.find((b) => value >= b.from && value <= b.to) ?? bands.find((b) => value >= b.from && value < b.to)\n  const arcColor = color ?? (current ? toneColor(current) : \"var(--chart-1)\")\n  const name = typeof label === \"string\" ? label : l.name\n  const valueText = l.valueText(fmt(value), current?.label ?? null)\n  const shownRatio = ready ? ratio : 0\n  const fillLength = Math.max(0.0001, (reduce ? ratio : (deg + 90) / 180))\n  // Arc variant: bands become a thin ring at the rim and the value arc sits inside it with a 4px gap.\n  const bandW = Math.max(3, thickness * 0.3)\n  const arcR = variant === \"arc\" && bands.length > 0 ? r - bandW / 2 - 4 - thickness / 2 : r\n\n  return (\n    <div\n      ref={rootRef}\n      role=\"meter\"\n      aria-label={ariaLabel ?? name}\n      aria-valuenow={value}\n      aria-valuemin={min}\n      aria-valuemax={max}\n      aria-valuetext={valueText}\n      className={cn(\"pc-gauge inline-flex flex-col items-center\", className)}\n      style={{ width: size }}\n      {...props}\n    >\n      <style href=\"pc-gauge\" precedence=\"default\">\n        {CSS}\n      </style>\n      <svg width={size} height={height} viewBox={`0 0 ${size} ${height}`} className=\"overflow-visible\" aria-hidden=\"true\">\n        {bands.length === 0 && <path d={arcPath(cx, cy, r, 180, 360)} fill=\"none\" strokeWidth={thickness} strokeLinecap=\"butt\" className=\"stroke-muted\" />}\n        {bands.map((b, i) => {\n          const a0 = angleOf(b.from) + (i === 0 ? 0 : gapDeg / 2)\n          const a1 = angleOf(b.to) - (i === bands.length - 1 ? 0 : gapDeg / 2)\n          if (a1 <= a0) return null\n          const isCurrent = current === b\n          return <path key={i} d={arcPath(cx, cy, r, a0, a1)} fill=\"none\" strokeWidth={variant === \"arc\" ? bandW : thickness} style={{ stroke: toneColor(b), opacity: variant === \"needle\" ? (isCurrent ? 1 : 0.4) : 0.9 }} strokeLinecap=\"butt\" />\n        })}\n        {variant === \"arc\" && (\n          <>\n            {bands.length > 0 && <path d={arcPath(cx, cy, arcR, 180, 360)} fill=\"none\" strokeWidth={thickness} strokeLinecap=\"round\" className=\"stroke-muted\" />}\n            <path\n              d={arcPath(cx, cy, arcR, 180, 360)}\n              fill=\"none\"\n              strokeWidth={thickness}\n              strokeLinecap=\"round\"\n              pathLength={1}\n              strokeDasharray={`${fillLength} 1`}\n              style={{ stroke: arcColor, transition: reduce ? \"none\" : undefined }}\n            />\n          </>\n        )}\n        {variant === \"needle\" && (\n          <g transform={`rotate(${deg} ${cx} ${cy})`} className=\"pc-gauge-needle\">\n            <path d={`M${cx - 3.5},${cy} L${cx},${cy - r + thickness / 2 + 4} L${cx + 3.5},${cy} Z`} style={{ fill: color ?? \"var(--foreground)\" }} strokeLinejoin=\"round\" />\n            <circle cx={cx} cy={cy} r={6} style={{ fill: color ?? \"var(--foreground)\" }} />\n            <circle cx={cx} cy={cy} r={2.5} className=\"fill-background\" />\n          </g>\n        )}\n        {showRange && (\n          <>\n            <text x={cx - r} y={cy + 16} textAnchor=\"middle\" className=\"fill-muted-foreground text-[10px] tabular-nums\">\n              {fmt(min)}\n            </text>\n            <text x={cx + r} y={cy + 16} textAnchor=\"middle\" className=\"fill-muted-foreground text-[10px] tabular-nums\">\n              {fmt(max)}\n            </text>\n          </>\n        )}\n      </svg>\n      <div className=\"-mt-1 flex flex-col items-center gap-1 text-center\" aria-hidden=\"true\">\n        <NumberFlow value={ready ? value : min} format={format} locale={locale} className=\"text-3xl leading-none font-semibold tracking-tight\" />\n        {label !== undefined && <span className=\"text-xs text-muted-foreground\">{label}</span>}\n        {current?.label && (\n          <span className=\"inline-flex items-center gap-1.5 rounded-full border px-2 py-0.5 text-[11px] font-medium\" style={{ opacity: shownRatio > 0 || reduce ? 1 : 0, transition: \"opacity .3s\" }}>\n            <span className=\"size-1.5 rounded-full\" style={{ background: toneColor(current) }} />\n            {current.label}\n          </span>\n        )}\n      </div>\n      <span className=\"sr-only\" aria-live=\"polite\">\n        {valueText}\n      </span>\n    </div>\n  )\n}\n",
      "type": "registry:component",
      "target": "components/goodui/gauge.tsx"
    }
  ],
  "categories": [
    "components",
    "charts"
  ],
  "type": "registry:component"
}
