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

CSS

              
                .App {
  background-color: royalblue;
  display: flex;
  flex-direction: column;
  font-family: 'Roboto Mono', monospace;
  border-radius: 50%;
  width: 450px;
  height: 450px;
  justify-content: center;
  align-items: center;
}

.guages {
  display: flex;
  align-items: center;
}

.session-cluster {
  position: relative;
  display: flex;
  flex-direction: column;
  background-color: #555;
  color: white;
  width: 250px;
  height: 250px;
  border-radius: 50%;
  justify-content: center;
  align-items: center;
  right: 25px;
  bottom: 10px;
}

#session-length {
  font-size: 4rem;
  padding: 0;
  margin: 0;
}

.break-cluster {
  position: relative;
  display: flex;
  flex-direction: column;
  width: 200px;
  height: 200px;
  border-radius: 50%;
  justify-content: center;
  align-items: center;
  background-color: #333;
  color: white;
  top: 20px;
  left: 10px;
  z-index: 10;
  padding: 0;
  margin: 0;
}


#break-length {
  font-size: 3rem;
  padding: 0;
  margin: 0;
}

.buttons {
  display: flex;
}

.buttons > button {
  padding: 0;
  margin: 0px 10px;
  width: 150px;
  height: 40px;
  font-family: 'Roboto Mono', monospace;
  font-weight: bolder;
  font-size: 20px;
}

.minute-control-button {
  width: 3rem;
  height: 3rem;
  font-size: 30px;
  border-radius: 50%;
  border: none;
  background-color: white;
  margin: 10px 5px 0px 5px;
  padding: 0;
}

.minute-control-button:active {
  background-color: #ddd;
}

.session-info {
  display: flex;
  color: white;
  font-weight: bolder;
}

#timer-label, #time-left {
  margin: 10px;
  font-size: 1.5rem;
}

button:disabled {
  background-color: #ccc;
}
              
            
!

JS

              
                import * as React from "https://cdn.skypack.dev/react@17.0.1";
import * as ReactDOM from "https://cdn.skypack.dev/react-dom@17.0.1";


class App extends React.Component {
  constructor(props) {
    super(props)
    // create references to DOM objects
    this.audioRef = React.createRef(null)
    this.startStopButtonRef = React.createRef(null)
    this.sessionIncRef = React.createRef(null)
    this.sessionDecRef = React.createRef(null)
    this.breakIncRef = React.createRef(null)
    this.breakDecRef = React.createRef(null)
    // set the initial state to start as a session timer
    this.state = {
      isRunning: false,
      sessionTime: 1500,
      breakTime: 300,
      sessionInterval: null,
      breakInterval: null,
      defaultSessionTime: 1500,
      defaultBreakTime: 300,
      timerLabel: 'Session Paused',
      startStopButton: 'Start',
      timerType: 'session'
    }
    this.getFormattedTime = this.getFormattedTime.bind(this)
    this.changeTime = this.changeTime.bind(this)
    this.sessionTimerStartStop = this.sessionTimerStartStop.bind(this)
    this.breakTimerStartStop = this.breakTimerStartStop.bind(this)
    this.reset = this.reset.bind(this)
    this.startTimer = this.startTimer.bind(this)
    this.stopTimer = this.stopTimer.bind(this)
  }

  getFormattedTime(time) {
    const min = Math.floor(time / 60)
    let sec = time % 60
    return (`${min < 10 ? '0' + min : min}:${sec < 10 ? '0' + sec : sec}`)
  }

  changeTime(e) {
    const key = e.target.id
    switch (key) {
      case 'session-increment':
        this.setState(
          {
            sessionTime: this.state.sessionTime += 60,
            defaultSessionTime: this.state.defaultSessionTime += 60
          })
        break;
      case 'session-decrement':
        this.setState(
          {
            sessionTime: this.state.sessionTime -= 60,
            defaultSessionTime: this.state.defaultSessionTime -= 60
          })
        break;
      case 'break-increment':
        this.setState(
          {
            breakTime: this.state.breakTime += 60,
            defaultBreakTime: this.state.defaultBreakTime += 60
          })
        break;
      case 'break-decrement':
        this.setState(
          {
            breakTime: this.state.breakTime -= 60,
            defaultBreakTime: this.state.defaultBreakTime -= 60
          })
        break;
    }
    // console.log(this.state.breakTime);

    // disable session dec button if time === 0
    this.state.sessionTime === 60 ?
      this.sessionDecRef.current.disabled = true :
      this.sessionDecRef.current.disabled = false
    // disable break dec button if time === 0
    this.state.defaultBreakTime === 60 ?
      this.breakDecRef.current.disabled = true :
      this.breakDecRef.current.disabled = false
    // disable session inc button if time === 60
    this.state.sessionTime === 3600 ?
      this.sessionIncRef.current.disabled = true :
      this.sessionIncRef.current.disabled = false
    // disable break inc button if time === 60
    this.state.defaultBreakTime === 3600 ?
      this.breakIncRef.current.disabled = true :
      this.breakIncRef.current.disabled = false
  }
  /**
   * 
   * @param {String} tType type of timer currently running, session or break
   */
  startTimer(tType) {
    this.startStopButtonRef.current.textContent = 'Stop'
    this.sessionDecRef.current.disabled = true
    this.sessionIncRef.current.disabled = true
    this.breakDecRef.current.disabled = true
    this.breakIncRef.current.disabled = true
    this.setState({ isRunning: true })
    tType === 'session' ?
      this.sessionTimerStartStop() :
      this.breakTimerStartStop()
  }
  /**
   * 
   * @param {String} tType type of timer currently running, session or break
   */
  stopTimer(tType) {
    this.startStopButtonRef.current.textContent = 'Start'
    this.sessionDecRef.current.disabled = false
    this.sessionIncRef.current.disabled = false
    this.breakDecRef.current.disabled = false
    this.breakIncRef.current.disabled = false
    this.setState({
      isRunning: false,
      timerLabel: tType === 'session' ? 'Session Paused' : 'Break Paused'
    })
    switch (tType) {
      case 'session':
        clearInterval(this.state.sessionInterval)
        break
      case 'break':
        clearInterval(this.state.breakInterval)
        break
    }
  }

  sessionTimerStartStop() {
    this.setState(
      {
        breakTime: this.state.defaultBreakTime,
        isRunning: true,
        timerLabel: 'Session Active',
        sessionInterval: setInterval(() => {
          // console.log(this.state)

          this.setState({ sessionTime: this.state.sessionTime -= 1 })
          if (this.state.sessionTime < 0) {
            this.audioRef.current.play()
            clearInterval(this.state.sessionInterval)
            this.setState({
              timerType: 'break',
              // sessionTime: this.state.defaultSessionTime
            })
            this.breakTimerStartStop()
          }
        }, 1000)
      })
  }

  breakTimerStartStop() {
    this.setState({
      sessionTime: this.state.defaultSessionTime,
      isRunning: true,
      timerLabel: 'Break Active',
      breakInterval: setInterval(() => {
        this.setState({ breakTime: this.state.breakTime -= 1 })
        if (this.state.breakTime < 0) {
          this.audioRef.current.play()
          clearInterval(this.state.breakInterval)
          this.setState({
            timerType: 'session',
            breakTime: this.state.defaultBreakTime
          })
          this.sessionTimerStartStop()
        }
      }, 1000)
    })
  }

  reset() {
    clearInterval(this.state.sessionInterval)
    clearInterval(this.state.breakInterval)
    this.audioRef.current.load()
    this.startStopButtonRef.current.textContent = 'Start'
    this.sessionDecRef.current.disabled = false
    this.sessionIncRef.current.disabled = false
    this.breakDecRef.current.disabled = false
    this.breakIncRef.current.disabled = false
    this.setState({
      sessionTime: 1500,
      isRunning: false,
      defaultSessionTime: 1500,
      breakTime: 300,
      defaultBreakTime: 300,
      timerType: 'session',
      timerLabel: 'Session Paused'
    })
  }

  render() {
    // console.log(this.state)
    const {
      timerType,
      isRunning
    } = this.state

    return (
      <div className="App">
        {/* Session Timer */}
        <div className="guages">
          <Timer
            className='break-cluster'
            name='break-length'
            decButtonRef={this.breakDecRef}
            incButtonRef={this.breakIncRef}
            incrementButton='break-increment'
            decrementButton='break-decrement'
            type="break-label"
            timerName='Break Length'
            time={Math.floor(this.state.defaultBreakTime / 60)}
            buttonHandler={this.changeTime} />
          <Timer
            className='session-cluster'
            name='session-length'
            decButtonRef={this.sessionDecRef}
            incButtonRef={this.sessionIncRef}
            incrementButton='session-increment'
            decrementButton='session-decrement'
            type='session-label'
            timerName='Session Length'
            time={Math.floor(this.state.defaultSessionTime / 60)}
            buttonHandler={this.changeTime} />
          {/* Break Timer */}
        </div>
          <div className="buttons">
            <button
              id='start_stop'
              onClick={this.state.isRunning ?
                (e) => this.stopTimer(timerType, e) :
                (e) => this.startTimer(timerType, e)}
              ref={this.startStopButtonRef}>Start</button>
            <button id='reset' onClick={this.reset}>Reset</button>
          </div>
          <div className="session-info">
            <label id='timer-label'>{this.state.timerLabel}</label>
            <h3
              id='time-left'
            >{this.state.timerType === 'session' ?
              this.getFormattedTime(this.state.sessionTime) :
              this.getFormattedTime(this.state.breakTime)}
            </h3>
          </div>
        <audio
          id='beep'
          ref={this.audioRef}
          src="https://raw.githubusercontent.com/freeCodeCamp/cdn/master/build/testable-projects-fcc/audio/BeepSound.wav" />
      </div>
    );
  }
}

function Timer(props) {
  return (
    <div class={props.className}>
      <h1 id={props.name}>{props.time}</h1>
      <label id={props.type}>{props.timerName}</label>
      <div>
        <button
          className='minute-control-button'
          ref={props.incButtonRef}
          id={props.incrementButton}
          onClick={props.buttonHandler}
          value="+"
        >+</button>
        <button
          className='minute-control-button'
          ref={props.decButtonRef}
          id={props.decrementButton}
          onClick={props.buttonHandler}
          value="-"
        >-</button>
      </div>
    </div>
  )
}

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

              
            
!
999px

Console