Numbers & Data Displayv0.1.0
Number Ticker
Counts up when it scrolls into view.
Installation
CLI
npx shadcn@latest add https://hantui-theta.vercel.app/r/number-ticker.jsonManual
Install the dependencies
npm install clsx motion tailwind-mergeAdd the shared helpers
Components use
cn()fromlib/utils.tsand the motion tokens below. See Installation if you have not set them up yet.lib/motion.tsimport type { Transition, Variants } from "motion/react"; /** Durations in seconds. */ export const duration = { instant: 0.1, quick: 0.18, base: 0.28, slow: 0.45, } as const; /** Cubic-bezier curves. */ export const ease = { /** Entering, UI response. */ out: [0.22, 1, 0.36, 1], /** Moving on screen. */ inOut: [0.65, 0, 0.35, 1], /** Sheets and the mobile menu. */ drawer: [0.32, 0.72, 0, 1], } as const; /** Everyday UI: presses, icon swaps, popovers and modals. Exits run at `exitFactor` of the enter. */ export const ui = { press: { scale: 0.96, duration: 0.12 }, icon: { type: "spring", duration: 0.3, bounce: 0 }, popover: { duration: 0.18, ease: ease.out }, modal: { duration: 0.24, ease: ease.out }, exitFactor: 0.6, } as const; /** Once-per-visit motion on the landing page: hero letters, the reel and the afterimage. */ export const kinetic = { heroLetter: { duration: 0.6, ease: ease.out }, /** Delay per letter, eased so the wave slows toward the end. */ heroStep: 0.035, reelSwap: { type: "spring", duration: 0.5, bounce: 0.1 }, /** Two afterimages: lag in seconds and opacity of each. */ ghost: { lag: [0.04, 0.09], opacity: [0.14, 0.06] }, } as const; /** Spring presets. */ export const spring = { snappy: { type: "spring", stiffness: 500, damping: 30 }, smooth: { type: "spring", stiffness: 260, damping: 26 }, bouncy: { type: "spring", stiffness: 300, damping: 15 }, } as const satisfies Record<string, Transition>; /** Stagger between siblings, in seconds (0.03–0.06). */ export const stagger = { tight: 0.03, base: 0.045, loose: 0.06, } as const; /** Blur + fade + y-offset. Falls back to a plain fade under reduced motion. */ export function enterVariants(reduceMotion?: boolean | null, y = 8): Variants { if (reduceMotion) { return { hidden: { opacity: 0 }, visible: { opacity: 1, transition: { duration: duration.base } }, exit: { opacity: 0, transition: { duration: duration.quick } }, }; } return { hidden: { opacity: 0, y, filter: "blur(4px)" }, visible: { opacity: 1, y: 0, filter: "blur(0px)", transition: { duration: duration.base, ease: ease.out }, }, exit: { opacity: 0, y: -y / 2, filter: "blur(4px)", transition: { duration: duration.quick, ease: ease.out }, }, }; } /** Scale 0.96 → 1 for popovers, menus and dialogs. */ export function overlayVariants(reduceMotion?: boolean | null): Variants { if (reduceMotion) { return { hidden: { opacity: 0 }, visible: { opacity: 1, transition: { duration: duration.quick } }, exit: { opacity: 0, transition: { duration: duration.instant } }, }; } return { hidden: { opacity: 0, scale: 0.96, filter: "blur(2px)" }, visible: { opacity: 1, scale: 1, filter: "blur(0px)", transition: { duration: duration.quick, ease: ease.out }, }, exit: { opacity: 0, scale: 0.96, filter: "blur(2px)", transition: { duration: duration.instant + 0.02, ease: ease.out }, }, }; } /** Parent variants that stagger their children in. */ export function staggerVariants(gap: number = stagger.base, delayChildren = 0): Variants { return { hidden: {}, visible: { transition: { staggerChildren: gap, delayChildren } }, exit: { transition: { staggerChildren: gap / 2, staggerDirection: -1 } }, }; }Copy the source
components/ui/number-ticker/number-ticker.tsx"use client"; import { useEffect, useRef, type ComponentProps } from "react"; import { animate, motion, useInView, useMotionValue, useReducedMotion, useTransform } from "motion/react"; import { cn } from "@/lib/utils"; import { ease } from "@/lib/motion"; export interface NumberTickerProps extends Omit<ComponentProps<"span">, "children" | "onAnimationStart" | "onDrag" | "onDragStart" | "onDragEnd"> { /** The number to count to. Changing it counts on from the number currently shown. */ value: number; /** Where the first count starts. @default 0 */ from?: number; /** Seconds the count takes. @default 1.4 */ duration?: number; /** Seconds before the first count starts. @default 0 */ delay?: number; /** Count once scrolled into view, or straight away. @default "inView" */ trigger?: "inView" | "mount"; /** Decimal places shown while counting. Defaults to the decimals in `value`. */ decimals?: number; /** Intl.NumberFormat options, e.g. { style: "currency", currency: "USD" }. */ format?: Intl.NumberFormatOptions; /** Locale for formatting. @default "en-US" */ locale?: string; /** Text before the number, e.g. "+". */ prefix?: string; /** Text after the number, e.g. "k users". */ suffix?: string; } export function NumberTicker({ value, from = 0, duration = 1.4, delay = 0, trigger = "inView", decimals, format, locale = "en-US", prefix = "", suffix = "", className, ...props }: NumberTickerProps) { const reduceMotion = useReducedMotion(); const ref = useRef<HTMLSpanElement>(null); const inView = useInView(ref, { once: true, amount: 0.6 }); const started = useRef(false); const places = decimals ?? (String(value).split(".")[1]?.length ?? 0); const formatter = new Intl.NumberFormat(locale, { minimumFractionDigits: places, maximumFractionDigits: places, ...format }); const count = useMotionValue(from); // Rendered straight into the DOM by Motion, so counting never re-renders React. const text = useTransform(count, (v) => `${prefix}${formatter.format(v)}${suffix}`); const final = `${prefix}${formatter.format(value)}${suffix}`; useEffect(() => { if (trigger === "inView" && !inView) return; if (reduceMotion) { count.set(value); return; } const first = !started.current; started.current = true; const controls = animate(count, value, { duration, delay: first ? delay : 0, ease: ease.out }); return () => controls.stop(); }, [value, inView, trigger, reduceMotion, duration, delay, count]); return ( <span className={cn("inline-grid", className)} {...props}> <span className="sr-only select-none">{final}</span> {/* The final value reserves the width, so counting never nudges the layout. */} <span aria-hidden="true" className="invisible col-start-1 row-start-1 select-none whitespace-nowrap"> {final} </span> <motion.span ref={ref} aria-hidden="true" className="col-start-1 row-start-1 whitespace-nowrap"> {text} </motion.span> </span> ); }
Usage
import { NumberTicker } from "@/components/ui/number-ticker/number-ticker";
export function Stat() {
return <NumberTicker value={12480} suffix="+" className="text-4xl font-semibold" />;
}
// Currency, compact
<NumberTicker value={1284000} format={{ style: "currency", currency: "USD", notation: "compact" }} />API reference
| Prop | Type | Default | Description |
|---|---|---|---|
value* | number | – | The number to count to. Changing it counts on from the number currently shown. |
from | number | 0 | Where the first count starts. |
duration | number | 1.4 | Seconds the count takes. |
delay | number | 0 | Seconds before the first count starts. |
trigger | "inView" | "mount" | "inView" | Count once scrolled into view, or straight away. |
decimals | number | – | Decimal places shown while counting. Defaults to the decimals in `value`. |
format | Intl.NumberFormatOptions | – | Intl.NumberFormat options, e.g. { style: "currency", currency: "USD" }. |
locale | string | "en-US" | Locale for formatting. |
prefix | string | – | Text before the number, e.g. "+". |
suffix | string | – | Text after the number, e.g. "k users". |
Also accepts every native <span> attribute, including ref and className.
- Counting runs on a Motion value written straight to the DOM, so it never re-renders React.
- An invisible copy of the final value reserves the space: counting never shifts the layout. Large standalone numbers use proportional figures, which read better than tabular ones at display sizes.
- Changing `value` counts on from the number on screen, not from `from`.
Accessibility
- Screen readers read the final value from a visually hidden copy; the counting digits are aria-hidden, so intermediate numbers are never announced.
- If the value updates live and should be announced, wrap it in your own aria-live region.
- With reduced motion enabled, the final number appears immediately.