Forked from Brian Hague's Pen WDI React Starter.
Created
October 13, 2021 13:04
-
-
Save VRamazing/3affa7bbac18acbaaceebbd7ac8b7b50 to your computer and use it in GitHub Desktop.
ReactJS Stopwatch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="container"></div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ReactDOM.render( | |
<React.StrictMode> | |
<App /> | |
</React.StrictMode>, | |
document.getElementById('container') | |
); | |
function Time(props){ | |
return ( | |
<p>{("0" + Math.floor((props.time / 60000) % 60)).slice(-2)}:{("0" + Math.floor((props.time / 1000) % 60)).slice(-2)}:{("0" + ((props.time / 10) % 100)).slice(-2)}</p>); | |
} | |
function App() { | |
const [isActive, setIsActive] = React.useState(false); | |
const [isPaused, setIsPaused] = React.useState(true); | |
const [time, setTime] = React.useState(0); | |
const [lap, setLap] = React.useState([]); | |
React.useEffect(() => { | |
let interval = null; | |
if (isActive && !isPaused) { | |
interval = setInterval(() => { | |
setTime((time) => time + 10); | |
}, 10); | |
} else { | |
clearInterval(interval); | |
} | |
return () => { | |
clearInterval(interval); | |
}; | |
}, [isActive, isPaused]); | |
const handleStart = () => { | |
setIsActive(true); | |
setIsPaused(false); | |
}; | |
const handlePauseResume = () => { | |
if(isActive){ | |
setIsPaused(!isPaused); | |
} | |
}; | |
const handleLap = (time) => { | |
if(isActive && lap.indexOf(time)===-1){ | |
setLap(lap => [time ,...lap]); | |
} | |
}; | |
const handleReset = () => { | |
setIsActive(false); | |
setTime(0); | |
setLap([]); | |
setIsPaused(true); | |
}; | |
return ( | |
<div className="App"> | |
<div className="title">Pesto Stopwatch</div> | |
<div className="stopwatch"> | |
<Time time={time} /> | |
<div className="lapTimer"> | |
{lap.map(lapTime => ( | |
<Time time={lapTime} /> | |
))} | |
</div> | |
</div> | |
<div className="controls"> | |
{!isActive && isPaused && ( | |
<div className="control-icon" onClick={handleStart}>Play <i className="fas fa-play"></i></div> | |
)} | |
{isActive && ( | |
<div className="control-icon" onClick={handlePauseResume}>{!isPaused ? "Pause" : "Resume"} | |
<i className="fas fa-pause" style={{"display": !isPaused ? "none": "inline"}}></i> | |
<i className="fas fa-play" style={{"display": isPaused ? "none": "inline"}}></i> | |
</div> | |
)} | |
<div className="control-icon" onClick={handleReset}>Reset <i className="fas fa-hourglass-start"></i> | |
</div> | |
<div className="control-icon" onClick={()=>handleLap(time)}>Lap <i className="fas fa-flag"></i> | |
</div> | |
</div> | |
</div> | |
); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.production.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js"></script> | |
<script src="https://use.fontawesome.com/releases/v5.0.6/js/all.js"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.App{ | |
width: 600px; | |
margin: auto; | |
text-align: center; | |
font-family: sans-serif; | |
} | |
.title{ | |
font-size: 50px; | |
text-transform: uppercase; | |
color: white; | |
background-color: pink; | |
border-radius: 10px; | |
margin: 30px 0; | |
} | |
.stopwatch{ | |
font-size: 80px; | |
.lapTimer{ | |
font-size: 60px; | |
font-weight: 100; | |
opacity: 0.8; | |
height: 130px; | |
overflow-y: scroll; | |
} | |
} | |
.controls{ | |
display: flex; | |
font-size: 20px; | |
align-items: center; | |
justify-content: center; | |
.control-icon{ | |
background-color: pink; | |
margin: 10px; | |
padding: 10px; | |
border-radius: 10px; | |
cursor: pointer; | |
i { | |
margin-left: 20px; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment