"use client"; import { motion, useSpring, useTransform } from 'motion/react'; import { useEffect } from 'react'; import './Counter.css'; function Number({ mv, number, height }) { let y = useTransform(mv, latest => { let placeValue = latest % 10; let offset = (10 + number - placeValue) % 10; let memo = offset * height; if (offset > 5) { memo -= 10 * height; } return memo; }); return ( {number} ); } function Digit({ place, value, height, digitStyle }) { let valueRoundedToPlace = Math.floor(value / place); let animatedValue = useSpring(valueRoundedToPlace); useEffect(() => { animatedValue.set(valueRoundedToPlace); }, [animatedValue, valueRoundedToPlace]); return (
{Array.from({ length: 10 }, (_, i) => ( ))}
); } export default function Counter({ value, fontSize = 100, padding = 0, places = [100, 10, 1], gap = 8, borderRadius = 4, horizontalPadding = 8, textColor = 'white', fontWeight = 'bold', containerStyle, counterStyle, digitStyle, gradientHeight = 16, gradientFrom = 'black', gradientTo = 'transparent', topGradientStyle, bottomGradientStyle }) { const height = fontSize + padding; const defaultCounterStyle = { fontSize, gap: gap, borderRadius: borderRadius, paddingLeft: horizontalPadding, paddingRight: horizontalPadding, color: textColor, fontWeight: fontWeight }; const defaultTopGradientStyle = { height: gradientHeight, background: `linear-gradient(to bottom, ${gradientFrom}, ${gradientTo})` }; const defaultBottomGradientStyle = { height: gradientHeight, background: `linear-gradient(to top, ${gradientFrom}, ${gradientTo})` }; return (
{places.map(place => ( ))}
); }