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>
              
            
!

CSS

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

/* General */

body {
  font-family: 'Roboto Mono', monospace;
  background-color: #c2e6e8;
}

#root {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
}

button,
button:active {
  background-color: rgb(234,178,156);
  border: none;
  width: 42px;
  height: 40px;
  border-radius: 100%;
  text-align: center;
  box-shadow: 0.2em 0.2em rgb(194, 91, 44);
  -webkit-box-shadow: 0.2em 0.2em rgb(194, 91, 44);
  -moz-box-shadow: 0.2em 0.2em rgb(194, 91, 44);
}

button:focus {
  translate: 0.2em 0.2em;
  box-shadow: 0.2em 0.2em rgb(194, 91, 44) inset;
  -webkit-box-shadow: 0.2em 0.2em rgb(194, 91, 44) inset;
  -moz-box-shadow: 0.2em 0.2em rgb(194, 91, 44) inset;
}

/* Clock Body */

.clock-container {
  background-color: rgb(240, 240, 190); 
  width: 350px;
  border-radius: 20px;
  padding: 30px;
  box-shadow: 10px 10px sienna;
}

/* Display + Stop/Start/Reset Controls */

#time-left {
  font-size: 70px;
  text-align: center;
  background-color: rgb(150,180,175);
  padding: 20px;
  border-radius: 20px;
  box-shadow: 3px 3px rgb(121, 145, 141) inset;
}

#display-controls {
  margin: 10px 0;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
}

#timer-label {
  font-size: 25px;
  text-align: center;
  padding-top: 2px;
  background-color: rgb(150,180,175);
  border-radius: 10px;
  height: 40px;
  flex: 2;
  box-shadow: 3px 3px rgb(121, 145, 141) inset;
}

#timer-ssr {
  flex: 1;
  margin-left: 30px;
  display: flex;
  justify-content: space-between;
  flex-start: end;
}

/* Timer Length Controls */

.length-container {
  margin-top: 20px;
  display: flex;
  flex-direction: row;
}

.inc-dec-btn-container {
  margin: 0 auto;
  display: flex;
  flex-direction: row;
  justify-content: center;
  width: 140px;
  height:60px;
}

.break-container, 
.session-container {
  text-align: center;
/*   display: inline-block; */
  border: 2px solid rgb(234,178,156);
  padding: 10px;
  border-radius: 10px;
  width: 48%;
}

.timer-length {
  font-size: 2em;
}

.break-container,
.inc-arrow {
  margin-right: 12px;
}
              
            
!

JS

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

const SESSION = "Session";
const BREAK = "Break";
const SESSIONLEN = 25;
const BREAKLEN = 5;

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      breakLen: BREAKLEN, // min
      sessionLen: SESSIONLEN, // min
      timeLeft: SESSIONLEN * 60, // sec
      timerType: SESSION,
      isTimerRunning: false,
      intervalId: ""
    };
    this.changeTimerType = this.changeTimerType.bind(this);
    this.handleDecrementBreak = this.handleDecrementBreak.bind(this);
    this.handleIncrementBreak = this.handleIncrementBreak.bind(this);
    this.handleDecrementSession = this.handleDecrementSession.bind(this);
    this.handleIncrementSession = this.handleIncrementSession.bind(this);
    this.resetTimer = this.resetTimer.bind(this);
    this.toggleStartStopTimer = this.toggleStartStopTimer.bind(this);
  }

  changeTimerType() {
    this.setState(
      {
        timerType: this.state.timerType === SESSION ? BREAK : SESSION,
        timeLeft:
          this.state.timerType === SESSION
            ? this.state.breakLen * 60
            : this.state.sessionLen * 60
      },
      () => {
        this.runTimer();
      }
    );
  }

  handleDecrementBreak() {
    if (!this.state.isTimerRunning && this.state.breakLen > 1) {
      this.setState({
        breakLen: this.state.breakLen - 1,
        timeLeft:
          this.state.timerType === BREAK
            ? (this.state.breakLen - 1) * 60
            : this.state.timeLeft
      });
    }
  }

  handleIncrementBreak() {
    if (!this.state.isTimerRunning && this.state.breakLen < 60) {
      this.setState({
        breakLen: this.state.breakLen + 1,
        timeLeft:
          this.state.timerType === BREAK
            ? (this.state.breakLen + 1) * 60
            : this.state.timeLeft
      });
    }
  }

  handleDecrementSession() {
    if (!this.state.isTimerRunning && this.state.sessionLen > 1) {
      this.setState({
        sessionLen: this.state.sessionLen - 1,
        timeLeft:
          this.state.timerType === SESSION
            ? (this.state.sessionLen - 1) * 60
            : this.state.timeLeft
      });
    }
  }

  handleIncrementSession() {
    if (!this.state.isTimerRunning && this.state.sessionLen < 60) {
      this.setState({
        sessionLen: this.state.sessionLen + 1,
        timeLeft:
          this.state.timerType === SESSION
            ? (this.state.sessionLen + 1) * 60
            : this.state.timeLeft
      });
    }
  }

  resetTimer() {
    clearInterval(this.state.intervalId);
    this.setState({
      breakLen: BREAKLEN, // min
      sessionLen: SESSIONLEN, // min
      timeLeft: SESSIONLEN * 60, // sec
      timerType: SESSION,
      isTimerRunning: false,
      intervalId: ""
    });
    this.beepSound.pause();
    this.beepSound.currentTime = 0;
  }

  runTimer() {
    let intervalId = setInterval(() => {
      this.setState(
        {
          timeLeft: this.state.timeLeft - 1
        },
        () => {
          if (this.state.timeLeft === 0) {
            this.beepSound.play();
          }
          if (this.state.timeLeft < 0) {
            if (this.state.intervalId) clearInterval(this.state.intervalId);
            this.changeTimerType();
          }
        }
      );
    }, 1000);
    this.setState({
      intervalId
    });
  }

  toggleStartStopTimer() {
    if (!this.state.isTimerRunning) {
      this.runTimer();
      this.setState({ isTimerRunning: true });
    } else {
      clearInterval(this.state.intervalId);
      this.setState({
        isTimerRunning: false,
        intervalId: ""
      });
    }
  }

  clockify() {
    let minutes = Math.floor(this.state.timeLeft / 60);
    let seconds = this.state.timeLeft - minutes * 60;
    seconds = seconds < 10 ? "0" + seconds : seconds;
    minutes = minutes < 10 ? "0" + minutes : minutes;
    return minutes + ":" + seconds;
  }

  render() {
    let stopStartTimer = this.state.isTimerRunning
      ? "fa fa-pause"
      : "fa fa-play";

    return (
      <div className="clock-container">
        <Timer
          timeLeft={this.clockify()}
          timerType={this.state.timerType}
          resetTimer={this.resetTimer}
          stopStartTimer={stopStartTimer}
          toggleStartStopTimer={this.toggleStartStopTimer}
        />
        <div className="length-container">
          <div className="break-container">
            <SetTimerLength
              timerLabelId="break-label"
              timerLabel="Break Length"
              timerLen={this.state.breakLen}
              timerLenId="break-length"
              decTimerId="break-decrement"
              handleDecrementTimer={this.handleDecrementBreak}
              incTimerId="break-increment"
              handleIncrementTimer={this.handleIncrementBreak}
            />
          </div>
          <div className="session-container">
            <SetTimerLength
              timerLabelId="session-label"
              timerLabel="Session Length"
              timerLen={this.state.sessionLen}
              timerLenId="session-length"
              decTimerId="session-decrement"
              handleDecrementTimer={this.handleDecrementSession}
              incTimerId="session-increment"
              handleIncrementTimer={this.handleIncrementSession}
            />
          </div>
        </div>
        <audio
          id="beep"
          load="auto"
          ref={(audio) => {
            this.beepSound = audio;
          }}
          src="https://raw.githubusercontent.com/freeCodeCamp/cdn/master/build/testable-projects-fcc/audio/BeepSound.wav"
        ></audio>
      </div>
    );
  }
}

class SetTimerLength extends React.Component {
  constructor(props) {
    super(props);
    this.buttonRefInc = React.createRef();
    this.buttonRefDec = React.createRef();
  }

  render() {
    return (
      <div>
        <div id={this.props.timerLabelId}>{this.props.timerLabel}</div>
        <span id={this.props.timerLenId} className="timer-length">
          {this.props.timerLen}
        </span>
        <div className="inc-dec-btn-container">
          <button
            ref={this.buttonRefInc}
            className="inc-arrow"
            onClick={() => {
              this.props.handleIncrementTimer();
              this.buttonRefInc.current.blur();
            }}
          >
            <i id={this.props.incTimerId} className="fa fa-arrow-up"></i>
          </button>
          <button
            ref={this.buttonRefDec}
            onClick={() => {
              this.props.handleDecrementTimer();
              this.buttonRefDec.current.blur();
            }}
          >
            <i id={this.props.decTimerId} className="fa fa-arrow-down"></i>
          </button>
        </div>
      </div>
    );
  }
}

class Timer extends React.Component {
  constructor(props) {
    super(props);
    this.buttonRefStartStop = React.createRef();
    this.buttonRefReset = React.createRef();
  }

  render() {
    return (
      <div>
        <div id="time-left">{this.props.timeLeft}</div>
        <div id="display-controls">
          <div id="timer-label">{this.props.timerType}</div>
          <div id="timer-ssr">
            <button
              ref={this.buttonRefStartStop}
              id="start-stop"
              onClick={() => {
                this.props.toggleStartStopTimer();
                this.buttonRefStartStop.current.blur();
              }}
            >
              <i className={this.props.stopStartTimer} aria-hidden="true"></i>
            </button>
            <button
              ref={this.buttonRefReset}
              id="reset"
              onClick={() => {
                this.props.resetTimer();
                this.buttonRefReset.current.blur();
              }}
            >
              <i className="fa fa-refresh" aria-hidden="true"></i>
            </button>
          </div>
        </div>
      </div>
    );
  }
}

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

              
            
!
999px

Console