css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              .container {
  padding: 30px;
}

.row {
  text-align: center;
  padding: 10px;
}

.pomodoroclock {
  border-radius: 50%;
  height: 200px;
  width: 200px;
  font-size: 48px;
}

.focusing {
  color: rgb(217, 83, 79)
}
.break {
  color: rgb(92, 184, 92)
}

.controls {
  height: 30px;
  width: 40px;
  margin-left: 6px;
  margin-right: 6px;
}

.start-stop-control {
  height: 40px;
}

.counter {
  margin: 2px;
  display: inline-block;
  vertical-align: top;
  border-radius: 50%;
  border: 8px solid;
  height: 14px;
  width: 14px;
}

.counter-line {
  height: 14px;
}

.btn:focus,.btn:active {
   outline: none !important;
}
            
          
!
            
              //import React from 'react';
//import ReactDOM from 'react-dom';
const INTERVAL = {
  FOCUSING: 0,
  BREAK: 1
}

const STATUS = {
  RUNNING: 0,
  STOP: 1
}

const INITIAL_STATE = {
  progress: 0,
  defaultFocusing: 25,
  defaultBreak: 5,
  minutes: 25,
  status: STATUS.STOP,
  interval: INTERVAL.FOCUSING,  
  intervalId: null
};

const PomodoroProgress = (props) => {
  let { progress } = props ;
  var progressElements = [];
  while(progress--) {
    progressElements.push(<div className="counter focusing"></div>);
  }
  return (
    <div className="counter-line">{progressElements}</div>
  );
}

const PomodoroClock = (props) => {
  
  const  { 
    minutes, 
    interval,
    onStartFocusing,
    onStartBreak,
    status
  } = props;
  
  const className = ((interval === INTERVAL.FOCUSING) ? "btn-danger" : "btn-success") + " btn pomodoroclock";
  
  let onClick = onStartFocusing;

  if (status === STATUS.RUNNING) {
    onClick = null;
  } else if (interval === INTERVAL.BREAK) {
    onClick = onStartBreak;
  }
  
  return (
    <div>
      <button onClick={onClick} type="button" className={className}>{minutes}</button>
    </div>
  );

}

const PomodoroSettings = (props) => {

  const  { status, onDefaultsUp, onDefaultsDown } = props;
  const className = ((status !== STATUS.STOP) ? "invisible" : "") + " btn btn-warning controls";
  return (
    <div>
      <button 
        onClick={onDefaultsDown}
        type="button" className={className}><span className="glyphicon glyphicon-chevron-down"></span></button>
      <button 
        onClick={onDefaultsUp}
        type="button" className={className}><span className="glyphicon glyphicon-chevron-up"></span></button>        
    </div>
  )
}

const PomodoroControls = (props) => {
  const  { 
    status, 
    interval, 
    onResetProgress,
    onStartFocusing,
    onStartBreak,
    onStopInterval
  } = props;
  
  // Start Focusing by default
  let intervalClass = " btn-danger";
  let label = "Start Focusing";
  let onClick = onStartFocusing;

  if (status === STATUS.RUNNING) {
    intervalClass = " btn-warning";
    label = "Stop";
    onClick = onStopInterval;
  } else if (interval === INTERVAL.BREAK) {
    intervalClass = " btn-success";
    label = "Start Break";
    onClick = onStartBreak;
  }
  
  const className = "btn start-stop-control" + intervalClass;
  const classNameCaret = "btn start-stop-control dropdown-toggle" + intervalClass;

  return (
    <div>
      <div className="btn-group dropup">
        <button type="button" onClick={onClick} className={className}>{label}</button>
        <button type="button" className={classNameCaret} data-toggle="dropdown">              <span className="caret"></span>
        </button>
        <ul className="dropdown-menu" role="menu">
          <li><a href="#" onClick={onStartFocusing}>Start Focusing</a></li>
          <li><a href="#" onClick={onStartBreak}>Start Break</a></li>
          <li role="separator" className="divider"></li>
          <li><a href="#" onClick={onResetProgress}>Reset</a></li>
          <li><a href="#" onClick={onStopInterval}>Stop</a></li>
        </ul>
      </div>        
    </div>
  );
}

class PomodoroApp extends React.Component {
  constructor(props) {
    super(props);
    this.state = INITIAL_STATE;
  }
  
  defaultsChange(delta) {
    const  { 
      minutes, 
      status, 
      interval, 
      defaultBreak, 
      defaultFocusing 
    } = this.state;
    
    // delta can be +1 or -1 so that it increases or decreases
    // the values (set by caller defaultsUp and defaultsDown)
    if (status === STATUS.STOP) {
      if (interval === INTERVAL.FOCUSING) {
        if (defaultFocusing+delta > 0) {
          this.setState({
            defaultFocusing: defaultFocusing+delta,
            minutes: defaultFocusing+delta
          });
        }
      } if (interval === INTERVAL.BREAK) {
        if (defaultBreak+delta > 0) {
          this.setState({
            defaultBreak: defaultBreak+delta,
            minutes: defaultBreak+delta          
          });        
        }
      }
    }
  }
  
  defaultsUp() {
    this.defaultsChange(+1);
  }
  
  defaultsDown() {
    this.defaultsChange(-1);
  }
  
  completeInterval() {
    const  { 
      progress,
      defaultFocusing,
      defaultBreak,
      minutes, 
      status,
      interval,
      intervalId
    } = this.state;

    clearInterval(intervalId); // stop interval
    
    // switch to the next interval (focusing / break)
    const nextInterval = (interval === INTERVAL.FOCUSING) ? INTERVAL.BREAK : INTERVAL.FOCUSING;
    const nextProgress = (interval === INTERVAL.FOCUSING) ? progress + 1 : progress;
    this.setState({
      progress: nextProgress,
      minutes: (nextInterval === INTERVAL.FOCUSING)? defaultFocusing: defaultBreak,
      status: STATUS.STOP,
      interval: nextInterval,  
      intervalId: null
    });
    
    if (interval === INTERVAL.FOCUSING) {
        this.refs.audioEndFocusing.play();
    } else {
        this.refs.audioEndBreak.play();
    }
  }
  
  intervalRun() {
    const  { minutes } = this.state;
    if (minutes-1 === 0) {
      this.completeInterval();
    } else {
      this.setState({minutes: minutes-1});
    }
  }

  startFocusing() {
    const  { defaultFocusing } = this.state;
    this.startInterval(defaultFocusing, INTERVAL.FOCUSING);
  }
  
  startBreak() {
    const  { defaultBreak } = this.state;
    this.startInterval(defaultBreak, INTERVAL.BREAK);
  }
  
  startInterval(totalMinutes, intervalToStart) {
    const  { intervalId } = this.state;

    // clear eventually bug-like previous timer ID
    if (intervalId) {
      clearInterval(intervalId);
    }

    this.setState({
      minutes: totalMinutes,
      interval: intervalToStart,
      status: STATUS.RUNNING
    });
    
    const newIntervalId = setInterval(() => this.intervalRun(), 60000);
    this.setState({
      intervalId: newIntervalId
    });    
    
  }
  
  resetProgress() {

    //TODO: ask confirmation to reset!
    clearInterval(this.state.intervalId);
    this.setState(INITIAL_STATE);
  }

  stopInterval() {
    const  { 
      defaultFocusing,
      defaultBreak,
      interval,
      intervalId
    } = this.state;

    clearInterval(intervalId); // stop interval
    
    this.setState({
      minutes: (interval === INTERVAL.FOCUSING)? defaultFocusing: defaultBreak,
      status: STATUS.STOP,
      intervalId: null
    });
  }
  
  render() {
    const  { progress, minutes, status, interval } = this.state;
    return (
      <div>
        
<div className="container">

  <div className="row">
    <div className="col-sm-4">
    </div>
    <div className="col-sm-4">
      <div className="row">
        <PomodoroProgress progress={progress} />
      </div>      
      <div className="row">
        <PomodoroClock
            status={status}
            minutes={minutes}
            interval={interval}
            onStartBreak={() => this.startBreak()}
            onStartFocusing={() => this.startFocusing()}          
          />
      </div>
      <div className="row">
        <PomodoroSettings 
          status={status}
          onDefaultsUp={() => this.defaultsUp()}
          onDefaultsDown={() => this.defaultsDown()}
          />
      </div>        
      <div className="row">
        <PomodoroControls 
          status={status}
          interval={interval} 
          onStartFocusing={() => this.startFocusing()}
          onResetProgress={() => this.resetProgress()}
          onStartBreak={() => this.startBreak()}
          onStopInterval={() => this.stopInterval()}
          />
      </div>        
        
      <div className="row">
        <h5>React Pomodoro Clock</h5>
        <audio ref="audioEndFocusing" preload="auto" src="http://archive.soundarchive.online/2-1-10044.mp3?_=1" type="audio/mpeg"></audio>        
        <audio ref="audioEndBreak" preload="auto" src="http://archive.soundarchive.online/3-5-10001.mp3?_=9" type="audio/mpeg"></audio>        
      </div>
      
    </div>
    <div className="col-sm-4">
    </div>
  </div>
  
</div>
        

      </div>
    )
  }
}

ReactDOM.render(
    <PomodoroApp/>, 
    document.getElementById('app')
);

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console