Installation
Add the component
npx shadcn@latest add https://prequeluianimate.terravidhal.me/r/scrolling-number.jsonFirst component? Set up your project first
Frameworks
The live preview is React. Each tab below is the file that ships for that framework, with the same durations, springs and reduced-motion rule.
"use client";
import * as React from "react";
import { motion, type Transition } from "motion/react";
import { cn } from "@/lib/utils";
import { srOnlyStyle, useReduceMotion, useSeen, useTempoTransition } from "./motion";
const bandSpring: Transition = {
type: "spring",
stiffness: 90,
damping: 30,
restDelta: 0.001,
restSpeed: 0.01,
};
export type ScrollingNumberProps = Omit<React.ComponentProps<"div">, "children"> & {
number: number;
fromNumber?: number;
step?: number;
itemsSize?: number;
sideItemsCount?: number;
inView?: boolean;
inViewOnce?: boolean;
delay?: number;
transition?: Transition;
onCompleted?: () => void;
};
export function ScrollingNumber({
number,
fromNumber = 0,
step = 1,
itemsSize = 30,
sideItemsCount = 2,
inView = false,
inViewOnce = true,
delay = 0,
transition,
onCompleted,
className,
...props
}: ScrollingNumberProps) {
const ref = React.useRef<HTMLDivElement>(null);
const seen = useSeen(ref, inView, inViewOnce);
const reduce = useReduceMotion();
const spring = useTempoTransition(transition ?? bandSpring);
const [started, setStarted] = React.useState(false);
React.useEffect(() => {
if (!seen) return;
if (reduce) {
setStarted(true);
onCompleted?.();
return;
}
const timer = window.setTimeout(() => setStarted(true), delay);
return () => window.clearTimeout(timer);
}, [delay, onCompleted, reduce, seen]);
const values = React.useMemo(() => {
const size = Math.max(1, step);
const from = Math.round(fromNumber / size) * size;
const to = Math.round(number / size) * size;
const direction = to >= from ? 1 : -1;
const count = Math.abs(Math.round((to - from) / size)) + 1;
return Array.from({ length: count }, (_, index) => from + index * size * direction);
}, [fromNumber, number, step]);
const targetIndex = values.length - 1;
const height = itemsSize * (1 + sideItemsCount * 2);
const initialY = sideItemsCount * itemsSize;
const finalY = (sideItemsCount - targetIndex) * itemsSize;
return (
<div
ref={ref}
data-slot="scrolling-number"
className={cn(
"relative isolate inline-block overflow-hidden rounded-lg [mask-image:linear-gradient(to_bottom,transparent,black_24%,black_76%,transparent)]",
className,
)}
style={{ height }}
{...props}
>
<div
aria-hidden="true"
className="pointer-events-none absolute inset-x-1 top-1/2 z-10 -translate-y-1/2 rounded-md border bg-foreground/[0.06] dark:bg-background/20"
style={{ height: itemsSize }}
/>
<motion.div
aria-hidden="true"
animate={{ y: started ? finalY : initialY }}
transition={reduce ? { duration: 0 } : spring}
onAnimationComplete={() => {
if (started) onCompleted?.();
}}
className="relative flex flex-col"
>
{values.map((value) => (
<div
key={value}
className={cn(
"flex shrink-0 items-center justify-center font-medium tabular-nums",
value === number ? "text-foreground" : "text-muted-foreground/50",
)}
style={{ height: itemsSize }}
>
{value.toLocaleString("en-US")}
</div>
))}
</motion.div>
<span className="sr-only" style={srOnlyStyle()}>
{number.toLocaleString("en-US")}
</span>
</div>
);
}Install
npx shadcn@latest add https://prequeluianimate.terravidhal.me/r/scrolling-number.jsonDependencies
motion lucide-reactThe registry keeps the component source, its motion tokens and the reduced-motion branch together, so the install never pulls a different animation from the preview.
Usage
Import the component and compose it exactly like the primitive it wraps. Motion is enabled by default; the tempo prop scales every duration and softens every spring.
import { ScrollingNumber } from "@/components/prequelui/scrolling-number";
export default function Demo() {
return (
<ScrollingNumber
number={48}
fromNumber={0}
itemsSize={34}
className="w-40 text-2xl"
/>
);
}import { ScrollingNumber } from "@/components/prequelui/scrolling-number";
export function Variant() {
return (
<ScrollingNumber tempo={1.2}>
…
</ScrollingNumber>
);
}Animation
The number is a mechanical band. On first view the band leaves its resting index and settles under a fixed marker with a damped spring; changing the value retargets the same motion instead of restarting it.
- Band spring: stiffness 90, damping 30, restDelta 0.001.
- Two neighbours above and below stay visible, faded by the mask.
- The marker and the edge mask are decorative and hidden from assistive technology.
- Reduced motion places the band at its final index with no travel.
API Reference
Each part accepts the usual HTML props in addition to the motion props below. No accessibility prop is hidden behind the animation layer.
ScrollingNumber
| Prop | Type | Default |
|---|---|---|
| number | number | 0 |
| fromNumber | number | 0 |
| step | number | 1 |
| itemsSize | number | 30 |
| sideItemsCount | number | 2 |
| inView | boolean | false |
| inViewOnce | boolean | true |
| delay | number | 0 |
| onCompleted | () => void | — |
| transition | Transition | component spring |
| className | string | — |