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 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

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

CSS

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

.Pomodoro{
  border:2px solid black;
  display:flex;
  flex-flow:column;
  justify-content:center;
  align-items:center;
  padding:1em;
}

button{
  cursor:pointer;
  background:white;
  border:1px solid black;
  border-radius:3px;
  margin:1em;
  padding:1em;
}

.controls{
  display:flex;
}

.break_control,.session_control{
  display:flex;
  flex-flow:column;
  justify-content:center;
  align-items:center;
  padding:0.5em;
}

.timer_control{
  margin:auto;
  text-align:center;
}
              
            
!

JS

              
                class Pomodoro extends React.Component{
  constructor(props) {
    super(props);
    this.state={
      breakLength:5,
      sessionLength:25,
      timeLabel:"Session",
      secondsLeft:25*60,
      currentTime:this.formatDisplay(25*60),
      toRun:true
    }
    this.formatDisplay=this.formatDisplay.bind(this);
    this.updateLength=this.updateLength.bind(this);
    this.increaseBreakLength=this.increaseBreakLength.bind(this);
    this.decreaseBreakLength=this.decreaseBreakLength.bind(this);
    this.increaseSessionLength=this.increaseSessionLength.bind(this);
    this.decreaseSessionLength=this.decreaseSessionLength.bind(this);
    this.triggerTimer=this.triggerTimer.bind(this);
    this.switchTimer=this.switchTimer.bind(this);
    this.toggleClock=this.toggleClock.bind(this);
    this.resetTimer=this.resetTimer.bind(this);
  }

  formatDisplay(secondsRemain){
    const minutes = Math.floor(secondsRemain / 60);
    const seconds = secondsRemain % 60; 
    return `${(minutes<10)? "0":""}${minutes}:${(seconds<10)? "0":""}${seconds}`;
  }

  updateLength(newSecondsLeft){ 
    this.setState({
      secondsLeft:newSecondsLeft,
      currentTime: this.formatDisplay(newSecondsLeft)
    }); 
  }

  increaseBreakLength(){
    //if we reach the limit or the timer is running, do nothing
    if(this.state.breakLength>=60 || !this.state.toRun){
      return;
    }
    //if not, increase the breaklength
    let newLength = this.state.breakLength+1;
    this.setState({
      breakLength: newLength
    });
    //and update the time if it's now a break
    if(this.state.timeLabel=="Break"){
      this.updateLength(newLength*60);  
    }
  }

  decreaseBreakLength(){
    //if we reach the limit or the timer is running, do nothing
    if(this.state.breakLength<=1 || !this.state.toRun){
      return;
    }
    //if not, decrease the breaklength
    let newLength = this.state.breakLength-1;
    this.setState({
      breakLength: newLength
    });
    //and update the time if it's now a break
    if(this.state.timeLabel=="Break"){
      this.updateLength(newLength*60);  
    }
  }

  increaseSessionLength(){
    //if we reach the limit or the timer is running, do nothing
    if(this.state.sessionLength>=60 || !this.state.toRun){
      return;
    }
    //if not, increase the breaklength
    let newLength = this.state.sessionLength+1;
    this.setState({
      sessionLength: newLength
    });
    //and update the time if it's now a session
    if(this.state.timeLabel=="Session"){
      this.updateLength(newLength*60);  
    }
  }

  decreaseSessionLength(){
    //if we reach the limit or the timer is running, do nothing
    if(this.state.sessionLength<=1 || !this.state.toRun){
      return;
    }
    //if not, decrease the breaklength
    let newLength = this.state.sessionLength-1;
    this.setState({
      sessionLength: newLength
    });
    //and update the time if it's now a session
    if(this.state.timeLabel=="Session"){   
      this.updateLength(newLength*60);  
    }
  }

  switchTimer(){
    if(this.state.secondsLeft==0){
      this.audioBeep.play();
      if(this.state.timeLabel == "Session"){
        this.setState({
          timeLabel:"Break",
          secondsLeft:this.state.breakLength*60+1,
          currentTime:this.formatDisplay(this.state.breakLength*60+1)
        });
      }else{
        this.setState({
          timeLabel:"Session",
          secondsLeft:this.state.sessionLength*60+1,
          currentTime:this.formatDisplay(this.state.sessionLength*60+1)
        });
      }
    } 
  }

  triggerTimer(){    
    //if the timer isn't running, start the timer
    if(this.state.toRun){
      this.interval=setInterval(()=>{
        //switch when one ends
        this.switchTimer();
        //tick
        const minutes = Math.floor(this.state.secondsLeft / 60);
        const seconds = this.state.secondsLeft % 60; 
        this.setState(
          state => ({
            secondsLeft: state.secondsLeft - 1,
            currentTime:this.formatDisplay(state.secondsLeft - 1)
          })
        );
      },1000); 
    }
  }

  toggleClock(){
    //clear existing timer
    clearInterval(this.interval);

    //flip the flags
    this.setState({
      toRun:!this.state.toRun
    });

    //start the timer(or not)
    this.triggerTimer();
  }

  resetTimer(){
    clearInterval(this.interval);
    this.setState({
      breakLength:5,
      sessionLength:25,
      timeLabel:"Session",
      secondsLeft:25*60,
      currentTime:this.formatDisplay(25*60),
      toRun:true
    });
    this.audioBeep.pause();
    this.audioBeep.currentTime = 0;
  }

  render(){
    return(
      <div className="Pomodoro">
        <div className="controls">
          <div className="break_control">
            <div id="break-label">Break Length</div>
            <div id="break-length">{this.state.breakLength}</div>
            <button id="break-increment" onClick={this.increaseBreakLength}>+ 1 minute</button>
            <button id="break-decrement" onClick={this.decreaseBreakLength}>- 1 minute</button>
          </div>
          <div className="session_control">
            <div id="session-label">Session Length</div>
            <div id="session-length">{this.state.sessionLength}</div>
            <button id="session-increment" onClick={this.increaseSessionLength}>+ 1 minute</button>
            <button id="session-decrement" onClick={this.decreaseSessionLength}>- 1 minute</button>
          </div>
        </div>
        <div className="timer_control">
          <div id="timer-label">{this.state.timeLabel}</div>
          <div id="time-left">{this.state.currentTime}</div>
          <button id="start_stop" onClick={this.toggleClock}>Start/Stop</button>
          <button id="reset" onClick={this.resetTimer}>Reset</button>
          <audio id="beep" preload="auto" 
            src="https://goo.gl/65cBl1"
            ref={(audio) => { this.audioBeep = audio; }} />
        </div>
      </div>
    );}
}

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

Console