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

CSS

              
                *
  margin: 0
  padding: 0

html
  height: 100%
  
body
  min-height: 100%
  background: Black linear-gradient(rgb(5, 10, 20), Black)
  background-attachment: fixed
  display: flex
  
button
  font: inherit
  cursor: pointer
  background: none
  border: none
  transition-property: transform, color
  transition-duration: .1s
  font-size: 1.5em
  vertical-align: middle
  &:enabled
    color: RoyalBlue
    &:hover
      color: CadetBlue
    &:active
      transform: scale(.8)
  &:disabled
    cursor: not-allowed
  
.bi
  color: LightSlateGray

#container
  font-size: clamp(1em, 7.5vw, 2em)
  font-family: Avenir, Helvetica, Arial, sans-serif, monospace
  color: white
  text-align: center
  display: flex
  flex-direction: column
  justify-content: center
  align-items: center
  gap: 1em
  padding: 1em
  margin: auto
  
#title
  user-select: none
  code
    color: OrangeRed
  
#settings
  display: grid
  grid-template-columns: repeat(2, 1fr)
  gap: 1em
      
#session, #break
  &-label .bi
    margin-left: .3em
  &-length
    color: OrangeRed
    display: inline-block
    min-width: 1.5em
 
#clock
  width: 100%
  max-width: 12em
  background-color: rgb(10,10,15)
  color: SteelBlue
  border: thin solid DarkSlateBlue
  padding: .5rem
  border-radius: 15px
  
#time-left
  color: crimson

#actions
  display: flex
  justify-content: space-around
  button
    font-size: 2em
              
            
!

JS

              
                const projectName = "25-5-clock",  // freeCodeCamp testing
      //
      {Component, createRef} = React,
      //
      MIN_LENGTH = 1,
      MAX_LENGTH = 60
      //
      STATE = {
        sessionLength: 25,
        breakLength: 5,
        isSession: true,
        timer: undefined
      }

STATE.time = (STATE.isSession ? STATE.sessionLength : STATE.breakLength) * 60;



class App extends Component {
  constructor(props) {
    super(props);
    
    this.state = STATE;
    
    this.inDe = this.inDe.bind(this);
    this.st = this.st.bind(this);
    this.reset = this.reset.bind(this);
    
    this.beep = createRef();
  }
  
  
  get type() {
    return this.state.isSession ? "session" : "break";
  }
  
  
  componentDidUpdate(prevProps, prevState) {
    const NAME = this.type + "Length";
    
    if (prevState[NAME] != this.state[NAME] || prevState.isSession != this.state.isSession) {
      this.setState(state => ({
        time: state[NAME] * 60
      }));
    }
  }
  
  
  inDe() {
    const [TYPE, COMMAND] = event.target.id.split("-"),
          NAME = TYPE + "Length";
    
    switch(COMMAND) {
      case "increment":
        if (this.state[NAME] < MAX_LENGTH) {
          this.setState(state => ({
            [NAME]: state[NAME] + 1
          }));
        }
        break;
      case "decrement":
        if (this.state[NAME] > MIN_LENGTH) {
          this.setState(state => ({
            [NAME]: state[NAME] - 1
          }));
        }
    }
  }
  
  
  st() {
    this.setState(({timer}) => ({
      timer: timer ? clearInterval(timer) : setInterval(() => {
        this.setState(({time, isSession}) => {
          if (time > 0) {
            time--;
            return {time}
          }
          this.beep.current.play();
          return {
            isSession: !isSession
          }
        });
      }, 1000)
    }));
  }
  
  
  reset() {
    clearInterval(this.state.timer);
    
    this.setState(STATE);
    
    const BEEP = this.beep.current;
    
    BEEP.pause();
    BEEP.currentTime = 0;
  }
  
  
  render() {
    return (
      <>
        <h1 id="title">
          <code>{this.state.sessionLength}</code> <i className="bi bi-plus-lg"/> <code>{this.state.breakLength}</code> Clock <i className="bi bi-clock-history"/>
        </h1>
        <div id="settings">
          {["session", "break"].map((name, i) => {
            const LENGTH = this.state[name + "Length"];
            return (
              <div key={i}>
                <var id={name + "-label"}>{name[0].toUpperCase() + name.substring(1)}<i className="bi bi-stopwatch"/></var>
                <div>
                  <button id={name + "-increment"} onClick={this.inDe} className="bi bi-caret-up-fill" disabled={LENGTH >= MAX_LENGTH}/>
                  <code id={name + "-length"}>{LENGTH}</code>
                  <button id={name + "-decrement"} onClick={this.inDe} className="bi bi-caret-down-fill" disabled={LENGTH <= MIN_LENGTH}/>
                </div>
              </div>
            );
          })}
        </div>
        <div id="clock">
          <p id="timer-label">{this.type.toUpperCase()}</p>
          <h1 id="time-left">
            <samp>{("0" + Math.floor(this.state.time / 60)).slice(-2)}:{("0" + this.state.time % 60).slice(-2)}</samp>
          </h1>
          <audio id="beep" src="https://github.com/SSbit01/freeCodeCamp/blob/main/25%20+%205%20Clock/Beep.mp3?raw=true" ref={this.beep}/>
          <div id="actions">
            <button id="start_stop" onClick={this.st} className={"bi bi-" + (this.state.timer ? "pause-fill" : "play-fill")}/>
            <button id="reset" onClick={this.reset} className="bi bi-arrow-counterclockwise"/>
          </div>
        </div>
      </>
    );
  }
}



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

Console