{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"tank-gauge","type":"registry:ui","title":"Tank Gauge","description":"Round liquid gauge whose level springs to each new value and sloshes side to side before settling, with the readout inverting where the liquid covers it. Drag or key it to set the level.","dependencies":[],"registryDependencies":[],"files":[{"path":"registry/ui/tank-gauge.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useEffect, useId, useRef, useState, type KeyboardEvent, type PointerEvent } from \"react\";\n\nexport interface TankGaugeProps {\n  /** Controlled value, 0-100. */\n  value?: number;\n  /** Initial value when uncontrolled. */\n  defaultValue?: number;\n  /** Called when the user drags or uses the keyboard. Omit both this and `value` for a free-running control. */\n  onChange?: (value: number) => void;\n  /** Liquid color. */\n  color?: string;\n  /** Diameter in px. */\n  size?: number;\n  /** How much the liquid sloshes, 0-2. */\n  slosh?: number;\n  /** Let the user drag or key the level. */\n  interactive?: boolean;\n  label?: string;\n  className?: string;\n}\n\nconst W = 200;\nconst R = 88;\n\nexport function TankGauge({\n  value,\n  defaultValue = 62,\n  onChange,\n  color = \"#38bdf8\",\n  size = 208,\n  slosh = 1,\n  interactive = true,\n  label = \"Tank level\",\n  className,\n}: TankGaugeProps) {\n  const uid = useId().replace(/[^a-zA-Z0-9_-]/g, \"\");\n  const [inner, setInner] = useState(defaultValue);\n  const current = Math.max(0, Math.min(100, value ?? inner));\n  const target = useRef(current / 100);\n  const sloshRef = useRef(slosh);\n  const root = useRef<HTMLDivElement>(null);\n  const front = useRef<SVGPathElement>(null);\n  const back = useRef<SVGPathElement>(null);\n  const clip = useRef<SVGPathElement>(null);\n\n  useEffect(() => {\n    target.current = current / 100;\n    sloshRef.current = slosh;\n  }, [current, slosh]);\n\n  useEffect(() => {\n    const el = root.current;\n    if (!el) return;\n    const reduce = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    const top = W / 2 - R;\n    let level = target.current; // 0-1\n    let vel = 0;\n    let amp = 0; // standing-wave amplitude, px\n    let ampV = 0;\n    let t = 0;\n    let raf = 0;\n    let last = 0;\n\n    const draw = () => {\n      const base = top + 2 * R * (1 - level);\n      const ripple = reduce ? 0 : 2.2 + Math.abs(amp) * 0.12;\n      let f = \"\";\n      let b = \"\";\n      for (let x = 0; x <= W; x += 8) {\n        const tilt = amp * Math.cos((Math.PI * x) / W);\n        const yf = base + tilt + ripple * Math.sin(x * 0.045 + t * 2.4);\n        const yb = base - tilt * 0.8 + ripple * 1.2 * Math.sin(x * 0.06 - t * 1.7 + 1.3) - 3;\n        f += `${x ? \"L\" : \"M\"}${x} ${yf.toFixed(2)}`;\n        b += `${x ? \"L\" : \"M\"}${x} ${yb.toFixed(2)}`;\n      }\n      f += `L${W} ${W}L0 ${W}Z`;\n      b += `L${W} ${W}L0 ${W}Z`;\n      front.current?.setAttribute(\"d\", f);\n      clip.current?.setAttribute(\"d\", f);\n      back.current?.setAttribute(\"d\", b);\n    };\n\n    const tick = (now: number) => {\n      const dt = Math.min(0.033, (now - (last || now)) / 1000);\n      last = now;\n      t += dt;\n      // Level: a slightly underdamped spring toward the target.\n      const acc = 70 * (target.current - level) - 9 * vel;\n      vel += acc * dt;\n      level += vel * dt;\n      // Slosh: a damped oscillator kicked by the level's acceleration.\n      const ampA = -110 * amp - 2.4 * ampV + acc * 60 * sloshRef.current;\n      ampV += ampA * dt;\n      amp = Math.max(-40, Math.min(40, amp + ampV * dt));\n      draw();\n      raf = requestAnimationFrame(tick);\n    };\n\n    if (reduce) {\n      // Static frame, redrawn whenever the target changes.\n      const id = window.setInterval(() => {\n        if (level !== target.current) {\n          level = target.current;\n          draw();\n        }\n      }, 100);\n      draw();\n      return () => window.clearInterval(id);\n    }\n\n    const io = new IntersectionObserver(([e]) => {\n      cancelAnimationFrame(raf);\n      last = 0;\n      if (e.isIntersecting) raf = requestAnimationFrame(tick);\n    });\n    io.observe(el);\n    draw();\n    return () => {\n      io.disconnect();\n      cancelAnimationFrame(raf);\n    };\n  }, []);\n\n  const set = (v: number) => {\n    const next = Math.round(Math.max(0, Math.min(100, v)));\n    if (next === current) return;\n    if (value === undefined) setInner(next);\n    onChange?.(next);\n  };\n\n  const fromPointer = (e: PointerEvent<HTMLDivElement>) => {\n    const r = e.currentTarget.getBoundingClientRect();\n    const y = ((e.clientY - r.top) / r.height) * W;\n    set(((W / 2 + R - y) / (2 * R)) * 100);\n  };\n\n  const onKey = (e: KeyboardEvent) => {\n    const map: Record<string, number> = {\n      ArrowUp: current + 5,\n      ArrowRight: current + 5,\n      ArrowDown: current - 5,\n      ArrowLeft: current - 5,\n      PageUp: current + 20,\n      PageDown: current - 20,\n      Home: 0,\n      End: 100,\n    };\n    if (!(e.key in map)) return;\n    e.preventDefault();\n    set(map[e.key]);\n  };\n\n  const vessel = `v${uid}`;\n  const wet = `w${uid}`;\n\n  return (\n    <div\n      ref={root}\n      role={interactive ? \"slider\" : \"meter\"}\n      tabIndex={interactive ? 0 : undefined}\n      aria-label={label}\n      aria-orientation={interactive ? \"vertical\" : undefined}\n      aria-valuemin={0}\n      aria-valuemax={100}\n      aria-valuenow={current}\n      aria-valuetext={`${current}%`}\n      onKeyDown={interactive ? onKey : undefined}\n      onPointerDown={\n        interactive\n          ? (e) => {\n              e.currentTarget.setPointerCapture(e.pointerId);\n              fromPointer(e);\n            }\n          : undefined\n      }\n      onPointerMove={interactive ? (e) => e.currentTarget.hasPointerCapture(e.pointerId) && fromPointer(e) : undefined}\n      className={`relative touch-none select-none rounded-full outline-none focus-visible:ring-[3px] focus-visible:ring-ring/60 focus-visible:ring-offset-4 focus-visible:ring-offset-background ${interactive ? \"cursor-ns-resize\" : \"\"} ${className ?? \"\"}`}\n      style={{ width: size, height: size }}\n    >\n      <svg viewBox={`0 0 ${W} ${W}`} className=\"size-full overflow-visible\" aria-hidden>\n        <defs>\n          <clipPath id={vessel}>\n            <circle cx={W / 2} cy={W / 2} r={R} />\n          </clipPath>\n          <clipPath id={wet}>\n            <path ref={clip} />\n          </clipPath>\n          <linearGradient id={`g${uid}`} x1=\"0\" y1=\"0\" x2=\"0\" y2=\"1\">\n            <stop offset=\"0\" stopColor={color} stopOpacity=\"0.95\" />\n            <stop offset=\"1\" stopColor={color} stopOpacity=\"0.7\" />\n          </linearGradient>\n        </defs>\n        <circle cx={W / 2} cy={W / 2} r={R + 7} className=\"fill-card stroke-border\" strokeWidth=\"1.5\" />\n        <circle cx={W / 2} cy={W / 2} r={R} className=\"fill-muted\" />\n        <g clipPath={`url(#${vessel})`}>\n          <path ref={back} fill={color} opacity=\"0.4\" />\n          <path ref={front} fill={`url(#g${uid})`} />\n        </g>\n        <text x={W / 2} y={W / 2 + 12} textAnchor=\"middle\" className=\"fill-foreground text-[40px] font-semibold tabular-nums\">\n          {current}\n          <tspan className=\"text-[20px] fill-muted-foreground\">%</tspan>\n        </text>\n        <g clipPath={`url(#${vessel})`}>\n          <text x={W / 2} y={W / 2 + 12} textAnchor=\"middle\" clipPath={`url(#${wet})`} className=\"fill-white text-[40px] font-semibold tabular-nums\">\n            {current}\n            <tspan className=\"text-[20px] fill-white/70\">%</tspan>\n          </text>\n        </g>\n        {/* Glass glint. */}\n        <path d={`M${W / 2 - 58} ${W / 2 - 48} A ${R - 14} ${R - 14} 0 0 1 ${W / 2 - 18} ${W / 2 - 72}`} fill=\"none\" stroke=\"white\" strokeOpacity=\".35\" strokeWidth=\"6\" strokeLinecap=\"round\" />\n      </svg>\n    </div>\n  );\n}\n"}]}