Number Ticker
magicui/number-tickerFreeComponent
Animate numbers to count up or down to a target number
Live preview
live · rendered by the registry
Rendered by magicui on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://magicui.design/r/number-ticker.jsonSource
1"use client"23import { useEffect, useRef, type ComponentPropsWithoutRef } from "react"4import { useInView, useMotionValue, useSpring } from "motion/react"56import { cn } from "@/lib/utils"78interface NumberTickerProps extends ComponentPropsWithoutRef<"span"> {9 value: number10 startValue?: number11 direction?: "up" | "down"12 delay?: number13 decimalPlaces?: number14}1516export function NumberTicker({17 value,18 startValue = 0,19 direction = "up",20 delay = 0,21 className,22 decimalPlaces = 0,23 ...props24}: NumberTickerProps) {25 const ref = useRef<HTMLSpanElement>(null)26 const motionValue = useMotionValue(direction === "down" ? value : startValue)27 const springValue = useSpring(motionValue, {28 damping: 60,29 stiffness: 100,30 })31 const isInView = useInView(ref, { once: true, margin: "0px" })3233 useEffect(() => {34 let timer: ReturnType<typeof setTimeout> | null = null3536 if (isInView) {37 timer = setTimeout(() => {38 motionValue.set(direction === "down" ? startValue : value)39 }, delay * 1000)40 }4142 return () => {43 if (timer !== null) {44 clearTimeout(timer)45 }46 }47 }, [motionValue, isInView, delay, value, direction, startValue])4849 useEffect(50 () =>51 springValue.on("change", (latest) => {52 if (ref.current) {53 ref.current.textContent = Intl.NumberFormat("en-US", {54 minimumFractionDigits: decimalPlaces,55 maximumFractionDigits: decimalPlaces,56 }).format(Number(latest.toFixed(decimalPlaces)))57 }58 }),59 [springValue, decimalPlaces]60 )6162 return (63 <span64 ref={ref}65 className={cn(66 "inline-block tracking-wider text-black tabular-nums dark:text-white",67 className68 )}69 {...props}70 >71 {startValue}72 </span>73 )74}
What it pulls in
npm packages
Files it writes
More from magicui
All 247 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Androidandroid | magicui | Components | Free | no deps | |
| Animated Beamanimated-beam | magicui | Components | Free | 1 dep | |
| Animated Circular Progress Baranimated-circular-progress-bar | magicui | Components | Free | no deps | |
| Animated Gradient Textanimated-gradient-text | magicui | Components | Free | no deps | |
| Animated Grid Patternanimated-grid-pattern | magicui | Components | Free | 1 dep | |
| Animated Listanimated-list | magicui | Components | Free | 1 dep | |
| Animated Shiny Textanimated-shiny-text | magicui | Components | Free | no deps | |
| Theme Toggleranimated-theme-toggler | magicui | Components | Free | 1 dep |
