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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

              
                <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.4.1/semantic.min.css" integrity="sha512-8bHTC73gkZ7rZ7vpqUQThUDhqcNFyYi2xgDgPDHc+GXVGHXq+xPjynxIopALmOPqzo9JZj0k6OqqewdGO3EsrQ==" crossorigin="anonymous" referrerpolicy="no-referrer" />

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

CSS

              
                * {
  margin: 0;
  padding: 0;
}

.icon {
  margin: 0 !important;
  cursor: pointer;
}

.down {
  position: relative;
  top: 0.5px;
}

#app-container {
  width: 360px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  display: flex;
  flex-direction: column;
  align-items: center;
}

.editor-container {
  display: flex;
  align-items: center;
  justify-content: center;
}

.timer > h2 {
  margin: 0;
}

.timer-buttons {
  display: flex;
  align-items: center;
  justify-content: center;
}

.editor-time {
  width: 45px;
}

#break-editor {
  margin-right: 20px;
}

#session-editor {
  margin-left: 20px;
}

#active-timer {
  width: 80% !important;
  margin-top: 20px;
  padding: 10px 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  border: 5px solid black;
  border-radius: 20px;
}

#time-left {
  font-size: 48px;
  font-weight: 800;
  margin-top: 20px;
  margin-bottom: 40px;
}

#active-timer-buttons {
  width: 60px;
  display: flex;
  justify-content: space-evenly;
  margin-bottom: 10px;
}

#start_stop {
  display: flex;
  margin-right: 8px;
}
#reset {
  margin-left: 8px;
}
              
            
!

JS

              
                const BreakEditor = props => {
  return (
    <div id="break-editor" className="timer ui center aligned">
      <h2 id="break-label">Break Length</h2>
      <div className="timer-buttons">
        <i id="break-decrement" onClick={props.reduceBreakLength} className="large arrow down icon"></i>
        <div className="editor-time">
          <h2 id="break-length">{props.breakLength}</h2>
        </div>
        <i id="break-increment" onClick={props.addBreakLength} className="large arrow up icon"></i>
      </div>
    </div>
  );
};

const SessionEditor = props => {
  return (
    <div id="session-editor" className="timer ui center aligned">
      <h2 id="session-label">Session Length</h2>
      <div className="timer-buttons">
        <i id="session-decrement" onClick={props.reduceSessionLength} className="large arrow down icon"></i>
        <div className="editor-time">
          <h2 id="session-length">{props.sessionLength}</h2>
        </div>
        <i id="session-increment" onClick={props.addSessionLength} className="large arrow up icon"></i>
      </div>
    </div>
  );
};

const Display = props => {
  const secondsToTime = secs => {
    let minutes = Math.floor(secs/60);
    let remainingSecs = secs % 60;
    minutes < 10 ? minutes = "0" + minutes : minutes;
    remainingSecs < 10 ? remainingSecs = "0" + remainingSecs : remainingSecs;
    return minutes + ":" + remainingSecs;
  }
  const displayTime = secondsToTime(props.time);
  return ( 
    <div>
      <h2 id="timer-label">{props.mode}</h2>
      <div id="time-left">{displayTime}</div>  
    </div>
  );
};

const ControlButtons = props => {
  return (
    <div id="active-timer-buttons">
      <div id='start_stop' onClick={props.handleStartStop}>
        <i className="large play icon" />
        <i className="large pause icon" />
      </div>
      <i id="reset" onClick={props.handleReset} className="large undo icon"></i>
    </div>
  );
}



const App = () => {
  const [time, setTime] = React.useState(1500);
  const [isPaused, setIsPaused] = React.useState(true);
  const [mode, setMode] = React.useState('Session');
  const [breakLength, setBreakLength] = React.useState(5);
  const [sessionLength, setSessionLength] = React.useState(25);
  
  const timeRef = React.useRef(time);
  const isPausedRef = React.useRef(isPaused);
  const modeRef = React.useRef(mode);
  
  const tick = () => {
    timeRef.current--;
    setTime(timeRef.current);
  }
  
  const beep = document.getElementById("beep");
  
  React.useEffect(() => {
    const switchMode = () => {
      beep.play();
      
      const nextMode = modeRef.current === 'Session' ? 'Break' : 'Session';
      const nextTime = (nextMode === 'Session' ? sessionLength : breakLength) * 60;
      
      console.log(nextMode + ", " + nextTime);
      
      setMode(nextMode);
      modeRef.current = nextMode;
      
      setTime(nextTime);
      timeRef.current = nextTime;
    }
    
    setTime(timeRef.current);
    
    const interval = setInterval(() => {
      if (isPausedRef.current) {
        return;
      }
      if (timeRef.current === 0) {
        return switchMode();
      }
      tick();
    }, 1000);
    
    return () => clearInterval(interval);
    
  }, [isPaused]);
  
  const handleStartStop = () => {
    console.log('clicked');
    if (isPaused) {
      setIsPaused(false);
      isPausedRef.current = false;
    }
    if (!isPaused) {
      setIsPaused(true);
      isPausedRef.current = true;
    }
  }
  
  const handleReset = () => {
    setTime(1500);
    timeRef.current = 1500;
    setIsPaused(true);
    isPausedRef.current = true;
    setBreakLength(5);
    setSessionLength(25);
    setMode('Session');
    modeRef.current = 'Session';
    beep.pause();
    beep.currentTime = 0;
  }
  
  const addSessionLength = () => {
    if (sessionLength < 60 && isPaused){
      setSessionLength(sessionLength + 1);
    }
    if (sessionLength < 60 && isPaused && mode === "Session"){
      setTime((sessionLength + 1) * 60)
      timeRef.current = (sessionLength + 1) * 60;
    }
  }
  
  const reduceSessionLength = () => {
    if (sessionLength > 1 && isPaused){
      setSessionLength(sessionLength - 1);
    }
    if (sessionLength > 1 && isPaused && mode === "Session"){
      setTime((sessionLength - 1) * 60);
      timeRef.current = (sessionLength - 1) * 60;
    }
  }
  
  const addBreakLength = () => {
    if (breakLength < 60 && isPaused){
      setBreakLength(breakLength + 1);
    }
    if (breakLength < 60 && isPaused && mode === "Break"){
      setTime((breakLength + 1) * 60);
      timeRef.current = (breakLength + 1) * 60;
    }
  }
  
  const reduceBreakLength = () => {
    if (breakLength > 1 && isPaused){
      setBreakLength(breakLength - 1);
    }
    if (breakLength > 1 && isPaused && mode === "Break"){
      setTime((breakLength - 1) * 60);
      timeRef.current = (breakLength - 1) * 60;
    }
  }
  
  return (
    <div id="app-container" className="ui center aligned container">
      <h1 className="app-header">Break Timer</h1>
      <div className="editor-container">
        <BreakEditor 
          breakLength={breakLength} 
          addBreakLength={() => addBreakLength()} 
          reduceBreakLength={() => reduceBreakLength()} 
        />
        <SessionEditor 
          sessionLength={sessionLength} 
          addSessionLength={() => addSessionLength()} 
          reduceSessionLength={() => reduceSessionLength()} 
        />
      </div>
      <div id="active-timer" className="timer ui center aligned container">
        <Display
          time={time}
          mode={mode} 
        />
        <ControlButtons
          handleStartStop={handleStartStop}
          handleReset={handleReset}
        />
      </div>
      <audio id="beep" src="https://sampleswap.org/samples-ghost/MELODIC%20LOOPS/HORN%20LOOPS/462[kb]179_acid-trumpet-bebop-down.wav.mp3" />
    </div>
  );
}

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

Console