{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"stat-ticker","type":"registry:block","title":"Stat ticker","description":"A horizontally scrolling ticker of live stats (MRR, signups, conversion, NPS) that loops seamlessly, fades at the edges, and pauses on hover or focus.","dependencies":["motion","lucide-react"],"registryDependencies":["utils"],"files":[{"path":"registry/prequel/stat-ticker/stat-ticker.tsx","type":"registry:component","content":"\"use client\";\n\nimport {\n  useEffect,\n  useRef,\n  useState,\n  type CSSProperties,\n} from \"react\";\nimport {\n  AnimatePresence,\n  MotionConfig,\n  motion,\n  useInView,\n  useReducedMotion,\n} from \"motion/react\";\nimport {\n  Activity,\n  MousePointerClick,\n  Smile,\n  TrendingDown,\n  TrendingUp,\n  UserPlus,\n  type LucideIcon,\n} from \"lucide-react\";\nimport { cn } from \"@/lib/utils\";\n\nconst EASE: [number, number, number, number] = [0.22, 1, 0.36, 1];\n\n/* a single keyframe, shared by every instance of the block */\nconst KEYFRAMES = `\n@keyframes prequel-ticker-scroll {\n  from { transform: translateX(0); }\n  to { transform: translateX(-50%); }\n}\n`;\n\n/* ------------------------------------------------------------------ */\n/* types                                                               */\n/* ------------------------------------------------------------------ */\n\nexport type StatTickerItem = {\n  id: string;\n  label: string;\n  /** Display values, cycled in order as the ticker refreshes. */\n  values: string[];\n  /** Optional Lucide icon in a small square before the value. */\n  icon?: LucideIcon;\n};\n\nexport type StatTickerProps = {\n  className?: string;\n  items?: StatTickerItem[];\n  /** Seconds between each value refresh. Defaults to 4. */\n  interval?: number;\n  /** Seconds for one full loop of the track. Defaults to 42. */\n  duration?: number;\n};\n\n/* ------------------------------------------------------------------ */\n/* deterministic demo data (no Math.random / Date.now at module load)  */\n/* ------------------------------------------------------------------ */\n\nconst DEFAULT_ITEMS: StatTickerItem[] = [\n  {\n    id: \"mrr\",\n    label: \"MRR\",\n    icon: TrendingUp,\n    values: [\"$84,260\", \"$84,268\", \"$84,277\", \"$84,271\", \"$84,285\"],\n  },\n  {\n    id: \"signups\",\n    label: \"Signups today\",\n    icon: UserPlus,\n    values: [\"1,284\", \"1,291\", \"1,298\", \"1,305\", \"1,312\"],\n  },\n  {\n    id: \"conversion\",\n    label: \"Conversion\",\n    icon: MousePointerClick,\n    values: [\"3.2%\", \"3.4%\", \"3.1%\", \"3.5%\", \"3.3%\"],\n  },\n  {\n    id: \"nps\",\n    label: \"NPS\",\n    icon: Smile,\n    values: [\"71\", \"72\", \"70\", \"73\", \"74\"],\n  },\n  {\n    id: \"active\",\n    label: \"Active now\",\n    icon: Activity,\n    values: [\"2,847\", \"2,902\", \"2,876\", \"2,931\", \"2,915\"],\n  },\n  {\n    id: \"churn\",\n    label: \"Revenue churn\",\n    icon: TrendingDown,\n    values: [\"1.9%\", \"1.8%\", \"1.9%\", \"1.7%\", \"1.8%\"],\n  },\n];\n\n/* ------------------------------------------------------------------ */\n/* a value that flips with a vertical slide when it changes            */\n/* ------------------------------------------------------------------ */\n\nfunction RollingValue({\n  value,\n  reduce,\n  className,\n}: {\n  value: string;\n  reduce: boolean;\n  className?: string;\n}) {\n  if (reduce) {\n    return (\n      <span className={cn(\"inline-block whitespace-nowrap tabular-nums\", className)}>\n        {value}\n      </span>\n    );\n  }\n\n  return (\n    <span className={cn(\"relative inline-flex overflow-hidden\", className)}>\n      <AnimatePresence initial={false} mode=\"popLayout\">\n        <motion.span\n          key={value}\n          initial={{ y: \"110%\" }}\n          animate={{ y: \"0%\" }}\n          exit={{ y: \"-110%\" }}\n          transition={{ duration: 0.55, ease: EASE }}\n          className=\"inline-block whitespace-nowrap tabular-nums\"\n        >\n          {value}\n        </motion.span>\n      </AnimatePresence>\n    </span>\n  );\n}\n\n/* ------------------------------------------------------------------ */\n/* one compact stat in the ticker                                      */\n/* ------------------------------------------------------------------ */\n\nfunction TickerStat({\n  item,\n  value,\n  reduce,\n}: {\n  item: StatTickerItem;\n  value: string;\n  reduce: boolean;\n}) {\n  const Icon = item.icon;\n\n  return (\n    <div className=\"flex shrink-0 items-center gap-3\">\n      {Icon ? (\n        <span className=\"grid h-8 w-8 shrink-0 place-items-center rounded-lg border border-border bg-muted text-muted-foreground\">\n          <Icon className=\"h-4 w-4\" />\n        </span>\n      ) : null}\n      <div className=\"flex min-w-0 flex-col gap-1\">\n        <span className=\"whitespace-nowrap text-xs font-medium text-muted-foreground\">\n          {item.label}\n        </span>\n        <RollingValue\n          value={value}\n          reduce={reduce}\n          className=\"text-[22px] font-semibold leading-none tracking-tight text-card-foreground\"\n        />\n      </div>\n    </div>\n  );\n}\n\n/* ------------------------------------------------------------------ */\n/* block                                                               */\n/* ------------------------------------------------------------------ */\n\nexport function StatTicker({\n  className,\n  items = DEFAULT_ITEMS,\n  interval = 4,\n  duration = 42,\n}: StatTickerProps) {\n  const ref = useRef<HTMLDivElement>(null);\n  const inView = useInView(ref, { once: true, margin: \"-10% 0px\" });\n  const reduce = Boolean(useReducedMotion());\n  const show = inView;\n  const [hovered, setHovered] = useState(false);\n  const [focused, setFocused] = useState(false);\n\n  const safeItems = items.length > 0 ? items : DEFAULT_ITEMS;\n  const intervalMs = Math.max(1, interval) * 1000;\n  const safeDuration = Math.max(1, duration);\n\n  /* one shared value step per stat, so the two copies stay in sync */\n  const [steps, setSteps] = useState<number[]>(() => safeItems.map(() => 0));\n\n  const paused = hovered || focused;\n  const running = show && !paused && !reduce;\n\n  /* refresh values on a gentle, per-stat stagger; everything pauses on\n     hover/focus and under reduced motion. */\n  useEffect(() => {\n    if (!running) return;\n    const timeouts: number[] = [];\n    const intervals: number[] = [];\n    for (let i = 0; i < safeItems.length; i++) {\n      const start = window.setTimeout(() => {\n        setSteps((prev) => prev.map((v, j) => (j === i ? v + 1 : v)));\n        const id = window.setInterval(() => {\n          setSteps((prev) => prev.map((v, j) => (j === i ? v + 1 : v)));\n        }, intervalMs);\n        intervals.push(id);\n      }, intervalMs + i * 650);\n      timeouts.push(start);\n    }\n    return () => {\n      timeouts.forEach((t) => window.clearTimeout(t));\n      intervals.forEach((id) => window.clearInterval(id));\n    };\n  }, [running, intervalMs, safeItems.length]);\n\n  const valueFor = (i: number) => {\n    const item = safeItems[i];\n    const step = steps[i] ?? 0;\n    return item.values.length > 0\n      ? item.values[step % item.values.length]\n      : \"\";\n  };\n\n  const maskStyle: CSSProperties = {\n    maskImage:\n      \"linear-gradient(to right, transparent, #000 7%, #000 93%, transparent)\",\n    WebkitMaskImage:\n      \"linear-gradient(to right, transparent, #000 7%, #000 93%, transparent)\",\n  };\n\n  const trackStyle: CSSProperties | undefined = reduce\n    ? undefined\n    : {\n        animation: `prequel-ticker-scroll ${safeDuration}s linear infinite`,\n        animationPlayState: paused ? \"paused\" : \"running\",\n      };\n\n  const renderGroup = (hidden: boolean) => (\n    <div\n      className=\"flex shrink-0 items-center gap-10 pr-10\"\n      aria-hidden={hidden ? \"true\" : undefined}\n    >\n      {safeItems.map((item, i) => (\n        <TickerStat\n          key={`${item.id}-${hidden ? \"copy\" : \"main\"}`}\n          item={item}\n          value={valueFor(i)}\n          reduce={reduce}\n        />\n      ))}\n    </div>\n  );\n\n  return (\n    <MotionConfig reducedMotion=\"user\">\n      <style>{KEYFRAMES}</style>\n      <motion.div\n        ref={ref}\n        initial={{ opacity: 0, y: reduce ? 0 : 16 }}\n        animate={show ? { opacity: 1, y: 0 } : { opacity: 0, y: reduce ? 0 : 16 }}\n        transition={{ duration: reduce ? 0 : 0.6, ease: EASE }}\n        tabIndex={0}\n        role=\"group\"\n        aria-label=\"Live statistics ticker\"\n        onPointerEnter={() => setHovered(true)}\n        onPointerLeave={() => setHovered(false)}\n        onFocus={() => setFocused(true)}\n        onBlur={() => setFocused(false)}\n        className={cn(\n          \"relative rounded-xl border border-border bg-card text-card-foreground shadow-sm outline-none\",\n          \"focus-visible:ring-2 focus-visible:ring-ring\",\n          className\n        )}\n      >\n        <div\n          className={cn(\n            \"relative flex items-center py-4\",\n            reduce ? \"overflow-x-auto\" : \"overflow-hidden\"\n          )}\n          style={maskStyle}\n        >\n          <div className=\"flex w-max\" style={trackStyle}>\n            {renderGroup(false)}\n            {reduce ? null : renderGroup(true)}\n          </div>\n        </div>\n      </motion.div>\n    </MotionConfig>\n  );\n}\n\nexport default StatTicker;\n"}]}