{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "funnel-chart",
  "title": "Funnel chart",
  "description": "Funnel whose stage bars grow open one after another on an ordinal ramp, with drop-off chips between stages, conversion from the first stage beside each value, and arrow-key rows.",
  "dependencies": [
    "motion"
  ],
  "registryDependencies": [
    "https://goodui.dev/r/goodui-motion.json",
    "https://goodui.dev/r/locale.json"
  ],
  "files": [
    {
      "path": "registry/components/charts/funnel-chart.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport { motion, useReducedMotion } from \"motion/react\"\nimport { cn } from \"@/lib/utils\"\nimport { useLabels } from \"@/lib/goodui-locale\"\nimport { spring, ease as easing } from \"@/lib/goodui-motion\"\n\nexport type FunnelStage = {\n  /** Defaults to the label. */\n  key?: string\n  label: string\n  value: number\n  /** Any CSS color. Defaults to an ordinal ramp of `color`, darkest first. */\n  color?: string\n}\n\nexport type FunnelChartLabels = {\n  summary: (name: string, first: string, last: string, conversion: string) => string\n  /** Announced for the active stage. */\n  stage: (label: string, value: string, conversion: string, dropOff: string | null) => string\n  /** Chip between stages, e.g. \"−38%\". */\n  dropOff: (percent: string) => string\n  /** Share of the first stage, e.g. \"31% of Visited\". */\n  conversion: (percent: string, first: string) => string\n  columnStage: string\n  columnValue: string\n  columnConversion: string\n  noData: string\n}\nconst DEFAULT_LABELS: FunnelChartLabels = {\n  summary: (name, first, last, conversion) => `${name ? name + \": \" : \"\"}from ${first} to ${last}, ${conversion} overall`,\n  stage: (label, value, conversion, dropOff) => `${label}: ${value}, ${conversion}${dropOff ? `, ${dropOff} from the previous stage` : \"\"}`,\n  dropOff: (percent) => `−${percent}`,\n  conversion: (percent, first) => `${percent} of ${first}`,\n  columnStage: \"Stage\",\n  columnValue: \"Value\",\n  columnConversion: \"Conversion\",\n  noData: \"No data\",\n}\n\nexport type FunnelChartProps = Omit<React.ComponentProps<\"div\">, \"children\"> & {\n  stages: FunnelStage[]\n  /** Chart name for screen readers and the table caption. */\n  title?: React.ReactNode\n  /** center: symmetric funnel. start: bars aligned to the start side, like a ranked list. */\n  align?: \"center\" | \"start\"\n  /** Base hue of the ordinal ramp (first stage full, later stages lighter). */\n  color?: string\n  /** \"−38%\" chips between stages. */\n  showDropOff?: boolean\n  /** Share of the first stage beside each value. */\n  showConversion?: boolean\n  /** Width of the label column in px. */\n  labelWidth?: number\n  rowHeight?: number\n  format?: Intl.NumberFormatOptions\n  locale?: string\n  formatValue?: (value: number) => string\n  /** Seconds the first bar takes to grow; each next one starts 0.08 s later. */\n  duration?: number\n  /** Text for built-in controls and screen readers; pass translations here. */\n  labels?: Partial<FunnelChartLabels>\n}\n\nfunction useReduce() {\n  const prefers = useReducedMotion()\n  const [mounted, setMounted] = React.useState(false)\n  React.useEffect(() => setMounted(true), [])\n  return mounted && !!prefers\n}\n\nfunction useInView(ref: React.RefObject<Element | null>, threshold = 0.2) {\n  const [inView, setInView] = React.useState(false)\n  React.useEffect(() => {\n    const el = ref.current\n    if (!el || inView) return\n    if (typeof IntersectionObserver === \"undefined\") {\n      setInView(true)\n      return\n    }\n    const io = new IntersectionObserver(\n      ([e]) => {\n        if (e?.isIntersecting) {\n          setInView(true)\n          io.disconnect()\n        }\n      },\n      { threshold },\n    )\n    io.observe(el)\n    return () => io.disconnect()\n  }, [ref, inView, threshold])\n  return inView\n}\n\n/** Funnel whose stage bars grow in sequence, with drop-off chips between stages, conversion from the first stage and arrow-key navigation. */\nexport function FunnelChart({\n  stages,\n  title,\n  align = \"center\",\n  color = \"var(--chart-1)\",\n  showDropOff = true,\n  showConversion = true,\n  labelWidth = 112,\n  rowHeight = 40,\n  format,\n  locale = \"en-US\",\n  formatValue,\n  duration = 0.8,\n  labels,\n  className,\n  \"aria-label\": ariaLabel,\n  ...props\n}: FunnelChartProps) {\n  const l = useLabels(\"funnel-chart\", DEFAULT_LABELS, labels)\n  const reduce = useReduce()\n  const rootRef = React.useRef<HTMLDivElement>(null)\n  const inView = useInView(rootRef)\n  const [active, setActive] = React.useState<number | null>(null)\n  const fmt = React.useMemo(() => formatValue ?? ((v: number) => new Intl.NumberFormat(locale, format).format(v)), [formatValue, locale, format])\n  const pct = React.useMemo(() => new Intl.NumberFormat(locale, { style: \"percent\", maximumFractionDigits: 0 }), [locale])\n\n  const n = stages.length\n  const first = stages[0]?.value ?? 0\n  const items = stages.map((s, i) => {\n    const prev = i > 0 ? (stages[i - 1]?.value ?? 0) : null\n    return {\n      ...s,\n      key: s.key ?? s.label,\n      share: first > 0 ? Math.max(0, s.value / first) : 0,\n      conversion: first > 0 ? s.value / first : 0,\n      dropOff: prev && prev > 0 ? Math.max(0, 1 - s.value / prev) : null,\n      color: s.color ?? `color-mix(in oklab, ${color} ${Math.round(100 - (n > 1 ? (i / (n - 1)) * 58 : 0))}%, var(--background))`,\n    }\n  })\n  const name = typeof title === \"string\" ? title : \"\"\n  const lastItem = items[n - 1]\n  const summary = ariaLabel ?? (n && lastItem ? l.summary(name, fmt(first), fmt(lastItem.value), pct.format(lastItem.conversion)) : l.noData)\n  const activeItem = active !== null ? items[active] : undefined\n  const announce = activeItem ? l.stage(activeItem.label, fmt(activeItem.value), l.conversion(pct.format(activeItem.conversion), items[0]?.label ?? \"\"), activeItem.dropOff !== null ? l.dropOff(pct.format(activeItem.dropOff)) : null) : \"\"\n\n  const onKeyDown = (e: React.KeyboardEvent) => {\n    if (!n) return\n    if ([\"ArrowUp\", \"ArrowDown\", \"ArrowLeft\", \"ArrowRight\"].includes(e.key)) {\n      e.preventDefault()\n      const dir = e.key === \"ArrowUp\" || e.key === \"ArrowLeft\" ? -1 : 1\n      setActive((i) => Math.min(n - 1, Math.max(0, (i ?? (dir > 0 ? -1 : n)) + dir)))\n    } else if (e.key === \"Home\" || e.key === \"End\") {\n      e.preventDefault()\n      setActive(e.key === \"Home\" ? 0 : n - 1)\n    } else if (e.key === \"Escape\") setActive(null)\n  }\n\n  const ready = inView || reduce\n  const center = align === \"center\"\n\n  return (\n    <div ref={rootRef} className={cn(\"pc-funnel-chart flex w-full min-w-0 flex-col gap-2\", className)} {...props}>\n      <div\n        role=\"group\"\n        aria-roledescription=\"funnel chart\"\n        aria-label={summary}\n        tabIndex={0}\n        onKeyDown={onKeyDown}\n        onFocus={() => setActive((i) => i ?? (n ? 0 : null))}\n        onBlur={() => setActive(null)}\n        onPointerLeave={() => setActive(null)}\n        className=\"flex flex-col rounded-md outline-none focus-visible:ring-3 focus-visible:ring-ring/50\"\n      >\n        {items.map((s, i) => {\n          const isActive = active === i\n          const dim = active !== null && !isActive\n          return (\n            <div\n              key={s.key}\n              data-active={isActive || undefined}\n              onPointerEnter={() => setActive(i)}\n              className={cn(\"grid items-center gap-3 rounded-md px-1 transition-colors\", isActive && \"bg-muted/60\")}\n              style={{ gridTemplateColumns: `${labelWidth}px minmax(0,1fr) auto`, height: rowHeight }}\n            >\n              <span className={cn(\"truncate text-sm transition-opacity\", dim && \"opacity-60\", isActive ? \"font-medium text-foreground\" : \"text-foreground/85\")}>{s.label}</span>\n              <div className=\"relative h-full\">\n                {showDropOff && s.dropOff !== null && (\n                  <motion.span\n                    className={cn(\"absolute top-0 z-[1] -translate-y-1/2 rounded-full border bg-background px-1.5 text-[10px] leading-4 font-medium tabular-nums text-muted-foreground\", center ? \"left-1/2 -translate-x-1/2\" : \"start-0\")}\n                    initial={false}\n                    animate={{ opacity: ready ? 1 : 0, scale: ready ? 1 : 0.6 }}\n                    transition={reduce ? { duration: 0 } : { ...spring.play, delay: i * 0.08 + duration * 0.55 }}\n                    aria-hidden=\"true\"\n                  >\n                    {l.dropOff(pct.format(s.dropOff))}\n                  </motion.span>\n                )}\n                <motion.div\n                  className={cn(\"absolute top-1/2 h-[55%] rounded-[5px] transition-opacity\", center ? \"origin-center\" : \"origin-left rtl:origin-right\", dim && \"opacity-55\")}\n                  style={{ background: s.color, width: `${Math.max(0.6, s.share * 100)}%`, insetInlineStart: center ? `${50 - Math.max(0.6, s.share * 100) / 2}%` : 0, y: \"-50%\" }}\n                  initial={false}\n                  animate={{ scaleX: ready ? 1 : 0 }}\n                  transition={reduce ? { duration: 0 } : { duration, delay: i * 0.08, ease: easing.outExpo }}\n                />\n              </div>\n              <motion.span className=\"flex flex-col items-end text-end leading-tight\" initial={false} animate={{ opacity: ready ? 1 : 0 }} transition={reduce ? { duration: 0 } : { duration: 0.35, delay: i * 0.08 + duration * 0.4 }}>\n                <span className=\"text-sm font-semibold tabular-nums\">{fmt(s.value)}</span>\n                {showConversion && <span className=\"text-[11px] text-muted-foreground tabular-nums\">{pct.format(s.conversion)}</span>}\n              </motion.span>\n            </div>\n          )\n        })}\n        {n === 0 && <div className=\"grid h-24 place-items-center text-sm text-muted-foreground\">{l.noData}</div>}\n        <span className=\"sr-only\" aria-live=\"polite\">\n          {announce}\n        </span>\n      </div>\n      {n > 0 && (\n        <div className=\"sr-only\">\n          <table>\n          <caption>{summary}</caption>\n          <thead>\n            <tr>\n              <th scope=\"col\">{l.columnStage}</th>\n              <th scope=\"col\">{l.columnValue}</th>\n              <th scope=\"col\">{l.columnConversion}</th>\n            </tr>\n          </thead>\n          <tbody>\n            {items.map((s) => (\n              <tr key={s.key}>\n                <th scope=\"row\">{s.label}</th>\n                <td>{fmt(s.value)}</td>\n                <td>{pct.format(s.conversion)}</td>\n              </tr>\n            ))}\n          </tbody>\n        </table>\n        </div>\n      )}\n    </div>\n  )\n}\n",
      "type": "registry:component",
      "target": "components/goodui/funnel-chart.tsx"
    }
  ],
  "categories": [
    "components",
    "charts"
  ],
  "type": "registry:component"
}