{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "kpi-counter",
  "type": "registry:ui",
  "title": "KPI Counter",
  "description": "A metric card whose number counts, delta lands and sparkline draws itself.",
  "dependencies": [
    "motion",
    "lucide-react"
  ],
  "files": [
    {
      "path": "components/prequelui/motion.tsx",
      "type": "registry:lib",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport {\n  MotionConfig,\n  useInView as useMotionInView,\n  type Transition,\n} from \"motion/react\";\nimport { useHydratedReducedMotion } from \"@/lib/use-reduced-motion\";\nimport { cn } from \"@/lib/utils\";\n\nconst TempoContext = React.createContext(1);\n\nexport function PrequelMotionProvider({\n  tempo = 1,\n  children,\n}: {\n  tempo?: number;\n  children: React.ReactNode;\n}) {\n  const value = React.useMemo(() => Math.max(0.4, tempo), [tempo]);\n  return (\n    <TempoContext.Provider value={value}>\n      <MotionConfig reducedMotion=\"user\">{children}</MotionConfig>\n    </TempoContext.Provider>\n  );\n}\n\nexport function useTempo() {\n  return React.useContext(TempoContext);\n}\n\nconst TIME_KEYS = [\n  \"duration\",\n  \"visualDuration\",\n  \"delay\",\n  \"repeatDelay\",\n  \"delayChildren\",\n] as const;\n\nfunction scaleValue(transition: Transition, tempo: number): Transition {\n  if (tempo === 1 || !transition || typeof transition !== \"object\") {\n    return transition;\n  }\n  const next: Record<string, unknown> = {};\n  for (const [key, value] of Object.entries(transition)) {\n    if (typeof value === \"number\" && (TIME_KEYS as readonly string[]).includes(key)) {\n      next[key] = value * tempo;\n    } else if (typeof value === \"number\" && key === \"stiffness\") {\n      next[key] = value / (tempo * tempo);\n    } else if (typeof value === \"number\" && key === \"damping\") {\n      next[key] = value / tempo;\n    } else if (value && typeof value === \"object\" && !Array.isArray(value)) {\n      next[key] = scaleValue(value as Transition, tempo);\n    } else {\n      next[key] = value;\n    }\n  }\n  return next as Transition;\n}\n\nexport function useTempoTransition(transition: Transition) {\n  const tempo = useTempo();\n  return React.useMemo(\n    () => scaleValue(transition, tempo),\n    [tempo, JSON.stringify(transition)],\n  );\n}\n\nexport function useReduceMotion() {\n  return useHydratedReducedMotion();\n}\n\nexport function usePageVisible() {\n  const [visible, setVisible] = React.useState(true);\n  React.useEffect(() => {\n    const update = () => setVisible(!document.hidden);\n    update();\n    document.addEventListener(\"visibilitychange\", update);\n    return () => document.removeEventListener(\"visibilitychange\", update);\n  }, []);\n  return visible;\n}\n\nexport function useMounted() {\n  return React.useSyncExternalStore(\n    () => () => {},\n    () => true,\n    () => false,\n  );\n}\n\nexport function useSeen(\n  ref: React.RefObject<Element | null>,\n  enabled = true,\n  once = true,\n) {\n  const inView = useMotionInView(ref, { once, amount: 0.35 });\n  return enabled ? inView : true;\n}\n\ntype SlotProps = React.HTMLAttributes<HTMLElement> & {\n  children?: React.ReactNode;\n  ref?: React.Ref<HTMLElement>;\n};\n\nexport function Slot({ children, ...props }: SlotProps) {\n  if (!React.isValidElement<Record<string, unknown>>(children)) {\n    return null;\n  }\n  const child = children as React.ReactElement<Record<string, unknown>>;\n  const childProps = child.props;\n  return React.cloneElement(child, {\n    ...props,\n    ...childProps,\n    className: cn(\n      String(props.className ?? \"\"),\n      String(childProps.className ?? \"\"),\n    ),\n    style: {\n      ...((props.style as React.CSSProperties) ?? {}),\n      ...((childProps.style as React.CSSProperties) ?? {}),\n    },\n  } as Record<string, unknown>);\n}\n\nexport function srOnlyStyle(): React.CSSProperties {\n  return {\n    position: \"absolute\",\n    width: 1,\n    height: 1,\n    padding: 0,\n    margin: -1,\n    overflow: \"hidden\",\n    clip: \"rect(0, 0, 0, 0)\",\n    whiteSpace: \"nowrap\",\n    borderWidth: 0,\n  };\n}\n\nlet segmenter: Intl.Segmenter | null | undefined;\n\nexport function splitGraphemes(text: string): string[] {\n  if (segmenter === undefined) {\n    segmenter =\n      typeof Intl !== \"undefined\" && typeof Intl.Segmenter === \"function\"\n        ? new Intl.Segmenter(undefined, { granularity: \"grapheme\" })\n        : null;\n  }\n  if (!segmenter) return Array.from(text);\n  return Array.from(segmenter.segment(text), (entry) => entry.segment);\n}\n\nexport const easeOut = [0.23, 1, 0.32, 1] as const;\nexport const easeSoft = [0.215, 0.61, 0.355, 1] as const;\nexport const easeInOut = [0.77, 0, 0.175, 1] as const;\nexport const easeEnter = [0.25, 0.1, 0.35, 1] as const;\nexport const easeLeave = [0, 0, 0.58, 1] as const;\nexport const easeResize = [0.4, 0, 0.1, 1] as const;\nexport const easeAway = [0.33, 0.17, 0.67, 0.5] as const;\n\nexport const pressSpring: Transition = {\n  type: \"spring\",\n  stiffness: 550,\n  damping: 30,\n  restSpeed: 10,\n};\n\nexport const selectionSpring: Transition = {\n  type: \"spring\",\n  stiffness: 300,\n  damping: 30,\n};\n\nexport const highlightSpring: Transition = {\n  type: \"spring\",\n  stiffness: 500,\n  damping: 40,\n};\n\nexport const popSpring: Transition = {\n  type: \"spring\",\n  stiffness: 400,\n  damping: 29,\n};\n\nexport const tooltipSpring: Transition = {\n  type: \"spring\",\n  stiffness: 400,\n  damping: 31,\n};\n\nexport const dialogSpring: Transition = {\n  type: \"spring\",\n  stiffness: 150,\n  damping: 25,\n};\n\nexport const toastSpring: Transition = {\n  type: \"spring\",\n  stiffness: 280,\n  damping: 24,\n};\n\nexport const heightSpring: Transition = {\n  type: \"spring\",\n  stiffness: 300,\n  damping: 30,\n  bounce: 0,\n  restDelta: 0.01,\n};\n\nexport const progressSpring: Transition = {\n  type: \"spring\",\n  stiffness: 300,\n  damping: 35,\n  restDelta: 0.01,\n  restSpeed: 0.1,\n};\n\nexport const scrollSpring: Transition = {\n  type: \"spring\",\n  stiffness: 250,\n  damping: 40,\n  bounce: 0,\n};\n\nexport const avatarSpring: Transition = {\n  type: \"spring\",\n  stiffness: 300,\n  damping: 17,\n};\n\nexport const pinnedSpring: Transition = {\n  type: \"spring\",\n  stiffness: 320,\n  damping: 25,\n  mass: 0.8,\n};\n\nexport const thumbSpring: Transition = {\n  type: \"spring\",\n  stiffness: 300,\n  damping: 25,\n};\n\nexport const checkSpring: Transition = {\n  type: \"spring\",\n  stiffness: 500,\n  damping: 24,\n};\n\nexport const cursorEntry: Transition = {\n  type: \"spring\",\n  stiffness: 550,\n  damping: 30,\n  restSpeed: 10,\n};\n\nexport const cursorExit: Transition = {\n  type: \"spring\",\n  stiffness: 550,\n  damping: 46.9,\n  restSpeed: 10,\n};\n\nexport const cursorLabel: Transition = {\n  type: \"spring\",\n  stiffness: 500,\n  damping: 50,\n  bounce: 0,\n};\n\nexport const swapTween: Transition = {\n  duration: 0.15,\n  ease: easeOut,\n};\n\nexport const overlayTween: Transition = {\n  duration: 0.4,\n  ease: easeSoft,\n};\n\nexport const disclosureTween: Transition = {\n  duration: 0.35,\n  ease: easeSoft,\n};\n\nexport const menuTween: Transition = {\n  duration: 0.22,\n  ease: easeSoft,\n};\n\nexport const panelOutTween: Transition = {\n  duration: 0.15,\n  ease: easeLeave,\n};\n\nexport const panelInTween: Transition = {\n  duration: 0.3,\n  ease: easeEnter,\n};\n\nexport const panelHeightTween: Transition = {\n  duration: 0.45,\n  ease: easeResize,\n};\n\nexport const exitTween: Transition = {\n  duration: 0.2,\n  ease: easeAway,\n};\n\nexport const faceSpring: Transition = {\n  type: \"spring\",\n  stiffness: 280,\n  damping: 20,\n};\n\nexport const blurIcon = \"blur(2px)\";\n\nexport function flipTransform(side: \"top\" | \"bottom\" | \"left\" | \"right\") {\n  const rotate =\n    side === \"top\"\n      ? \"rotateX(-20deg)\"\n      : side === \"bottom\"\n        ? \"rotateX(20deg)\"\n        : side === \"left\"\n          ? \"rotateY(20deg)\"\n          : \"rotateY(-20deg)\";\n  return `perspective(500px) ${rotate} scale(0.8)`;\n}\n\nexport function uprightTransform(side: \"top\" | \"bottom\" | \"left\" | \"right\") {\n  const rotate = side === \"left\" || side === \"right\" ? \"rotateY(0deg)\" : \"rotateX(0deg)\";\n  return `perspective(500px) ${rotate} scale(1)`;\n}\n",
      "target": "components/prequelui/motion.tsx"
    },
    {
      "path": "components/prequelui/counting-number.tsx",
      "type": "registry:ui",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport {\n  animate,\n  useMotionValue,\n  useMotionValueEvent,\n  type Transition,\n} from \"motion/react\";\nimport { cn } from \"@/lib/utils\";\nimport {\n  srOnlyStyle,\n  useReduceMotion,\n  useSeen,\n  useTempoTransition,\n} from \"./motion\";\n\ntype Format = {\n  decimals: number;\n  decimalSeparator: string;\n  thousandSeparator?: string;\n  padLength: number;\n};\n\nfunction formatNumber(value: number, options: Format) {\n  const { decimals, decimalSeparator, thousandSeparator, padLength } = options;\n  const fixed = Math.abs(value).toFixed(decimals);\n  const sign = value < 0 && Number(fixed) !== 0 ? \"-\" : \"\";\n  const [integer = \"0\", fraction] = fixed.split(\".\");\n  const padded = integer.padStart(padLength, \"0\");\n  const grouped =\n    thousandSeparator === undefined\n      ? padded\n      : padded.replace(/\\B(?=(\\d{3})+$)/g, thousandSeparator);\n  return fraction\n    ? `${sign}${grouped}${decimalSeparator}${fraction}`\n    : `${sign}${grouped}`;\n}\n\nconst countSpring: Transition = {\n  type: \"spring\",\n  stiffness: 90,\n  damping: 50,\n  restDelta: 0.001,\n  restSpeed: 0.01,\n};\n\nexport type CountingNumberProps = Omit<React.ComponentProps<\"span\">, \"children\"> & {\n  number: number;\n  fromNumber?: number;\n  padStart?: boolean;\n  decimalPlaces?: number;\n  decimalSeparator?: string;\n  thousandSeparator?: string;\n  inView?: boolean;\n  inViewOnce?: boolean;\n  delay?: number;\n  initiallyStable?: boolean;\n  transition?: Transition;\n  onCompleted?: () => void;\n};\n\nexport function CountingNumber({\n  number,\n  fromNumber = 0,\n  padStart = false,\n  decimalPlaces = 0,\n  decimalSeparator = \".\",\n  thousandSeparator,\n  inView = false,\n  inViewOnce = true,\n  delay = 0,\n  initiallyStable = false,\n  transition,\n  onCompleted,\n  className,\n  ...props\n}: CountingNumberProps) {\n  const ref = React.useRef<HTMLSpanElement>(null);\n  const visualRef = React.useRef<HTMLSpanElement>(null);\n  const seen = useSeen(ref, inView, inViewOnce);\n  const reduce = useReduceMotion();\n  const spring = useTempoTransition(transition ?? countSpring);\n  const value = useMotionValue(initiallyStable ? number : fromNumber);\n\n  const format = React.useMemo<Format>(\n    () => ({\n      decimals: decimalPlaces,\n      decimalSeparator,\n      thousandSeparator,\n      padLength: padStart ? Math.floor(Math.abs(number)).toString().length : 0,\n    }),\n    [decimalPlaces, decimalSeparator, number, padStart, thousandSeparator],\n  );\n\n  const write = React.useCallback(\n    (latest: number) => {\n      const node = visualRef.current;\n      if (node) node.textContent = formatNumber(latest, format);\n    },\n    [format],\n  );\n\n  useMotionValueEvent(value, \"change\", write);\n\n  React.useLayoutEffect(() => {\n    write(value.get());\n  }, [write, value]);\n\n  const springRef = React.useRef(spring);\n  React.useEffect(() => {\n    springRef.current = spring;\n  }, [spring]);\n\n  React.useEffect(() => {\n    if (!seen) return;\n    if (reduce) {\n      value.jump(number);\n      onCompleted?.();\n      return;\n    }\n    let controls: ReturnType<typeof animate> | undefined;\n    const timer = window.setTimeout(() => {\n      controls = animate(value, number, {\n        ...springRef.current,\n        onComplete: onCompleted,\n      } as Transition);\n    }, delay);\n    return () => {\n      window.clearTimeout(timer);\n      controls?.stop();\n    };\n  }, [delay, number, onCompleted, reduce, seen, value]);\n\n  React.useEffect(() => () => value.stop(), [value]);\n\n  const finalText = formatNumber(number, format);\n\n  return (\n    <span\n      ref={ref}\n      data-slot=\"counting-number\"\n      className={cn(\"inline-block tabular-nums\", className)}\n      {...props}\n    >\n      <span aria-hidden=\"true\" ref={visualRef} />\n      <span className=\"sr-only\" style={srOnlyStyle()}>\n        {finalText}\n      </span>\n    </span>\n  );\n}\n",
      "target": "components/prequelui/counting-number.tsx"
    },
    {
      "path": "components/prequelui/kpi-counter.tsx",
      "type": "registry:ui",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { motion } from \"motion/react\";\nimport { cn } from \"@/lib/utils\";\nimport { CountingNumber } from \"./counting-number\";\nimport { useReduceMotion, useSeen } from \"./motion\";\n\nexport type KpiCounterProps = React.ComponentProps<\"div\"> & {\n  label: string;\n  value: number;\n  from?: number;\n  prefix?: string;\n  suffix?: string;\n  decimals?: number;\n  delta?: number;\n  deltaSuffix?: string;\n  points?: number[];\n  inView?: boolean;\n  hint?: string;\n};\n\ntype Point = { x: number; y: number };\n\nfunction smoothPath(points: Point[]) {\n  if (points.length === 0) return \"\";\n  if (points.length === 1) return `M ${points[0].x} ${points[0].y}`;\n  let d = `M ${points[0].x} ${points[0].y}`;\n  for (let index = 0; index < points.length - 1; index += 1) {\n    const current = points[index];\n    const next = points[index + 1];\n    const midX = (current.x + next.x) / 2;\n    d += ` C ${midX} ${current.y}, ${midX} ${next.y}, ${next.x} ${next.y}`;\n  }\n  return d;\n}\n\nfunction Arrow({ up }: { up: boolean }) {\n  return (\n    <svg viewBox=\"0 0 12 12\" fill=\"none\" aria-hidden=\"true\" className=\"size-3\">\n      <path\n        d={up ? \"M3 9 9 3M4.4 3H9v4.6\" : \"M3 3l6 6M4.4 9H9V4.4\"}\n        stroke=\"currentColor\"\n        strokeWidth=\"1.5\"\n        strokeLinecap=\"round\"\n        strokeLinejoin=\"round\"\n      />\n    </svg>\n  );\n}\n\nexport function KpiCounter({\n  label,\n  value,\n  from = 0,\n  prefix,\n  suffix,\n  decimals = 0,\n  delta,\n  deltaSuffix = \"%\",\n  points = [18, 24, 21, 32, 29, 36, 34, 44, 48, 46, 57, 62],\n  inView = true,\n  hint,\n  className,\n  ...props\n}: KpiCounterProps) {\n  const ref = React.useRef<HTMLDivElement>(null);\n  const seen = useSeen(ref, inView, true);\n  const reduce = useReduceMotion();\n  const id = React.useId().replace(/:/g, \"\");\n  const width = 240;\n  const height = 62;\n\n  const geometry = React.useMemo(() => {\n    if (points.length < 2) return null;\n    const min = Math.min(...points);\n    const max = Math.max(...points);\n    const span = max - min || 1;\n    const coords = points.map((point, index) => ({\n      x: (index / (points.length - 1)) * width,\n      y: height - 6 - ((point - min) / span) * (height - 14),\n    }));\n    const line = smoothPath(coords);\n    const area = `${line} L ${width} ${height} L 0 ${height} Z`;\n    return { line, area, coords };\n  }, [points]);\n\n  const up = (delta ?? 0) >= 0;\n\n  return (\n    <div\n      ref={ref}\n      data-slot=\"kpi-counter\"\n      className={cn(\n        \"flex w-full max-w-[300px] flex-col gap-4 rounded-xl border bg-card p-5 text-card-foreground\",\n        className,\n      )}\n      {...props}\n    >\n      <div className=\"flex items-start justify-between gap-3\">\n        <div className=\"flex min-w-0 flex-col gap-1\">\n          <span className=\"truncate text-[13px] leading-4 text-muted-foreground\">\n            {label}\n          </span>\n          <span className=\"flex items-baseline gap-0.5 text-[32px] font-medium leading-none tracking-[-0.03em]\">\n            {prefix ? <span className=\"text-[20px] text-muted-foreground\">{prefix}</span> : null}\n            <CountingNumber\n              number={value}\n              fromNumber={from}\n              inView={inView}\n              decimalPlaces={decimals}\n              thousandSeparator=\",\"\n            />\n            {suffix ? (\n              <span className=\"text-[15px] font-normal text-muted-foreground\">{suffix}</span>\n            ) : null}\n          </span>\n        </div>\n        {delta !== undefined ? (\n          <motion.span\n            initial={reduce ? false : { opacity: 0, y: 4 }}\n            animate={{ opacity: 1, y: 0 }}\n            transition={{ duration: 0.3, ease: [0.23, 1, 0.32, 1], delay: 0.15 }}\n            className={cn(\n              \"inline-flex shrink-0 items-center gap-1 rounded-md px-1.5 py-1 text-[13px] font-medium tabular-nums\",\n              up\n                ? \"bg-emerald-500/12 text-emerald-600 dark:text-emerald-400\"\n                : \"bg-rose-500/12 text-rose-600 dark:text-rose-400\",\n            )}\n          >\n            <Arrow up={up} />\n            {Math.abs(delta)}\n            {deltaSuffix}\n          </motion.span>\n        ) : null}\n      </div>\n\n      {geometry ? (\n        <div className=\"relative\">\n          <svg\n            viewBox={`0 0 ${width} ${height}`}\n            preserveAspectRatio=\"none\"\n            className=\"h-[62px] w-full overflow-visible\"\n            aria-hidden=\"true\"\n          >\n            <defs>\n              <linearGradient id={`kpi-${id}`} x1=\"0\" y1=\"0\" x2=\"0\" y2=\"1\">\n                <stop offset=\"0%\" stopColor=\"currentColor\" stopOpacity=\"0.26\" />\n                <stop offset=\"100%\" stopColor=\"currentColor\" stopOpacity=\"0\" />\n              </linearGradient>\n            </defs>\n            <motion.path\n              key={`area-${value}`}\n              d={geometry.area}\n              fill={`url(#kpi-${id})`}\n              stroke=\"none\"\n              initial={{ opacity: 0 }}\n              animate={{ opacity: seen ? 1 : 0 }}\n              transition={{ duration: 0.45, ease: [0.23, 1, 0.32, 1] }}\n            />\n            <motion.path\n              key={`line-${value}`}\n              d={geometry.line}\n              fill=\"none\"\n              stroke=\"currentColor\"\n              strokeWidth=\"2\"\n              strokeLinecap=\"round\"\n              strokeLinejoin=\"round\"\n              initial={{ pathLength: 0, opacity: 0.4 }}\n              animate={{ pathLength: seen ? 1 : 0, opacity: 1 }}\n              transition={{ duration: reduce ? 0 : 0.9, ease: [0.33, 0, 0.2, 1] }}\n            />\n          </svg>\n        </div>\n      ) : null}\n\n      {hint ? (\n        <p className=\"text-[13px] leading-4 text-muted-foreground\">{hint}</p>\n      ) : null}\n    </div>\n  );\n}\n",
      "target": "components/prequelui/kpi-counter.tsx"
    },
    {
      "path": "lib/use-reduced-motion.ts",
      "target": "lib/use-reduced-motion.ts",
      "type": "registry:lib",
      "content": "\"use client\";\n\nimport { useReducedMotion } from \"motion/react\";\nimport { useSyncExternalStore } from \"react\";\n\nconst subscribe = () => () => {};\n\nexport function useHydratedReducedMotion() {\n  const reduce = useReducedMotion() ?? false;\n  const mounted = useSyncExternalStore(\n    subscribe,\n    () => true,\n    () => false,\n  );\n  return mounted ? reduce : false;\n}\n"
    }
  ]
}
