diff --git a/client/src/App.js b/client/src/App.js index 41b27b8..5c3a89f 100644 --- a/client/src/App.js +++ b/client/src/App.js @@ -3,7 +3,6 @@ import io from "socket.io-client"; import Form from "./components/Form"; import Listen from "./components/Listen"; import CarouselSliders from "./components/CarouselSliders"; -import AnimatedNumber from "react-animated-numbers"; import { FaMicrophoneLines } from "react-icons/fa6"; import { LiaLaptopSolid } from "react-icons/lia"; import { ToastContainer, toast, Slide } from "react-toastify"; @@ -11,6 +10,8 @@ import "react-toastify/dist/ReactToastify.css"; import { MediaRecorder, register } from "extendable-media-recorder"; import { connect } from "extendable-media-recorder-wav-encoder"; +import AnimatedNumber from "./components/AnimatedNumber"; + const server = process.env.REACT_APP_BACKEND_URL || "http://localhost:5000"; var socket = io(server); @@ -245,12 +246,7 @@ function App() {

!Shazam

- +  Songs

diff --git a/client/src/components/AnimatedNumber.js b/client/src/components/AnimatedNumber.js new file mode 100644 index 0000000..aeedb0a --- /dev/null +++ b/client/src/components/AnimatedNumber.js @@ -0,0 +1,143 @@ +import React from "react"; +import { motion } from "framer-motion"; + +const NUMBERS = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; + +const usePrevious = (value) => { + const ref = React.useRef(); + React.useEffect(() => { + ref.current = value; + }); + + if (typeof ref.current === "undefined") { + return 0; + } + + return ref.current; +}; + +const AnimatedNumber = ({ + animateToNumber, + fontStyle, + transition = { duration: 0.8 }, + includeComma, + delay, + onFinish, + onStart, +}) => { + const prevNumber = usePrevious(animateToNumber); + const animateToNumberString = String(Math.abs(animateToNumber)); + const prevNumberString = String(Math.abs(prevNumber)); + const animateToNumbersArr = Array.from(animateToNumberString, Number); + const prevNumbersArr = Array.from(prevNumberString, Number); + const [onStarted, setOnStarted] = React.useState(false); + const [onFinished, setOnFinished] = React.useState(false); + + React.useEffect(() => { + if (onStarted && onStart) { + const delayTime = typeof delay === "undefined" ? 0 : delay; + + setTimeout(() => { + onStart(); + }, delayTime); + } + }, [onStarted]); + + React.useEffect(() => { + if (onFinished && onFinish) { + onFinish(); + } + }, [onFinished]); + + if (includeComma) { + const reducedArray = new Array( + Math.ceil(animateToNumberString.length / 3) + ).fill(0); + + const startReducedArray = new Array( + Math.ceil(prevNumberString.length / 3) + ).fill(0); + + reducedArray.map((__, index) => { + if (index === 0) { + return; + } + + animateToNumbersArr.splice( + animateToNumberString.length - index * 3, + 0, + "," + ); + }); + + startReducedArray.map((__, index) => { + if (index === 0) { + return; + } + + prevNumbersArr.splice(prevNumberString.length - index * 3, 0, ","); + }); + } + + const [numberHeight, setNumberHeight] = React.useState(0); + + const numberDivRef = React.useRef(null); + + React.useEffect(() => { + setNumberHeight(numberDivRef.current.clientHeight); + }, [animateToNumber]); + + return ( + <> + {numberHeight !== 0 && ( +
+ {animateToNumber < 0 &&
-
} + {animateToNumbersArr.map((n, index) => { + if (typeof n === "string") { + return ( +
+ {n} +
+ ); + } + + return ( +
+ setOnStarted(true)} + onAnimationComplete={() => setOnFinished(true)} + > + {NUMBERS.map((number, i) => ( +
+ {number} +
+ ))} +
+
+ ); + })} +
+ )} + +
+ {0} +
+ + ); +}; + +export default AnimatedNumber;