{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "liquid-fill-bar-chart",
  "title": "Liquid Fill Bar Chart",
  "description": "Usage limits as bars of liquid: gentle waves on every surface, levels that spring to new values with a slosh, and a warning line near the top.",
  "dependencies": [],
  "files": [
    {
      "path": "registry/new-york/liquid-fill-bar-chart/liquid-fill-bar-chart.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface LiquidBar {\n  label: string;\n  value: number;\n  /** Full level. Defaults to 100. */\n  max?: number;\n  /** Word after the value in the readout, e.g. \"GB\". */\n  unit?: string;\n}\n\nexport interface LiquidView {\n  label: string;\n  bars: LiquidBar[];\n}\n\nexport interface LiquidFillBarChartProps extends Omit<\n  React.ComponentPropsWithoutRef<\"section\">,\n  \"title\"\n> {\n  theme?: \"dark\" | \"light\";\n  title?: string;\n  subtitle?: string;\n  /** Sets of levels to switch between. They should list the same bars in the same order. */\n  views?: LiquidView[];\n  /** Index of the view shown first. */\n  defaultView?: number;\n  /** A line across every bar, as a share of full, e.g. 0.85. Pass null to hide it. */\n  warnAt?: number | null;\n  /** Colour of the liquid. */\n  color?: string;\n  /** Height of the bars, in px. */\n  height?: number;\n}\n\nconst PALETTES = {\n  dark: {\n    card: \"#08090A\",\n    surface: \"#0F1011\",\n    border: \"rgba(255,255,255,0.08)\",\n    grid: \"rgba(255,255,255,0.05)\",\n    text: \"#F7F8F8\",\n    soft: \"#D0D6E0\",\n    sub: \"#8A8F98\",\n    faint: \"#62666D\",\n    tip: \"#161718\",\n    tipShadow: \"0 4px 24px rgba(0,0,0,0.4)\",\n    chip: \"rgba(255,255,255,0.05)\",\n    chipOn: \"rgba(255,255,255,0.1)\",\n    accent: \"#5E6AD2\",\n  },\n  light: {\n    card: \"#FFFFFF\",\n    surface: \"#F7F8F8\",\n    border: \"rgba(8,9,10,0.08)\",\n    grid: \"rgba(8,9,10,0.06)\",\n    text: \"#08090A\",\n    soft: \"#3C3F44\",\n    sub: \"#62666D\",\n    faint: \"#8A8F98\",\n    tip: \"#FFFFFF\",\n    tipShadow: \"0 3px 12px rgba(8,9,10,0.09)\",\n    chip: \"rgba(8,9,10,0.04)\",\n    chipOn: \"#FFFFFF\",\n    accent: \"#5E6AD2\",\n  },\n};\n\ntype Palette = (typeof PALETTES)[\"dark\"];\n\nconst CARD_FONT =\n  '\"Inter Variable\", Inter, \"SF Pro Display\", -apple-system, BlinkMacSystemFont, sans-serif';\n\nfunction useReducedMotion() {\n  return React.useSyncExternalStore(\n    (callback) => {\n      const query = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n      query.addEventListener(\"change\", callback);\n      return () => query.removeEventListener(\"change\", callback);\n    },\n    () => window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches,\n    () => false,\n  );\n}\n\n/** The chart is drawn at its real width, so its text stays the same size on a phone. */\nfunction useWidth(ref: React.RefObject<HTMLElement | null>) {\n  const [width, setWidth] = React.useState(700);\n  React.useEffect(() => {\n    const el = ref.current;\n    if (!el) return;\n    const ro = new ResizeObserver(([entry]) =>\n      setWidth(Math.max(240, Math.round(entry.contentRect.width))),\n    );\n    ro.observe(el);\n    return () => ro.disconnect();\n  }, [ref]);\n  return width;\n}\n\nfunction Stat({\n  value,\n  label,\n  pal,\n}: {\n  value: string;\n  label: string;\n  pal: Palette;\n}) {\n  return (\n    <div className=\"text-left sm:text-right\">\n      <p className=\"m-0 text-[18px] leading-[1.2] font-[510] tracking-[-0.018em] tabular-nums\">\n        {value}\n      </p>\n      <p\n        className=\"m-0 mt-0.5 text-[12px] tracking-[-0.011em]\"\n        style={{ color: pal.sub }}\n      >\n        {label}\n      </p>\n    </div>\n  );\n}\n\nfunction Pills({\n  label,\n  options,\n  value,\n  onChange,\n  pal,\n}: {\n  label: string;\n  options: string[];\n  value: number;\n  onChange: (index: number) => void;\n  pal: Palette;\n}) {\n  return (\n    <div\n      className=\"flex rounded-full p-0.5\"\n      style={{ background: pal.chip }}\n      role=\"radiogroup\"\n      aria-label={label}\n    >\n      {options.map((option, i) => (\n        <button\n          key={option}\n          type=\"button\"\n          role=\"radio\"\n          aria-checked={i === value}\n          onClick={() => onChange(i)}\n          className=\"h-7 cursor-pointer rounded-full px-3 text-[13px] font-[510] tracking-[-0.011em] transition-colors outline-none focus-visible:ring-2\"\n          style={{\n            background: i === value ? pal.chipOn : \"transparent\",\n            color: i === value ? pal.text : pal.sub,\n          }}\n        >\n          {option}\n        </button>\n      ))}\n    </div>\n  );\n}\n\nfunction Tip({\n  x,\n  y,\n  width,\n  pal,\n  children,\n}: {\n  x: number;\n  y: number;\n  width: number;\n  pal: Palette;\n  children: React.ReactNode;\n}) {\n  return (\n    <div\n      aria-hidden=\"true\"\n      className=\"pointer-events-none absolute z-10 -translate-x-1/2 rounded-lg px-2.5 py-1.5 text-[12px] tracking-[-0.011em] whitespace-nowrap\"\n      style={{\n        left: Math.max(110, Math.min(width - 110, x)),\n        top: Math.max(0, y - 42),\n        background: pal.tip,\n        color: pal.sub,\n        boxShadow: `inset 0 0 0 1px ${pal.border}, ${pal.tipShadow}`,\n      }}\n    >\n      {children}\n    </div>\n  );\n}\n\nconst DEFAULT_VIEWS: LiquidView[] = [\n  {\n    label: \"Today\",\n    bars: [\n      { label: \"Storage\", value: 68, max: 100, unit: \"GB\" },\n      { label: \"Bandwidth\", value: 412, max: 800, unit: \"GB\" },\n      { label: \"Builds\", value: 74, max: 120, unit: \"min\" },\n      { label: \"Seats\", value: 41, max: 50, unit: \"seats\" },\n      { label: \"API calls\", value: 38, max: 100, unit: \"k\" },\n      { label: \"Emails\", value: 22, max: 40, unit: \"k\" },\n    ],\n  },\n  {\n    label: \"Peak\",\n    bars: [\n      { label: \"Storage\", value: 91, max: 100, unit: \"GB\" },\n      { label: \"Bandwidth\", value: 744, max: 800, unit: \"GB\" },\n      { label: \"Builds\", value: 96, max: 120, unit: \"min\" },\n      { label: \"Seats\", value: 47, max: 50, unit: \"seats\" },\n      { label: \"API calls\", value: 83, max: 100, unit: \"k\" },\n      { label: \"Emails\", value: 35, max: 40, unit: \"k\" },\n    ],\n  },\n  {\n    label: \"Quiet\",\n    bars: [\n      { label: \"Storage\", value: 61, max: 100, unit: \"GB\" },\n      { label: \"Bandwidth\", value: 158, max: 800, unit: \"GB\" },\n      { label: \"Builds\", value: 22, max: 120, unit: \"min\" },\n      { label: \"Seats\", value: 19, max: 50, unit: \"seats\" },\n      { label: \"API calls\", value: 12, max: 100, unit: \"k\" },\n      { label: \"Emails\", value: 6, max: 40, unit: \"k\" },\n    ],\n  },\n];\n\nconst GAP = 10;\nconst RADIUS = 12;\nconst STEPS = 10;\n/** Both waves repeat every two seconds. */\nconst CYCLE = 2000;\n\nconst fill = (b: LiquidBar) => Math.min(1, b.value / (b.max ?? 100));\n\ninterface Surface {\n  level: number;\n  speed: number;\n  slosh: number;\n  sloshSpeed: number;\n}\n\nexport function LiquidFillBarChart({\n  theme = \"dark\",\n  title = \"Plan usage\",\n  subtitle = \"How full each limit is. Stir them with the pointer.\",\n  views = DEFAULT_VIEWS,\n  defaultView = 0,\n  warnAt = 0.85,\n  color,\n  height = 230,\n  className,\n  style,\n  ...props\n}: LiquidFillBarChartProps) {\n  const pal = PALETTES[theme];\n  const accent = color ?? pal.accent;\n  const reduce = useReducedMotion();\n  const id = React.useId().replace(/[^a-zA-Z0-9]/g, \"\");\n  const boxRef = React.useRef<HTMLDivElement>(null);\n  const width = useWidth(boxRef);\n  const [view, setView] = React.useState(defaultView);\n  const [active, setActive] = React.useState<number | null>(null);\n  const bars = React.useMemo(\n    () => (views[view] ?? views[0])?.bars ?? [],\n    [views, view],\n  );\n  const count = bars.length;\n  const frontRefs = React.useRef<(SVGPathElement | null)[]>([]);\n  const backRefs = React.useRef<(SVGPathElement | null)[]>([]);\n  const surfaces = React.useRef<Surface[]>([]);\n  const targets = React.useRef<number[]>([]);\n  const lastMove = React.useRef({ x: 0, t: 0 });\n\n  const barWidth = Math.max(\n    20,\n    Math.min(112, (width - GAP * (count - 1)) / Math.max(1, count)),\n  );\n  const startX = (width - (barWidth * count + GAP * (count - 1))) / 2;\n  const left = (i: number) => startX + i * (barWidth + GAP);\n\n  // Every new set of levels gives the liquid a shove, bigger the further it has to travel.\n  React.useEffect(() => {\n    const next = bars.map(fill);\n    next.forEach((t, i) => {\n      const s = (surfaces.current[i] ??= {\n        level: reduce ? t : 0,\n        speed: 0,\n        slosh: 0,\n        sloshSpeed: 0,\n      });\n      s.sloshSpeed += (t - s.level) * (i % 2 ? -46 : 46);\n    });\n    targets.current = next;\n  }, [bars, reduce]);\n\n  // One loop runs the springs and redraws the surfaces, and only while the chart is on screen.\n  React.useEffect(() => {\n    const box = boxRef.current;\n    if (!box) return;\n    let raf = 0;\n    let prev = 0;\n    const draw = (now: number) => {\n      const phase = ((now % CYCLE) / CYCLE) * Math.PI * 2;\n      surfaces.current.forEach((s, i) => {\n        const front = frontRefs.current[i];\n        const back = backRefs.current[i];\n        if (!front || !back) return;\n        const x0 = left(i);\n        const surface = (shift: number, swell: number, lift: number) => {\n          let d = `M${x0.toFixed(1)},${height}`;\n          for (let k = 0; k <= STEPS; k++) {\n            const u = k / STEPS;\n            const wave = reduce\n              ? 0\n              : Math.sin(u * 5 + phase * shift + i) * swell;\n            const tilt = (u - 0.5) * s.slosh * 2;\n            const top = height - s.level * height - lift + wave + tilt;\n            d += `L${(x0 + u * barWidth).toFixed(1)},${top.toFixed(1)}`;\n          }\n          return `${d}L${(x0 + barWidth).toFixed(1)},${height}Z`;\n        };\n        front.setAttribute(\"d\", surface(1, 2.2, 0));\n        back.setAttribute(\"d\", surface(2, 2.8, 3));\n      });\n    };\n    const frame = (now: number) => {\n      const dt = Math.min(0.033, prev ? (now - prev) / 1000 : 1 / 60);\n      prev = now;\n      surfaces.current.forEach((s, i) => {\n        const goal = targets.current[i] ?? 0;\n        s.speed += ((goal - s.level) * 26 - s.speed * 7.5) * dt;\n        s.level += s.speed * dt;\n        s.sloshSpeed += (-s.slosh * 58 - s.sloshSpeed * 3.2) * dt;\n        s.slosh += s.sloshSpeed * dt;\n      });\n      draw(now);\n      raf = requestAnimationFrame(frame);\n    };\n    if (reduce) {\n      surfaces.current.forEach((s, i) => {\n        s.level = targets.current[i] ?? 0;\n      });\n      draw(0);\n      return;\n    }\n    const io = new IntersectionObserver(([entry]) => {\n      cancelAnimationFrame(raf);\n      prev = 0;\n      if (entry.isIntersecting) raf = requestAnimationFrame(frame);\n    });\n    io.observe(box);\n    return () => {\n      io.disconnect();\n      cancelAnimationFrame(raf);\n    };\n    // left() only depends on the sizes listed here.\n    // eslint-disable-next-line react-hooks/exhaustive-deps\n  }, [reduce, height, barWidth, startX, view]);\n\n  const pick = (clientX: number, el: Element) => {\n    const r = el.getBoundingClientRect();\n    const px = clientX - r.left;\n    return bars.findIndex(\n      (_, i) => px >= left(i) - GAP / 2 && px <= left(i) + barWidth + GAP / 2,\n    );\n  };\n\n  const average = bars.reduce((n, b) => n + fill(b), 0) / Math.max(1, count);\n  const fullest = bars.reduce(\n    (best, b) => (fill(b) > fill(best) ? b : best),\n    bars[0] ?? { label: \"None\", value: 0 },\n  );\n  const warned =\n    warnAt === null ? 0 : bars.filter((b) => fill(b) >= warnAt).length;\n  const bar = active === null ? null : bars[active];\n\n  return (\n    <section\n      className={`w-full rounded-2xl p-5 sm:p-6 ${className ?? \"\"}`}\n      style={{\n        background: pal.card,\n        boxShadow: `inset 0 0 0 1px ${pal.border}`,\n        color: pal.text,\n        fontFamily: CARD_FONT,\n        ...style,\n      }}\n      {...props}\n    >\n      <div className=\"flex flex-wrap items-start justify-between gap-x-8 gap-y-3\">\n        <div>\n          <h3 className=\"m-0 text-[17px] leading-[1.33] font-[590] tracking-[-0.012em]\">\n            {title}\n          </h3>\n          <p\n            className=\"m-0 mt-0.5 text-[13px] tracking-[-0.011em]\"\n            style={{ color: pal.sub }}\n          >\n            {subtitle}\n          </p>\n        </div>\n        <div className=\"flex gap-7\">\n          <Stat\n            value={`${Math.round(average * 100)}%`}\n            label=\"Average fill\"\n            pal={pal}\n          />\n          <Stat value={fullest.label} label=\"Fullest\" pal={pal} />\n          <Stat value={String(warned)} label=\"Near the limit\" pal={pal} />\n        </div>\n      </div>\n\n      {!count ? (\n        <p className=\"py-16 text-center text-sm\" style={{ color: pal.sub }}>\n          No data available.\n        </p>\n      ) : (\n        <div\n          ref={boxRef}\n          className=\"relative mt-5 rounded-lg outline-none focus-visible:ring-2\"\n          tabIndex={0}\n          role=\"group\"\n          aria-label={`${title}. Use left and right arrow keys to read each limit.`}\n          onPointerMove={(e) => {\n            const i = pick(e.clientX, e.currentTarget);\n            setActive(i < 0 ? null : i);\n            const now = e.timeStamp;\n            const dt = Math.max(8, now - lastMove.current.t);\n            const speed = ((e.clientX - lastMove.current.x) / dt) * 16;\n            lastMove.current = { x: e.clientX, t: now };\n            const s = surfaces.current[i];\n            if (s && e.pointerType === \"mouse\" && !reduce) {\n              s.sloshSpeed += Math.max(-12, Math.min(12, speed * 1.5));\n            }\n          }}\n          onPointerLeave={() => setActive(null)}\n          onBlur={() => setActive(null)}\n          onKeyDown={(e) => {\n            if (e.key !== \"ArrowLeft\" && e.key !== \"ArrowRight\") return;\n            e.preventDefault();\n            const dir = e.key === \"ArrowRight\" ? 1 : -1;\n            setActive((a) => Math.max(0, Math.min(count - 1, (a ?? -1) + dir)));\n          }}\n        >\n          <svg\n            viewBox={`0 0 ${width} ${height}`}\n            className=\"block w-full\"\n            style={{ height }}\n            role=\"img\"\n            aria-label={bars\n              .map((b) => `${b.label} ${Math.round(fill(b) * 100)}%`)\n              .join(\", \")}\n          >\n            <defs>\n              {bars.map((b, i) => (\n                <clipPath key={b.label} id={`${id}-clip-${i}`}>\n                  <rect\n                    x={left(i)}\n                    y=\"0\"\n                    width={barWidth}\n                    height={height}\n                    rx={RADIUS}\n                  />\n                </clipPath>\n              ))}\n            </defs>\n            {bars.map((b, i) => (\n              <g key={b.label}>\n                <rect\n                  x={left(i)}\n                  y=\"0\"\n                  width={barWidth}\n                  height={height}\n                  rx={RADIUS}\n                  fill={pal.surface}\n                />\n                <g clipPath={`url(#${id}-clip-${i})`}>\n                  <path\n                    ref={(el) => {\n                      backRefs.current[i] = el;\n                    }}\n                    fill={accent}\n                    opacity=\"0.38\"\n                  />\n                  <path\n                    ref={(el) => {\n                      frontRefs.current[i] = el;\n                    }}\n                    fill={accent}\n                  />\n                </g>\n                {warnAt !== null && (\n                  <line\n                    x1={left(i) + 1}\n                    x2={left(i) + barWidth - 1}\n                    y1={height * (1 - warnAt)}\n                    y2={height * (1 - warnAt)}\n                    stroke={pal.text}\n                    strokeOpacity=\"0.28\"\n                    strokeDasharray=\"3 3\"\n                  />\n                )}\n                <rect\n                  x={left(i) + 0.5}\n                  y=\"0.5\"\n                  width={barWidth - 1}\n                  height={height - 1}\n                  rx={RADIUS - 0.5}\n                  fill=\"none\"\n                  stroke={active === i ? pal.text : pal.border}\n                  strokeOpacity={active === i ? 0.6 : 1}\n                />\n                <text\n                  x={left(i) + barWidth / 2}\n                  y={height - 12}\n                  textAnchor=\"middle\"\n                  fontSize=\"13\"\n                  fontWeight=\"510\"\n                  fill={fill(b) > 0.12 ? \"#FFFFFF\" : pal.text}\n                  style={{ fontVariantNumeric: \"tabular-nums\" }}\n                >\n                  {Math.round(fill(b) * 100)}%\n                </text>\n              </g>\n            ))}\n          </svg>\n\n          <div\n            aria-hidden=\"true\"\n            className=\"relative mt-2 h-4 text-[10px] tracking-[-0.011em] sm:text-[12px]\"\n            style={{ color: pal.sub }}\n          >\n            {bars.map((b, i) => (\n              <span\n                key={b.label}\n                className=\"absolute top-0 text-center whitespace-nowrap\"\n                style={{ left: left(i) - GAP / 2, width: barWidth + GAP }}\n              >\n                {b.label}\n              </span>\n            ))}\n          </div>\n\n          {bar && active !== null && (\n            <Tip\n              x={left(active) + barWidth / 2}\n              y={height * (1 - fill(bar))}\n              width={width}\n              pal={pal}\n            >\n              <span className=\"font-[510]\" style={{ color: pal.text }}>\n                {bar.value.toLocaleString(\"en-US\")} of{\" \"}\n                {(bar.max ?? 100).toLocaleString(\"en-US\")} {bar.unit ?? \"\"}\n              </span>{\" \"}\n              {bar.label}\n            </Tip>\n          )}\n        </div>\n      )}\n\n      <div className=\"mt-3 flex flex-wrap items-center justify-between gap-2\">\n        <Pills\n          label=\"View\"\n          options={views.map((v) => v.label)}\n          value={view}\n          onChange={setView}\n          pal={pal}\n        />\n        {warnAt !== null && (\n          <span\n            className=\"flex items-center gap-2 text-[12px]\"\n            style={{ color: pal.sub }}\n          >\n            <span\n              className=\"w-4 border-t border-dashed\"\n              style={{ borderColor: pal.faint }}\n            />\n            Warning at {Math.round(warnAt * 100)}%\n          </span>\n        )}\n      </div>\n    </section>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}