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

              
                <script src="https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js"></script>

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

CSS

              
                body {
  background-image: linear-gradient(to bottom right, #7A3339, #FF99CC);
  color: #F9FFEF;
  font-family: 'Dosis', sans-serif;
  height: 100vh;
}

button {
  color: white;
  font-size: 36px;
  background-color: transparent;
  border: none;
}

button:hover {
  cursor: pointer;
}

i:hover {
  cursor: pointer;
}

#settings {
  border: 3px solid #E384AD;
  padding: 15px;
  float: left;
  margin: 30px;
  text-align: center;
}

.label {
  font-size: 24px;  
}

.length {
  font-size: 30px;
  font-weight: bold;
}

#timer {
  width: 400px;
  margin: auto;
  text-align: center;
}

#time-left {
  font-size: 128px;
  font-family: 'Space Mono', monospace;
}

#controls {
  width: 120px;
  margin: auto;
}

#controls i {
  font-size: 36px;
  margin: 5px;
}





              
            
!

JS

              
                class Timer extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      sessionTime: 0,
      breakTime: 0,
      breakLength: 5,
      sessionLength: 25,
      running: false,
      session: true,
      initialState: true,
      
    }
    this.baseState = this.state;
  }
  
  componentWillUnmount() {
    clearInterval(this.timerID);
  }
  
formatTime = (ms) => {
  if(ms < 0) 
    return "invalid time";
  
  else {
    let minutes =  Math.floor(ms/60000);
    let seconds = Math.floor((ms % 60000)/1000);

    return `${minutes < 10 ? "0" : ""}${minutes}:${seconds < 10 ? "0" : ""}${seconds}`;
  }
}
  
  runTimer = () => {
    const {session, breakLength, sessionLength} = this.state;
    
    const intervalTime = session ? "sessionTime" : "breakTime";

    
    this.setState(state => ({[intervalTime]: state[intervalTime] - 1000}));
    
    if(this.state[intervalTime] === 0) {
      const alarm = document.getElementById("beep");
      alarm.play();
    }
    
    if(this.state[intervalTime] < 0) {
        this.setState(state => ({
          session: !session,
          // [intervalTime]: state[intervalLength] * 60000
          breakTime: breakLength * 60000,
          sessionTime: sessionLength * 60000
        }));}
  }
  
  handleStopStart = () => {
    const {running, initialState, sessionLength} = this.state;

    if(initialState) this.setState(state => ({
      sessionTime: sessionLength * 60000,
      initialState: false,
    }))
    
    if(!running) {
      this.setState( state => ({
        running: true
      }));
      this.timerID = setInterval( () => this.runTimer(), 1000);

    }
   else if(running) {
      clearInterval(this.timerID);
      this.setState({running: false});
    }

  }
  
  handleIncrement = (controlledTime) => {
    const {initialState} = this.state;
    
    if(this.state[controlledTime] < 60 && initialState)  //so that I can't change the time again until it's reset
        this.setState(state => ({[controlledTime]: state[controlledTime] + 1}));
    }
  
  handleDecrement = (controlledTime) => {
    const {initialState} = this.state;
    
    if(this.state[controlledTime] > 1 && initialState)
      this.setState(state => ({[controlledTime]: state[controlledTime] - 1}));
    }
  
  handleReset = () => {
    const alarm = document.getElementById("beep");
    alarm.pause();
    alarm.currentTime = 0;
    
    clearInterval(this.timerID);
    this.setState(this.baseState);
    
  }

  render() {
    const {sessionLength, 
           breakLength, 
           sessionTime, 
           breakTime,
           running, 
           session, 
           initialState
          } = this.state;


    let currentTime;
    
        if(initialState)
          currentTime = this.formatTime(sessionLength * 60000);

        else
          currentTime = this.formatTime(session? sessionTime : breakTime);
    
    const timerLabel = session? "Session" : "Break";
    
    const playStopClassName = running? "fas fa-stop-circle" : "fas fa-play-circle";
    
    const alarmSoundSrc = "https://raw.githubusercontent.com/jessicadavey/codepen-assets/master/timerAlarm.wav";
    
    return (
    <div id="app">
        <h1>Pomodoro Clock</h1>
        <div id="settings">
            <div id="break-label" className="label">Break Length</div>
            <div id="break-length" className="length">{breakLength}</div>
            <button 
              id="break-decrement" 
              onClick={() => this.handleDecrement("breakLength")}
              >
              -</button>
            <button 
              id="break-increment" 
              onClick={() => this.handleIncrement("breakLength")}
              >
              +</button>
            <div id="session-label" className="label">Session Length</div>
            <div id="session-length" className="length">{sessionLength}</div>
            <button 
              id="session-decrement" 
              onClick={() => this.handleDecrement("sessionLength")}
              >
              -</button>
             <button 
              id="session-increment" 
              onClick={() => this.handleIncrement("sessionLength")}
              >
              +</button>
        </div>
        <div id="timer">
            <div id="timer-label">{timerLabel}</div>
            <div id="time-left">{currentTime}</div>
            <div id="controls">
                <i 
                  id="start_stop" 
                  onClick={this.handleStopStart}
                  className={playStopClassName}
                  >
                </i>
                <i
                  id="reset"
                  onClick={this.handleReset}
                  className="fas fa-backward"
                  ></i>
            </div>
        </div>
        <audio 
          id="beep"
          src={alarmSoundSrc} />
        
    </div>
    )
  }
  
}

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

Console