123

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css" integrity="sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf" crossorigin="anonymous">
<div id="root"></div>


            
          
!
            
              @import 'https://fonts.googleapis.com/css?family=Share+Tech+Mono';
@font-face {
    font-family: "Digital";
    src: url("//db.onlinewebfonts.com/t/8e22783d707ad140bffe18b2a3812529.eot");
    src: url("//db.onlinewebfonts.com/t/8e22783d707ad140bffe18b2a3812529.eot?#iefix") format("embedded-opentype"), url("//db.onlinewebfonts.com/t/8e22783d707ad140bffe18b2a3812529.woff2") format("woff2"), url("//db.onlinewebfonts.com/t/8e22783d707ad140bffe18b2a3812529.woff") format("woff"), url("//db.onlinewebfonts.com/t/8e22783d707ad140bffe18b2a3812529.ttf") format("truetype"), url("//db.onlinewebfonts.com/t/8e22783d707ad140bffe18b2a3812529.svg#Digital-7") format("svg");
  }
*{
    padding:0;
    margin: 0;
    box-sizing: border-box;
}
h1 {
    padding: 0.5vh;
}


#root {
    display: flex;
    height: 90vw;
}

#pomodoroBody{
    margin: auto;
    border-radius: 10px;
    background-color: gray;
    max-width: 400px;
    text-align: center;
    font-family: digital;
    padding: 10px;
}


i {
    padding: 1vh;
    font-size: 1.5em;
    color: white;
}
            
          
!
            
              const Header = () => <h1>Pomodoro Clock</h1>

      /*depending on props break or session, Settimeout generate title*/
      const SetTimer = ({type, value, handleClick}) => (
        <div className = 'SetTimer'>
          <div id={`${type}-label`}>{`${type == 'session' ? 'Session ': 'Break '}Length`}</div>
            <div className = 'SetTimer-controls'>
              <a id={`${type}-decrement`} onClick={() => handleClick(false,`${type}Value`)}><i className="fas fa-minus-circle"></i></a>
              <h1 id={`${type}-length`}>{value}</h1>
              <a id={`${type}-increment`} onClick={() => handleClick(true, `${type}Value`)}><i className="fas fa-plus-circle"></i></a>
            </div>
        </div>
      )

      /*conditional output*/
      const Timer = ({mode, time }) =>(
        <div className='Timer'>
          <h1 id="timer-label">{mode == 'session' ? 'Session' : 'Break'}</h1>
          <h1 id="time-left">{time}</h1>
        </div>
      ) 

      /*contorls rendering buttons for pause start reset*/
      const Controls = ({active, handleReset, handlePlayPause}) => (
        <div className='Controls'>
          <a id="start_stop" onClick = {handlePlayPause}>
            {active ? <span><i className="fas fa-pause-circle"></i></span>:<span><i className="fas fa-play"></i></span>}
          </a>
          <a id="reset" onClick= {handleReset}><i className="fas fa-redo"></i></a>
        </div>
      )


      class App extends React.Component {
      constructor(props) {
          super(props);
          this.state = {
            breakValue: 5,
            sessionValue: 25,
            mode:'session',
            time: 25*60*1000, //25min
            active: false,
            touched: false
          }    
        }

        componentDidUpdate(prevProps, prevState){
            if(prevState.time === 0 && prevState.mode === 'session'){
                this.setState({time : this.state.breakValue * 60 * 1000, mode: 'break'})
                this.audio.play()
            }
            if(prevState.time === 0 && prevState.mode === 'break'){
              this.setState({time : this.state.sessionValue * 60 * 1000, mode: 'session'})
              this.audio.play()
            }
        }

        handleReset = ()=> {
          this.setState({ breakValue: 5, sessionValue: 25, time: 25*60*1000,mode:'session', active: false, touched: false})
          clearInterval(this.pomodoro)
          this.audio.pause()
          this.audio.currentTime = 0
        }

        handlePlayPause = () => {
          //if active is true (pause button) then stop timer, and show play button
          if(this.state.active){
              clearInterval(this.pomodoro)
              this.setState({active: false})
          //if active is false (play) then check for touched state
          } else {
              //if touched is true continue to countdown and set active to pause
              if(this.state.touched){
                  this.pomodoro = setInterval(() => this.setState({time: this.state.time - 1000}), 1000);
                  this.setState({active: true})
              //if touchen is false set users session(not a default), and start countdown, touched true active(pause button)
              }else {
                  this.setState({
                      time: this.state.sessionValue *60 * 1000, 
                      touched: true, 
                      active: true}, ()=>this.pomodoro = setInterval(() => this.setState({time: this.state.time - 1000}), 1000))
              }
          }  
        }

        handleSetTimers = (inc, type) =>{
          //stop function if state is of type is 60 and add button was clicked
          if(this.state[type] == 60 && inc) return
          //stop function if state is of type is 1 and minus button was clicked
          if(this.state[type] == 1 && !inc) return
          //change state from type break / session, same type state if it is currently inc true add increment for one if false decrement for one
          this.setState({ [type] : this.state[type] + (inc ? 1: -1) })
        } 

        render() {
          return (
            <div id="pomodoroBody">
              <Header/>
              <Timer mode= {this.state.mode} time ={moment(this.state.time).format('mm:ss')}/>

              <Controls 
                  active={this.state.active} 
                  handleReset={this.handleReset} 
                  handlePlayPause={this.handlePlayPause}
              /> 

              <div className= 'settings'>
                <SetTimer type = 'break' value = {this.state.breakValue} handleClick = {this.handleSetTimers}/>
                <SetTimer type = 'session' value = {this.state.sessionValue} handleClick = {this.handleSetTimers}/>
              </div>
              
              <audio id='beep' 
                src='https://s3-us-west-1.amazonaws.com/benjaminadk/Data+synth+beep+high+and+sweet.mp3'  
                //reference to actual DOM element
                ref = {ref => this.audio = ref}>
              </audio>
            </div>
          );
        }
      }


      ReactDOM.render(<App date={new Date()} /> ,document.getElementById('root')); 
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console