@arolariu/components
Version:
🎨 70+ beautiful, accessible React components built on Base UI. TypeScript-first, CSS Modules styling, tree-shakeable, SSR-ready. Perfect for modern web apps, design systems & rapid prototyping. Zero config, maximum flexibility! ⚡
66 lines (65 loc) • 3.03 kB
JavaScript
"use client";
import { jsx } from "react/jsx-runtime";
import { useInView, useMotionValue, useSpring } from "motion/react";
import { cn } from "../../lib/utilities.js";
import counting_number_module from "./counting-number.module.js";
import * as __rspack_external_react from "react";
const CountingNumber = /*#__PURE__*/ __rspack_external_react.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 = __rspack_external_react.useRef(null);
__rspack_external_react.useImperativeHandle(ref, ()=>localRef.current, []);
const numberStr = number.toString();
const decimals = numberStr.includes(".") ? numberStr.split(".")[1]?.length ?? 0 : 0;
const resolvedDecimalPlaces = "number" == typeof decimalPlaces ? decimalPlaces : decimals;
const motionValue = useMotionValue(fromNumber);
const springValue = useSpring(motionValue, transition);
const inViewResult = useInView(localRef, {
once: inViewOnce,
margin: inViewMargin
});
const isInView = !inView || inViewResult;
__rspack_external_react.useEffect(()=>{
if (isInView) motionValue.set(number);
}, [
isInView,
motionValue,
number
]);
__rspack_external_react.useEffect(()=>{
const unsubscribe = springValue.on("change", (latest)=>{
if (!localRef.current) return;
let formatted = resolvedDecimalPlaces > 0 ? latest.toFixed(resolvedDecimalPlaces) : Math.round(latest).toString();
if (resolvedDecimalPlaces > 0) formatted = formatted.replace(".", decimalSeparator);
if (padStart) {
const finalIntegerLength = Math.floor(Math.abs(number)).toString().length;
const [integerPart, fractionPart] = formatted.split(decimalSeparator);
const paddedInteger = integerPart.padStart(finalIntegerLength, "0");
formatted = fractionPart ? `${paddedInteger}${decimalSeparator}${fractionPart}` : paddedInteger;
}
localRef.current.textContent = formatted;
});
return ()=>{
unsubscribe();
};
}, [
decimalSeparator,
number,
padStart,
resolvedDecimalPlaces,
springValue
]);
const finalIntegerLength = Math.floor(Math.abs(number)).toString().length;
const suffix = resolvedDecimalPlaces > 0 ? `${decimalSeparator}${"0".repeat(resolvedDecimalPlaces)}` : "";
const initialText = padStart ? `${"0".padStart(finalIntegerLength, "0")}${suffix}` : `0${suffix}`;
return /*#__PURE__*/ jsx("span", {
ref: localRef,
className: cn(counting_number_module.countingNumber, className),
...props,
children: initialText
});
});
CountingNumber.displayName = "CountingNumber";
export { CountingNumber };
//# sourceMappingURL=counting-number.js.map