{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "funnel-chart",
  "title": "Funnel Chart",
  "description": "A conversion funnel as one smooth flowing shape: each step's width follows its count, curving into the next, with step-to-step conversion pills on the joins, ghost bands that show how much was lost, a drop-off tooltip and a stacked-rows layout on phones.",
  "dependencies": [
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/funnel-chart/funnel-chart.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 ChartTheme = \"dark\" | \"light\";\n\nexport interface FunnelStage {\n  label: string;\n  value: number;\n}\n\nexport interface FunnelChartProps extends Omit<\n  React.ComponentPropsWithoutRef<\"div\">,\n  \"title\"\n> {\n  theme?: ChartTheme;\n  title?: string;\n  subtitle?: string;\n  stages?: FunnelStage[];\n  /** Colours at the wide and narrow ends of the funnel. */\n  colors?: [string, string];\n  /** Height of the funnel, in px. */\n  height?: number;\n  formatValue?: (value: number) => string;\n}\n\nconst PALETTES = {\n  dark: {\n    card: \"#0E0E0E\",\n    border: \"rgba(255,255,255,0.08)\",\n    text: \"#F4F5F7\",\n    sub: \"rgba(255,255,255,0.55)\",\n    grid: \"rgba(255,255,255,0.08)\",\n    ghost: \"rgba(255,255,255,0.035)\",\n    tip: \"rgba(20,20,20,0.96)\",\n    tipBorder: \"rgba(255,255,255,0.12)\",\n    pill: \"rgba(14,14,14,0.85)\",\n    pillBorder: \"rgba(255,255,255,0.16)\",\n    track: \"rgba(255,255,255,0.06)\",\n    drop: \"#FF7A6B\",\n    ends: [\"#8FB8FF\", \"#87FFE3\"] as [string, string],\n  },\n  light: {\n    card: \"#FFFFFF\",\n    border: \"rgba(0,0,0,0.07)\",\n    text: \"#111317\",\n    sub: \"rgba(17,19,23,0.55)\",\n    grid: \"rgba(17,19,23,0.08)\",\n    ghost: \"rgba(17,19,23,0.035)\",\n    tip: \"rgba(255,255,255,0.98)\",\n    tipBorder: \"rgba(0,0,0,0.08)\",\n    pill: \"rgba(255,255,255,0.92)\",\n    pillBorder: \"rgba(17,19,23,0.12)\",\n    track: \"rgba(17,19,23,0.06)\",\n    drop: \"#E5484D\",\n    ends: [\"#3B6FD8\", \"#0E9F80\"] as [string, string],\n  },\n};\n\nconst DEFAULT_STAGES: FunnelStage[] = [\n  { label: \"Visited site\", value: 48210 },\n  { label: \"Signed up\", value: 12384 },\n  { label: \"Activated\", value: 6942 },\n  { label: \"Started trial\", value: 3105 },\n  { label: \"Paid\", value: 1178 },\n];\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\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\n/** 0 → 1 (linear) once the element scrolls into view; 1 straight away with reduced motion. */\nfunction useReveal(\n  ref: React.RefObject<HTMLElement | null>,\n  reduce: boolean,\n  duration = 1400,\n) {\n  const [t, setT] = React.useState(0);\n  React.useEffect(() => {\n    const el = ref.current;\n    if (!el || reduce) return;\n    let raf = 0;\n    const io = new IntersectionObserver(\n      ([entry]) => {\n        if (!entry.isIntersecting) return;\n        io.disconnect();\n        const start = performance.now();\n        const tick = (now: number) => {\n          const k = Math.min(1, (now - start) / duration);\n          setT(k);\n          if (k < 1) raf = requestAnimationFrame(tick);\n        };\n        raf = requestAnimationFrame(tick);\n      },\n      { threshold: 0.2 },\n    );\n    io.observe(el);\n    return () => {\n      io.disconnect();\n      cancelAnimationFrame(raf);\n    };\n  }, [ref, reduce, duration]);\n  return reduce ? 1 : t;\n}\n\nconst easeOut = (t: number) => 1 - Math.pow(1 - Math.min(1, Math.max(0, t)), 3);\nconst pct = (v: number) => `${v >= 10 ? v.toFixed(0) : v.toFixed(1)}%`;\n\n/** Mix two hex colours. */\nfunction mix(a: string, b: string, t: number) {\n  const p = (h: string) => {\n    const s = h.replace(\"#\", \"\");\n    const f =\n      s.length === 3\n        ? s\n            .split(\"\")\n            .map((c) => c + c)\n            .join(\"\")\n        : s;\n    return [0, 2, 4].map((i) => parseInt(f.slice(i, i + 2), 16));\n  };\n  const [x, y] = [p(a), p(b)];\n  return `rgb(${x.map((v, i) => Math.round(v + (y[i] - v) * t)).join(\",\")})`;\n}\n\nexport function FunnelChart({\n  theme = \"dark\",\n  title = \"Signup funnel\",\n  subtitle = \"Last 30 days, all channels\",\n  stages = DEFAULT_STAGES,\n  colors,\n  height = 260,\n  formatValue = (v) => v.toLocaleString(\"en-US\"),\n  className,\n  style,\n  ...props\n}: FunnelChartProps) {\n  const pal = PALETTES[theme];\n  const [c0, c1] = colors ?? pal.ends;\n  const reduce = useReducedMotion();\n  const boxRef = React.useRef<HTMLDivElement>(null);\n  const width = useWidth(boxRef);\n  const reveal = useReveal(boxRef, reduce);\n  const [active, setActive] = React.useState<number | null>(null);\n  const id = React.useId();\n\n  const n = stages.length;\n  const top = stages[0]?.value || 1;\n  const overall = n > 1 ? (stages[n - 1].value / top) * 100 : 100;\n  const rows = width > 0 && width < 560;\n  const colW = n ? width / n : 0;\n  const labelH = 64;\n  const mid = labelH + height / 2;\n  const tw = colW * 0.38;\n  const grow = stages.map((_, i) =>\n    easeOut(reveal * 1.6 - (i / Math.max(1, n)) * 0.6),\n  );\n  const half = stages.map(\n    (s, i) => Math.max(2, (s.value / top) * (height / 2 - 4)) * grow[i],\n  );\n\n  // One continuous outline: flat across each column, curving between neighbours.\n  const edge = (sign: 1 | -1) => {\n    let d = \"\";\n    for (let i = 0; i < n; i++) {\n      const x0 = i * colW;\n      const x1 = (i + 1) * colW;\n      const y = mid - sign * half[i];\n      if (i === 0) d += `M${x0},${y}`;\n      d += `L${i < n - 1 ? x1 - tw / 2 : x1},${y}`;\n      if (i < n - 1) {\n        const yn = mid - sign * half[i + 1];\n        d += `C${x1},${y} ${x1},${yn} ${x1 + tw / 2},${yn}`;\n      }\n    }\n    return d;\n  };\n  const reversed = (d: string) => {\n    // Re-trace the bottom edge right to left by mirroring the top edge's structure.\n    let out = \"\";\n    for (let i = n - 1; i >= 0; i--) {\n      const x0 = i * colW;\n      const y = mid + half[i];\n      if (i === n - 1) out += `L${(i + 1) * colW},${y}`;\n      out += `L${i > 0 ? x0 + tw / 2 : x0},${y}`;\n      if (i > 0) {\n        const yp = mid + half[i - 1];\n        out += `C${x0},${y} ${x0},${yp} ${x0 - tw / 2},${yp}`;\n      }\n    }\n    return d + out + \"Z\";\n  };\n  const shape = n ? reversed(edge(1)) : \"\";\n\n  const onKey = (event: React.KeyboardEvent) => {\n    if (!n) return;\n    if (\n      [\"ArrowRight\", \"ArrowDown\", \"ArrowLeft\", \"ArrowUp\"].includes(event.key)\n    ) {\n      event.preventDefault();\n      const dir =\n        event.key === \"ArrowRight\" || event.key === \"ArrowDown\" ? 1 : -1;\n      setActive((a) =>\n        Math.max(\n          0,\n          Math.min(n - 1, a === null ? (dir > 0 ? 0 : n - 1) : a + dir),\n        ),\n      );\n    } else if (event.key === \"Escape\") setActive(null);\n  };\n  const summary = `${title}. ${stages.map((s, i) => `${s.label}: ${formatValue(s.value)}${i ? `, ${pct((s.value / stages[i - 1].value) * 100)} of the previous step` : \"\"}`).join(\"; \")}. Overall conversion ${pct(overall)}.`;\n\n  const tip =\n    active !== null && active > 0\n      ? {\n          lost: stages[active - 1].value - stages[active].value,\n          conv: (stages[active].value / stages[active - 1].value) * 100,\n        }\n      : null;\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=\"text-[15px] font-semibold tracking-[-0.01em]\">\n            {title}\n          </p>\n          {subtitle && (\n            <p className=\"mt-1 text-[12.5px]\" style={{ color: pal.sub }}>\n              {subtitle}\n            </p>\n          )}\n        </div>\n        <div className=\"text-right\">\n          <p className=\"text-[22px] leading-none font-semibold tracking-[-0.02em] tabular-nums\">\n            {pct(overall)}\n          </p>\n          <p className=\"mt-1 text-[11.5px]\" style={{ color: pal.sub }}>\n            {stages[0]?.label.toLowerCase()} →{\" \"}\n            {stages[n - 1]?.label.toLowerCase()}\n          </p>\n        </div>\n      </div>\n\n      <div\n        ref={boxRef}\n        role=\"img\"\n        aria-label={summary}\n        tabIndex={0}\n        onKeyDown={onKey}\n        onBlur={() => setActive(null)}\n        className=\"relative mt-5 w-full rounded-lg outline-none focus-visible:ring-2\"\n        style={{ minHeight: rows ? undefined : height + labelH + 8 }}\n      >\n        {rows ? (\n          <ol className=\"flex flex-col gap-3.5\">\n            {stages.map((s, i) => {\n              const share = (s.value / top) * 100;\n              return (\n                <li\n                  key={s.label}\n                  onPointerEnter={() => setActive(i)}\n                  onPointerLeave={() => setActive(null)}\n                >\n                  <div className=\"flex items-baseline justify-between gap-3 text-[12.5px]\">\n                    <span className=\"font-medium\">{s.label}</span>\n                    <span className=\"tabular-nums\">\n                      <span className=\"font-semibold\">\n                        {formatValue(s.value)}\n                      </span>\n                      <span className=\"ml-2\" style={{ color: pal.sub }}>\n                        {pct(share)}\n                      </span>\n                    </span>\n                  </div>\n                  <div\n                    className=\"mt-1.5 h-2.5 overflow-hidden rounded-full\"\n                    style={{ background: pal.track }}\n                  >\n                    <div\n                      className=\"h-full rounded-full\"\n                      style={{\n                        width: `${share * grow[i]}%`,\n                        background: mix(c0, c1, n > 1 ? i / (n - 1) : 0),\n                      }}\n                    />\n                  </div>\n                  {i < n - 1 && (\n                    <p\n                      className=\"mt-1.5 text-[11px] tabular-nums\"\n                      style={{ color: pal.sub }}\n                    >\n                      ↓ {pct((stages[i + 1].value / s.value) * 100)} continue ·{\" \"}\n                      <span style={{ color: pal.drop }}>\n                        {formatValue(s.value - stages[i + 1].value)} drop off\n                      </span>\n                    </p>\n                  )}\n                </li>\n              );\n            })}\n          </ol>\n        ) : (\n          width > 0 && (\n            <>\n              <svg\n                width={width}\n                height={height + labelH + 8}\n                className=\"block\"\n                aria-hidden=\"true\"\n              >\n                <defs>\n                  <linearGradient id={`${id}-fill`} x1=\"0\" y1=\"0\" x2=\"1\" y2=\"0\">\n                    <stop offset=\"0%\" stopColor={c0} />\n                    <stop offset=\"100%\" stopColor={c1} />\n                  </linearGradient>\n                  <linearGradient\n                    id={`${id}-shade`}\n                    x1=\"0\"\n                    y1=\"0\"\n                    x2=\"0\"\n                    y2=\"1\"\n                  >\n                    <stop offset=\"0%\" stopColor=\"#fff\" stopOpacity={0.18} />\n                    <stop offset=\"50%\" stopColor=\"#fff\" stopOpacity={0} />\n                    <stop offset=\"100%\" stopColor=\"#000\" stopOpacity={0.18} />\n                  </linearGradient>\n                </defs>\n                {stages.map((s, i) => (\n                  <g key={s.label}>\n                    {/* Ghost band: the full height of the first step, so drop-off reads at a glance. */}\n                    <rect\n                      x={i * colW + 2}\n                      y={labelH}\n                      width={colW - 4}\n                      height={height}\n                      rx={10}\n                      fill={pal.ghost}\n                    />\n                    {i > 0 && (\n                      <line\n                        x1={i * colW}\n                        x2={i * colW}\n                        y1={labelH - 6}\n                        y2={labelH + height}\n                        stroke={pal.grid}\n                        strokeDasharray=\"2 4\"\n                      />\n                    )}\n                  </g>\n                ))}\n                <path\n                  d={shape}\n                  fill={`url(#${id}-fill)`}\n                  fillOpacity={theme === \"dark\" ? 0.9 : 0.85}\n                />\n                <path d={shape} fill={`url(#${id}-shade)`} />\n                {active !== null && (\n                  <rect\n                    x={active * colW + 2}\n                    y={labelH}\n                    width={colW - 4}\n                    height={height}\n                    rx={10}\n                    fill={\n                      theme === \"dark\"\n                        ? \"rgba(255,255,255,0.08)\"\n                        : \"rgba(17,19,23,0.05)\"\n                    }\n                    stroke={pal.pillBorder}\n                  />\n                )}\n                {stages.map((s, i) => (\n                  <g\n                    key={`l${s.label}`}\n                    opacity={active === null || active === i ? 1 : 0.5}\n                    style={{ transition: \"opacity 160ms\" }}\n                  >\n                    <text\n                      x={i * colW + 12}\n                      y={18}\n                      fontSize={11.5}\n                      fill={pal.sub}\n                    >\n                      {s.label}\n                    </text>\n                    <text\n                      x={i * colW + 12}\n                      y={42}\n                      fontSize={20}\n                      fontWeight={600}\n                      fill={pal.text}\n                      letterSpacing=\"-0.02em\"\n                      className=\"tabular-nums\"\n                    >\n                      {formatValue(s.value)}\n                    </text>\n                    <text\n                      x={i * colW + 12}\n                      y={58}\n                      fontSize={11}\n                      fill={pal.sub}\n                      className=\"tabular-nums\"\n                    >\n                      {pct((s.value / top) * 100)} of total\n                    </text>\n                  </g>\n                ))}\n              </svg>\n              {/* Step conversion pills, sitting on the boundaries. */}\n              {stages.slice(1).map((s, k) => {\n                const i = k + 1;\n                return (\n                  <span\n                    key={`p${s.label}`}\n                    className=\"pointer-events-none absolute -translate-1/2 rounded-full px-2 py-0.5 text-[11px] font-semibold whitespace-nowrap tabular-nums shadow-sm\"\n                    style={{\n                      left: i * colW,\n                      top: mid,\n                      background: pal.pill,\n                      border: `1px solid ${pal.pillBorder}`,\n                      color: pal.text,\n                      opacity: grow[i],\n                      backdropFilter: \"blur(6px)\",\n                    }}\n                  >\n                    {pct((s.value / stages[i - 1].value) * 100)}\n                  </span>\n                );\n              })}\n              {/* Hover targets: one per column. */}\n              <div className=\"absolute inset-0 flex\">\n                {stages.map((s, i) => (\n                  <div\n                    key={`h${s.label}`}\n                    className=\"h-full flex-1\"\n                    onPointerEnter={() => setActive(i)}\n                    onPointerDown={() => setActive(i)}\n                    onPointerLeave={(e) =>\n                      e.pointerType === \"mouse\" && setActive(null)\n                    }\n                  />\n                ))}\n              </div>\n              {active !== null && (\n                <div\n                  className=\"pointer-events-none absolute z-10 min-w-[180px] rounded-xl px-3 py-2.5 text-[12px] shadow-xl\"\n                  style={{\n                    left: Math.min(\n                      width - 196,\n                      Math.max(4, active * colW + colW / 2 - 92),\n                    ),\n                    top: Math.min(\n                      labelH + height - 96,\n                      mid + half[active] + 10,\n                    ),\n                    background: pal.tip,\n                    border: `1px solid ${pal.tipBorder}`,\n                    backdropFilter: \"blur(8px)\",\n                  }}\n                >\n                  <p className=\"font-semibold\">{stages[active].label}</p>\n                  <div className=\"mt-1.5 flex justify-between gap-4\">\n                    <span style={{ color: pal.sub }}>People</span>\n                    <span className=\"font-semibold tabular-nums\">\n                      {formatValue(stages[active].value)}\n                    </span>\n                  </div>\n                  {tip ? (\n                    <>\n                      <div className=\"mt-1 flex justify-between gap-4\">\n                        <span style={{ color: pal.sub }}>\n                          From previous step\n                        </span>\n                        <span className=\"font-semibold tabular-nums\">\n                          {pct(tip.conv)}\n                        </span>\n                      </div>\n                      <div className=\"mt-1 flex justify-between gap-4\">\n                        <span style={{ color: pal.sub }}>Dropped off</span>\n                        <span\n                          className=\"font-semibold tabular-nums\"\n                          style={{ color: pal.drop }}\n                        >\n                          −{formatValue(tip.lost)}\n                        </span>\n                      </div>\n                    </>\n                  ) : (\n                    <p className=\"mt-1\" style={{ color: pal.sub }}>\n                      Top of the funnel\n                    </p>\n                  )}\n                </div>\n              )}\n            </>\n          )\n        )}\n      </div>\n    </div>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}