72 lines
1.6 KiB
TypeScript
72 lines
1.6 KiB
TypeScript
"use client";
|
|
|
|
import React, { useEffect, useRef, useState } from "react";
|
|
|
|
interface AnimatedNumberProps {
|
|
value: number;
|
|
duration?: number;
|
|
decimals?: number;
|
|
prefix?: string;
|
|
suffix?: string;
|
|
className?: string;
|
|
}
|
|
|
|
/**
|
|
* Animated Number Component
|
|
* Counts up from current value to target value with smooth animation
|
|
*/
|
|
export function AnimatedNumber({
|
|
value,
|
|
duration = 1500,
|
|
decimals = 0,
|
|
prefix = "",
|
|
suffix = "",
|
|
className = "",
|
|
}: AnimatedNumberProps) {
|
|
const [displayValue, setDisplayValue] = useState(value);
|
|
const animationFrameRef = useRef<number>();
|
|
|
|
useEffect(() => {
|
|
const startValue = displayValue;
|
|
const endValue = value;
|
|
const startTime = Date.now();
|
|
|
|
const animate = () => {
|
|
const elapsed = Date.now() - startTime;
|
|
const progress = Math.min(elapsed / duration, 1);
|
|
|
|
// Easing function (ease-out)
|
|
const easeOut = 1 - Math.pow(1 - progress, 3);
|
|
|
|
const currentValue = startValue + (endValue - startValue) * easeOut;
|
|
setDisplayValue(currentValue);
|
|
|
|
if (progress < 1) {
|
|
animationFrameRef.current = requestAnimationFrame(animate);
|
|
} else {
|
|
setDisplayValue(endValue);
|
|
}
|
|
};
|
|
|
|
animationFrameRef.current = requestAnimationFrame(animate);
|
|
|
|
return () => {
|
|
if (animationFrameRef.current) {
|
|
cancelAnimationFrame(animationFrameRef.current);
|
|
}
|
|
};
|
|
}, [value, duration]);
|
|
|
|
const formattedValue = displayValue.toFixed(decimals);
|
|
const numberWithCommas = formattedValue.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
|
|
|
|
return (
|
|
<span className={className}>
|
|
{prefix}
|
|
{numberWithCommas}
|
|
{suffix}
|
|
</span>
|
|
);
|
|
}
|
|
|