{"version":3,"file":"components\\ui\\counting-number.cjs","sources":["webpack://@arolariu/components/./src/components/ui/counting-number.tsx"],"sourcesContent":["\n\nimport * as React from \"react\";\nimport {\n  type SpringOptions,\n  type UseInViewOptions,\n  useInView,\n  useMotionValue,\n  useSpring,\n} from \"motion/react\";\n\ninterface CountingNumberProps extends React.HTMLAttributes<HTMLSpanElement> {\n  number: number;\n  fromNumber?: number;\n  padStart?: boolean;\n  inView?: boolean;\n  inViewMargin?: UseInViewOptions[\"margin\"];\n  inViewOnce?: boolean;\n  decimalSeparator?: string;\n  transition?: SpringOptions;\n  decimalPlaces?: number;\n}\n\nconst CountingNumber = React.forwardRef<HTMLSpanElement, CountingNumberProps>(\n  (\n    {\n      number,\n      fromNumber = 0,\n      padStart = false,\n      inView = false,\n      inViewMargin = \"0px\",\n      inViewOnce = true,\n      decimalSeparator = \".\",\n      transition = { stiffness: 90, damping: 50 },\n      decimalPlaces = 0,\n      className,\n      ...props\n    },\n    ref\n  ) => {\n    const localRef = React.useRef<HTMLSpanElement>(null);\n    React.useImperativeHandle(ref, () => localRef.current as HTMLSpanElement);\n\n    const numberStr = number.toString();\n    const decimals =\n      typeof decimalPlaces === \"number\"\n        ? decimalPlaces\n        : numberStr.includes(\".\")\n        ? numberStr.split(\".\")[1].length\n        : 0;\n\n    const motionVal = useMotionValue(fromNumber);\n    const springVal = useSpring(motionVal, transition);\n    const inViewResult = useInView(localRef, {\n      once: inViewOnce,\n      margin: inViewMargin,\n    });\n    const isInView = !inView || inViewResult;\n\n    React.useEffect(() => {\n      if (isInView) motionVal.set(number);\n    }, [isInView, number, motionVal]);\n\n    React.useEffect(() => {\n      const unsubscribe = springVal.on(\"change\", (latest) => {\n        if (localRef.current) {\n          let formatted =\n            decimals > 0\n              ? latest.toFixed(decimals)\n              : Math.round(latest).toString();\n\n          if (decimals > 0) {\n            formatted = formatted.replace(\".\", decimalSeparator);\n          }\n\n          if (padStart) {\n            const finalIntLength = Math.floor(Math.abs(number)).toString()\n              .length;\n            const [intPart, fracPart] = formatted.split(decimalSeparator);\n            const paddedInt = intPart.padStart(finalIntLength, \"0\");\n            formatted = fracPart\n              ? `${paddedInt}${decimalSeparator}${fracPart}`\n              : paddedInt;\n          }\n\n          localRef.current.textContent = formatted;\n        }\n      });\n      return () => unsubscribe();\n    }, [springVal, decimals, padStart, number, decimalSeparator]);\n\n    const finalIntLength = Math.floor(Math.abs(number)).toString().length;\n    const initialText = padStart\n      ? \"0\".padStart(finalIntLength, \"0\") +\n        (decimals > 0 ? decimalSeparator + \"0\".repeat(decimals) : \"\")\n      : \"0\" + (decimals > 0 ? decimalSeparator + \"0\".repeat(decimals) : \"\");\n\n    return (\n      <span ref={localRef} className={className} {...props}>\n        {initialText}\n      </span>\n    );\n  }\n);\n\nCountingNumber.displayName = \"CountingNumber\";\n\nexport { CountingNumber, type CountingNumberProps };\n"],"names":["CountingNumber","React","number","fromNumber","padStart","inView","inViewMargin","inViewOnce","decimalSeparator","transition","decimalPlaces","className","props","ref","localRef","numberStr","decimals","motionVal","useMotionValue","springVal","useSpring","inViewResult","useInView","isInView","unsubscribe","latest","formatted","Math","finalIntLength","intPart","fracPart","paddedInt","initialText"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuBA,MAAMA,iBAAiB,WAAjBA,GAAiBC,+BAAAA,UAAgB,CACrC,CACE,EACEC,MAAM,EACNC,aAAa,CAAC,EACdC,WAAW,KAAK,EAChBC,SAAS,KAAK,EACdC,eAAe,KAAK,EACpBC,aAAa,IAAI,EACjBC,mBAAmB,GAAG,EACtBC,aAAa;IAAE,WAAW;IAAI,SAAS;AAAG,CAAC,EAC3CC,gBAAgB,CAAC,EACjBC,SAAS,EACT,GAAGC,OACJ,EACDC;IAEA,MAAMC,WAAWb,+BAAAA,MAAY,CAAkB;IAC/CA,+BAAAA,mBAAyB,CAACY,KAAK,IAAMC,SAAS,OAAO;IAErD,MAAMC,YAAYb,OAAO,QAAQ;IACjC,MAAMc,WACJ,mBAAON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