{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"stats-section","type":"registry:block","title":"Stats Section","description":"A quiet marketing stats band with four large numbers that count up on scroll, each paired with a trend and a thin animated underline.","dependencies":["motion","lucide-react"],"registryDependencies":["utils"],"files":[{"path":"registry/prequel/stats-section/stats-section.tsx","type":"registry:component","content":"\"use client\";\n\nimport { useEffect, useId, useRef, useState } from \"react\";\nimport {\n  MotionConfig,\n  animate,\n  motion,\n  useInView,\n  useReducedMotion,\n} from \"motion/react\";\nimport { ArrowDownRight, ArrowUpRight } from \"lucide-react\";\nimport { cn } from \"@/lib/utils\";\n\nconst EASE: [number, number, number, number] = [0.22, 1, 0.36, 1];\nconst COUNT_DURATION = 1.1;\n\n/* ------------------------------------------------------------------ */\n/* types                                                              */\n/* ------------------------------------------------------------------ */\n\nexport type StatsSectionStat = {\n  id: string;\n  label: string;\n  value: number;\n  prefix?: string;\n  suffix?: string;\n  /** Decimals shown on the value. Defaults to 0. */\n  decimals?: number;\n  /** Signed change vs the previous period, e.g. 24.6 for +24.6%. */\n  trend: number;\n  /** Unit after the trend value. Defaults to \"%\". */\n  trendSuffix?: string;\n  /** Decimals shown on the trend value. Defaults to 1. */\n  trendDecimals?: number;\n  /** Short context after the trend, e.g. \"vs last year\". */\n  trendLabel?: string;\n  /** When true a negative trend is good (e.g. churn going down). */\n  goodWhenDown?: boolean;\n};\n\nexport type StatsSectionProps = {\n  className?: string;\n  /** Optional section heading. Pass an empty string to hide it. */\n  heading?: string;\n  /** Optional supporting line under the heading. */\n  subheading?: string;\n  stats?: StatsSectionStat[];\n  formatValue?: (value: number, stat: StatsSectionStat) => string;\n};\n\n/* ------------------------------------------------------------------ */\n/* deterministic demo data (no Math.random / Date.now at module load)  */\n/* ------------------------------------------------------------------ */\n\nconst DEFAULT_HEADING = \"Momentum, measured\";\nconst DEFAULT_SUBHEADING =\n  \"A quiet snapshot of how teams put the platform to work, updated every quarter.\";\n\nconst DEFAULT_STATS: StatsSectionStat[] = [\n  {\n    id: \"teams\",\n    label: \"Active teams\",\n    value: 18600,\n    trend: 24.6,\n    trendLabel: \"vs last year\",\n  },\n  {\n    id: \"projects\",\n    label: \"Projects shipped\",\n    value: 1280000,\n    suffix: \"+\",\n    trend: 18.4,\n    trendLabel: \"vs last year\",\n  },\n  {\n    id: \"uptime\",\n    label: \"Platform uptime\",\n    value: 99.98,\n    suffix: \"%\",\n    decimals: 2,\n    trend: 0.02,\n    trendSuffix: \" pts\",\n    trendDecimals: 2,\n    trendLabel: \"last 90 days\",\n  },\n  {\n    id: \"rating\",\n    label: \"Average rating\",\n    value: 4.9,\n    suffix: \"/5\",\n    decimals: 1,\n    trend: 6.5,\n    trendLabel: \"vs last year\",\n  },\n];\n\n/* ------------------------------------------------------------------ */\n/* helpers                                                            */\n/* ------------------------------------------------------------------ */\n\nfunction defaultFormat(value: number, stat: StatsSectionStat) {\n  const decimals = stat.decimals ?? 0;\n  const number = value.toLocaleString(\"en-US\", {\n    minimumFractionDigits: decimals,\n    maximumFractionDigits: decimals,\n  });\n  return `${stat.prefix ?? \"\"}${number}${stat.suffix ?? \"\"}`;\n}\n\nfunction formatTrend(stat: StatsSectionStat) {\n  const decimals = stat.trendDecimals ?? 1;\n  const sign = stat.trend > 0 ? \"+\" : stat.trend < 0 ? \"−\" : \"\";\n  return `${sign}${Math.abs(stat.trend).toFixed(decimals)}${stat.trendSuffix ?? \"%\"}`;\n}\n\n/* ------------------------------------------------------------------ */\n/* count-up, driven by the scroll position of the whole band          */\n/* ------------------------------------------------------------------ */\n\nfunction useCountUp(\n  target: number,\n  opts: { start: boolean; reduce: boolean; delay: number }\n) {\n  const [value, setValue] = useState(0);\n  const started = useRef(false);\n\n  useEffect(() => {\n    if (!opts.start) return;\n    if (opts.reduce) {\n      setValue(target);\n      return;\n    }\n    if (started.current) return;\n    started.current = true;\n\n    const controls = animate(0, target, {\n      duration: COUNT_DURATION,\n      ease: EASE,\n      delay: opts.delay,\n      onUpdate: (v) => setValue(v),\n      onComplete: () => setValue(target),\n    });\n\n    return () => controls.stop();\n  }, [opts.start, opts.reduce, opts.delay, target]);\n\n  return opts.reduce ? target : value;\n}\n\n/* ------------------------------------------------------------------ */\n/* one stat cell                                                      */\n/* ------------------------------------------------------------------ */\n\ntype StatCellProps = {\n  stat: StatsSectionStat;\n  index: number;\n  show: boolean;\n  reduce: boolean;\n  format: (value: number, stat: StatsSectionStat) => string;\n};\n\nfunction StatCell({ stat, index, show, reduce, format }: StatCellProps) {\n  const value = useCountUp(stat.value, {\n    start: show,\n    reduce,\n    delay: index * 0.08,\n  });\n\n  const neutral = stat.trend === 0;\n  const positive = stat.trend > 0;\n  const good = stat.goodWhenDown ? stat.trend < 0 : stat.trend > 0;\n\n  return (\n    <motion.div\n      initial={reduce ? false : { opacity: 0, y: 12 }}\n      animate={reduce || show ? { opacity: 1, y: 0 } : { opacity: 0, y: 12 }}\n      transition={{\n        duration: reduce ? 0 : 0.55,\n        delay: reduce ? 0 : index * 0.07,\n        ease: EASE,\n      }}\n      className={cn(\n        \"group relative flex min-w-0 flex-col gap-2 px-3 py-6 sm:px-6 md:px-5 md:py-7 lg:px-7\",\n        /* 2x2 on phones and tablets: a vertical rule in the right column and a\n           horizontal rule under the first row. */\n        index % 2 === 1 && \"border-l border-border\",\n        index >= 2 && \"border-t border-border\",\n        /* one row of four on desktop: only the vertical dividers remain. */\n        \"lg:border-t-0\",\n        index > 0 && \"lg:border-l lg:border-border\"\n      )}\n    >\n      <dt className=\"text-[12px] font-medium text-muted-foreground sm:text-[13px]\">\n        {stat.label}\n      </dt>\n\n      <dd className=\"m-0 flex flex-col gap-2\">\n        <span className=\"text-[18px] font-semibold leading-none tracking-[-0.02em] tabular-nums text-foreground sm:text-[22px] md:text-[24px] lg:text-[27px]\">\n          <span aria-hidden=\"true\">{format(value, stat)}</span>\n          <span className=\"sr-only\">{format(stat.value, stat)}</span>\n        </span>\n\n        <span\n          className={cn(\n            \"flex flex-wrap items-center gap-x-2 gap-y-1 text-[11.5px] leading-none\",\n            neutral\n              ? \"text-muted-foreground\"\n              : good\n                ? \"text-success\"\n                : \"text-destructive\"\n          )}\n        >\n          <span className=\"inline-flex items-center gap-0.5 font-medium tabular-nums\">\n            {neutral ? null : positive ? (\n              <ArrowUpRight className=\"h-3 w-3\" aria-hidden=\"true\" />\n            ) : (\n              <ArrowDownRight className=\"h-3 w-3\" aria-hidden=\"true\" />\n            )}\n            {formatTrend(stat)}\n          </span>\n          {stat.trendLabel ? (\n            <span className=\"font-normal text-muted-foreground\">\n              {stat.trendLabel}\n            </span>\n          ) : null}\n        </span>\n\n        {/* the thin underline draws itself while the number counts up */}\n        <span\n          aria-hidden=\"true\"\n          className=\"relative mt-1 block h-px w-full bg-border\"\n        >\n          <motion.span\n            className=\"absolute inset-y-0 left-0 block w-full origin-left bg-foreground/45\"\n            initial={reduce ? false : { scaleX: 0 }}\n            animate={{ scaleX: reduce || show ? 1 : 0 }}\n            transition={\n              reduce\n                ? { duration: 0 }\n                : {\n                    duration: COUNT_DURATION,\n                    delay: 0.1 + index * 0.08,\n                    ease: EASE,\n                  }\n            }\n          />\n          {/* the one lens glint: a tiny ring that settles on hover */}\n          <span\n            className=\"pointer-events-none absolute -top-[3px] right-0 grid h-[7px] w-[7px] place-items-center rounded-full border bg-card opacity-0 transition-opacity duration-200 group-hover:opacity-100\"\n            style={{ borderColor: \"var(--chart-1)\" }}\n          >\n            <span\n              className=\"h-[2px] w-[2px] translate-x-[1px] -translate-y-[1px] rounded-full\"\n              style={{ background: \"var(--chart-1)\" }}\n            />\n          </span>\n        </span>\n      </dd>\n    </motion.div>\n  );\n}\n\n/* ------------------------------------------------------------------ */\n/* block                                                              */\n/* ------------------------------------------------------------------ */\n\nexport function StatsSection({\n  className,\n  heading = DEFAULT_HEADING,\n  subheading = DEFAULT_SUBHEADING,\n  stats = DEFAULT_STATS,\n  formatValue,\n}: StatsSectionProps) {\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 format = formatValue ?? defaultFormat;\n  const headingId = useId();\n\n  const safeStats = stats.length > 0 ? stats : DEFAULT_STATS;\n  const hasHeading = Boolean(heading || subheading);\n\n  return (\n    <MotionConfig reducedMotion=\"user\">\n      <motion.div\n        ref={ref}\n        role=\"group\"\n        aria-labelledby={heading ? headingId : undefined}\n        aria-label={heading ? undefined : \"Key statistics\"}\n        initial={reduce ? false : { opacity: 0, y: 16 }}\n        animate={reduce || show ? { opacity: 1, y: 0 } : { opacity: 0, y: 16 }}\n        transition={{ duration: reduce ? 0 : 0.6, ease: EASE }}\n        className={cn(\n          \"w-full overflow-hidden rounded-xl border border-border bg-card text-card-foreground shadow-sm\",\n          className\n        )}\n      >\n        {hasHeading ? (\n          <div className=\"flex flex-col gap-1.5 border-b border-border px-5 py-5 sm:px-7 sm:py-6\">\n            {heading ? (\n              <h2\n                id={headingId}\n                className=\"text-[15px] font-medium tracking-[-0.01em] text-foreground sm:text-base\"\n              >\n                {heading}\n              </h2>\n            ) : null}\n            {subheading ? (\n              <p className=\"max-w-2xl text-[13px] leading-relaxed text-muted-foreground sm:text-sm\">\n                {subheading}\n              </p>\n            ) : null}\n          </div>\n        ) : null}\n\n        <dl className=\"grid grid-cols-2 lg:grid-cols-4\">\n          {safeStats.map((stat, index) => (\n            <StatCell\n              key={stat.id}\n              stat={stat}\n              index={index}\n              show={show}\n              reduce={reduce}\n              format={format}\n            />\n          ))}\n        </dl>\n      </motion.div>\n    </MotionConfig>\n  );\n}\n\nexport default StatsSection;\n"}]}