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

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

<div id="root"></div>
              
            
!

CSS

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

$bg-color-black: #1f2a2e;
$color-green: #00e940;

body {
  font-family: 'Teko', sans-serif;
  background-color: $bg-color-black;
  color: $color-green;
  font-weight: 300;
  font-size: 10px;
}

button {
  border: none;
  background: none;
  cursor: pointer;
  
  &:hover {
    opacity: .75;
  }
  
  &:focus {
    outline: none;
  }
  
  i.fas {
    color: $color-green;
    font-size: 2rem;
    
    
  }
}

.container {
  font-size: 1.6rem;
  max-width: 80%;
  margin: 100px auto;
  display: grid;
  grid-template-areas:
    "break-control session-control"
    "timer-card timer-card"
    "btn-card btn-card";
  
  .control {
    text-align: center;
    font-size: 2rem;
    
    .control-title {
      margin-bottom: 10px;
    }
    
    .control-content {
      display: flex;
      justify-content: center;
    }
  }
  
  .break-control {
    grid-area: break-control;
  }
  
  .session-control {
    grid-area: session-control;
  }
  
  .timer-card {
    grid-area: timer-card;
    border-color: $color-green;
    border-style: solid;
    border-width: 20px 50px;
    border-radius: 10px;
    padding: 30px;
    text-align: center;
    margin-top: 20px;
    margin-bottom: 20px;
    
    #time-left {
      font-weight: 700;
      font-size: 7.2rem;
    }
  }
  
  .btn-card {
    grid-area: btn-card;
    text-align: center;
  }
}
              
            
!

JS

              
                const BtnTextPlay = () => (
  <i className="fas fa-play"></i>
);
const BtnTextPause = () => (
  <i className="fas fa-pause"></i>
);

momentDurationFormatSetup(moment);

let countdown = null;

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      currentSessionType: "Session",
      breakLength: 60 * 5,
      sessionLength: 60 * 25,
      timeLeft: undefined,
      isStarted: false,
    };

    this.handleStartStop = this.handleStartStop.bind(this);
    this.handleReset = this.handleReset.bind(this);
    this.decBreakLength = this.decBreakLength.bind(this);
    this.incBreakLength = this.incBreakLength.bind(this);
    this.decSessionLength = this.decSessionLength.bind(this);
    this.incSessionLength = this.incSessionLength.bind(this);
  }

  decBreakLength() {
    const newBreakLength = this.state.breakLength - 60;
    if (newBreakLength < 60) {
      this.setState({
        breakLength: 60,
      });
    } else {
      this.setState({
        breakLength: newBreakLength,
      });
    }
  }

  incBreakLength() {
    console.log("clicked the inc button");
    const newBreakLength = this.state.breakLength + 60;
    console.log(newBreakLength);

    if (newBreakLength >= 3600) {
      this.setState({
        breakLength: 3600,
      });
    } else {
      this.setState({
        breakLength: newBreakLength,
      });
    }
  }

  decSessionLength() {
    const newSessionLength = this.state.sessionLength - 60;
    if (newSessionLength < 60) {
      this.setState({
        sessionLength: 60,
        timeLeft: 60,
      });
    } else {
      this.setState({
        sessionLength: newSessionLength,
        timeLeft: newSessionLength,
      });
    }
  }

  incSessionLength() {
    const newSessionLength = this.state.sessionLength + 60;
    if (newSessionLength >= 3600) {
      this.setState({
        sessionLength: 3600,
        timeLeft: 3600,
      });
    } else {
      this.setState({
        sessionLength: newSessionLength,
        timeLeft: newSessionLength,
      });
    }
  }

  handleStartStop() {
    console.log("isStarted", this.state.isStarted);
    if (this.state.isStarted) {
      // stop the countdown
      clearInterval(countdown);
      countdown = null;
      this.setState({
        isStarted: false,
      });
    } else {
      countdown = setInterval(() => {
        const newTimeLeft = this.state.timeLeft - 1;
        if (newTimeLeft >= 0) {
          this.setState({
            timeLeft: newTimeLeft,
            isStarted: true,
          });
        } else {
          // time left is less than zero
          this.audioBeep.play();
          // if session:
          if (this.state.currentSessionType === "Session") {
            // switch to break
            this.setState({
              currentSessionType: "Break",
              timeLeft: this.state.breakLength,
              isStarted: true,
            });
          }
          // if break:
          else if (this.state.currentSessionType === "Break") {
            this.setState({
              currentSessionType: "Session",
              timeLeft: this.state.sessionLength,
              isStarted: true,
            });
          }
        }
      }, 1000);
    }
  }

  handleReset() {
    // stop the countdown
    clearInterval(countdown);

    // set the countdown to null
    countdown = null;

    this.setState({
      currentSessionType: "Session",
      sessionLength: 60 * 25,
      breakLength: 60 * 5,
      timeLeft: 60 * 25,
      isStarted: false,
    });

    // reset audio
    this.audioBeep.pause();
    this.audioBeep.currentTime = 0;
  }

  componentDidMount() {
    this.setState({
      timeLeft: this.state.sessionLength,
    });
  }
  render() {
    return (
      <div className="container">
        <div className="break-control control">
          <div id="break-label" className="control-title">
            Break Length
          </div>
          <div className="control-content">
            <button id="break-decrement" onClick={this.decBreakLength}>
              <i className="fas fa-angle-down"></i>
            </button>
            <div id="break-length">
              {moment.duration(this.state.breakLength, "s").asMinutes()}
            </div>
            <button id="break-increment" onClick={this.incBreakLength}>
              <i className="fas fa-angle-up"></i>
            </button>
          </div>
        </div>

        <div className="session-control control">
          <div id="session-label" className="control-title">
            Session Length
          </div>
          <div className="control-content">
            <button id="session-decrement" onClick={this.decSessionLength}>
              <i className="fas fa-angle-down"></i>
            </button>
            <div id="session-length">
              {moment.duration(this.state.sessionLength, "s").asMinutes()}
            </div>
            <button id="session-increment" onClick={this.incSessionLength}>
              <i className="fas fa-angle-up"></i>
            </button>
          </div>
        </div>

        <div
          className={
            this.state.currentSessionType === "Break"
              ? "timer-card orange"
              : "timer-card"
          }
        >
          <div id="timer-label">{this.state.currentSessionType}</div>
          <div id="time-left">
            {moment
              .duration(this.state.timeLeft, "s")
              .format("mm:ss", { trim: false })}
          </div>
        </div>

        <div className="btn-card">
          <button id="start_stop" onClick={this.handleStartStop}>
            {this.state.isStarted ? <BtnTextPause /> : <BtnTextPlay />}
          </button>
          <button id="reset" onClick={this.handleReset}>
            <i className="fas fa-sync-alt"></i>
          </button>
        </div>
        <audio
          id="beep"
          preload="auto"
          src="https://raw.githubusercontent.com/freeCodeCamp/cdn/master/build/testable-projects-fcc/audio/BeepSound.wav"
          ref={(audio) => {
            this.audioBeep = audio;
          }}
        />
      </div>
    );
  }
}

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

Console