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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

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

              
            
!

CSS

              
                .App {
  text-align: center;
}

.App-logo {
  height: 40vmin;
  pointer-events: none;
}

@media (prefers-reduced-motion: no-preference) {
  .App-logo {
    animation: App-logo-spin infinite 20s linear;
  }
}

.App-header {
  background-color: #282c34;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font-size: calc(10px + 2vmin);
  color: white;
}

.App-link {
  color: #61dafb;
}

@keyframes App-logo-spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

              
            
!

JS

              
                
class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      sessionSeconds: 1500,
      activeTimer: false,
      displayWorkMinutes: 25,
      displayBreakMinutes: 5,
      breakComponentEnabled: true,
      breakStarted: false,
      runOnce: false
    }
    this.toggleTimer = this.toggleTimer.bind(this);
    this.incrementSessionLength = this.incrementSessionLength.bind(this);
    this.decrementSessionLength = this.decrementSessionLength.bind(this);
    this.incrementBreakLength = this.incrementBreakLength.bind(this);
    this.decrementBreakLength = this.decrementBreakLength.bind(this);
    this.fancyTimeFormat = this.fancyTimeFormat.bind(this);
    this.reset = this.reset.bind(this);
  }

  componentDidMount() {
  }

  componentWillUnmount() {
    clearInterval(this.timer);
  }

  toggleTimer() {
    if (this.state.activeTimer === true) {
      clearInterval(this.timer);
      this.setState({ activeTimer: false });
    }
    else {
      this.timer = setInterval(() => {
        if (this.state.sessionSeconds <= 0) {

          document.getElementById("beep").play();

          if (this.state.breakComponentEnabled === false) {
            //No break timer for us; we're done 
            clearInterval(this.timer);
            this.setState({ sessionSeconds: 25, activeTimer: false });
          }
          else {
            if (this.state.breakStarted === false) {
              //By the time this should be executing; we should beginning our break; having completed the session 
              let breakMinutes = this.state.displayBreakMinutes * 60;
              this.setState({ sessionSeconds: breakMinutes, breakStarted: true, activeTimer: true })
            }
            else {
              //By the time this should be executing; we should done; having completed the session and break
              let sessionMinutes = this.state.displayWorkMinutes * 60;
              this.setState({ sessionSeconds: sessionMinutes, activeTimer: false, breakStarted: false });
              if (this.state.runOnce === true) {
                clearInterval(this.timer);
              }
            }

          }
          return;
        }
        this.setState({ sessionSeconds: this.state.sessionSeconds - 1, activeTimer: true });
      }, 1000);
    }
  }



  /**
   * 
   * @param {Number} duration 
   * @returns {String}
   */
  fancyTimeFormat(duration) {
    // Hours, minutes and seconds
    if (duration === 3600) {
      return "60:00";
    }
    var mins = Math.floor((duration % 3600) / 60);
    var secs = Math.floor(duration % 60);

    // Output like "1:01" or "4:03:59" or "123:03:59"
    var ret = "";

    ret += "" + (mins < 10 ? "0" : "") + mins + ":" + (secs < 10 ? "0" : "");
    ret += "" + secs;
    return ret;
  }

  incrementSessionLength() {
    //Increment it by a minute 
    let newLength = this.state.sessionSeconds + 60;
    let newDisplayMinutes = this.state.displayWorkMinutes + 1;
    if (newLength <= 3600) {
      this.setState({ sessionSeconds: newLength, displayWorkMinutes: newDisplayMinutes });
    }
  }

  decrementSessionLength() {
    //Decrement it by a minute 
    let newLength = this.state.sessionSeconds - 60;
    let newDisplayMinutes = this.state.displayWorkMinutes - 1;
    if (newLength > 0) {
      this.setState({ sessionSeconds: newLength, displayWorkMinutes: newDisplayMinutes });
    }
  }


  incrementBreakLength() {
    //Increment it by a minute 
    let newDisplayMinutes = this.state.displayBreakMinutes + 1;
    if (newDisplayMinutes <= 60) {
      this.setState({ displayBreakMinutes: newDisplayMinutes });
    }
  }

  decrementBreakLength() {
    //Decrement it by a minute 
    let newDisplayMinutes = this.state.displayBreakMinutes - 1;
    if (newDisplayMinutes > 0) {
      this.setState({ displayBreakMinutes: newDisplayMinutes });
    }
  }

  reset() {
    this.setState({
      sessionSeconds: 1500,
      activeTimer: false,
      displayWorkMinutes: 25,
      displayBreakMinutes: 5,
      breakComponentEnabled: true,
      breakStarted: false,
      runOnce: false
    });

    this.timer && clearInterval(this.timer);

    document.getElementById("beep").pause();
    document.getElementById("beep").currentTime = 0;
  }

  render() {
    let element = (<p></p>);
    if (this.state.breakComponentEnabled === true) {
      element = (
        <div>
          <p id="break-label">Break length:</p>
          <p id="break-length">{this.state.displayBreakMinutes}</p>
          <button id="break-increment" onClick={this.incrementBreakLength}>+</button>
          <button id="break-decrement" onClick={this.decrementBreakLength}>-</button>
        </div>
      );
    }
    return (
      <div className="App">
        <p id="session-label">Session length:</p>
        <p id="session-length">{this.state.displayWorkMinutes}</p>
        <button id="session-increment" onClick={this.incrementSessionLength}>+</button>
        <button id="session-decrement" onClick={this.decrementSessionLength}>-</button>
        <button id="start_stop" onClick={this.toggleTimer}>
          Toggle timer
        </button>
        <button id="reset" onClick={this.reset}>
          Reset
        </button>
        {element}
        <p id="timer-label">{this.state.breakStarted ? "Break time:" : "Time Left:"}</p>
        <p id="time-left">{this.fancyTimeFormat(this.state.sessionSeconds)}</p>
        <audio id="beep" preload="auto" src="https://raw.githubusercontent.com/freeCodeCamp/cdn/master/build/testable-projects-fcc/audio/BeepSound.wav"></audio>

      </div>
    );
  }
}



ReactDOM.render(<App/>,app);
              
            
!
999px

Console