{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "funnel-chart",
  "type": "registry:component",
  "title": "Funnel Chart",
  "description": "A conversion funnel of centered trapezoid stages with absolute values, share of the first stage and the drop-off between stages.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "charts",
    "funnel",
    "conversion",
    "pipeline",
    "chart",
    "stages",
    "analytics"
  ],
  "docs": "https://hyperblur.dev/effects/funnel-chart",
  "files": [
    {
      "path": "registry/hyperblur/funnel-chart.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/funnel-chart.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface FunnelStage {\n  label: string;\n  value: number;\n  color?: string;\n}\n\nexport interface FunnelChartProps extends React.HTMLAttributes<HTMLDivElement> {\n  stages?: FunnelStage[];\n  colors?: string[];\n  stageHeight?: number;\n  valueFormatter?: (n: number) => string;\n  animate?: boolean;\n}\n\nconst RAMP = [\"#2f7bff\", \"#4d86ff\", \"#6a8ffa\", \"#8b8cf6\", \"#8b5cf6\", \"#34d399\"];\n\nconst DEFAULT_STAGES: FunnelStage[] = [\n  { label: \"Visitors\", value: 48200 },\n  { label: \"Sign-ups\", value: 14600 },\n  { label: \"Activated\", value: 8900 },\n  { label: \"Trial\", value: 3400 },\n  { label: \"Paid\", value: 1260 },\n];\n\nconst css = `\n.hb-fnl { position: relative; display: block; width: 100%; min-width: 0; box-sizing: border-box; color: #ededed; font-family: inherit; }\n.hb-fnl *, .hb-fnl *::before, .hb-fnl *::after { box-sizing: border-box; }\n.hb-fnl-plot { position: relative; width: 100%; max-width: 560px; margin: 0 auto; }\n.hb-fnl-svg { display: block; width: 100%; height: auto; overflow: visible; }\n.hb-fnl-stage {\n  cursor: default;\n  outline: none;\n  animation: hb-fnl-in .6s cubic-bezier(.22, .8, .24, 1) backwards;\n  animation-delay: calc(var(--hb-fnl-i, 0) * 90ms);\n  transition: opacity .2s ease;\n}\n.hb-fnl-stage[data-dim=\"true\"] { opacity: .35; }\n.hb-fnl-shape { transition: filter .2s ease; }\n.hb-fnl-stage[data-on=\"true\"] .hb-fnl-shape { filter: brightness(1.18); }\n.hb-fnl-stage:focus-visible .hb-fnl-shape { stroke: #ededed; stroke-width: 1.5; }\n.hb-fnl-name { fill: #ededed; font-size: 11.5px; font-family: inherit; }\n.hb-fnl-val {\n  fill: #8f8f8f;\n  font-size: 10.5px;\n  font-family: var(--font-mono, ui-monospace, SFMono-Regular, Menlo, monospace);\n  font-variant-numeric: tabular-nums;\n}\n.hb-fnl-pct {\n  fill: #ffffff;\n  font-size: 11px;\n  font-weight: 600;\n  font-family: var(--font-mono, ui-monospace, SFMono-Regular, Menlo, monospace);\n  font-variant-numeric: tabular-nums;\n}\n.hb-fnl-drop {\n  fill: #fb7185;\n  font-size: 10.5px;\n  font-family: var(--font-mono, ui-monospace, SFMono-Regular, Menlo, monospace);\n  font-variant-numeric: tabular-nums;\n}\n.hb-fnl-tip {\n  position: absolute;\n  z-index: 2;\n  min-width: 120px;\n  padding: 6px 9px;\n  border-radius: 8px;\n  border: 1px solid #262626;\n  background: #171717;\n  box-shadow: 0 10px 24px -8px rgba(0, 0, 0, .8);\n  font-size: 11px;\n  line-height: 1.4;\n  white-space: nowrap;\n  pointer-events: none;\n}\n.hb-fnl-tip-h { color: #ededed; font-weight: 600; }\n.hb-fnl-tip-r { display: flex; justify-content: space-between; gap: 14px; color: #8f8f8f; }\n.hb-fnl-tip-r b {\n  color: #ededed;\n  font-weight: 500;\n  font-family: var(--font-mono, ui-monospace, SFMono-Regular, Menlo, monospace);\n  font-variant-numeric: tabular-nums;\n}\n.hb-fnl-tip-r b[data-down=\"true\"] { color: #fb7185; }\n.hb-fnl[data-animate=\"false\"] .hb-fnl-stage { animation: none; }\n@keyframes hb-fnl-in {\n  from { opacity: 0; transform: scaleX(.2); }\n}\n@media (prefers-reduced-motion: reduce) {\n  .hb-fnl-stage { animation: none !important; }\n  .hb-fnl-stage, .hb-fnl-shape { transition: none !important; }\n}\n`;\n\nfunction f(n: number) {\n  return n.toFixed(2);\n}\n\nexport function FunnelChart({\n  stages = DEFAULT_STAGES,\n  colors = RAMP,\n  stageHeight = 34,\n  valueFormatter,\n  animate = true,\n  className = \"\",\n  ...rest\n}: FunnelChartProps) {\n  const [active, setActive] = React.useState<number | null>(null);\n\n  const fmt = React.useMemo(() => {\n    if (valueFormatter) return valueFormatter;\n    const nf = new Intl.NumberFormat(\"en-US\", { maximumFractionDigits: 0 });\n    return (n: number) => nf.format(n);\n  }, [valueFormatter]);\n\n  const W = 320;\n  const labelW = 88;\n  const dropW = 46;\n  const funnelX = labelW + 6;\n  const maxW = W - funnelX - dropW;\n  const cx = funnelX + maxW / 2;\n  const gapY = 3;\n  const rh = Math.max(18, stageHeight);\n  const n = stages.length;\n  const H = Math.max(1, n) * rh;\n  const first = stages[0]?.value > 0 ? stages[0].value : 1;\n\n  const widths = stages.map((s) => Math.max(0.16, Math.min(1, (Number.isFinite(s.value) ? s.value : 0) / first)) * maxW);\n  const rows = stages.map((s, i) => {\n    const prev = i > 0 ? stages[i - 1].value : 0;\n    const top = widths[i];\n    const bottom = i < n - 1 ? widths[i + 1] : top * 0.78;\n    const y0 = i * rh;\n    const y1 = y0 + rh - gapY;\n    const share = s.value / first;\n    const drop = i > 0 && prev > 0 ? 1 - s.value / prev : null;\n    return { ...s, i, top, bottom, y0, y1, share, drop, color: s.color ?? colors[i % colors.length] };\n  });\n\n  const pct = (x: number) => `${(Math.round(x * 1000) / 10).toFixed(x * 100 >= 10 ? 0 : 1)}%`;\n  const summary = n\n    ? `Funnel chart with ${n} stages. ${rows\n        .map((r) => `${r.label} ${fmt(r.value)}, ${pct(r.share)} of ${stages[0].label}`)\n        .join(\". \")}.`\n    : \"Funnel chart with no data\";\n\n  const act = active === null ? null : rows[active];\n  let tip: React.ReactNode = null;\n  if (act) {\n    const below = act.i < 2;\n    tip = (\n      <div\n        className=\"hb-fnl-tip\"\n        aria-hidden=\"true\"\n        style={{\n          left: `${((cx / W) * 100).toFixed(2)}%`,\n          top: `${(((below ? act.y1 : act.y0) / H) * 100).toFixed(2)}%`,\n          transform: below ? \"translate(-50%, 6px)\" : \"translate(-50%, calc(-100% - 6px))\",\n        }}\n      >\n        <div className=\"hb-fnl-tip-h\">{act.label}</div>\n        <div className=\"hb-fnl-tip-r\">\n          Count <b>{fmt(act.value)}</b>\n        </div>\n        <div className=\"hb-fnl-tip-r\">\n          Of first stage <b>{pct(act.share)}</b>\n        </div>\n        {act.drop !== null ? (\n          <div className=\"hb-fnl-tip-r\">\n            Drop-off <b data-down=\"true\">{`-${pct(Math.max(0, act.drop))}`}</b>\n          </div>\n        ) : null}\n      </div>\n    );\n  }\n\n  return (\n    <div className={`hb-fnl ${className}`} data-animate={animate ? \"true\" : \"false\"} {...rest}>\n      <style href=\"hb-funnel-chart\" precedence=\"default\">{css}</style>\n      <div className=\"hb-fnl-plot\" onMouseLeave={() => setActive(null)}>\n        <svg className=\"hb-fnl-svg\" viewBox={`0 0 ${W} ${H}`} role=\"img\" aria-label={summary}>\n          {rows.map((r) => {\n            const midY = (r.y0 + r.y1) / 2;\n            const pts = [\n              [cx - r.top / 2, r.y0],\n              [cx + r.top / 2, r.y0],\n              [cx + r.bottom / 2, r.y1],\n              [cx - r.bottom / 2, r.y1],\n            ]\n              .map(([x, y]) => `${f(x)},${f(y)}`)\n              .join(\" \");\n            const aria = `${r.label}: ${fmt(r.value)}, ${pct(r.share)} of first stage${\n              r.drop !== null ? `, down ${pct(Math.max(0, r.drop))} from previous stage` : \"\"\n            }`;\n            return (\n              <g\n                key={r.i}\n                className=\"hb-fnl-stage\"\n                data-on={active === r.i ? \"true\" : \"false\"}\n                data-dim={active !== null && active !== r.i ? \"true\" : \"false\"}\n                tabIndex={0}\n                aria-label={aria}\n                style={{\n                  transformOrigin: `${f(cx)}px ${f(midY)}px`,\n                  [\"--hb-fnl-i\" as string]: r.i,\n                }}\n                onMouseEnter={() => setActive(r.i)}\n                onFocus={() => setActive(r.i)}\n                onBlur={() => setActive(null)}\n              >\n                <rect x={0} y={r.y0} width={W} height={rh - gapY} fill=\"transparent\" />\n                <text className=\"hb-fnl-name\" x={0} y={f(midY - 2)} dominantBaseline=\"auto\">\n                  {r.label}\n                </text>\n                <text className=\"hb-fnl-val\" x={0} y={f(midY + 9)} dominantBaseline=\"auto\">\n                  {fmt(r.value)}\n                </text>\n                <polygon className=\"hb-fnl-shape\" points={pts} fill={r.color} />\n                <text className=\"hb-fnl-pct\" x={f(cx)} y={f(midY)} textAnchor=\"middle\" dominantBaseline=\"middle\">\n                  {pct(r.share)}\n                </text>\n                {r.drop !== null ? (\n                  <text className=\"hb-fnl-drop\" x={W} y={f(midY)} textAnchor=\"end\" dominantBaseline=\"middle\">\n                    {`-${pct(Math.max(0, r.drop))}`}\n                  </text>\n                ) : null}\n              </g>\n            );\n          })}\n        </svg>\n        {tip}\n      </div>\n    </div>\n  );\n}\n"
    }
  ]
}