BlockDex

Components

Registries

Free blocks

Method

BlockDex

Components

Registries

Free blocks

Method

Open API

BlockDex

Components

Registries

Free blocks

Method

Open API

Components/motion-primitives/sliding-number

Sliding Number

motion-primitives/sliding-number
FreeComponent

A component that transitions between numbers with a sliding animation.

Install it

npx shadcn@latest add https://raw.githubusercontent.com/ibelick/motion-primitives/HEAD/public/c/sliding-number.json

Source

sliding-number.tsxraw.githubusercontent.com/ibelick/motion-primitives/HEAD/public/c/sliding-number.json
1'use client';
2import { useEffect, useId } from 'react';
3import {
4 MotionValue,
5 motion,
6 useSpring,
7 useTransform,
8 motionValue,
9} from 'motion/react';
10import useMeasure from 'react-use-measure';
11
12const TRANSITION = {
13 type: 'spring',
14 stiffness: 280,
15 damping: 18,
16 mass: 0.3,
17};
18
19function Digit({ value, place }: { value: number; place: number }) {
20 const valueRoundedToPlace = Math.floor(value / place) % 10;
21 const initial = motionValue(valueRoundedToPlace);
22 const animatedValue = useSpring(initial, TRANSITION);
23
24 useEffect(() => {
25 animatedValue.set(valueRoundedToPlace);
26 }, [animatedValue, valueRoundedToPlace]);
27
28 return (
29 <div className='relative inline-block w-[1ch] overflow-x-visible overflow-y-clip leading-none tabular-nums'>
30 <div className='invisible'>0</div>
31 {Array.from({ length: 10 }, (_, i) => (
32 <Number key={i} mv={animatedValue} number={i} />
33 ))}
34 </div>
35 );
36}
37
38function Number({ mv, number }: { mv: MotionValue<number>; number: number }) {
39 const uniqueId = useId();
40 const [ref, bounds] = useMeasure();
41
42 const y = useTransform(mv, (latest) => {
43 if (!bounds.height) return 0;
44 const placeValue = latest % 10;
45 const offset = (10 + number - placeValue) % 10;
46 let memo = offset * bounds.height;
47
48 if (offset > 5) {
49 memo -= 10 * bounds.height;
50 }
51
52 return memo;
53 });
54
55 // don't render the animated number until we know the height
56 if (!bounds.height) {
57 return (
58 <span ref={ref} className='invisible absolute'>
59 {number}
60 </span>
61 );
62 }
63
64 return (
65 <motion.span
66 style={{ y }}
67 layoutId={`${uniqueId}-${number}`}
68 className='absolute inset-0 flex items-center justify-center'
69 transition={TRANSITION}
70 ref={ref}
71 >
72 {number}
73 </motion.span>
74 );
75}
76
77type SlidingNumberProps = {
78 value: number;
79 padStart?: boolean;
80 decimalSeparator?: string;
81};
82
83export function SlidingNumber({
84 value,
85 padStart = false,
86 decimalSeparator = '.',
87}: SlidingNumberProps) {
88 const absValue = Math.abs(value);
89 const [integerPart, decimalPart] = absValue.toString().split('.');
90 const integerValue = parseInt(integerPart, 10);
91 const paddedInteger =
92 padStart && integerValue < 10 ? `0${integerPart}` : integerPart;
93 const integerDigits = paddedInteger.split('');
94 const integerPlaces = integerDigits.map((_, i) =>
95 Math.pow(10, integerDigits.length - i - 1)
96 );
97
98 return (
99 <div className='flex items-center'>
100// … truncated

What it pulls in

npm packages

  • motion
  • react-use-measure

Files it writes

  • components/core/sliding-number.tsx

Facts

Registry
motion-primitives
Type
registry:ui
Access
Free
Files written
1
Licence
the repository states none
First indexed
2026-08-01
Last confirmed
2 days ago

Go to the source

motion-primitives.com ibelick/motion-primitives on GitHub Raw registry item This item as JSON

More from motion-primitives

All 33 items
Same registry, same kind — the ones you would have found next
ComponentRegistryKindAccessInstallsCommand
Accordionaccordionmotion-primitivesComponentsFree1 dep
Animated Backgroundanimated-backgroundmotion-primitivesComponentsFree1 dep
Animated Groupanimated-groupmotion-primitivesComponentsFree1 dep
Animated Numberanimated-numbermotion-primitivesComponentsFree1 dep
Border Trailborder-trailmotion-primitivesComponentsFree1 dep
Carouselcarouselmotion-primitivesComponentsFree1 dep
Cursorcursormotion-primitivesComponentsFree1 dep
Dialogdialogmotion-primitivesComponentsFree1 dep · 2 files
BlockDex

Built by

Kynth Studio

Index

Components
Registries
Method
Open API

Guides

shadcn component libraries
Best shadcn registries
Free shadcn blocks

Reference

Corpus counts
Crawl health
hello@kynth.studio
Privacy
Terms

BlockDex

BlockDex

Built by

Kynth Studio

Index

Components
Registries
Method
Open API

Guides

shadcn component libraries
Best shadcn registries
Free shadcn blocks

Reference

Corpus counts
Crawl health
hello@kynth.studio
Privacy
Terms

BlockDex

BlockDex

Built by

Kynth Studio

Index

Components
Registries
Method
Open API

Guides

shadcn component libraries
Best shadcn registries
Free shadcn blocks

Reference

Corpus counts
Crawl health
hello@kynth.studio
Privacy
Terms

BlockDex