diff --git a/src/App.jsx b/src/App.jsx index c85699e..a163f12 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -8,8 +8,11 @@ import HandlingForm from "./components/Hooks/useRefHook/HandlingForm"; import FormDetails from "./tryityourself/Akash/FormDetails"; import RandomBackground from "./tryityourself/Akash/RandomBackground"; +// PROJECTS +import StopWatch from "./project/StopWatch/StopWatch"; + const App = () => { - return ; + return ; }; export default App; diff --git a/src/index.css b/src/index.css index bd01d27..3a47964 100644 --- a/src/index.css +++ b/src/index.css @@ -2,3 +2,4 @@ @import "./components/Hooks/useRefHook/HandlingForm.style.css"; @import "./tryityourself/Akash/FormDetails.style.css"; @import "./tryityourself/Akash/RandomBackground.style.css"; +@import "./project/StopWatch/StopWatch.style.css"; diff --git a/src/project/StopWatch/StopWatch.jsx b/src/project/StopWatch/StopWatch.jsx new file mode 100644 index 0000000..fc01131 --- /dev/null +++ b/src/project/StopWatch/StopWatch.jsx @@ -0,0 +1,66 @@ +import React, { useEffect, useRef, useState } from "react"; + +// ICONS +import { FaPlay } from "react-icons/fa"; +import { FaPause } from "react-icons/fa6"; +import { LuTimerReset } from "react-icons/lu"; + +const StopWatch = () => { + // STATES + const [isRunning, setIsRuning] = useState(() => { + return false; + }); + const [elapsedTime, setElapsedTime] = useState(0); + const startTimeRef = useRef(0); + const intervalTimeRef = useRef(null); + + // SIDE-EFFECTS + useEffect(() => { + if (isRunning) { + startTimeRef.current = Date.now() - elapsedTime; + intervalTimeRef.current = setInterval(() => { + setElapsedTime(Date.now() - startTimeRef.current); + }, 10); + + return () => clearInterval(intervalTimeRef.current); + } + }, [isRunning]); + + // HANDLER FUNCTIONS + const handleStartOrStop = () => { + return setIsRuning((previous) => !previous); + }; + + const handleReset = () => { + setIsRuning(false); + setElapsedTime(0); + }; + + const displayStopWatchTime = (elapsedTime) => { + let hours = Math.floor(elapsedTime / (1000 * 60 * 60)); + let minutes = Math.floor((elapsedTime / (1000 * 60)) % 60); + let seconds = Math.floor((elapsedTime / 1000) % 60); + let milliseconds = Math.floor((elapsedTime % 1000) / 10); + return ` + ${hours.toString().padStart(2, "0")}: + ${minutes.toString().padStart(2, "0")}: + ${seconds.toString().padStart(2, "0")}: + ${milliseconds.toString().padStart(2, "0")}`; + }; + + return ( +
+
{displayStopWatchTime(elapsedTime)}
+
+ + +
+
+ ); +}; + +export default StopWatch; diff --git a/src/project/StopWatch/StopWatch.style.css b/src/project/StopWatch/StopWatch.style.css new file mode 100644 index 0000000..9185d4b --- /dev/null +++ b/src/project/StopWatch/StopWatch.style.css @@ -0,0 +1,7 @@ +.CONTAINER { + @apply h-screen w-full flex flex-col items-center justify-center bg-slate-500 text-black; +} + +.BUTTON { + @apply p-2 rounded-md border-1 border-black hover:bg-black hover:text-white hover:cursor-pointer; +}