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

              
                <script src="https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js"></script>
 <script src="https://use.fontawesome.com/8250cfda7d.js"></script>
<div id="root"></div>
              
            
!

CSS

              
                body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
    'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
    sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

code {
  font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
    monospace;
}


.App {
    text-align: center;
}

.App-logo {
    height: 40vmin;
    pointer-events: none;
}

@media (prefers-reduced-motion: no-preference) {
    .App-logo {
        animation: App-logo-spin infinite 20s linear;
    }
}

.App-header {
    background-color: #282c34;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    font-size: calc(10px + 2vmin);
    color: white;

}

.App-link {
    color: #61dafb;
}

@keyframes App-logo-spin {
    from {
        transform: rotate(0deg);
    }

    to {
        transform: rotate(360deg);
    }
}

.Clock {
    width: 300px;
    height: 200px;
    background-color: rgb(86, 144, 194);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    border-radius: 15px;
    margin-top: 30px;
}

#timer-label {
    display: block;
    margin-bottom: 60px;
}

.breakers {
    color: white;
    margin: 20px;
}

button {
    margin: 20px;
    min-width: 60px;
    background-color: rgb(86, 144, 194);
    border-radius: 10px;
}
              
            
!

JS

              
                
const sessionName = 'Practicing'
const breakName = 'Take 5'
// const alarm = "http://soundbible.com/mp3/Zen%20Buddhist%20Temple%20Bell-SoundBible.com-331362457.mp3"
const alarm = "http://soundbible.com/mp3/bells-tibetan-daniel_simon.mp3"
const soundatrib = "Zen Buddhist Temple Bell recorded by Mike Koenig and hosted at soundbible.com"
class App extends React.Component {
    constructor() {
        super()

        // activeState is ['running' | 'idle'] Indicates the session is ticking or not
        // workingState is [sessionName | breakName ] Indicates whether the session or the break timer is active.
        this.state = {
            breakLength: 5,
            sessionLength: 25,
            timeLeft: 1500,
            timerId: null,
            activeState: "idle",
            workingState: sessionName

        }
        this.handleClickBreakDown = this.handleClickBreakDown.bind(this);
        this.handleClickBreakUp = this.handleClickBreakUp.bind(this);
        this.handleClickSessionDown = this.handleClickSessionDown.bind(this);
        this.handleClickSessionUp = this.handleClickSessionUp.bind(this);
        this.handleStartStop = this.handleStartStop.bind(this);
        this.tick = this.tick.bind(this);
        this.handleReset = this.handleReset.bind(this);
    }

    handleClickBreakDown(e) {
        if (this.state.timerId === null) {
            if (this.state.breakLength >= 2) {
                this.setState({ breakLength: (this.state.breakLength - 1) })
                if (this.state.workingState === breakName) {
                    this.setState({ timeLeft: (this.state.breakLength - 1) * 60 })
                }
            }
        }
    }
    handleClickBreakUp(e) {
        if (this.state.timerId === null) {
            if (this.state.breakLength <= 59) {
                this.setState({ breakLength: (this.state.breakLength + 1) })
                if (this.state.workingState === breakName) {
                    this.setState({ timeLeft: (this.state.breakLength + 1) * 60 })
                }
            }
        }
    }
    handleClickSessionDown(e) {
        if (this.state.timerId === null) {
            if (this.state.sessionLength >= 2) {
                this.setState({ sessionLength: this.state.sessionLength - 1 })
                if (this.state.workingState === sessionName) {
                    this.setState({ timeLeft: (this.state.sessionLength - 1) * 60 })
                }
            }
        }
    }
    handleClickSessionUp(e) {
        if (this.state.timerId === null) {
            if (this.state.sessionLength <= 59) {
                this.setState({ sessionLength: this.state.sessionLength + 1 })
                if (this.state.workingState === sessionName) {
                    this.setState({ timeLeft: (this.state.sessionLength + 1) * 60 })
                }
            }
        }
    }

    handleStartStop(e) {
        let btext = e.target.innerText;
        if (btext === "Start") {
            if (this.state.timerId === null) {
                let tid = setInterval(this.tick, 100);
                this.setState({ timerId: tid })
            }
            e.target.innerText = "Stop";
        }
        else if (btext === "Stop") {
            if (this.state.timerId != null) {
                clearInterval(this.state.timerId)
                this.setState({ timerId: null })
            }
            e.target.innerText = "Start";
        }
    }

    tick() {
        // console.log(this.state.timeLeft, this.state.timeLeft === 0, this.state.timeLeft == 0)
        if (this.state.timeLeft === 0) {
            console.log("got 0 and " + this.state.workingState, sessionName)
            if (this.state.workingState === sessionName) {
                this.setState({ workingState: breakName })
                this.setState({ timeLeft: this.state.breakLength * 60 })
                let aud = document.getElementById("beep");
                // aud.src = alarm;
                aud.play();
            } else if (this.state.workingState === breakName) {
                this.setState({ workingState: sessionName })
                this.setState({ timeLeft: this.state.sessionLength * 60 })
                let aud = document.getElementById("beep");
                // aud.src = alarm;
                aud.play();
            }
        } else {
            this.setState({ timeLeft: this.state.timeLeft - 1 })
        }
    }

    handleReset() {
        if (this.state.timerId != null) {
            clearInterval(this.state.timerId)
            this.setState({ timerId: null })


            let stopOrStart = document.getElementById("start_stop")
            stopOrStart.innerText = "Start"
            let aud = document.getElementById("beep")
            
        }
        let aud = document.getElementById("beep")
        aud.pause();
        aud.currentTime = 0;
      
        this.setState({ breakLength: 5 })
        this.setState({ sessionLength: 25 })
        this.setState({ timeLeft: 1500 })
        this.setState({ activeState: 'idle' })
        this.setState({ workingState: sessionName })
    }

    render() {
        let minLeft = Math.floor(this.state.timeLeft / 60) > 9 ? Math.floor(this.state.timeLeft / 60) : "0" + Math.floor(this.state.timeLeft / 60)
        let secLeft = (this.state.timeLeft % 60) > 9 ? this.state.timeLeft % 60 : "0" + this.state.timeLeft % 60
        let displayTime = minLeft + ":" + secLeft
        return (
            < div className="App" >
                <header className="App-header">
                    <div>
                        <div id="timer-label">{this.state.workingState}</div>

                        <span className="breakers" id="break-label">
                            <i className="fa fa-long-arrow-down fa-2x" onClick={this.handleClickBreakDown} id="break-decrement"></i>
                        Break Length  <span id="break-length">{this.state.breakLength}</span>
                            <i className="fa fa-long-arrow-up fa-2x" onClick={this.handleClickBreakUp} id="break-increment"></i> </span>
                        <span className="breakers" id="session-label">
                            <i className="fa fa-long-arrow-down fa-2x" onClick={this.handleClickSessionDown} id="session-decrement"></i>
                        Session Length  <span id="session-length">{this.state.sessionLength}</span>
                            <i className="fa fa-long-arrow-up fa-2x" onClick={this.handleClickSessionUp} id="session-increment"></i> </span>

                    </div>
                    <div className="Clock">

                        <div id="time-left">{displayTime}</div>
                        <audio id="beep" src={alarm} attributed={soundatrib}></audio>

                    </div>
                    <div>
                        <span><button id="start_stop" onClick={this.handleStartStop}>Start</button></span>
                        <span><button id="reset" onClick={this.handleReset}>Reset</button></span>
                    </div>
                </header>
            </div >
        )
    }
}

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

Console