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

              
                  <body>
    <noscript>You need to enable JavaScript to run this app.</noscript>
    <div id="root"></div>
    <!--
      This HTML file is a template.
      If you open it directly in the browser, you will see an empty page.

      You can add webfonts, meta tags, or analytics to this file.
      The build step will place the bundled scripts into the <body> tag.

      To begin the development, run `npm start` or `yarn start`.
      To create a production bundle, use `npm run build` or `yarn build`.
    -->
    <script src="https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js"></script>
    <script src="https://kit.fontawesome.com/b93ebf65d7.js" crossorigin="anonymous"></script>
  </body>
              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css2?family=Comfortaa:wght@600&display=swap');

:root {

}

body {
  margin: 0;
  padding: 0;
  font-family: 'Comfortaa', cursive;
  font-size: 1.1em;
  background-color: rgb(81, 244, 250);
}

.clock {
  margin: 3em auto;
  width: 70vw;
  min-width: 360px;
  max-width: 475px;
}

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

.clock-settings {
  display: flex;
  justify-content: space-around;
}

.clock-setting {
  padding: 0.5em 1em;
  background-color: rgb(187, 180, 245);
  border-radius: 25px;
  box-shadow: 1px 1px 3px rgb(117, 113, 153);

}

.clock-setting-label {
  margin-bottom: 0.5em;
  font-size: 1em;
  text-transform: capitalize;
}

.clock-setting-content {
  margin-bottom: 0.5em;
  display: flex;
  justify-content: space-around;
}

.clock-timer {
  margin: 2em auto 2em;
  padding: 1em;
  width: 70%;
  text-align: center;
  box-shadow: inset 1px 1px 10px black;
  border-radius: 50%;
  background-color: #fff;
}

.clock-timer-label {
  margin: 0.5em;
  font-size: 2em;
}

.clock-timer-time-left {
  margin-top: 0;
  margin-bottom: 0.5em;
  font-size: 3.5em;
}

.clock-controls {
  display: flex;
  justify-content: space-around;
  margin: auto;
  padding: 1em;
  width: 70%;
}

.clock-controls-icon {
  padding: 1em;
  min-width: 75px;
  text-align: center;
  background-color: rgb(187, 180, 245);
  box-shadow: 1px 1px 3px rgb(117, 113, 153);
  border-radius: 25px;
}
              
            
!

JS

              
                class App extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      timerIsActive: null,
      currTimer: 'Session',
      timeLeft: 10,
      session: 10,
      break: 300,
      timer: null,
      beepPlaying: false
    }
    this.incrementSetting = this.incrementSetting.bind(this)
    this.decrementSetting = this.decrementSetting.bind(this)
    this.controlClick = this.controlClick.bind(this)
  }

  componentDidMount() {
    this.setState({beep: document.getElementById('beep')})
  }
  
  incrementSetting(setting, val) {
    if (val < 3600) {
      this.setState((state) => ({
        [setting]: state[setting] + 60,
        timeLeft: setting === 'session' ? state[setting] + 60 : state.timeLeft
      }))
    }
  }

  decrementSetting(setting, val) {
    if (val > 60) {
      this.setState((state) => ({
        [setting]: state[setting] - 60,
        timeLeft: setting === 'session' ? state[setting] - 60 : state.timeLeft

      }))
    }
  }

  controlClick(ev) {
    ev.target.id === 'reset'
      ? this.reset()
      : this.state.timerIsActive === true
        ? this.stopTimer()
        : this.startTimer()
  }
  
  startTimer() {
    this.timer = setInterval(() => {
      this.setState((state) => {
        if (state.timeLeft === 0) {
          state.beep.play()
            // Commented to pass test 
            // Test 29 fails when handling play() async
            // Following code fixes browser errors
            // .then(() => {
            //   this.setState({beepPlaying: true})
            // })
            // .catch(err => {
            //   console.error("Unable to play beep")
            // })
        }
        return !state.timerIsActive && state.currTimer
            // Start timer
            ? {
                timerIsActive: true,
                currTimer: state.currTimer,
                timeLeft: state.timeLeft - 1
              }
            // Switch timer
            : state.timeLeft === 0 && state.timerIsActive
              ? {
                  currTimer: state.currTimer === "Session" ? "Break" : "Session",
                  timeLeft: state.currTimer === "Session" ? state.break : state.session
                }
              // Countdown
              : {
                  timeLeft: state.timeLeft - 1
                }
      })
    }, 1000)
  }

  stopTimer() {
    clearInterval(this.timer)
    this.setState({timerIsActive: false})
  }

  reset() {
    this.stopTimer()
    this.setState((state) => {
      console.log(state.beepPlaying)
      // Commented to pass final test - Enable to handle play() async
      // if (state.beepPlaying) { 
        state.beep.pause()
        state.beep.currentTime = 0
      // } 
      return {
        session: 1500,
        break: 300,
        timeLeft: 1500,
        currTimer: 'Session',
        timerIsActive: null,
        beepPlaying: false
      }
    })
  }

  render() { 
    return (
      <main className="clock">
        <h1 className="clock-title">Pomodoro Clock</h1>
        <section className="clock-settings">
          <TimerSetting name="break" 
                dec={this.decrementSetting} 
                inc={this.incrementSetting} 
                value={this.state.break}
          />
          <TimerSetting name="session" 
                inc={this.incrementSetting} 
                dec={this.decrementSetting} 
                value={this.state.session}
          />
        </section>
        <Timer type={this.state.currTimer}
                time={this.state.timeLeft}
        />
        <audio src="http://www.weigel.site/assets/A-Tone-His_Self-1266414414.mp3" 
                id="beep"
                preload="auto"
        ></audio>
        <footer className="clock-controls">
          <TimerControl name="start_stop" 
                    icon={this.state.timerIsActive ? "fas fa-pause" : "fas fa-play"} 
                    clickHandler={this.controlClick}
                    activeTimer={this.state.timerIsActive}
          />
          <TimerControl name="reset" 
                    icon="fas fa-history" 
                    clickHandler={this.controlClick}
          />
        </footer>
      </main>
    )
  }
}


class TimerSetting extends React.Component {
    constructor(props) {
        super(props)
        this.handleClick = this.handleClick.bind(this)
    }

    handleClick(ev) {
        let type = ev.target.id.split('-')[1]
        let setting = ev.target.id.split('-')[0]
        
        type && type === 'increment'
        ? this.props.inc(setting, this.props.value)
        : this.props.dec(setting, this.props.value)
    }

    render() {
        let name = this.props.name
        // console.log(name)
        return (
            <section className="clock-setting">
                <h3 id={`${name}-label`} 
                    className="clock-setting-label">
                        {`${name} Length`}
                </h3>
                <div className="clock-setting-content">
                    <i id={`${name}-decrement`}
                        className="fas fa-arrow-down"
                        onClick={this.handleClick}
                    >
                    </i>
                    <span id={`${name}-length`}>
                        {this.props.value/60}
                    </span>
                    <i id={`${name}-increment`}
                        className="fas fa-arrow-up"
                        onClick={this.handleClick}
                    >
                    </i>
                </div>
            </section>
        )
    }
}


class Timer extends React.Component {
    render() {
        let minutes = Math.floor(this.props.time/60)
        let seconds = this.props.time%60 

        return (
            <section className="clock-timer">
                <h2 id="timer-label" className="clock-timer-label">
                    {this.props.type}
                </h2>
                <p id="time-left" className="clock-timer-time-left">
                    {minutes < 10 ? '0'.concat(minutes) : minutes}:
                    {seconds < 10 ? '0'.concat(seconds) : seconds}
                </p>
            </section>
        )
    }
}


class TimerControl extends React.Component {
    render() {
        return(
            <i id={this.props.name} 
                className={`clock-controls-icon ${this.props.icon}`}
                onClick={this.props.clickHandler}
            >
            </i>
        )
    }
}

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById('root')
);
              
            
!
999px

Console