Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <html lang="en">
	<head>
		<meta charset="UTF-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<!--These are the two React Scripts-->
		<script crossorigin src="https://unpkg.com/react@17/umd/react.production.min.js"></script>
        <script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.production.min.js"></script>
		<!--These are the Babel (React Compiler) scripts-->
		<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
		<script type="text/babel" src="./index.js"></script>
		<!--This is Font Awesome-->
		<script src="https://kit.fontawesome.com/12bc923b2b.js" crossorigin="anonymous"></script>
		<!--This is the bootstrap link (CSS only)-->
		<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-0evHe/X+R7YkIZDRvuzKMRqM+OrBnVFBL6DOitfPri4tjfHxaWutUpFmBp4vmVor" crossorigin="anonymous">
		<!--CSS local file reference-->
		<link rel="stylesheet" href="./style.css">
		<title>25 + 5 Clock</title>
	</head>
	<body>
        <!--freeCodeCampTestBundle-->
        <script src="https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js"></script>
		<div id="root"></div>
	</body>
	</html>
              
            
!

CSS

              
                * {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    font-family: sans-serif;
  }

  .background {
    height: 100vh;
    width: 100vw;
    background-image: url("https://cdn.pixabay.com/photo/2016/06/25/12/52/laptop-1478822_960_720.jpg");
    background-repeat: no-repeat;
    background-attachment: fixed;
    background-size: cover;
    display: flex;
    justify-content: center;
    align-items: center;
  }

  main {
    font-size: 1.8rem;
    color: rgb(226, 226, 226);
    background-color: rgba(0, 0, 0, 0.85);
    border-radius: 30px;
    padding: 1rem 0rem;
    border: 3px solid goldenrod;
    box-shadow: 0px 0px 14px 2px rgba(255,255,46,0.9);
    width: 600px;
    height: 400px;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    align-items: center;
  }

  .lengthControls {
    display: flex;
    justify-content: space-between;
    width: 90%;
  }

  .sessionWrapper {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 50%;
    margin-left: -20px;
  }

  #session-length {
    margin: -10px;
  }

  .breakWrapper {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 50%;
    margin-right: -20px;
  }

  #break-length {
    margin: -10px;
  }

  .timerWrapper {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    align-items: center;
    margin: -110px 0px 0px 0px;
    
  }

  .timerDisplay {
    border: 3px solid goldenrod;
    box-shadow: 0px 0px 14px 2px rgba(255,255,46,0.9);
    border-radius: 15px;
    padding: 0.4rem 1.4rem;
    text-align: center;
    font-size: 3rem;
  }

  #timer-label {
    font-size: 1.5rem;
  }

  .timerControls {
    margin-top: 1rem;
    width: 6rem;
    display: flex;
    justify-content: space-between;
  }

  i {
    cursor: pointer;
    color: goldenrod;
    text-shadow: rgba(255,162,0,0.84) 0px 0px 20px;
  }
              
            
!

JS

              
                function App(){
    const [displayTime, setDisplayTime] = React.useState(25 * 60);
    const [sessionLength, setSessionLength] = React.useState(25 * 60);
    const [breakLength, setBreakLength] = React.useState(5 * 60);
    const [sessionOrBreak, setSessionOrBreak] = React.useState(true);
    const [timerOn, setTimerOn] = React.useState(false);
    
    React.useEffect(() => {if (displayTime == 0) {playAlarm()}}, [displayTime])
    
    function reset() {
        setDisplayTime(25*60)
        setBreakLength(5*60)
        setSessionLength(25*60)
        setSessionOrBreak(true)
        beep.pause();
        beep.currentTime = 0;
        if (timerOn) {
            clearInterval(localStorage.getItem("interval-id"));
            setTimerOn(prev => !prev)
        }   
    }

    function playAlarm() {
        beep.currentTime = 0;
        beep.play();
    }

    function formatTime(time) {
        let minutes = Math.floor(time/60);
        let seconds = time % 60;
        return (
            (minutes < 10? "0" + minutes: minutes) + 
            ":" +
            (seconds < 10? "0" + seconds : seconds)
            );
    };

    //this function adjusts session & break length
    function adjustTime(timeType, amount) {
        //conditions for ignoring input
        if (timeType == "session" && amount > 0 && sessionLength >= 60 * 60) {return}
        if (timeType == "session" && amount < 0 && sessionLength <= 1 * 60) {return}
        if (timeType == "break" && amount > 0 && breakLength >= 60 * 60) {return}
        if (timeType == "break" && amount < 0 && breakLength <= 1 * 60) {return}
        //logic
        if (timeType == "session") {
            setSessionLength(prev => prev + amount)
            if (sessionOrBreak) {setDisplayTime(prev => prev + amount)}
        }
        if (timeType == "break") {
            setBreakLength(prev => prev + amount)
            if (!sessionOrBreak) {setDisplayTime(prev => prev + amount)}
        }
    }
    
    //this function sets the `setInterval` unique ID to local storage so it can be reliably retrived anywhere in the code to stop the timer (using a global variable didn't work)
    function timeFlow() {
        let second = 1000;
        let date = new Date().getTime();
        let nextDate = new Date().getTime() + second;
        let sessionBreakControl = sessionOrBreak
        if (!timerOn) {
            let interval = setInterval(() => {
                date = new Date().getTime();
                if (date > nextDate){
                    setDisplayTime(prev => {
                        if(prev <= 0 && sessionBreakControl){
                            sessionBreakControl = false;
                            setSessionOrBreak(false)
                            return breakLength;
                        }else if(prev <= 0 && !sessionBreakControl) {
                            sessionBreakControl = true;
                            setSessionOrBreak(true)
                            return sessionLength;
                        }
                        return prev - 1;
                    });
                    nextDate += second;
                }
            }, 50)
            localStorage.clear();
            localStorage.setItem("interval-id", interval)
        }

        if (timerOn) {
            clearInterval(localStorage.getItem("interval-id"));
        }
        setTimerOn(prev => !prev)
    }

    return (
        <div className="background">
            <main>
                <h2 className="title">The 25 + 5 Clock</h2>
                <div className="lengthControls">
                    <div className="sessionWrapper">
                        <div id="break-label">Session Length</div>
                        <div id="session-increment" onClick={() => adjustTime("session", 60)}><i className="fa-solid fa-angle-up"></i></div>
                        <div id="session-length">{sessionLength / 60}</div>
                        <div id="session-decrement" onClick={() => adjustTime("session", -60)}><i className="fa-solid fa-angle-down"></i></div>
                    </div>
                    <div className="breakWrapper">
                        <div id="session-label">Break Length</div>
                        <div id="break-increment" onClick={() => adjustTime("break", 60)}><i className="fa-solid fa-angle-up"></i></div>
                        <div id="break-length">{breakLength / 60}</div>
                        <div id="break-decrement" onClick={() => adjustTime("break", -60)}><i className="fa-solid fa-angle-down"></i></div>
                    </div>
                </div>
                <Timer 
                displayTime={displayTime} 
                timerOn={timerOn} 
                timeFlow={timeFlow} 
                reset={reset}
                formatTime={formatTime}
                sessionOrBreak={sessionOrBreak}
                sessionLength={sessionLength} 
                breakLength={breakLength}
                />
            </main>
            <audio id="beep" src="https://raw.githubusercontent.com/freeCodeCamp/cdn/master/build/testable-projects-fcc/audio/BeepSound.wav" preload="auto"/>
        </div>
    );
}

function Timer (props) {

    return (
        <div className="timerWrapper">
            <div className="timerDisplay">
                <div id="timer-label">{props.sessionOrBreak ? "session" : "break"}</div>
                <div id="time-left">{props.formatTime(props.displayTime)}</div>
            </div>
            <div className="timerControls">
                <i className="fa-solid fa-play" id="start_stop" onClick={props.timeFlow}></i>
                <i class="fa-solid fa-pause" onClick={props.timeFlow}></i>
                <i className="fa-solid fa-arrows-rotate" id="reset" onClick={props.reset}></i>
            </div>
        </div>
    );
}

ReactDOM.render(<App/>, document.getElementById("root"));
              
            
!
999px

Console