Motion Number
Animated Number component with transition, format, and localization. It is accessible, customizable, and ideal for smooth UI interactions.
Animated Number component with transition, format, and localization. It is accessible, customizable, and ideal for smooth UI interactions.
Read Docs to know more motion number
npm install motion-number
// @ts-nocheckimport React, { useState, useEffect, ReactNode } from 'react';import dynamic from 'next/dynamic';import { cn } from '@/lib/utils';// Dynamically import MotionNumber with SSR disabledconst MotionNumber = dynamic(() => import('motion-number'), { ssr: false });interface NumberFormat extends Intl.NumberFormatOptions {}interface MotionNumberProps {value: number;format?: NumberFormat;className?: string;animate?: Record<string, any>;transition?: Record<string, any>;}interface OptionProps extends MotionNumberProps {content?: ReactNode;}interface NumberShuffleProps {currentValue: number;format?: NumberFormat;before?: OptionProps;first?: OptionProps;last?: OptionProps;after?: OptionProps;clasname?: string;}const NumberShuffle: React.FC<NumberShuffleProps> = ({currentValue,format = {},before,first,last,after,clasname,}) => {const [isClient, setIsClient] = useState(false);useEffect(() => {setIsClient(true);}, []);if (!isClient) {return (<div className='w-fit grid place-content-center mx-auto space-y-4 py-10'><div className='text-6xl'>{new Intl.NumberFormat('en-US', format).format(currentValue)}</div></div>);}const renderOption = (option, defaultContent = null) => {if (option && typeof option === 'object') {return (<MotionNumbervalue={option.value}format={option.format || {}}className={option.className || ''}animate={option.animate || {}}transition={option.transition || {}}>{option.content}</MotionNumber>);}return defaultContent;};return (<div className='w-fit grid place-content-center mx-auto space-y-4 py-10'><div><MotionNumbervalue={currentValue}format={format}className={cn('text-6xl', clasname)}before={() => renderOption(before)}first={() => renderOption(first)}last={() => renderOption(last)}after={() => renderOption(after)}/></div></div>);};export default NumberShuffle;
Prop | Type | Default | Description |
---|---|---|---|
currentValue | number | The current number value to be displayed. | |
format | NumberFormat | {} | Options for formatting the number. |
before | OptionProps | Optional props for content displayed before the number. | |
first | OptionProps | Optional props for content displayed before the first number. | |
last | OptionProps | Optional props for content displayed after the last number. | |
after | OptionProps | Optional props for content displayed after the number. | |
clasname | string | Optional CSS class for styling the component. |