Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's 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 it's URL and the proper URL extention.

+ 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

Save Automatically?

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

              
                <script src="https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js"></script>
<script src="https://kit.fontawesome.com/e541811050.js" crossorigin="anonymous"></script>

<div id='root'></div>
              
            
!

CSS

              
                * {margin: 0; padding: 0; box-sizing: border-box}

#root {
  width: 100%;
  height: 100%;
  background-color: #274472;
  color: #C3E0E5;
  padding: 5vh 5vw;
}

.container {
  border: 5px solid #C3E0E5;
  border-radius: 15px;
  padding: 10vh 5vw;
  font-family: 'Orbitron', sans-serif;
}

#title {
  display: flex;
  justify-content: center;
  align-items: center;
  text-align: center;
  font-size: 60px;
}

.controlers {
  margin-top: 10vh;
  display: flex;
  justify-content: space-around;
}

.controlers .control .control-label {
  font-size: 40px;
  text-align: center;
}

.controlers .control .button-box {
  margin-top: 4vh;
  margin-bottom: 4px;
  display: flex;
  justify-content: space-evenly;
}

.controlers .control .button-box .fas {
  font-size: 30px;
  cursor: pointer;
  transition: opacity 0.5s;
}

.controlers .control .button-box .fas:hover {
  opacity: 0.5;
}

.controlers .control .button-box .control-length {
  font-size: 40px;
}

.controlers .control .button-box .control-button {
  background-color: #274472;
  color: #C3E0E5;
  border: none;
  outline: none;
}


.timer {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  margin-top: 10vh;
}

.timer #timer-label {
  font-size: 60px
}

.timer #time-left {
  margin: 3vh;
  font-size: 75px;
  font-weigth: bold;
}

.timer .timer-controls {
  display: flex;
  justify-content: space-around;
  width: 200px;
}

.timer .timer-controls button {
  background-color: #274472;
  color: #C3E0E5;
  border: none;
  outline: none;
}

.timer .timer-controls #start_stop {
  font-size: 30px;
  cursor: pointer;
  transition: opacity 0.5s;
}

.timer .timer-controls #start_stop:hover {
  opacity: 0.5;
}

.timer .timer-controls #reset .fas {
  font-size: 30px;
  cursor: pointer;
  transition: opacity 0.5s;
}

.timer .timer-controls #reset .fas:hover {
  opacity: 0.5;
}


              
            
!

JS

              
                // useInterval is a custom Hook wrote by Dan Ambrov,
// source: https://overreacted.io/making-setinterval-declarative-with-react-hooks/
function useInterval(callback, delay) {
  const savedCallback = React.useRef();

  // Remember the latest function.
  React.useEffect(() => {
    savedCallback.current = callback;
  }, [callback]);

  // Set up the interval.
  React.useEffect(() => {
    function tick() {
      savedCallback.current();
    }
    if (delay !== null) {
      let id = setInterval(tick, delay);
      return () => clearInterval(id);
    }
  }, [delay]);
}

//Components
function Control({id, label, time, setTime}){
  
  function reduceTime() {
    if (time > 1) setTime(--time)
  }
  
  function increaseTime() {
    if (time < 60) setTime(++time)
  }
  
  return(
    <div className='control'>
      <h2 className='control-label'id={`${id}-label`}>{label}</h2>
      <div className='button-box'>
        <button 
          className='control-button' 
          id={`${id}-decrement`}
          onClick={reduceTime}
        >
            <i className="fas fa-minus"></i>
        </button>
        <p className='control-length' id={`${id}-length`}>{time}</p>
        <button 
          className='control-button' 
          id={`${id}-increment`}
          onClick={increaseTime}
        >
            <i className="fas fa-plus"></i>
        </button>
      </div>
    </div>
  )
}

function Timer(
  {
    timer, 
    setTimer, 
    isRunning, 
    setIsRunning, 
    timerLabel, 
    setTimerLabel,
    changeTimer,
    resetAll,
    beep
  }
 ) {
  
  function displayTimer(timer) {
        let minutes = parseInt(timer / 60, 10)
        let seconds = parseInt(timer % 60, 10)

        minutes = minutes < 10 ? "0" + minutes : minutes
        seconds = seconds < 10 ? "0" + seconds : seconds
    
        return `${minutes}:${seconds}`
  }
  
   useInterval(() => {
    if(timer > 0){
       setTimer(--timer) 
    } else {
      setIsRunning(false)
      playBeep()
      setTimeout(changeTimer, 4000)
    }
  }, isRunning ? 1000 : null);
  
   function playBeep() {
     var playPromise = beep.play()
     
     if (playPromise !== undefined) {
       playPromise.then(() => {
         setTimeout(() => beep.pause(), 2800)
         setTimeout(() => beep.load(), 3000)
       })
       .catch(error => console.log(error))
     }
     
   }
    
   function controlTimer(){
    setIsRunning(!isRunning)
  }
    
  return(
    <div className='timer'>
      <h2 id='timer-label'>{timerLabel}</h2>
      <p id='time-left'>{displayTimer(timer)}</p>
      <div className='timer-controls'>
        <button id='start_stop' onClick={controlTimer}>
          <i className="fas fa-play"></i>
          <i className="fas fa-pause"></i>  
        </button>
        <button id='reset' onClick={resetAll}>
          <i className="fas fa-undo-alt"></i>
        </button>
        <audio
            id="beep"
            preload="auto"                   src="https://raw.githubusercontent.com/freeCodeCamp/cdn/master/build/testable-projects-fcc/audio/BeepSound.wav"
          />
      </div>
    </div>
  )
}

function App(){
  const [breakTime, setBreakTime] = React.useState(5)
  const [sessionTime, setSessionTime] = React.useState(25)
  const [timer, setTimer] = React.useState(sessionTime * 60)
  
  const [isRunning, setIsRunning] = React.useState(false)
  const [timerLabel, setTimerLabel] = React.useState('Session')
  
  React.useEffect(() => setTimer(sessionTime*60), [sessionTime])
  
  const beep = document.querySelector('#beep')
  
  function resetAll() {
    setIsRunning(false)
    setBreakTime(5)
    setSessionTime(25)
    setTimer(sessionTime*60)
    setTimerLabel('Session')
    beep.pause()
    beep.load()
  }
  
  function changeTimer() {
     if(timerLabel === 'Session'){
       setTimerLabel('Break') 
       setTimer(breakTime*60)
     } else {
       setTimerLabel('Session')
       setTimer(sessionTime*60)
     }
    
    setIsRunning(true)
   }
  
  
  return(
    <div class='container'>
      <h1 id="title">Pomodoro's Clock</h1>
      <div className='controlers'> 
        <Control 
          id='break' 
          label='Break Length' 
          time={breakTime}
          setTime={setBreakTime}
        />
        <Control 
          id='session' 
          label='Session Length' 
          time={sessionTime}
          setTime={setSessionTime}
        />
      </div>
     
      <Timer 
        timer={timer}
        setTimer={setTimer}
        isRunning={isRunning}
        setIsRunning={setIsRunning}
        timerLabel={timerLabel}
        setTimerLabel={setTimerLabel}
        changeTimer={changeTimer}
        resetAll={resetAll}
        beep={beep}
      />
    </div>
  )
}

ReactDOM.render(<App />, document.querySelector('#root'));
              
            
!
999px

Console