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="appContainer"></div>
              
            
!

CSS

              
                body {
  background-color: #283593;
  color: black;
}

#appContainer {
  margin: 10vh auto;
  display: flex;
  width: 50vw;
}

#container {
  display: grid;
  grid-template-areas:
    "break session"
    "timer timer";
  border: 10px solid black;
  border-radius: 20%;
  background-color: #C62828;
  width: 100%;
}

h1 {
  font-size: 60px;
}

.btn {
  margin: 0.5em;
  background-color: #C62828;
}

.change {
  display: flex;
  flex-direction: row;
  align-items: center;
}

#break {
  display: flex;
  flex-direction: column;
  align-items: center;
  grid-area: break;
  margin: 5% auto;
}

#session {
  display: flex;
  flex-direction: column;
  align-items: center;
  grid-area: session;
  margin: auto;
}

#timer {
  display: flex;
  flex-direction: column;
  align-items: center;
  grid-area: timer;
  margin: auto;
}
              
            
!

JS

              
                class ActButton extends React.Component {
  constructor(props) {
    super(props);
    this.handleAction = this.handleAction.bind(this);
  }

  handleAction() {
    const MAX_LENGTH = 60;
    const MIN_LENGTH = 1;
    switch (this.props.action) {
      case "increment":
        if (this.props.parentState.isPaused) {
          switch (this.props.id) {
            case "session-increment":
              if (this.props.parentState.sessionLength < MAX_LENGTH) {
                let label = Number(this.props.parentState.sessionLength) + 1;
                let newLength = this.props.parentState.sessionLength + 1;
                newLength = newLength >= 10 ? newLength : "0" + newLength;
                this.props.handler({
                  type: this.props.parentState.type,
                  len: newLength,
                  sessionLength: label,
                  breakLength: this.props.parentState.breakLength,
                  isPaused: this.props.parentState.isPaused
                });
                resetClock({
                  minutes: newLength,
                  seconds: "00"
                });
              }
              break;
            case "break-increment":
              if (this.props.parentState.breakLength < MAX_LENGTH) {
                let newLength = Number(this.props.parentState.breakLength) + 1;
                this.props.handler({
                  type: this.props.parentState.type,
                  len: this.props.parentState.len,
                  sessionLength: this.props.parentState.sessionLength,
                  breakLength: newLength,
                  isPaused: this.props.parentState.isPaused
                });
              }
              break;
          }
        }
        break;
      case "decrement":
        if (this.props.parentState.isPaused) {
          switch (this.props.id) {
            case "session-decrement":
              if (this.props.parentState.sessionLength > MIN_LENGTH) {
                let label = Number(this.props.parentState.sessionLength) - 1;
                let newLength =
                  Number(this.props.parentState.sessionLength) - 1;
                newLength = newLength >= 10 ? newLength : "0" + newLength;
                this.props.handler({
                  type: this.props.parentState.type,
                  len: newLength,
                  sessionLength: label,
                  breakLength: this.props.parentState.breakLength,
                  isPaused: this.props.parentState.isPaused
                });
                resetClock({
                  minutes: newLength,
                  seconds: "00"
                });
              }
              break;
            case "break-decrement":
              if (this.props.parentState.breakLength > MIN_LENGTH) {
                let newLength = Number(this.props.parentState.breakLength) - 1;
                this.props.handler({
                  type: this.props.parentState.type,
                  len: this.props.parentState.len,
                  sessionLength: this.props.parentState.sessionLength,
                  breakLength: newLength,
                  isPaused: this.props.parentState.isPaused
                });
              }
              break;
          }
        }
        break;
      case "reset":
        this.props.handler({
          type: "Session",
          len: "25",
          sessionLength: "25",
          breakLength: "5",
          isPaused: true
        });
        resetClock({
          minutes: "25",
          seconds: "00"
        });
        toggleClock();
        resetSound();
        break;
      case "toggle":
        this.props.handler({
          type: this.props.parentState.type,
          sessionLength: this.props.parentState.sessionLength,
          breakLength: this.props.parentState.breakLength,
          isPaused: !this.props.parentState.isPaused
        });
        toggleClock();
        break;
      default:
        console.log("error");
    }
  }

  render() {
    return (
      <button id={this.props.id} className="btn" onClick={this.handleAction}>
        <i className={this.props.icon} />
      </button>
    );
  }
}
let timerID = null;
function toggleClock() {
  if (!this.props.parentState.isPaused === false) {
    clearInterval(timerID);
    timerID = setInterval(() => this.getTime(), 1000);
  } else {
    clearInterval(timerID);
  }
}

function resetClock(newState) {
  this.setState(newState);
}

function resetSound() {
  this.refs.beepSound.pause();
  this.refs.beepSound.currentTime = 0;
}

class Clock extends React.Component {
  constructor(props) {
    super(props);
    this.getTime = this.getTime.bind(this);
    this.playBeep = this.playBeep.bind(this);
    resetClock = resetClock.bind(this);
    resetSound = resetSound.bind(this);
    toggleClock = toggleClock.bind(this);
    this.state = {
      minutes: this.props.minutes,
      seconds: "00"
    };
  }
  /*
  componentDidMount() {
    this.timerID = setInterval(() => this.getTime(), 1000);
  }
*/
  playBeep() {
    this.refs.beepSound.play();
  }

  getTime() {
    if (!this.props.isPaused) {
      if (this.state.seconds === "00" && this.state.minutes != "00") {
        let newMin = Number(this.state.minutes) - 1;
        newMin = newMin >= 10 ? newMin : "0" + newMin;
        this.setState({
          minutes: newMin,
          seconds: "59"
        });
      } else {
        let newSec = Number(this.state.seconds) - 1;
        newSec = newSec >= 10 ? newSec : "0" + newSec;
        this.setState({
          minutes: this.state.minutes,
          seconds: newSec
        });
      }
      if (this.state.minutes === "00" && this.state.seconds === "01") {
        this.playBeep();
        const nextTimerType =
          this.props.parentState.type === "Break" ? "Session" : "Break";
        let nextTimerLength =
          this.props.parentState.type === "Break"
            ? this.props.parentState.sessionLength
            : this.props.parentState.breakLength;
        nextTimerLength =
          nextTimerLength >= 10 ? nextTimerLength : "0" + nextTimerLength;
        setTimeout(() => {
          this.props.handler({
            type: nextTimerType,
            len: nextTimerLength,
            sessionLength: this.props.parentState.sessionLength,
            breakLength: this.props.parentState.breakLength,
            isPaused: false
          });
          this.setState({
            minutes: nextTimerLength,
            seconds: "00"
          });
        }, 1000);
      }
    }
  }

  render() {
    //console.log(this.props.parentState.type, this.state.minutes + ':' + this.state.seconds);
    //https://onlineclock.net/audio/options/default.mp3
    const time = this.state.minutes + ":" + this.state.seconds;
    return (
      <div>
        <h1 id={this.props.id}>{time}</h1>
        <audio id="beep" ref="beepSound" src="https://goo.gl/65cBl1">
          <source src="https://goo.gl/65cBl1" type="audio/wav" />
        </audio>
      </div>
    );
  }
}

class Pomodoro extends React.Component {
  constructor(props) {
    super(props);
    this.handler = this.handler.bind(this);
    this.state = {
      type: "Session",
      len: "25",
      sessionLength: "25",
      breakLength: "5",
      isPaused: true
    };
  }

  handler(newState) {
    this.setState(newState);
  }

  render() {
    return (
      <div id="container">
        <div id="break">
          <h3 id="break-label">Break Length</h3>
          <div className="change">
            <ActButton
              id="break-decrement"
              action="decrement"
              handler={this.handler}
              parentState={this.state}
              icon="fa fa-arrow-down"
            />
            <h3 id="break-length">{this.state.breakLength}</h3>
            <ActButton
              id="break-increment"
              action="increment"
              handler={this.handler}
              parentState={this.state}
              icon="fa fa-arrow-up"
            />
          </div>
        </div>
        <div id="session">
          <h3 id="session-label">Session Length</h3>
          <div className="change">
            <ActButton
              id="session-decrement"
              action="decrement"
              handler={this.handler}
              parentState={this.state}
              icon="fa fa-arrow-down"
            />
            <h3 id="session-length">{this.state.sessionLength}</h3>
            <ActButton
              id="session-increment"
              action="increment"
              handler={this.handler}
              parentState={this.state}
              icon="fa fa-arrow-up"
            />
          </div>
        </div>
        <div id="timer">
          <h3 id="timer-label">{this.state.type}</h3>
          <Clock
            id="time-left"
            minutes={this.state.len}
            isPaused={this.state.isPaused}
            parentState={this.state}
            handler={this.handler}
          />
          <div className="change">
            <ActButton
              id="start_stop"
              action="toggle"
              handler={this.handler}
              parentState={this.state}
              icon="fa fa-play"
            />
            <ActButton
              id="reset"
              action="reset"
              handler={this.handler}
              parentState={this.state}
              icon="fa fa-undo"
            />
          </div>
        </div>
      </div>
    );
  }
}

ReactDOM.render(<Pomodoro />, document.getElementById("appContainer"));

              
            
!
999px

Console