@arolariu/components
Version:
🎨 70+ beautiful, accessible React components built on Radix UI. TypeScript-first, tree-shakeable, SSR-ready. Perfect for modern web apps, design systems & rapid prototyping. Zero config, maximum flexibility! ⚡
62 lines (61 loc) • 2.61 kB
JavaScript
"use client";
import { jsx } from "react/jsx-runtime";
import { useInView, useMotionValue, useSpring } from "motion/react";
import { forwardRef, useEffect, useImperativeHandle, useRef } from "react";
const CountingNumber = /*#__PURE__*/ forwardRef(({ number, fromNumber = 0, padStart = false, inView = false, inViewMargin = "0px", inViewOnce = true, decimalSeparator = ".", transition = {
stiffness: 90,
damping: 50
}, decimalPlaces = 0, className, ...props }, ref)=>{
const localRef = useRef(null);
useImperativeHandle(ref, ()=>localRef.current);
const numberStr = number.toString();
const decimals = "number" == typeof decimalPlaces ? decimalPlaces : numberStr.includes(".") ? numberStr?.split(".")[1].length : 0;
const motionVal = useMotionValue(fromNumber);
const springVal = useSpring(motionVal, transition);
const inViewResult = useInView(localRef, {
once: inViewOnce,
margin: inViewMargin
});
const isInView = !inView || inViewResult;
useEffect(()=>{
if (isInView) motionVal.set(number);
}, [
isInView,
number,
motionVal
]);
useEffect(()=>{
const unsubscribe = springVal.on("change", (latest)=>{
if (localRef.current) {
let formatted = decimals > 0 ? latest.toFixed(decimals) : Math.round(latest).toString();
if (decimals > 0) formatted = formatted.replace(".", decimalSeparator);
if (padStart) {
const finalIntLength = Math.floor(Math.abs(number)).toString().length;
const [intPart, fracPart] = formatted.split(decimalSeparator);
const paddedInt = intPart.padStart(finalIntLength, "0");
formatted = fracPart ? `${paddedInt}${decimalSeparator}${fracPart}` : paddedInt;
}
localRef.current.textContent = formatted;
}
});
return ()=>unsubscribe();
}, [
springVal,
decimals,
padStart,
number,
decimalSeparator
]);
const finalIntLength = Math.floor(Math.abs(number)).toString().length;
const suffix = decimals > 0 ? `${decimalSeparator}${"0".repeat(decimals)}` : "";
const initialText = padStart ? `${"0".padStart(finalIntLength, "0")}${suffix}` : `0${suffix}`;
return /*#__PURE__*/ jsx("span", {
ref: localRef,
className: className,
...props,
children: initialText
});
});
CountingNumber.displayName = "CountingNumber";
export { CountingNumber };
//# sourceMappingURL=counting-number.js.map