{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "volume-tier-graph",
  "title": "Volume Tier Graph",
  "description": "Tiered pricing you can see: a stepped price curve with a draggable marker, graduated or volume modes, a live total and average per unit, and the cost split across each tier.",
  "dependencies": [
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/volume-tier-graph/volume-tier-graph.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { clsx, type ClassValue } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nfunction cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n\nexport type TierGraphTheme = \"dark\" | \"light\";\nexport type TierMode = \"graduated\" | \"volume\";\n\nexport interface PriceTier {\n  /** Last unit in this tier; the final tier can leave it out. */\n  upTo?: number;\n  /** Price per unit in this tier. */\n  price: number;\n}\n\nexport interface VolumeTierGraphProps extends Omit<\n  React.ComponentPropsWithoutRef<\"div\">,\n  \"title\"\n> {\n  theme?: TierGraphTheme;\n  title?: string;\n  subtitle?: string;\n  tiers?: PriceTier[];\n  /** Range of the slider. */\n  min?: number;\n  max?: number;\n  defaultUnits?: number;\n  /** \"graduated\" charges each tier at its own rate; \"volume\" charges every unit at the tier reached. */\n  mode?: TierMode;\n  /** Show the Graduated / Volume switch. */\n  showModeSwitch?: boolean;\n  unit?: string;\n  currency?: string;\n  /** Height of the graph, in px. */\n  height?: number;\n}\n\nconst PALETTES = {\n  dark: {\n    card: \"#0E0E0E\",\n    border: \"rgba(255,255,255,0.09)\",\n    text: \"#F4F5F7\",\n    sub: \"rgba(255,255,255,0.55)\",\n    faint: \"rgba(255,255,255,0.36)\",\n    grid: \"rgba(255,255,255,0.07)\",\n    chip: \"rgba(255,255,255,0.06)\",\n    line: \"#87FFE3\",\n    tiers: [\"#87FFE3\", \"#8FB8FF\", \"#B69CFF\", \"#F2A841\"],\n    handle: \"#F4F5F7\",\n  },\n  light: {\n    card: \"#FFFFFF\",\n    border: \"rgba(0,0,0,0.09)\",\n    text: \"#111317\",\n    sub: \"rgba(17,19,23,0.58)\",\n    faint: \"rgba(17,19,23,0.4)\",\n    grid: \"rgba(17,19,23,0.08)\",\n    chip: \"rgba(17,19,23,0.05)\",\n    line: \"#0E9F80\",\n    tiers: [\"#0E9F80\", \"#3B6FD8\", \"#7B5CE0\", \"#D9822B\"],\n    handle: \"#111317\",\n  },\n};\n\nconst DEFAULT_TIERS: PriceTier[] = [\n  { upTo: 1000, price: 0.1 },\n  { upTo: 10000, price: 0.06 },\n  { upTo: 100000, price: 0.03 },\n  { price: 0.012 },\n];\n\nfunction useWidth(ref: React.RefObject<HTMLElement | null>) {\n  const [width, setWidth] = React.useState(0);\n  React.useEffect(() => {\n    const el = ref.current;\n    if (!el) return;\n    const ro = new ResizeObserver(([entry]) =>\n      setWidth(entry.contentRect.width),\n    );\n    ro.observe(el);\n    return () => ro.disconnect();\n  }, [ref]);\n  return width;\n}\n\nconst short = (v: number) =>\n  v >= 1e6\n    ? `${+(v / 1e6).toFixed(1)}M`\n    : v >= 1e3\n      ? `${+(v / 1e3).toFixed(v >= 1e4 ? 0 : 1)}K`\n      : String(Math.round(v));\nconst niceRound = (v: number) => {\n  if (v < 100) return Math.round(v);\n  const mag = Math.pow(10, Math.floor(Math.log10(v)) - 1);\n  return Math.round(v / mag) * mag;\n};\n\nconst M = { top: 16, right: 14, bottom: 26, left: 52 };\n\nexport function VolumeTierGraph({\n  theme = \"dark\",\n  title = \"The more you send, the less you pay\",\n  subtitle = \"Drag the marker along the curve\",\n  tiers = DEFAULT_TIERS,\n  min = 100,\n  max = 1000000,\n  defaultUnits = 25000,\n  mode: initialMode = \"graduated\",\n  showModeSwitch = true,\n  unit = \"emails\",\n  currency = \"$\",\n  height = 220,\n  className,\n  style,\n  ...props\n}: VolumeTierGraphProps) {\n  const pal = PALETTES[theme];\n  const plotRef = React.useRef<HTMLDivElement>(null);\n  const width = useWidth(plotRef);\n  const [units, setUnits] = React.useState(defaultUnits);\n  const [mode, setMode] = React.useState(initialMode);\n  const dragging = React.useRef(false);\n\n  const plotW = Math.max(0, width - M.left - M.right);\n  const logMin = Math.log(min);\n  const logMax = Math.log(max);\n  const xAt = (u: number) =>\n    M.left +\n    ((Math.log(Math.max(min, u)) - logMin) / (logMax - logMin)) * plotW;\n  const unitsAt = (x: number) =>\n    niceRound(\n      Math.exp(\n        logMin +\n          Math.max(0, Math.min(1, (x - M.left) / Math.max(1, plotW))) *\n            (logMax - logMin),\n      ),\n    );\n  const top = Math.max(...tiers.map((t) => t.price));\n  const yAt = (p: number) => M.top + height - (p / (top * 1.15)) * height;\n\n  // Each tier's span of units, and what the current quantity costs in it.\n  const spans = tiers.map((t, i) => ({\n    ...t,\n    i,\n    from: i === 0 ? 0 : (tiers[i - 1].upTo ?? 0),\n    to: t.upTo ?? Infinity,\n  }));\n  const reached = spans.find((s) => units <= s.to) ?? spans[spans.length - 1];\n  const rows = spans.map((s) => {\n    const inTier = Math.max(0, Math.min(units, s.to) - s.from);\n    return {\n      ...s,\n      inTier,\n      cost:\n        mode === \"graduated\"\n          ? inTier * s.price\n          : s === reached\n            ? units * s.price\n            : 0,\n    };\n  });\n  const total = rows.reduce((a, r) => a + r.cost, 0);\n  const avg = units ? total / units : 0;\n  const money = (v: number) =>\n    `${currency}${v.toLocaleString(\"en-US\", { minimumFractionDigits: v < 100 ? 2 : 0, maximumFractionDigits: v < 100 ? 2 : 0 })}`;\n  const rate = (v: number) => `${currency}${v.toFixed(v < 0.1 ? 3 : 2)}`;\n\n  const pick = (clientX: number) => {\n    const r = plotRef.current?.getBoundingClientRect();\n    if (r) setUnits(Math.max(min, Math.min(max, unitsAt(clientX - r.left))));\n  };\n  const onKey = (e: React.KeyboardEvent) => {\n    const dir =\n      e.key === \"ArrowRight\" || e.key === \"ArrowUp\"\n        ? 1\n        : e.key === \"ArrowLeft\" || e.key === \"ArrowDown\"\n          ? -1\n          : 0;\n    if (e.key === \"Home\") setUnits(min);\n    else if (e.key === \"End\") setUnits(max);\n    else if (dir)\n      setUnits((u) =>\n        Math.max(\n          min,\n          Math.min(max, niceRound(u * (dir > 0 ? 1.12 : 1 / 1.12))),\n        ),\n      );\n    else return;\n    e.preventDefault();\n  };\n\n  const hx = xAt(units);\n  const ticks = [100, 1000, 10000, 100000, 1000000].filter(\n    (t) => t >= min && t <= max,\n  );\n  const yTicks = [...new Set(tiers.map((t) => t.price))];\n\n  return (\n    <div\n      className={cn(\n        \"w-full overflow-hidden rounded-[20px] p-5 sm:p-7\",\n        className,\n      )}\n      style={{\n        background: pal.card,\n        border: `1px solid ${pal.border}`,\n        color: pal.text,\n        fontFamily: \"Inter, -apple-system, BlinkMacSystemFont, sans-serif\",\n        ...style,\n      }}\n      {...props}\n    >\n      <div className=\"flex flex-wrap items-start justify-between gap-4\">\n        <div className=\"min-w-0\">\n          <p className=\"m-0 text-[17px] font-semibold tracking-[-0.01em]\">\n            {title}\n          </p>\n          <p className=\"m-0 mt-1 text-[13px]\" style={{ color: pal.sub }}>\n            {subtitle}\n          </p>\n        </div>\n        {showModeSwitch && (\n          <div\n            role=\"radiogroup\"\n            aria-label=\"Pricing model\"\n            className=\"flex rounded-full p-0.5 text-[11.5px] font-medium\"\n            style={{ background: pal.chip }}\n          >\n            {([\"graduated\", \"volume\"] as const).map((m) => (\n              <button\n                key={m}\n                type=\"button\"\n                role=\"radio\"\n                aria-checked={mode === m}\n                onClick={() => setMode(m)}\n                className=\"cursor-pointer rounded-full px-2.5 py-1 capitalize transition-colors\"\n                style={{\n                  background: mode === m ? pal.card : \"transparent\",\n                  color: mode === m ? pal.text : pal.sub,\n                  boxShadow: mode === m ? \"0 1px 2px rgba(0,0,0,.2)\" : \"none\",\n                }}\n              >\n                {m}\n              </button>\n            ))}\n          </div>\n        )}\n      </div>\n\n      <dl className=\"m-0 mt-4 grid grid-cols-3 gap-3\">\n        {(\n          [\n            [\n              unit.charAt(0).toUpperCase() + unit.slice(1),\n              units.toLocaleString(\"en-US\"),\n            ],\n            [\"Per month\", money(total)],\n            [\"Average each\", rate(avg)],\n          ] as const\n        ).map(([k, v]) => (\n          <div key={k}>\n            <dt className=\"text-[11.5px]\" style={{ color: pal.sub }}>\n              {k}\n            </dt>\n            <dd className=\"m-0 mt-0.5 text-[21px] leading-tight font-semibold tracking-[-0.02em] tabular-nums\">\n              {v}\n            </dd>\n          </div>\n        ))}\n      </dl>\n\n      <div\n        ref={plotRef}\n        className=\"relative mt-3 w-full cursor-ew-resize touch-none select-none\"\n        style={{ height: height + M.top + M.bottom }}\n        onPointerDown={(e) => {\n          dragging.current = true;\n          e.currentTarget.setPointerCapture(e.pointerId);\n          pick(e.clientX);\n        }}\n        onPointerMove={(e) => dragging.current && pick(e.clientX)}\n        onPointerUp={() => (dragging.current = false)}\n        onPointerCancel={() => (dragging.current = false)}\n      >\n        {width > 0 && (\n          <svg\n            width={width}\n            height={height + M.top + M.bottom}\n            className=\"block\"\n            aria-hidden=\"true\"\n          >\n            {yTicks.map((p) => (\n              <g key={p}>\n                <line\n                  x1={M.left}\n                  x2={M.left + plotW}\n                  y1={yAt(p)}\n                  y2={yAt(p)}\n                  stroke={pal.grid}\n                  strokeDasharray=\"3 4\"\n                />\n                <text\n                  x={M.left - 8}\n                  y={yAt(p) + 4}\n                  textAnchor=\"end\"\n                  fontSize={11}\n                  fill={pal.faint}\n                  className=\"tabular-nums\"\n                >\n                  {rate(p)}\n                </text>\n              </g>\n            ))}\n            {ticks.map((t) => (\n              <text\n                key={t}\n                x={xAt(t)}\n                y={M.top + height + 18}\n                textAnchor={t === max ? \"end\" : t === min ? \"start\" : \"middle\"}\n                fontSize={11}\n                fill={pal.faint}\n                className=\"tabular-nums\"\n              >\n                {short(t)}\n              </text>\n            ))}\n            {spans.map((s, i) => {\n              const x0 = xAt(Math.max(s.from, min));\n              const x1 = xAt(Math.min(s.to, max));\n              if (x1 <= x0) return null;\n              const c = pal.tiers[i % pal.tiers.length];\n              const on = s === reached;\n              const y = yAt(s.price);\n              // The filled part runs up to the marker in graduated mode; volume mode fills only the tier reached.\n              const fillTo =\n                mode === \"graduated\" ? Math.min(x1, hx) : on ? hx : x0;\n              return (\n                <g key={i}>\n                  <rect\n                    x={x0}\n                    y={y}\n                    width={x1 - x0}\n                    height={M.top + height - y}\n                    fill={c}\n                    fillOpacity={0.06}\n                  />\n                  {fillTo > x0 && (\n                    <rect\n                      x={x0}\n                      y={y}\n                      width={fillTo - x0}\n                      height={M.top + height - y}\n                      fill={c}\n                      fillOpacity={theme === \"dark\" ? 0.28 : 0.2}\n                    />\n                  )}\n                  <line\n                    x1={x0}\n                    x2={x1}\n                    y1={y}\n                    y2={y}\n                    stroke={c}\n                    strokeWidth={on ? 3 : 2}\n                    strokeLinecap=\"round\"\n                  />\n                  {i > 0 && (\n                    <line\n                      x1={x0}\n                      x2={x0}\n                      y1={yAt(spans[i - 1].price)}\n                      y2={y}\n                      stroke={pal.faint}\n                      strokeDasharray=\"2 3\"\n                    />\n                  )}\n                </g>\n              );\n            })}\n            <line\n              x1={M.left}\n              x2={M.left + plotW}\n              y1={M.top + height}\n              y2={M.top + height}\n              stroke={pal.grid}\n            />\n            <line\n              x1={hx}\n              x2={hx}\n              y1={M.top}\n              y2={M.top + height}\n              stroke={pal.handle}\n              strokeOpacity={0.5}\n              strokeDasharray=\"3 3\"\n            />\n          </svg>\n        )}\n        {width > 0 && (\n          <button\n            type=\"button\"\n            role=\"slider\"\n            aria-label={`Number of ${unit}`}\n            aria-valuemin={min}\n            aria-valuemax={max}\n            aria-valuenow={units}\n            aria-valuetext={`${units.toLocaleString(\"en-US\")} ${unit}, ${money(total)} a month`}\n            onKeyDown={onKey}\n            className=\"absolute size-[18px] -translate-1/2 cursor-ew-resize rounded-full outline-none focus-visible:ring-2\"\n            style={{\n              left: hx,\n              top: yAt(reached.price),\n              background: pal.handle,\n              boxShadow: `0 0 0 4px ${pal.card}, 0 2px 8px rgba(0,0,0,.4)`,\n            }}\n          />\n        )}\n      </div>\n\n      <ul className=\"m-0 mt-2 grid list-none grid-cols-2 gap-x-5 gap-y-1.5 p-0 text-[12px] sm:grid-cols-4\">\n        {rows.map((r, i) => (\n          <li\n            key={i}\n            className=\"min-w-0\"\n            style={{ opacity: r.cost > 0 ? 1 : 0.5 }}\n          >\n            <span\n              className=\"flex items-center gap-1.5\"\n              style={{ color: pal.sub }}\n            >\n              <span\n                className=\"size-2 shrink-0 rounded-full\"\n                style={{ background: pal.tiers[i % pal.tiers.length] }}\n              />\n              <span className=\"truncate tabular-nums\">\n                {Number.isFinite(r.to)\n                  ? `to ${short(r.to)}`\n                  : `${short(r.from)}+`}{\" \"}\n                · {rate(r.price)}\n              </span>\n            </span>\n            <span className=\"mt-0.5 block pl-3.5 font-semibold tabular-nums\">\n              {money(r.cost)}\n            </span>\n          </li>\n        ))}\n      </ul>\n    </div>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}