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="app"></div>
              
            
!

CSS

              
                * {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  user-select: none;
}

@import url('https://fonts.googleapis.com/css2?family=Inconsolata:wght@300&display=swap');

#main-container {
  position: relative;
  margin: 24px auto;
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  width: 900px + (6 * 12) + (6 * 8);
  overflow: hidden;
  
  @media screen and (max-width: 768px) {
    width: 100%;
    margin: 0;
    padding: 0;
  }
}

#break-label, #session-label, #timer-label {
  color: #ffffff;
  text-align: center;
  font-size: 1.5rem;
  font-weight: 500;
  padding: 8px 12px;
  border-radius: 10px;
}

#timer-label {
  font-size: 2.5rem;
}

#break-length, #session-length, #time-left {
  background: #ffffff;
  font-family: 'Inconsolata';
  text-align: center;
  font-size: 1.2rem;
  padding: 8px 0;
  margin: 8px 0;
  border-radius: 10px;
  width: 300px;
  
  @media screen and (max-width: 768px) {
    width: 100%;
  }
}

#time-left {
  font-size: 2.5rem;
}

#controls {
  position: relative;
  display: flex;
  justify-content: space-around;
  width: 100%;
  margin: 8px 0 2px 0;
  
  #break-label, #session-label {
    width: 100%;
  }
  
  #break-length, #session-length {
    width: 50px;
  }
  
  .timer {
    flex-direction: row;
  }
}

.timer {
  position: relative;
  background: #0000ff;
  border: solid 2px #00aa00;
  border-radius: 8px;
  display: flex;
  align-items: center;
  flex-direction: column;
  flex-wrap: wrap;
  margin: 0 8px;
  padding: 8px;
  
  @media screen and (max-width: 768px) {
    width: 100%;
    margin: 8px 4px;
  }
}

.button {
  position: relative;
  background: #00aa00;
  color: #ffffff;
  font-weight: 500;
  cursor: pointer;
  margin: 4px auto;
  padding: 12px;
  border-radius: 12px;
  transition: linear 50ms;
  text-align: center;
}

.button-disabled {
  position: relative;
  background: lightgray;
  color: #ffffff;
  font-weight: 500;
  cursor: pointer;
  display: block;
  margin: 4px auto;
  padding: 12px;
  border-radius: 12px;
  transition: linear 50ms;
  text-align: center;
}

.button:active {
  transition: linear 50ms;
  transform: scale(0.98);
}

.hidden {
  display: none;
}
              
            
!

JS

              
                import * as React from "https://cdn.skypack.dev/react@17.0.1";
import * as ReactDOM from "https://cdn.skypack.dev/react-dom@17.0.1";
import * as redux from "https://cdn.skypack.dev/redux@4.1.2";
import * as reactRedux from "https://cdn.skypack.dev/react-redux@7.2.6";

const App = document.getElementById('app');

// Redux
const ACTION = {
  BREAK_INCREMENT: 'BREAK_INCREMENT',
  BREAK_DECREMENT: 'BREAK_DECREMENT',
  SESSION_INCREMENT: 'SESSION_INCREMENT',
  SESSION_DECREMENT: 'SESSION_DECREMENT',
  COUNTING: 'COUNTING',
  NOT_COUNTING: 'NOT_COUNTING',
  RESET: 'RESET'
}

const increment = (incType) => {
  if (incType == 'break') {
    return { type: ACTION.BREAK_INCREMENT }
  } else {
    return { type: ACTION.SESSION_INCREMENT }
  }
}

const decrement = (decType) => {
  if (decType == 'break') {
    return { type: ACTION.BREAK_DECREMENT }
  } else {
    return { type: ACTION.SESSION_DECREMENT }
  }
}

const counting = (count) => {
  if (count == true) {
    return { type: ACTION.COUNTING }
  } else {
    return { type: ACTION.NOT_COUNTING }
  }
}

const reset = () => {
  return {
    type: ACTION.RESET
  }
}

const breakReducer = (state = 5, action) => {
  switch (action.type) {
    case ACTION.BREAK_INCREMENT:
      return state + 1;
    case ACTION.BREAK_DECREMENT:
      return state - 1;
    case ACTION.RESET:
      return state = 5;
    default:
      return state;
  }
};

const sessionReducer = (state = 25, action) => {
  switch (action.type) {
    case ACTION.SESSION_INCREMENT:
      return state + 1;
    case ACTION.SESSION_DECREMENT:
      return state - 1;
    case ACTION.RESET:
      return state = 25;
    default:
      return state;
  }
};

const countingReducer = (state = false, action) => {
  switch (action.type) {
    case ACTION.COUNTING:
      return state = true;
    case ACTION.NOT_COUNTING:
      return state = false;
    default:
      return state;
  }
}

const rootReducer = redux.combineReducers({
  break: breakReducer,
  session: sessionReducer,
  counter: countingReducer
})

const store = redux.createStore(rootReducer);
const Provider = reactRedux.Provider;
const connect = reactRedux.connect;

const mapStateToProps = (state) => {
  return {
    break: state.break,
    session: state.session,
    counter: state.counter
  }
}

const mapDispatchToProps = (dispatch) => {
  return {
    increment: (incType) => {
      dispatch(increment(incType));
    },
    decrement: (decType) => {
      dispatch(decrement(decType));
    },
    reset: () => {
      dispatch(reset());
    },
    counting: (count) => {
      dispatch(counting(count))
    }
  }
}

// React Components
const Break = (props) => {
  const enUp = 'button fas fa-chevron-up';
  const disUp = 'button-disabled fas fa-chevron-up';
  const enDown = 'button fas fa-chevron-down';
  const disDown = 'button-disabled fas fa-chevron-down';
  
  return (
    <div className="timer">
      <p id="break-label">Break Length</p>
      <i onClick={ props.break >= 60 || props.counter ? '' : props.breakIncrement } className={ props.break >= 60 || props.counter ? disUp : enUp } id="break-increment"></i>
      <p id="break-length">{ props.checkTime(props.break) }</p>
      <i onClick={ props.break <= 1 || props.counter ? '' : props.breakDecrement } className={ props.break <= 1 || props.counter ? disDown : enDown } id="break-decrement"></i>
    </div>
  );
}

const Session = (props) => {
  const enUp = 'button fas fa-chevron-up';
  const disUp = 'button-disabled fas fa-chevron-up';
  const enDown = 'button fas fa-chevron-down';
  const disDown = 'button-disabled fas fa-chevron-down';
  
  return (
    <div className="timer">
      <p id="session-label">Session Length</p>
       <i onClick={ props.session >= 60 || props.counter ? '' : props.sessionIncrement } className={ props.session >= 60 || props.counter ? disUp : enUp } id="session-increment"></i>
      <p id="session-length">{ props.checkTime(props.session) }</p>
      <i onClick={ props.session <= 1 || props.counter ? '' : props.sessionDecrement } className={ props.session <= 1 || props.counter ? disDown : enDown } id="session-decrement"></i>
    </div>
  );
}

const StopReset = (props) => {
  return (
    <div className="timer">
      <p id="timer-label">{ props.isSession ? 'Session' : 'Break' }</p>
      <p id="time-left">{ props.timeLeft != 0 ? props.timeLeft : props.setTime(props.session) }</p>
      <i onClick={ props.session != 0 && props.break != 0 ? props.playBtn : '' } className={ props.session != 0 && props.break != 0 ? 'button fas fa-play' : 'button-disabled fas fa-play' } id="start_stop"></i>
      <i onClick={ props.reset } className="button fas fa-arrow-rotate-left" id="reset"></i>
    </div>
  );
}

class AppMain extends React.Component {
  constructor(props) {
    super(props),
    this.state = {
      interval: '',
      timer: 0,
      left: 0,
      count: 0,
      startStop: false,
      isSession: true
    },
    this.increment = this.increment.bind(this);
    this.decrement = this.decrement.bind(this);
    this.counting = this.counting.bind(this);
    this.reset = this.reset.bind(this);
    this.checkNumber = this.checkNumber.bind(this);
    this.setValue = this.setValue.bind(this);
    this.countdown = this.countdown.bind(this);
    this.countdownBreak = this.countdownBreak.bind(this);
    this.playBtn = this.playBtn.bind(this);
  }
  
  checkNumber(number) {
    return number >= 60 ? '❎60' : number <= 0 ? '❎0' : number;
  }
  
  increment(event) {
    if (event.target.id.match('break')) {
      this.props.increment('break');
    } else {
      this.props.increment('session');
    }
  }
  
  decrement(event) {
    if (event.target.id.match('break')) {
      this.props.decrement('break');
    } else {
      this.props.decrement('session');
    }
  }
  
  counting(value) {
    this.props.counting(value);
  }
  
  reset() {
    let audio = document.getElementById('beep');
    audio.currentTime = 0;
    audio.pause();
    this.props.reset();
    this.setState({
      timer: 0,
      left: 0,
      count: 0,
      startStop: false,
      isSession: true
    });
    clearInterval(this.state.interval);
    this.counting(false);
  }
  
  setTime(time) {
    let number = new Date();

    number.setMinutes(time)
    let minutes = Math.floor(number % (1000 * 60 * 60) / (1000 * 60));
    minutes = minutes < 10 ? "0" + minutes : minutes;
    minutes = time * 60 > 3599 ? time : minutes;

    number.setSeconds(time * 60);
    let seconds = Math.floor(number % (1000 * 60) / 1000);
    seconds = seconds < 10 ? "0" + seconds : seconds;

    return minutes + ':' + seconds;
  }
  
  setValue(value) {
    this.setState({ timer: value });
  }

  countdown(session = this.props.session) {
    let time = session;
    let audio = document.getElementById('beep');
    audio.volume = .25;
    time *= 60;
    
    this.setState(state => ({
      left: time - this.state.count
    }));
    
    this.setState(state => ({
      left: state.left /= 60
    }));

    if (this.state.left <= 0) {
      audio.play();
      clearInterval(this.state.interval);
      this.setState({
        interval: setInterval(this.countdownBreak, 1000),
        isSession: false,
        count: 0
      })
    }

    console.log(this.setTime(this.state.left));
    this.setState(state => ({
      timer: this.setTime(this.state.left),
      count: state.count += 1
    }))
  }
  
  countdownBreak(session = this.props.break) {
    let time = session;
    let audio = document.getElementById('beep');
    audio.volume = .25;
    time *= 60;
    
    this.setState(state => ({
      left: time - this.state.count
    }));
    
    this.setState(state => ({
      left: state.left /= 60
    }));

    if (this.state.left <= 0) {
      audio.play();
      clearInterval(this.state.interval);
      this.setState({
        interval: setInterval(this.countdown, 1000),
        isSession: true,
        count: 0
      })
      console.log('Finished.');
    }

    console.log(this.setTime(this.state.left));
    this.setState(state => ({
      timer: this.setTime(this.state.left),
      count: state.count += 1
    }))
  }

  playBtn() {
    if (this.state.session <= 1 || this.state.break <= 1) {
      return console.log('Countdown can\'t start at 0.')
    }
    
    if (this.state.startStop != false) {
      console.log('Stopped')
      clearInterval(this.state.interval);
      this.setState(state => ({
        interval: '',
        startStop: false
      }));
      this.counting(false);
      return;
    }

    if (this.state.isSession != true) {
      console.log('Started Break')
      this.setState(state => ({
        interval: setInterval(this.countdownBreak, 1000),
        startStop: true,
      }));
      this.counting(true);
      return;
    }
    
    console.log('Started Session')
    this.setState(state => ({
      interval: setInterval(this.countdown, 1000),
      startStop: true
    }));
    this.counting(true);
  }

  render() {
    return (
      <div id="main-container">
        <div id="controls">
          <Break
            checkTime={ this.checkNumber }
            break={ this.props.break }
            breakIncrement={ this.increment }
            breakDecrement={ this.decrement }
            counter={ this.props.counter } />

          <Session
            checkTime={ this.checkNumber }
            session={ this.props.session }
            sessionIncrement={ this.increment }
            sessionDecrement={ this.decrement }
            counter={ this.props.counter } />
        </div>
        
        <StopReset
          session={ this.props.session }
          break={ this.props.break }
          reset={ this.reset }
          setTime={ this.setTime }
          playBtn={ this.playBtn }
          timeLeft={ this.state.timer }
          isSession={ this.state.isSession } />
        
        <audio id="beep" src="https://raw.githubusercontent.com/freeCodeCamp/cdn/master/build/testable-projects-fcc/audio/BeepSound.wav"></audio>
      </div>
    );
  }
}

const ReduxContainer = connect(mapStateToProps, mapDispatchToProps)(AppMain);

class AppRedux extends React.Component {
  render() {
    return (
      <Provider store={ store }>
        <ReduxContainer />
      </Provider>
    );
  }
}

ReactDOM.render(<AppRedux />, App);
              
            
!
999px

Console