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

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

CSS

              
                @import url('https://fonts.googleapis.com/css2?family=Source+Sans+Pro&display=swap');
@import url('https://fonts.googleapis.com/css2?family=Playfair+Display&display=swap');

$background: #00001a;
$mainOutline: #d1d1e0; 
$mainLight: #e6e6ff;
$mainHover: #3333ff;

@mixin mainFlex {
  display: flex;
  justify-content: center;
  align-items: center;
}

@mixin btnStyle {
  @include mainFlex;
  background: none;
  border: 1px solid $mainOutline;
  border-radius: 3px;
  font-family: 'Source Sans Pro', sans-serif;
  font-size: 0.8em;
  color: $mainLight;
  padding: 8px 12px 8px 12px;
  margin: 3px;
  cursor: pointer;
  outline: none;
  &:hover {
    background: $mainHover;
  }
}

* {
  margin: 0;
  padding: 0;
}

body {
  @include mainFlex;
  height: 100vh;
  background: $background;
  font-family: 'Source Sans Pro', sans-serif;
  font-size: 1.3em;
  color: $mainLight;
  text-align: center;
}

#pomodoro {
  @include mainFlex;
  flex-direction: column;
  width: 802px;
  height: 500px;
  border: 1px dotted $mainOutline;
  button {
    @include btnStyle;
  }
  #title {
    @include mainFlex;
    width: 800px;
    height: 100px;
    border-bottom: 1px dotted $mainOutline;
    font-family: 'Playfair Display', serif;
    a {
      color: $mainLight;
      text-decoration: none;
      &:hover {
        color: $mainHover;
      }
    }
  }
  #content {
    @include mainFlex;
    #break-group,
    #session-group {
      @include mainFlex;
      flex-direction: column;
      justify-content: space-evenly;
      width: 400px;
      height: 200px;
      .label {
        padding: 0 0 15px 0;
      }
      .time-control {
        @include mainFlex;
        .display {
          padding: 0 15px 0 15px;
        }
      }
    }
    #session-group {
      border-top: 1px dotted $mainOutline;
    }
    #right-display {
      width: 400px;
      height: 400px;
      border-left: 1px dotted $mainOutline;
      #timer-group {
        @include mainFlex;
        flex-direction: column;
        justify-content: space-evenly;
        width: 400px;
        height: 300px;
        .control-wrap {
          @include mainFlex;
        }
      }
      #interval-count {
        @include mainFlex;
        width: 400px;
        height: 100px;
        border-top: 1px dotted $mainOutline;
      }
    }
  }
}

@media only screen and (max-width: 850px) {
  #pomodoro {
    width: 602px;
    height: 500px;
    #title {
      width: 600px;
      height: 100px;
    }
    #content {
      #break-group,
      #session-group {
        width: 300px;
        height: 200px;
      }
      #right-display {
        width: 300px;
        height: 400px;
        #timer-group {
          width: 300px;
          height: 300px;
        }
        #interval-count {
          width: 300px;
          height: 100px;
        }
      }
    }
  }
}

@media only screen and (max-width: 650px) {
  #pomodoro {
    width: 350px;
    height: 90%;
    #title {
      width: 350px;
      height: 100px;
    }
    #content {
      flex-direction: column-reverse;
      #break-group,
      #session-group {
        width: 350px;
        height: 150px;
        border-top: 1px dotted $mainOutline;
      }
      #right-display {
        width: 350px;
        height: 300px;
        border-left: none;
        #timer-group {
          width: 350px;
          height: 200px;
        }
        #interval-count {
          width: 350px;
          height: 100px;
        }
      }
    }
  }
  #footnote {
    display: none;
  }
}
              
            
!

JS

              
                 const accurateInterval = (fn, time) => {
  let cancel, nextAt, timeout, wrapper;
  nextAt = new Date().getTime() + time;
  timeout = null;
  wrapper = () => {
    nextAt += time;
    timeout = setTimeout(wrapper, nextAt - new Date().getTime());
    return fn();
  }
  cancel = () => {
    return clearTimeout(timeout);
  }
  timeout = setTimeout(wrapper, nextAt - new Date().getTime());
  return {
    cancel: cancel
  }
}

class TimeController extends React.Component {
  render() {
    return (
      <div>
        <h4 
          id={this.props.titleID}
          className="label"
        >{this.props.title}</h4>
        
        <div className="time-control">
          <button
            id={this.props.minusID}
            value="−"
            onClick={this.props.onClick}
          >−</button>

          <p 
            id={this.props.lengthID}
            className="display"
          >{this.props.length}</p>

          <button
            id={this.props.plusID}
            value="+"
            onClick={this.props.onClick}
          >+</button>
        </div>
      </div>  
    );
  }
}

class PomodoroTimer extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      breakLength: 5, // min
      sessionLength: 25, // min
      timerState: 'off',
      timerType: 'Session',
      timer: 1500, // # of sec in 25 min
      intervalID: '',
      intervalCount: 0,
      tomatoes: ''
    };
    this.setBreakLength = this.setBreakLength.bind(this);
    this.setSessionLength = this.setSessionLength.bind(this);
    this.lengthControl = this.lengthControl.bind(this);
    this.timerControl = this.timerControl.bind(this);
    this.startCountDown = this.startCountDown.bind(this);
    this.decrementTimer = this.decrementTimer.bind(this);
    this.phaseControl = this.phaseControl.bind(this);
    this.switchTimer = this.switchTimer.bind(this);
    this.addIntervalCount = this.addIntervalCount.bind(this);
    this.beep = this.beep.bind(this);
    this.formatTime = this.formatTime.bind(this);
    this.reset = this.reset.bind(this);
  }
  
  setBreakLength(e) {
    this.lengthControl(
      'breakLength',
      e.currentTarget.value,
      this.state.breakLength,
      'Session'
    );
  }
  
  setSessionLength(e) {
    this.lengthControl(
      'sessionLength',
      e.currentTarget.value,
      this.state.sessionLength,
      'Break'
    );
  }
  
  lengthControl(stateToChange, operator, currentLength, timerType) {
    if (this.state.timerState === 'on') {
      return false;
    }
    if (this.state.timerType === timerType) {
      if (operator === '−' && currentLength !== 1) {
        this.setState({ [stateToChange]: currentLength - 1 }); 
      } else if (operator === '+' && currentLength !== 60) {
        this.setState({ [stateToChange]: currentLength + 1 });
      }
    } else if (operator === '−' && currentLength !== 1) {
      this.setState({ 
        [stateToChange]: currentLength - 1,
        timer: currentLength * 60 - 60 
      });
    } else if (operator === '+' && currentLength !== 60) {
      this.setState({
        [stateToChange]: currentLength + 1,
        timer: currentLength * 60 + 60
      });
    }
  }
  
  timerControl() {
    if (this.state.timerState === 'off') {
      this.startCountDown();
      this.setState({ timerState: 'on' });
    } else {
      this.setState({ timerState: 'off' });
      if (this.state.intervalID) {
        this.state.intervalID.cancel();
      }
    }
  }
  
  startCountDown() {
    this.setState({
      intervalID: accurateInterval(() => {
        this.decrementTimer();
        this.phaseControl();
      }, 1000)
    });
  }
  
  decrementTimer() {
    this.setState({ timer: this.state.timer - 1 });
  }
  
  phaseControl() {
    let timer = this.state.timer;
    this.beep(timer);
    if (timer < 0) {
      if (this.state.intervalID) {
        this.state.intervalID.cancel();
      }
      if (this.state.intervalCount > 4) {
        this.setState({
          tomatoes: ''
        });
      }
      if (this.state.timerType === 'Session') {
        this.addIntervalCount();
        this.startCountDown();
        this.switchTimer(this.state.breakLength * 60, 'Break');
      } else {
        this.addIntervalCount();
        this.startCountDown();
        this.switchTimer(this.state.sessionLength * 60, 'Session');
      }
    }
  }
  
  switchTimer(num, str) {
    this.setState({
      timer: num, 
      timerType: str,
    });
  }
  
  addIntervalCount() {
    if (this.state.sessionCount === this.state.breakCount) {
      this.setState({
        intervalCount: this.state.intervalCount + 0.5
      });
    } 
    if (Number.isInteger(this.state.intervalCount)) {
      this.setState({
        tomatoes: this.state.tomatoes + '🍅'
      });
    }
  }
  
  beep(_timer) {
    if (_timer === 0) {
      this.audioBeep.play();
    }
  }
  
  formatTime() {
    let minutes = Math.floor(this.state.timer / 60);
    let seconds = this.state.timer - minutes * 60;
    seconds = seconds < 10 ? `0${seconds}` : seconds;
    minutes = minutes < 10 ? `0${minutes}` : minutes;
    return `${minutes}:${seconds}`;
  }
  
  reset() {
    this.setState({
      breakLength: 5, 
      sessionLength: 25, 
      timerState: 'off',
      timerType: 'Session',
      timer: 1500, 
      intervalID: '',
      intervalCount: 0,
      tomatoes: ''
    });
    if (this.state.intervalID) {
      this.state.intervalID.cancel();
    } 
    this.audioBeep.pause();
    this.audioBeep.currentTime = 0;
  }
  
  render() {
    
    return (
      <div id="pomodoro">
        <div id="title">
          <h1>
            <a 
              href="https://en.wikipedia.org/wiki/Pomodoro_Technique"
              target="_blank"
              >Pomodoro Timer
            </a>
          </h1>
        </div>

        <div id="content">
          <div id="settings">
            <div id="break-group">
              <TimeController
                title="Break Length"
                titleID="break-label"
                minusID="break-decrement"
                plusID="break-increment"
                length={this.state.breakLength}
                lengthID="break-length"
                onClick={this.setBreakLength}
              />
            </div>
            <div id="session-group">
              <TimeController
                title="Session Length"
                titleID="session-label"
                minusID="session-decrement"
                plusID="session-increment"
                length={this.state.sessionLength}
                lengthID="session-length"
                onClick={this.setSessionLength}
              />
            </div>
          </div>

          <div id="right-display">
            <div id="timer-group">
              <h4 id="timer-label">{this.state.timerType}</h4> 
              <h1 id="time-left">{this.formatTime()}</h1>
              <div className="time-control control-wrap">
                <button 
                  id="start_stop" 
                  onClick={this.timerControl}
                  >Start/Pause
                </button>

                <button 
                  id="reset" 
                  onClick={this.reset}
                  >Reset
                </button>
              </div>
            </div>
            <div id="interval-count">
              <h4>Interval Count: <span id="num">{this.state.tomatoes}</span></h4>
            </div>
          </div>
        </div>

        <audio 
          id="beep"
          preload="auto"
          ref={(audio) => {
            this.audioBeep = audio;
          }}
          src="https://actions.google.com/sounds/v1/cartoon/cowbell_ringing.ogg"  
        />
      </div>
    );
  }
}

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

Console