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

              
            
!

CSS

              
                * {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    font-family: 'Rubik', sans-serif;
}

:root {
    --element-color: rgb(243, 243, 243);
    --element-muted: rgba(240, 240, 240, 0.5);
}

body {
    background: radial-gradient(rgb(24, 63, 100), rgb(11, 21, 31));
    color: var(--element-color);
}

.flex-column-h-center {
    display: flex;
    flex-direction: column;
    align-items: center;
}

.text-center {
    text-align: center;
}

.text-muted {
    color: var(--element-muted);
}

.text-spaced {
    letter-spacing: 4px;
}

.loading-bar-bg {
    width: 100%;
    margin: 40px 0;
    height: 4px;
    border: none;
    background-color: var(--element-muted);
    box-shadow: 10px 6px 12px rgba(0, 0, 0, 0.596);
    border-radius: 50px;
}

.loading-bar {
    background-color: var(--element-color);
    width: 100%;
    height: 100%;
    transition: width 1s linear;
    border-radius: 50px;
}

.btn {
    border: 2px solid var(--element-muted);
    border-radius: 100px;
    background: none;
    padding: 10px 13px;
    font-size: 1rem;
    margin: 0 10px;
    cursor: pointer;
    color: var(--element-muted);
    transition: color .3s, border-color .3s, transform .3s;
}

.btn:hover {
    border-color: var(--element-color);
    color: var(--element-color);
    transform: translate3d(0, -4px, 0);
}

.btn:disabled {
    color: var(--element-muted);
    border-color: var(--element-muted);
    transform: translate3d(0, 0, 0);
    cursor: not-allowed;
}

.settings-number {
    font-size: 2rem;
}

#main {
    height: 100vh;
}

#clock-container {
    margin-top: 20vh;
}

#time-left {
    font-size: 6rem;
    line-height: 1;
    font-family: 'Anonymous Pro', monospace;
}

#time-left-label-container {
    margin-top: 50px;
    padding: 50px 50px 10px 50px;
}

#length-settings-group-container {
    display: flex;
}

.length-settings-group {
    padding: 0 50px;
}

#main-heading {
    font-size: 2rem;
    font-weight: normal;
}

@media (max-width: 500px) {
    #time-left {
        font-size: 4rem;
    }

    .length-settings-group {
        padding: 0 5px;
    }

    #main-heading {
        font-size: 1.5rem;
    }

    #clock-container {
        margin-top: 50px;
    }
}
              
            
!

JS

              
                class PomodoroClock extends React.Component {
  constructor() {
    super();
    this.resetClock = this.resetClock.bind(this);
    this.updateTimerSetting = this.updateTimerSetting.bind(this);
    this.startOrStopTimer = this.startOrStopTimer.bind(this);
    this.countDownOneSecond = this.countDownOneSecond.bind(this);
  }

  state = {
    breakLength: 5,
    breakLengthLeft: 5,
    sessionLength: 25,
    sessionLengthLeft: 25,
    timerIsRunning: false,
    seconds: 0,
    intervalHandle: 0,
    breakOngoing: false,
    displayTextBetweenSessions: "",
    loadingBarWidth: 100
  };

  startOrStopTimer() {
    this.setState({ timerIsRunning: !this.state.timerIsRunning });
    if (this.state.timerIsRunning) clearInterval(this.state.intervalHandle);
    else {
      let handle = setInterval(this.countDownOneSecond, 1000);
      this.setState({ intervalHandle: handle });
    }
  }

  resetClock() {
    this.setState({
      breakLength: 5,
      breakLengthLeft: 5,
      sessionLength: 25,
      sessionLengthLeft: 25,
      seconds: 0,
      timerIsRunning: false,
      breakOngoing: false,
      loadingBarWidth: 100
    });
    clearInterval(this.state.intervalHandle);
    document.getElementById("beep").pause();
    document.getElementById("beep").currentTime = 0;
  }

  // Increases or decreases the specified timer (sessionTimer or breakTimer) by 1 minute if no timer is currently running
  updateTimerSetting(timerName, timeAmount) {
    if (this.state.timerIsRunning) return;

    if (
      this.state[timerName] + timeAmount >= 1 &&
      this.state[timerName] + timeAmount <= 60
    ) {
      const timerNameLeft = timerName + "Left";
      this.setState(prevState => ({
        [timerName]: prevState[timerName] + timeAmount,
        [timerNameLeft]: prevState[timerName] + timeAmount
      }));
    }
  }

  countDownOneSecond() {
    if (this.state.seconds - 1 < 0) {
      // The block below manages the minute-timer when a break is going on
      if (this.state.breakOngoing) {
        if (this.state.breakLengthLeft - 1 < 0) {
          this.setState({
            breakLengthLeft: this.state.breakLength,
            breakOngoing: false,
            displayTextBetweenSessions: "SESSION"
          });
          setTimeout(() => {
            this.setState({ displayTextBetweenSessions: "" });
          }, 1000);
          document.getElementById("beep").play();
        } else
          this.setState(prevState => ({
            breakLengthLeft: prevState.breakLengthLeft - 1,
            seconds: 59
          }));
      } else {
        // The block below manages the minute-timer when a session (no break) is going on
        if (this.state.sessionLengthLeft - 1 < 0) {
          this.setState({
            sessionLengthLeft: this.state.sessionLength,
            breakOngoing: true,
            displayTextBetweenSessions: "BREAK"
          });
          setTimeout(() => {
            this.setState({ displayTextBetweenSessions: "" });
          }, 1000);
          document.getElementById("beep").play();
        } else
          this.setState(prevState => ({
            sessionLengthLeft: prevState.sessionLengthLeft - 1,
            seconds: 59
          }));
      }
    } else
      // Seconds decrease independently of break or session status
      this.setState(prevState => ({ seconds: prevState.seconds - 1 }));

    // calculate and set new loading bar width
    const secondsLeft =
      (this.state.breakOngoing
        ? this.state.breakLengthLeft
        : this.state.sessionLengthLeft) *
        60 +
      this.state.seconds;
    const initialTimeInSeconds =
      (this.state.breakOngoing
        ? this.state.breakLength
        : this.state.sessionLength) * 60;
    const loadingBarWidth = secondsLeft / initialTimeInSeconds * 100;
    this.setState({ loadingBarWidth });
  }

  render() {
    // Gets last two digits (eg. 020 becomes 20, 07 becomes 7)
    const formattedSeconds = ("0" + this.state.seconds).slice(-2);
    const currentActiveTimerLeft = this.state.breakOngoing
      ? this.state.breakLengthLeft
      : this.state.sessionLengthLeft;
    const formattedMinutes = ("0" + currentActiveTimerLeft).slice(-2);
    let displayText = formattedMinutes + ":" + formattedSeconds;

    let startStopButtonStyles = this.state.timerIsRunning
      ? "fas fa-pause"
      : "fas fa-play";

    return (
      <main id="main" className="flex-column-h-center">
        <div className="flex-column-h-center" id="clock-container">
          <h1 id="main-heading" className="text-center text-muted">
            <span className="far fa-clock" /> Pomodoro Clock
          </h1>
          <div id="time-left-label-container">
            <p id="timer-label" className="text-muted text-spaced">
              {this.state.breakOngoing ? "BREAK" : "SESSION"}
            </p>
            <p id="time-left" className="text-center">
              {displayText}
            </p>
          </div>
          <div>
            <button
              id="start_stop"
              className="btn"
              onClick={this.startOrStopTimer}
            >
              <span className={startStopButtonStyles} />
            </button>
            <button id="reset" className="btn" onClick={this.resetClock}>
              <span className="fas fa-stop" />
            </button>
          </div>
          <div className="loading-bar-bg">
            <div
              className="loading-bar"
              style={{ width: this.state.loadingBarWidth + "%" }}
            />
          </div>
          <div id="length-settings-group-container">
            <div className="length-settings-group flex-column-h-center">
              <p id="break-label" className="text-muted text-spaced">
                BREAK
              </p>
              <p id="break-length" className="settings-number">
                {this.state.breakLength}
              </p>
              <div>
                <button
                  disabled={this.state.timerIsRunning}
                  id="break-increment"
                  className="btn"
                  onClick={() => {
                    this.updateTimerSetting("breakLength", 1);
                  }}
                >
                  <span className="fas fa-plus" />
                </button>
                <button
                  disabled={this.state.timerIsRunning}
                  id="break-decrement"
                  className="btn"
                  onClick={() => {
                    this.updateTimerSetting("breakLength", -1);
                  }}
                >
                  <span className="fas fa-minus" />
                </button>
              </div>
            </div>
            <div className="length-settings-group flex-column-h-center">
              <p id="session-label" className="text-muted text-spaced">
                SESSION
              </p>
              <p id="session-length" className="settings-number">
                {this.state.sessionLength}
              </p>
              <div>
                <button
                  disabled={this.state.timerIsRunning}
                  id="session-increment"
                  className="btn"
                  onClick={() => {
                    this.updateTimerSetting("sessionLength", 1);
                  }}
                >
                  <span className="fas fa-plus" />
                </button>
                <button
                  disabled={this.state.timerIsRunning}
                  id="session-decrement"
                  className="btn"
                  onClick={() => {
                    this.updateTimerSetting("sessionLength", -1);
                  }}
                >
                  <span className="fas fa-minus" />
                </button>
              </div>
            </div>
          </div>
          <audio id="beep" preload="auto" src="https://goo.gl/65cBl1" />
        </div>
      </main>
    );
  }
}

ReactDOM.render(<PomodoroClock />, document.getElementById("root"));

              
            
!
999px

Console