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

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

CSS

              
                @font-face {
  font-family: Gilroy;
  src: url(https://res.cloudinary.com/sh4r10/raw/upload/v1616889608/Fonts/Gilroy-Light_otfm5f.otf);
}

*{
  font-family: Gilroy, sans-serif;
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}


body{
  background: #fff;
}

.main-container{
  width: 100vw;
  height: 100vh;
}

#settings-toggle{
  position: absolute;
  top: 20px;
  right: 20px;
  cursor: pointer;
  font-size: 32px;
}

.settings-container{
  width: 300px;
  position: absolute;
  right: 2%;
  top: 5%;
  font-size: 20px;
  display: block;
  p{
    font-size: 24px;
    margin: 10px;
  }
}

.numDisplay{
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  .numContainer{
    display: flex;
    align-items: center;
    span{
      cursor: pointer;
    }
  }
}

.timer-container{
  position: absolute;
  top: 50%;
  lefT: 50%;
  transform: translate(-50%, -50%);
  width: 400px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  .timer-display{
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;  
  }
  #timer-label{
    font-size: 32px;
    width: 100%;
    text-align: center;
    margin: 10px 0;
  }
  #time-left{
    font-size: 90px;
    margin: 20px auto;
  }
  button, button:focus{
    background: none;
    border: none;
    cursor: pointer; 
    margin: 2px;
    outline: none;
    span{
      font-size: 32px;
    }
  }
}
              
            
!

JS

              
                const {createStore, combineReducers} = Redux;
const {Provider, connect} = ReactRedux;
let countdown, secondsLeft, handleTimer; //setInterval

// Redux Variables
const SESSION_LENGTH = "SESSION_LENGTH";
const BREAK_LENGTH = "BREAK_LENGTH";
const TIMER_MODE = "TIMER_MODE";
const TIMER_STATUS = "TIMER_STATUS";
const NEW_TIME = "UPDATE_TIMER";
const RESET = "RESET";
const PAUSE = "PAUSE";
const SETTINGS = "SETTINGS";

// REDUX ACTIONS 
const SessionAction = (length) => ({type: SESSION_LENGTH, length: length});
const BreakAction = (length) => ({type: BREAK_LENGTH, length: length});
const TimerModeAction = (mode) => ({type: TIMER_MODE, mode: mode});
const TimerStatusAction = (status) => ({type: TIMER_STATUS, status: status});
const UpdateTimerAction = (time, timeRemaining) => ({type: NEW_TIME, time: time, timeRemaining: timeRemaining});
const PauseAction = (val) => ({type: PAUSE, val: val});
const SettingsAction = val => ({type: SETTINGS, val: val});
const ResetAction = () => ({type: RESET});

// Initial State
const timerLengths = {
  timerLength: 25,
  breakLength: 5
}
const timerStatus = {
  currentTimer: 'session',
  countingDown: false,
  currentTime: "25:00",
  timeRemaining: 0,
  paused: false,
  showSettings: true
}

// Reducers
const timerEditReducer = (state=timerLengths, action) => {
  switch(action.type){
    case SESSION_LENGTH:
      return Object.assign({}, state, {timerLength: action.length})
    case BREAK_LENGTH:
      return Object.assign({}, state, {breakLength: action.length})
    case RESET:
      return {timerLength: 25, breakLength: 5}
    default:
      return state;
  }
}

const timerStatusReducer = (state=timerStatus, action) => {
  switch(action.type){
    case TIMER_MODE:
      return Object.assign({}, state, {currentTimer: action.mode})
    case TIMER_STATUS:
      return Object.assign({}, state, {countingDown: action.status})
    case NEW_TIME:
      return Object.assign({}, state, {currentTime: action.time, timeRemaining: action.timeRemaining})
    case PAUSE:
      return Object.assign({}, state, {paused: action.val})
    case SETTINGS:
      return Object.assign({}, state, {showSettings: action.val})
    case RESET:
      return {currentTimer: 'session', countingDown: false, currentTime: "25:00", secondsRemaining: 0, paused: false, showSettings: true}
    default:
      return state;
  }
}

//Selectors
const BreakLength = (props) => {
  setBreak = (increment) => {
    if(increment && props.length < 60){
      props.setValue(props.length+1);
    } else if(!increment && props.length > 1){
      props.setValue(props.length-1);
    }
  }
  
  return (
    <div className="numDisplay">
      <h3 id="break-label">Break Length</h3>
      <div className="numContainer">
        {!props.countingDown ? <span id="break-increment" className="material-icons" onClick={()=>setBreak(true)}>arrow_drop_up</span> : "" }
        <p id="break-length">{props.length}</p>
        {!props.countingDown ? <span id="break-decrement" className="material-icons" onClick={()=>setBreak(false)}>arrow_drop_down</span> : "" }
      </div>
    </div>
  )
}

const SessionLength = (props) => {
  setSession = (increment) => {
    if(increment && props.length < 60){
      props.setValue(props.length+1);
    } else if(!increment && props.length > 1){
      props.setValue(props.length-1);
    }
  }
  return (
    <div className="numDisplay">
      <h3 id="session-label">Session Length</h3>
      <div className="numContainer">
        {!props.countingDown ? <span id="session-increment" className="material-icons" onClick={()=>setSession(true)}>arrow_drop_up</span> : ""}
        <p id="session-length">{props.length}</p>
        {!props.countingDown ? <span id="session-decrement" className="material-icons" onClick={()=>setSession(false)}>arrow_drop_down</span> : ""}
      </div>
    </div>
  )
}


// Main Timer Component
const Timer = props => {  
  
  const getDisplayTime = (seconds) => {
    const minutes = Math.floor(seconds / 60);
    const remainderSeconds = seconds % 60;
    const displayedTime = `${minutes < 10 ? '0' : ''}${minutes}:${remainderSeconds < 10 ? '0' : ''}${remainderSeconds}`;
    props.setCurrentTime(displayedTime, seconds);
  }
  
 const interimFunction = () => {
    console.log("interim call");
    handleTimer();
 }
   
  handleTimer = () => {
    if(!props.countingDown || props.paused){
      const audio = document.getElementById("beep");
     
      const timerLength = props.paused ? props.timeRemaining : (props.currentTimer == "session") ? props.sessionLength*60:props.breakLength*60;
      const now = Date.now();
      const then = now + timerLength * 1000;
      
      props.setTimerStatus(true);
      props.setPaused(false);
      getDisplayTime(timerLength, props.timeRemaining);
      
      countdown = setInterval(() => {
          const secondsLeft = Math.round((then - Date.now())/1000);
          getDisplayTime(secondsLeft);
        
          if(secondsLeft <= 0){
            clearInterval(countdown);
            audio.play();
            getDisplayTime(0);
            setTimeout(()=>{
            props.setTimerStatus(false)
            props.setTimerMode(props.currentTimer == "session" ? "break" : "session");
            interimFunction();
            }, 1000);
          }
        
      },1000)
      
    } 
    
    else {
      props.setPaused(true);
      clearInterval(countdown);
    }                     
  }
  
  const reset = () => {
    const audio = document.getElementById("beep");
    audio.pause();
    audio.currentTime = 0;
    if(props.countingDown){
     clearInterval(countdown);
     props.resetState();
    } else{
      props.resetState();
    }    
  }
  
  const settingsToggle = () =>  {
    props.setSettingsDisplay(!props.showSettings);
  }
  
  return (
    <div className="main-container">
      <span id="settings-toggle" className="material-icons" onClick={settingsToggle}>settings</span>
      
      {props.showSettings ? <div className="settings-container">
        <BreakLength length={props.breakLength} setValue={props.setBreakLength} countingDown={props.countingDown}/>
        <SessionLength length={props.sessionLength} setValue={props.setSessionLength} countingDown={props.countingDown}/>
        </div> : ''}
        <div className="timer-container">
          <div className="timer-display">
            <h2 id="timer-label">{props.currentTimer === "session" ? "Session" : "Break"}</h2>
            <h1 id="time-left">{props.countingDown ? props.currentTime : `${props.sessionLength}:00`}</h1>
          </div> 
          <div className="timer-controls">
            <button><span className="material-icons" id="start_stop" onClick={handleTimer}>{!props.countingDown ? "play_arrow" : !props.paused ? "pause" : "play_arrow"}</span></button>
            <button><span className="material-icons" id="reset" onClick={reset}>restart_alt</span></button>
          </div>
      </div>
      <audio src="https://res.cloudinary.com/sh4r10/video/upload/v1617057276/Drum%20Kit/horn_vbux45.mp3" id="beep"/>
    </div>
  )
}

// Connecting Redux to Timer Component
const mapPropsToState = (state) => ({
  sessionLength: state.timerEdit.timerLength,
  breakLength: state.timerEdit.breakLength,
  countingDown: state.timerStatus.countingDown,
  currentTimer: state.timerStatus.currentTimer,
  currentTime: state.timerStatus.currentTime,
  paused: state.timerStatus.paused,
  timeRemaining: state.timerStatus.timeRemaining,
  showSettings: state.timerStatus.showSettings
});

const mapDispatchToState = dispatch => ({
  setSessionLength: (length) => dispatch(SessionAction(length)),
  setBreakLength: (length) => dispatch(BreakAction(length)),
  setTimerStatus: status => dispatch(TimerStatusAction(status)),
  setTimerMode: mode => dispatch(TimerModeAction(mode)),
  setCurrentTime: (time, timeRemaining) => dispatch(UpdateTimerAction(time, timeRemaining)),
  setPaused: val => dispatch(PauseAction(val)),
  setSettingsDisplay: val => dispatch(SettingsAction(val)),
  resetState: () => dispatch(ResetAction())
});

const ConnectedTimer = connect(mapPropsToState, mapDispatchToState)(Timer);

// Main Render
const rootReducer = combineReducers({timerEdit: timerEditReducer, timerStatus:timerStatusReducer});
const store = createStore(rootReducer, window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__());

const App = () => {
  return (
    <Provider store={store}>
      <div className="main-container">
        <ConnectedTimer />
      </div>
    </Provider>
  )
}

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

Console