{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"conversion-funnel","type":"registry:block","title":"Conversion funnel","description":"Visitors to paid with narrowing bars that fill in cascade, step-to-step drop-off percentages, and a slow flow shimmer on hover.","dependencies":["motion","lucide-react"],"registryDependencies":["utils"],"files":[{"path":"registry/prequel/conversion-funnel/conversion-funnel.tsx","type":"registry:component","content":"\"use client\";\n\nimport { Fragment, useMemo, useRef, useState } from \"react\";\nimport {\n  MotionConfig,\n  motion,\n  useInView,\n  useReducedMotion,\n} from \"motion/react\";\nimport { TrendingDown } from \"lucide-react\";\nimport { cn } from \"@/lib/utils\";\n\nconst EASE: [number, number, number, number] = [0.22, 1, 0.36, 1];\n\n/* timing shared by every stage so the bars fill in cascade */\nconst FILL_DURATION = 0.9;\nconst STAGGER = 0.12;\nconst BASE_DELAY = 0.15;\n\n/* ------------------------------------------------------------------ */\n/* types                                                               */\n/* ------------------------------------------------------------------ */\n\nexport type ConversionFunnelStep = {\n  id: string;\n  label: string;\n  /** Count at this stage (visitors, signups, …). */\n  value: number;\n  /** Short context line under the label, e.g. \"Sessions\". */\n  detail?: string;\n  /** Chart colour token 1–5. Defaults to the step index + 1. */\n  tone?: number;\n};\n\nexport type ConversionFunnelProps = {\n  className?: string;\n  title?: string;\n  description?: string;\n  /** Funnel stages, widest first. The first stage defines the 100% width. */\n  steps?: ConversionFunnelStep[];\n  formatValue?: (value: number) => string;\n  /** Receives the share of the top of funnel, e.g. 39.7. */\n  formatShare?: (share: number) => string;\n  /** Receives the step-to-step drop-off, e.g. 60.3. */\n  formatDropoff?: (dropoff: number) => string;\n};\n\n/* ------------------------------------------------------------------ */\n/* deterministic demo data (no Math.random / Date.now during render)   */\n/* ------------------------------------------------------------------ */\n\nconst DEFAULT_STEPS: ConversionFunnelStep[] = [\n  { id: \"visitors\", label: \"Visitors\", value: 24800, detail: \"Sessions\", tone: 1 },\n  { id: \"signups\", label: \"Signups\", value: 9840, detail: \"New accounts\", tone: 2 },\n  { id: \"trials\", label: \"Trials\", value: 2614, detail: \"Free trials\", tone: 3 },\n  { id: \"paid\", label: \"Paid\", value: 776, detail: \"Subscribers\", tone: 4 },\n];\n\n/* ------------------------------------------------------------------ */\n/* helpers                                                             */\n/* ------------------------------------------------------------------ */\n\nfunction clamp(v: number, min: number, max: number) {\n  return Math.min(max, Math.max(min, v));\n}\n\nfunction trimZero(n: number) {\n  const s = n.toFixed(1);\n  return s.endsWith(\".0\") ? s.slice(0, -2) : s;\n}\n\n/* ------------------------------------------------------------------ */\n/* geometry                                                             */\n/* ------------------------------------------------------------------ */\n\ntype FunnelRow = {\n  step: ConversionFunnelStep;\n  index: number;\n  color: string;\n  /** Bar width relative to the first (top) stage, 0–1. */\n  fraction: number;\n  /** Share of the top of funnel, as a percentage. */\n  share: number;\n  /** Drop-off to the next stage, as a percentage; null on the last stage. */\n  dropoff: number | null;\n  nextLabel: string | null;\n};\n\nfunction computeRows(steps: ConversionFunnelStep[]): FunnelRow[] {\n  const firstValue = steps[0]?.value ?? 0;\n\n  return steps.map((step, i) => {\n    const tone = clamp(step.tone ?? i + 1, 1, 5);\n    const fraction = firstValue > 0 ? clamp(step.value / firstValue, 0, 1) : 0;\n    const share = firstValue > 0 ? (step.value / firstValue) * 100 : 0;\n\n    const next = steps[i + 1];\n    const dropoff =\n      next && step.value > 0\n        ? clamp((1 - next.value / step.value) * 100, 0, 100)\n        : null;\n\n    return {\n      step,\n      index: i,\n      color: `var(--chart-${tone})`,\n      fraction,\n      share,\n      dropoff,\n      nextLabel: next ? next.label : null,\n    };\n  });\n}\n\n/* ------------------------------------------------------------------ */\n/* stage row                                                           */\n/* ------------------------------------------------------------------ */\n\ntype FunnelRowProps = {\n  row: FunnelRow;\n  show: boolean;\n  reduce: boolean;\n  formatValue: (value: number) => string;\n  formatShare: (share: number) => string;\n  formatDropoff: (dropoff: number) => string;\n};\n\nfunction FunnelRow({\n  row,\n  show,\n  reduce,\n  formatValue,\n  formatShare,\n  formatDropoff,\n}: FunnelRowProps) {\n  const [hovered, setHovered] = useState(false);\n  const { step, index, color, fraction, share, dropoff, nextLabel } = row;\n\n  const fillDelay = BASE_DELAY + index * STAGGER;\n\n  const tooltipText =\n    dropoff != null && nextLabel\n      ? `${step.label}: ${formatValue(step.value)} · ${formatDropoff(dropoff)} drop-off to ${nextLabel}`\n      : `${step.label}: ${formatValue(step.value)} · ${formatShare(share)} of total`;\n\n  return (\n    <motion.li\n      initial={{ opacity: 0, y: reduce ? 0 : 14 }}\n      animate={{ opacity: show ? 1 : 0, y: 0 }}\n      transition={{\n        duration: reduce ? 0 : 0.5,\n        delay: reduce ? 0 : index * 0.06,\n        ease: EASE,\n      }}\n    >\n      <div className=\"relative\">\n        {/* tooltip: follows keyboard focus as well as the pointer */}\n        <motion.div\n          aria-hidden=\"true\"\n          initial={false}\n          animate={{ opacity: hovered ? 1 : 0, y: hovered ? 0 : 4, x: \"-50%\" }}\n          transition={{ duration: 0.18, ease: EASE }}\n          className=\"pointer-events-none absolute bottom-full left-1/2 z-10 mb-2 w-max max-w-[calc(100%-1rem)] rounded-lg border border-border bg-card px-2.5 py-1.5 text-center text-[11.5px] leading-4 text-muted-foreground shadow-md\"\n        >\n          <span className=\"tabular-nums\">{tooltipText}</span>\n        </motion.div>\n\n        <button\n          type=\"button\"\n          aria-label={tooltipText}\n          onMouseEnter={() => setHovered(true)}\n          onMouseLeave={() => setHovered(false)}\n          onFocus={() => setHovered(true)}\n          onBlur={() => setHovered(false)}\n          className=\"flex w-full items-center gap-2 rounded-lg px-2 py-1.5 text-left transition-colors hover:bg-muted/40 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring sm:gap-3\"\n        >\n          {/* label + detail */}\n          <div className=\"w-14 min-w-0 shrink-0 sm:w-24\">\n            <div className=\"truncate text-[13.5px] font-medium text-card-foreground\">\n              {step.label}\n            </div>\n            {step.detail ? (\n              <div className=\"truncate text-[11.5px] text-muted-foreground\">\n                {step.detail}\n              </div>\n            ) : null}\n          </div>\n\n          {/* the bar: centred, narrowing, fills in from the left */}\n          <div className=\"relative h-9 min-w-0 flex-1\">\n            <div\n              className=\"relative mx-auto h-full overflow-hidden rounded-lg\"\n              style={{ width: `${clamp(fraction * 100, 0, 100)}%` }}\n            >\n              <motion.div\n                className=\"absolute inset-0 rounded-lg\"\n                style={{ background: color, transformOrigin: \"0% 50%\" }}\n                initial={{ scaleX: reduce ? 1 : 0 }}\n                animate={{ scaleX: show ? 1 : 0 }}\n                transition={\n                  reduce\n                    ? { duration: 0 }\n                    : { duration: FILL_DURATION, delay: fillDelay, ease: EASE }\n                }\n              />\n\n              {/* slow flow shimmer, only while hovered */}\n              <motion.span\n                aria-hidden=\"true\"\n                className=\"absolute inset-y-0 left-0 w-1/2 bg-gradient-to-r from-transparent via-foreground/20 to-transparent\"\n                initial={false}\n                animate={\n                  hovered && show && !reduce\n                    ? { x: [\"-100%\", \"200%\"], opacity: 1 }\n                    : { x: \"-100%\", opacity: 0 }\n                }\n                transition={\n                  hovered && show && !reduce\n                    ? { duration: 2.4, repeat: Infinity, ease: \"linear\" }\n                    : { duration: 0.25 }\n                }\n              />\n            </div>\n          </div>\n\n          {/* value + share */}\n          <div className=\"w-16 shrink-0 text-right sm:w-20\">\n            <div className=\"text-[15px] font-semibold leading-tight tabular-nums text-card-foreground\">\n              {formatValue(step.value)}\n            </div>\n            <div className=\"text-[11.5px] tabular-nums text-muted-foreground\">\n              {formatShare(share)}\n            </div>\n          </div>\n        </button>\n      </div>\n    </motion.li>\n  );\n}\n\n/* ------------------------------------------------------------------ */\n/* drop-off connector between stages                                    */\n/* ------------------------------------------------------------------ */\n\nfunction DropoffConnector({\n  label,\n  show,\n  reduce,\n  index,\n}: {\n  label: string;\n  show: boolean;\n  reduce: boolean;\n  index: number;\n}) {\n  const delay = BASE_DELAY + index * STAGGER + FILL_DURATION * 0.4;\n\n  return (\n    <motion.div\n      aria-hidden=\"true\"\n      initial={{ opacity: reduce ? 1 : 0 }}\n      animate={{ opacity: show ? 1 : 0 }}\n      transition={{\n        duration: reduce ? 0 : 0.4,\n        delay: reduce ? 0 : delay,\n        ease: EASE,\n      }}\n      className=\"my-0.5 flex items-center justify-center gap-1.5 text-muted-foreground\"\n    >\n      <TrendingDown className=\"h-3 w-3\" />\n      <span className=\"text-[11.5px] font-medium tabular-nums\">{label}</span>\n      <span className=\"text-[11.5px]\">drop-off</span>\n    </motion.div>\n  );\n}\n\n/* ------------------------------------------------------------------ */\n/* block                                                               */\n/* ------------------------------------------------------------------ */\n\nexport function ConversionFunnel({\n  className,\n  title = \"Conversion funnel\",\n  description = \"Visitors to paid, one step at a time.\",\n  steps = DEFAULT_STEPS,\n  formatValue,\n  formatShare,\n  formatDropoff,\n}: ConversionFunnelProps) {\n  const cardRef = useRef<HTMLDivElement>(null);\n  const inView = useInView(cardRef, { once: true, margin: \"-10% 0px\" });\n  const reduce = Boolean(useReducedMotion());\n  const show = inView;\n\n  const rows = useMemo(() => computeRows(steps), [steps]);\n\n  const formatVal =\n    formatValue ?? ((n: number) => Math.round(n).toLocaleString(\"en-US\"));\n  const formatPct = formatShare ?? ((s: number) => `${trimZero(s)}%`);\n  const formatDrop =\n    formatDropoff ?? ((d: number) => `${trimZero(d)}%`);\n\n  const overallShare = rows.length ? rows[rows.length - 1].share : 0;\n\n  const ariaLabel = `${title}. Conversion funnel from ${steps\n    .map((s) => `${s.label} ${formatVal(s.value)}`)\n    .join(\", \")}. Overall conversion ${formatPct(overallShare)}.`;\n\n  return (\n    <MotionConfig reducedMotion=\"user\">\n      <motion.div\n        ref={cardRef}\n        initial={{ opacity: 0, y: 20 }}\n        animate={show ? { opacity: 1, y: 0 } : { opacity: 0, y: 20 }}\n        transition={{ duration: 0.6, ease: EASE }}\n        className={cn(\n          \"rounded-xl border border-border bg-card text-card-foreground shadow-sm\",\n          className\n        )}\n      >\n        <div className=\"p-5 sm:p-6\">\n          <div className=\"flex flex-wrap items-start justify-between gap-3\">\n            <div>\n              <h2 className=\"text-[15px] font-semibold tracking-tight\">\n                {title}\n              </h2>\n              {description ? (\n                <p className=\"mt-0.5 text-[13px] text-muted-foreground\">\n                  {description}\n                </p>\n              ) : null}\n            </div>\n\n            {/* the lens: one small glint dot inside the overall conversion badge */}\n            <motion.div\n              initial={{ opacity: reduce ? 1 : 0, y: reduce ? 0 : 6 }}\n              animate={{ opacity: show ? 1 : 0, y: 0 }}\n              transition={{\n                duration: reduce ? 0 : 0.5,\n                delay: reduce ? 0 : 0.15,\n                ease: EASE,\n              }}\n              className=\"inline-flex items-center gap-1.5 rounded-full border border-border px-2.5 py-1 text-[12px] font-medium text-muted-foreground\"\n            >\n              <span className=\"relative inline-flex h-[14px] w-[14px] items-center justify-center\">\n                <span className=\"absolute inset-0 rounded-full border border-foreground/20\" />\n                <motion.span\n                  className=\"h-[5px] w-[5px] rounded-full bg-foreground\"\n                  initial={{ scale: reduce ? 1 : 0 }}\n                  animate={{ scale: show ? 1 : 0 }}\n                  transition={{\n                    duration: reduce ? 0 : 0.4,\n                    delay: reduce ? 0 : 0.45,\n                    ease: EASE,\n                  }}\n                />\n              </span>\n              <span>\n                Overall{\" \"}\n                <span className=\"tabular-nums\">{formatPct(overallShare)}</span>\n              </span>\n            </motion.div>\n          </div>\n\n          <ul className=\"mt-5 space-y-1\" role=\"list\" aria-label={ariaLabel}>\n            {rows.map((row) => (\n              <Fragment key={row.step.id}>\n                <FunnelRow\n                  row={row}\n                  show={show}\n                  reduce={reduce}\n                  formatValue={formatVal}\n                  formatShare={formatPct}\n                  formatDropoff={formatDrop}\n                />\n                {row.dropoff != null ? (\n                  <DropoffConnector\n                    label={`−${formatDrop(row.dropoff)}`}\n                    show={show}\n                    reduce={reduce}\n                    index={row.index}\n                  />\n                ) : null}\n              </Fragment>\n            ))}\n          </ul>\n        </div>\n      </motion.div>\n    </MotionConfig>\n  );\n}\n\nexport default ConversionFunnel;\n"}]}