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. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                <html>
  <head>
    <script src="https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js"></script>
    <link rel="stylesheet" media="screen" href="https://fontlibrary.org/face/lcd" type="text/css"/>
    <link href="https://fonts.googleapis.com/css?family=Archivo+Black&display=swap" rel="stylesheet">
  </head>
  <body>
    <div id="root" />
  </body>
</html>

              
            
!

CSS

              
                #root {
  display: flex;
  height: 100vh;
  justify-content: center;
  align-items: center;
  text-align: center;
}

body {
  font-family: 'Archivo Black', cursive;
  background-color: #8395a7;
  color: #ecf0f1;
  cursor: default;
}

.main-title {
  font-size: 36px;
}

.pomodoro-display {
  border: 5px solid #576574;
  padding: 20px;
  border-radius: 7px;
  width: 200px;
  margin: 5px auto 10px auto;

  #timer-label {
    margin: 0px;
    font-size: 24px;
    margin-bottom: 10px;
  }
  
  #time-left {
    font-family: 'LCD14';
    font-size: 48px;
  }
}

.step-control-wrapper {
  display: flex;
  justify-content: space-between;
  margin: 10px 0px;

  .step-label {
    margin: 0 5px;
    font-size: 20px;
  }

  .step-control {
    display: flex;
    justify-content: space-between;
    font-size: 24px;
    width: 100px;
    margin: 5px auto;
  }
}

#reset, #start_stop {
  margin: 0 20px;
  font-size: 24px;
}

#session-increment,
#session-decrement,
#break-increment,
#break-decrement,
#reset,
#start_stop
{
  cursor: pointer;
  line-height: 32px;
  
  &:active {
    color: #48dbfb;
  }
}
              
            
!

JS

              
                function Timer(callback, delay) {
    let timerId, start, remaining = delay;

    this.pause = function() {
        window.clearTimeout(timerId);
        remaining -= Date.now() - start;
    };

    this.resume = function() {
        start = Date.now();
        window.clearTimeout(timerId);
        timerId = window.setTimeout(callback, remaining);
    };
  
    this.stop = function() {
      window.clearTimeout(timerId);
    }

    this.resume();
}

const PomodoroClock = () => {
  const initialBreakLength = 5;
  const initialSessionLength = 25;
  const SESSION = 'Session';
  const BREAK = 'Break';
  const PAUSED = 'paused';
  const STARTED = 'started';
  const STOPPED = 'stopped';
  const [breakLength, setBreakLength] = React.useState(initialBreakLength);
  const [sessionLength, setSessionLength] = React.useState(initialSessionLength);
  const [time, setTime] = React.useState(initialSessionLength * 60);
  const [timer, setTimer] = React.useState(null);
  const [timerType, setTimerType] = React.useState(SESSION);
  const [timerState, setTimerState] = React.useState(STOPPED);
  
  const startStopCls = timerState === STARTED ? 'fa fa-pause' : 'fa fa-play';
  
  // Continues the timer after it reaches 0
  React.useEffect(() => {
    if(time === 0) {
      let newTime;
      const audio = document.getElementById('beep');
      if(timerType === SESSION) {
        newTime = breakLength * 60 + 1;
        setTimerType(BREAK);
      } else if (timerType === BREAK) {
        newTime = sessionLength * 60 + 1;
        setTimerType(SESSION);
      }
      const timer = new Timer(() => { countdown(newTime) }, 1000);
      setTimer(timer);
      audio.play();
    }
  }, [time]);
  
  const handleLengthChange = (type, decrement) => {
    const currentType = type === SESSION ? SESSION : BREAK;
    const typeLength = type === SESSION ? sessionLength : breakLength;
    const typeCallback = type === SESSION ? setSessionLength : setBreakLength;
    const stepValue = decrement ? -1 : 1;
    const shouldSetTime = decrement ? typeLength > 1 : typeLength < 60;

    if(timerState !== STARTED) {
      if(timerState === PAUSED) {
        timer.stop();
      }
      if(timerType === currentType && shouldSetTime) {
        setTime((typeLength + stepValue) * 60);
      }
      typeCallback((prevState) => {
        if(shouldSetTime) {
          return prevState + stepValue;
        }
        return prevState;
      });
    }
  };
  
  const handleSessionIncrement = () => {
    handleLengthChange(SESSION);
  };
  
  const handleSessionDecrement = () => {
    handleLengthChange(SESSION, true);
  };
  
  const handleBreakIncrement = () => {
    handleLengthChange(BREAK);
  };
  
  const handleBreakDecrement = () => {
    handleLengthChange(BREAK, true);
  };
  
  const handleResetClick = () => {
    if(timer) {
      timer.stop();
    }
    const audio = document.getElementById('beep');
    audio.pause();
    audio.currentTime = 0;
    setBreakLength(initialBreakLength);
    setSessionLength(initialSessionLength);
    setTime(initialSessionLength*60);
    setTimerState(STOPPED);
    setTimerType(SESSION);
  }
  
  const countdown = (timeLeft) => {
    if(timeLeft > 0) {
      setTime(timeLeft - 1);
      const timer = new Timer(() => { 
        countdown(timeLeft - 1);
      }, 1000);
      setTimer(timer);
    }
  };
  
  const handleStartStopClick = () => {
    if (timerState === STARTED) {
      setTimerState(PAUSED);
      timer.pause();
    } else if (timerState === PAUSED) {
      setTimerState(STARTED);
      timer.resume();
    } else if (timerState === STOPPED) {
      const timer = new Timer(() => { countdown(time) }, 1000);
      setTimerState(STARTED);
      setTimer(timer);
    }
  }
  
  return (
    <div className='pomodoro-clock'>
      <h1 className='main-title'>Pomodoro Clock</h1>
      <div className='step-control-wrapper'>
        <StepControl
          prefix='break'
          title='Break Length'
          length={breakLength}
          onIncrement={handleBreakIncrement}
          onDecrement={handleBreakDecrement}
         />
        <StepControl
          prefix='session'
          title='Session Length'
          length={sessionLength}
          onIncrement={handleSessionIncrement}
          onDecrement={handleSessionDecrement}
         />
      </div>
      <PomodoroDisplay time={time} timerLabel={timerType}/>
      <i id='start_stop' onClick={handleStartStopClick} className={startStopCls}></i>
      <i id='reset' onClick={handleResetClick} className="fa fa-sync-alt"></i>
      <audio id="beep" src="https://goo.gl/65cBl1"></audio>
    </div>
  );
};

const StepControl = (props) => {
  const { prefix, title, length, onIncrement, onDecrement } = props;
  const titleId = `${prefix}-label`;
  const lengthId = `${prefix}-length`;
  const incrementId = `${prefix}-increment`;
  const decrementId = `${prefix}-decrement`;
  
  return (
    <div>
      <h3 id={titleId} className='step-label'>{title}</h3>
      <div className='step-control'>
        <i id={incrementId} className="fa fa-arrow-up" onClick={onIncrement}></i>
        <div id={lengthId}>{length}</div>
        <i id={decrementId} className="fa fa-arrow-down" onClick={onDecrement}></i>
      </div>
    </div>
  );
};

const PomodoroDisplay = (props) => {
  const { time, timerLabel } = props;
  
  const getFormattedTime = () => {
    const minutes = Math.floor(time/60);
    const seconds = time - minutes * 60;
    
    const formattedMinutes = minutes / 10 >= 1 ? minutes : `0${minutes}`;
    const formattedSeconds = seconds / 10 >= 1 ? seconds : `0${seconds}`;
    
    return `${formattedMinutes}:${formattedSeconds}`;
  } 
  
  return (
    <div className='pomodoro-display'>
      <h3 id='timer-label'>
        {timerLabel}
      </h3>
      <div id='time-left'>
        {getFormattedTime()}
      </div>
    </div>
  );
}

ReactDOM.render(<PomodoroClock />, document.getElementById('root'));

              
            
!
999px

Console