# Number Ticker

An animated number that springs from a start value to its target when it scrolls into view, with configurable direction, delay, and decimal places.

- Category: Data Display
- License: MIT
- Page: https://www.saasuji.com/components/number-ticker
- Install: npx shadcn@latest add https://www.saasuji.com/r/number-ticker.json

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| value | `number` | — | Target number the ticker animates to when it enters the viewport; required. |
| startValue | `number` | `0` | Initial number rendered before the animation runs. |
| direction | `"up" | "down"` | `"up"` | Animate upward from startValue to value, or downward in reverse. |
| delay | `number` | `0` | Delay in seconds before the animation starts once the ticker is in view. |
| decimalPlaces | `number` | `0` | Number of fraction digits used when formatting the animated value. |
| className | `string` | — | Additional classes merged onto the ticker span. |
| ...props | `React.ComponentPropsWithoutRef<"span">` | — | All remaining span attributes are forwarded (id, aria-*, …). |

## Source

```tsx
"use client"

import { useEffect, useRef, type ComponentPropsWithoutRef } from "react"
import { useInView, useMotionValue, useSpring } from "motion/react"

import { cn } from "@/lib/utils"

interface NumberTickerProps extends ComponentPropsWithoutRef<"span"> {
  value: number
  startValue?: number
  direction?: "up" | "down"
  delay?: number
  decimalPlaces?: number
}

export function NumberTicker({
  value,
  startValue = 0,
  direction = "up",
  delay = 0,
  className,
  decimalPlaces = 0,
  ...props
}: NumberTickerProps) {
  const ref = useRef<HTMLSpanElement>(null)
  const motionValue = useMotionValue(direction === "down" ? value : startValue)
  const springValue = useSpring(motionValue, {
    damping: 60,
    stiffness: 100,
  })
  const isInView = useInView(ref, { once: true, margin: "0px" })

  useEffect(() => {
    let timer: ReturnType<typeof setTimeout> | null = null

    if (isInView) {
      timer = setTimeout(() => {
        motionValue.set(direction === "down" ? startValue : value)
      }, delay * 1000)
    }

    return () => {
      if (timer !== null) {
        clearTimeout(timer)
      }
    }
  }, [motionValue, isInView, delay, value, direction, startValue])

  useEffect(
    () =>
      springValue.on("change", (latest) => {
        if (ref.current) {
          ref.current.textContent = Intl.NumberFormat("en-US", {
            minimumFractionDigits: decimalPlaces,
            maximumFractionDigits: decimalPlaces,
          }).format(Number(latest.toFixed(decimalPlaces)))
        }
      }),
    [springValue, decimalPlaces]
  )

  return (
    <span
      ref={ref}
      className={cn(
        "inline-block tracking-wider text-current tabular-nums",
        className
      )}
      {...props}
    >
      {startValue}
    </span>
  )
}

```
