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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                <div id='root'></div>

              
            
!

CSS

              
                

.fas{
  transform : scale(1.5);
}
.fas:hover{
  cursor:pointer;
  transform:scale(1.8)
}
.box{
  border:2px transparent solid;
  border-radius:100px;
  padding-bottom:15px;
  width:30%;
  color:white;
  margin:auto;
  background-color:#262626;
  margin-top:10px;
}

.main{
  border:2px solid transparent;
  border-radius: 40%;
  width: max-content;
  padding:40px;
  margin:auto;
  margin-top:10px;
  margin-bottom:10px;
  background-color:#969696;
  font-weight:bold;
  color:white;
   font-family: 'Orbitron', sans-serif;
  font-size:30px;

}

button[id='reset']{
  margin-top:10px
}
              
            
!

JS

              
                const Break = (props) => {
  
 
  const breakLengthInMinutes = moment.duration(props.breakLength, 's').asMinutes();
  return(
    <div className='container'>
    <div className='column box'>
    <div className='row-2 text-center' id='break-label'>Break</div>
    <div className='row-4'>  
    <div className='row'>
      <div className='col-4'></div>
      <div className='col-1 text-center'><i className="fas fa-arrow-alt-circle-up" id='break-increment' onClick={props.increaseBreak}></i></div>
      <div className='col-2 text-center' id='break-length'>{breakLengthInMinutes}</div>
      <div className='col-1 text-center'><i className="fas fa-arrow-alt-circle-down" id='break-decrement' onClick={props.decreaseBreak}></i></div>
      <div className='col-4'></div>
    </div>
    </div>
    </div>
    </div>
  )
}


const Session = (props) => {
  const sessionlengthInMinutes = moment.duration(props.sessionLength,'s').asMinutes();
  return(
    <div className='container'>
    <div className='column box'>
    <div className='row-2 text-center' id='session-label'>Session</div>
    <div className='row-4'>  
    <div className='row'>
      <div className='col-4'></div>
      <div className='col-1 text-center' ><i className="fas fa-arrow-alt-circle-up" id='session-increment' onClick={props.increaseSession}></i></div>
      <div className='col-2 text-center' id='session-length'>{sessionlengthInMinutes}</div>
      <div className='col-1 text-center'><i className="fas fa-arrow-alt-circle-down" id='session-decrement' onClick={props.decreaseSession}></i></div>
      <div className='col-4'></div>
    </div>
    </div>
    </div>
    </div>
  )
}




momentDurationFormatSetup(moment);

const TimeLeft = (props) => {

    const  formattedTimeLeft = moment.duration(props.timeLeft, 's').format('mm:ss', {trim:false});
    return(
        <div className='text-center'>
        <p id='timer-label'>{props.timerLabel}</p>
        <p  className='text-center' id='time-left'>{formattedTimeLeft}</p>
        <button className='btn btn-success' id='start_stop' onClick={props.handleStartStopClick}>{props.startStopButtonLabel}</button>
        </div>
    )
}




const App = () => {
  const audioElement = React.useRef(null);
    const [sessionLength, setSessionLength] = React.useState(25*60);
    const [breakLength, setBreakLength] = React.useState(300);
    const [currentSessionType, setCurrentSessionType] = React.useState('Session');
    const [timeLeft, setTimeLeft] = React.useState(sessionLength);    
    const [intervalId, setIntervalId] = React.useState(null);
  
   
   const decreaseSession = () => {
        const newSessionLength = sessionLength - 60;
        if(newSessionLength>0){
          setSessionLength(newSessionLength);
        }
    };

  const increaseSession = () => {
    const newSessionLength = sessionLength + 60;
    if(newSessionLength <= 60*60){
      setSessionLength(newSessionLength);
    }
  }
   
  const increaseBreak = () => {
    const newBreakLength = breakLength + 60;
    if(newBreakLength <= 60*60){
      setBreakLength(newBreakLength);
    }
  }
  
  const decreaseBreak = () => {
    var newBreakLength = breakLength - 60;
    if(newBreakLength>0){
      setBreakLength(newBreakLength);
    }
  }
  
   React.useEffect(() => {
      setTimeLeft(sessionLength)
    },[sessionLength]);

  React.useEffect(() => {
    if(timeLeft === 0){
      audioElement.current.play();
      if(currentSessionType === 'Session'){
        setCurrentSessionType('Break');
        setTimeLeft(breakLength);
      }else if(currentSessionType = 'Break'){
        setCurrentSessionType('Session');
        setTimeLeft(sessionLength);
      }
    }
  }, [timeLeft, currentSessionType, sessionLength, breakLength])
  
  const isStarted = intervalId !== null;
  const handleStartStopClick = () => {
    if(isStarted){
      clearInterval(intervalId);
      setIntervalId(null);
    }else{
      const newIntervalId = setInterval(() => {
         setTimeLeft(prevTimeLeft => prevTimeLeft - 1)         
    },100);
      setIntervalId(newIntervalId);
    }
    
  }
  
  const handleResetButtonClick = () =>{
    audioElement.current.load();
    clearInterval(intervalId);
    setIntervalId(null);
    setCurrentSessionType('Session');
    setSessionLength(60*25);
    setBreakLength(60*5);
    setTimeLeft(60*25);
    
  }

    return(
      <div className='text-center'>
        <Break 
          breakLength={breakLength}
          increaseBreak={increaseBreak}
          decreaseBreak={decreaseBreak}/>
        <div className='main'>
        <TimeLeft 
         handleStartStopClick={handleStartStopClick}
        timerLabel={currentSessionType}
        startStopButtonLabel={isStarted ? 'Stop' : 'Start'}
        timeLeft={timeLeft}/>
         </div>
        
        <Session 
          sessionLength={sessionLength}
          increaseSession={increaseSession}
          decreaseSession={decreaseSession}/>
        
        <button className='btn btn-danger' id='reset' onClick={handleResetButtonClick}>Reset</button>
        <audio id='beep' ref={audioElement}>
          <source src='https://onlineclock.net/audio/options/default.mp3' type='audio/mpeg' />
        </audio>
      </div>
    )
  }
 



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

Console