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

              
                @import url('https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css');
@import url('https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans&display=swap');


*{
    margin: 0;
    padding: 0;
}

#app {
    width: 100vw;
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    font-family: 'Plus Jakarta Sans', sans-serif;
    
    background: linear-gradient(330deg, #1c075e, #4c2fab, #6c0042);
    background-size: 600% 600%;
    -webkit-animation: animation 12s ease infinite;
    -moz-animation: animation 12s ease infinite;
    -o-animation: animation 12s ease infinite;
    animation: animation 12s ease infinite;
}

@-webkit-keyframes animation {
    0%{background-position:0% 97%}
    50%{background-position:100% 4%}
    100%{background-position:0% 97%}
}
@-moz-keyframes animation {
    0%{background-position:0% 97%}
    50%{background-position:100% 4%}
    100%{background-position:0% 97%}
}
@-o-keyframes animation {
    0%{background-position:0% 97%}
    50%{background-position:100% 4%}
    100%{background-position:0% 97%}
}
@keyframes animation {
    0%{background-position:0% 97%}
    50%{background-position:100% 4%}
    100%{background-position:0% 97%}
}

h1{
    font-size: 50px;
    margin-bottom: .5em;
    color: #FFE35A;
}
#clock {
    color: white;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}
#time-left{
    font-size: 100px;
}
#config{
    display: flex;
    align-items: center;
    justify-content: center;
}
#config > div{
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}
.button{
    background-color: rgba(255, 255, 255, 0);
    font-size: 40px;
    padding: .25em;
    color: white;
}
.configBtn{
    display: flex;
    align-items: center;
    justify-content: center;
    margin: .5em;
}
.configBtn > div{
    padding: .25em;
    font-size: 40px;
}
#display{
    width: 500px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    border: 1px white solid;
    border-radius: 10px;
    margin-bottom: 2em;
    padding: 10px;
    background-color: #00000042;
    box-shadow: rgba(0, 0, 0, 0.35) 0px 5px 15px;
}
#display > div{
    font-size: 150px;
    width: 100%;
    text-align: center;
}
#display > h3{
    font-size: 50px;
    text-transform: capitalize;
    text-align: center;
}
#controls > button{
    margin: 0 .25em .5em .25em;
}
#session-label,
#break-label{
    text-transform: capitalize;
    font-size: 20px;
}
#session-length,
#break-length{
    width: 1.5em;
    text-align: center;    
}
#config > div{
    margin: 0 2em;
}
button{
    border: 1px white solid;
    border-radius: 10px;
    transition: all 0.2s;
    background-color: #00000042;
}
button:hover{
    border-radius: 0;
    background-color: #311491;
    
    box-shadow: rgba(0, 0, 0, 0.35) 0px 5px 15px;
}

@media (max-width: 540px) {
    #display{
        width: 95%;
    }
    #display > div{
        font-size: 90px;
    }
    #config{
        flex-direction: column;
    }    
}
              
            
!

JS

              
                const accurateInterval = function (fn, time) {
  var cancel, nextAt, timeout, wrapper;
  nextAt = new Date().getTime() + time;
  timeout = null;
  wrapper = function () {
    nextAt += time;
    timeout = setTimeout(wrapper, nextAt - new Date().getTime());
    return fn();
  };
  cancel = function () {
    return clearTimeout(timeout);
  };
  timeout = setTimeout(wrapper, nextAt - new Date().getTime());
  return {
    cancel: cancel
  };
};
const secondsToTime = (seconds) => {
  let minutes = Math.floor(seconds / 60);
  let segundos = seconds - minutes * 60;
  if(segundos < 10){
      segundos = '0'+segundos.toString();
  }
  if(minutes < 10){
      minutes = '0'+minutes.toString()
  }
  return((minutes+':'+segundos).toString())
}
const inicialBreakTime = 5;
const inicialSessionTime = 25;
const TIC = 1000;
class Clock extends React.Component {
  constructor(props){
    super(props);
    this.state = {
      break_time: inicialBreakTime,
      session_time: inicialSessionTime,
      time: inicialSessionTime*60,
      counting: false,
      intervalID: '',
      stage: 'session'
    }
    this.decreaseCount = this.decreaseCount.bind(this);
    this.handleClick = this.handleClick.bind(this);
    this.startStopCount = this.startStopCount.bind(this);
    this.breakSessionSwitch = this.breakSessionSwitch.bind(this);
    this.resetClock = this.resetClock.bind(this);
    this.sessionIncrementDecrement = this.sessionIncrementDecrement.bind(this);
    this.breakIncrementDecrement = this.breakIncrementDecrement.bind(this);
    this.notification = this.notification.bind(this);
    this.startCount = this.startCount.bind(this);
    this.phaseControl = this.phaseControl.bind(this);
  }


  decreaseCount(){
    this.setState((state,props)=>({time: state.time-1}));
  }

  startStopCount(){
    let { counting } = this.state;
    if(!counting){
      this.startCount();
      this.setState({counting: true});
    }else{
      this.setState(()=>({counting: false}));
      if (this.state.intervalID) {
        this.state.intervalID.cancel();
      }
    }
  }
  startCount(){
    this.setState({
      intervalID: accurateInterval(() => {
        this.decreaseCount();
        this.phaseControl();
      }, TIC),
      counting: true,
    })
  }
  phaseControl() {
    let time = this.state.time;
    if (time === -1) {
      if (this.state.intervalID) {
        this.state.intervalID.cancel();
      }
      if(this.state.stage === 'session'){
        this.breakSessionSwitch('session');
      }else{
        this.breakSessionSwitch('break');
      }
    }
    if(!this.state.counting){
      if (this.state.intervalID) {
        this.state.intervalID.cancel();
      }
    }
  }
  breakSessionSwitch(stage){
    if(stage === 'session'){
      let { break_time } = this.state;
      this.notification('break');
      this.setState(()=>(
      {
        stage: 'break',
        time: break_time*60
      }));
      this.startCount();
      return;
    }
    if(stage === 'break'){
      let { session_time } = this.state;
      this.notification('session');
      this.setState(()=>(
      {
        stage: 'session',
        time: session_time*60
      }));
      this.startCount();
      return;
    }
  }
  resetClock(){
    if(this.state.intervalID){
      this.state.intervalID.cancel();
    }
    this.setState({
      break_time: inicialBreakTime,
      session_time: inicialSessionTime,
      time: inicialSessionTime*60,
      counting: false,
      intervalID: '',
      stage: 'session'
    }); 
  }
  notification(type){
    let audioSrc = '' 
    if(type === 'break'){
      audioSrc = 'https://www.myinstants.com/media/sounds/ding-sound-effect_2.mp3'
    }else if(type === 'session'){
      audioSrc = 'https://www.myinstants.com/media/sounds/spongebob-disappointed-sound-effect.mp3'
    }else if(!this.audioBeep.paused && type === 'stop'){
      this.audioBeep.pause();
      this.audioBeep.currentTime = 0;
    }else{return}
    
    if(type!=='stop' ){
      this.audioBeep.setAttribute(
        'src', audioSrc 
      )
      this.audioBeep.volume = 0.5;
      this.audioBeep.currentTime = 0;
      this.audioBeep.play();
    }
  }
  sessionIncrementDecrement(id){
    if(id === 'session-increment'){
      this.setState((prevState, props)=>({
        session_time: ((prevState.session_time<60)?prevState.session_time + 1:60)
      }));
    }else if(id === 'session-decrement'){
      this.setState((prevState, props)=>({
        session_time: (prevState.session_time>1)?(prevState.session_time - 1):1
      }));
    }
    if(this.state.stage === 'session'){
      this.setState((prevState, props)=>({
        time: prevState.session_time*60
      }));
    }
    return;
  }
  breakIncrementDecrement(id){
    if(id === 'break-increment'){

      this.setState((prevState, props)=>({
        break_time: ((prevState.break_time < 60)?prevState.break_time + 1:60),
      }));
    }else if(id === 'break-decrement'){
      this.setState((prevState, props)=>({
        break_time: ((prevState.break_time>1)?(prevState.break_time - 1):1),
      }));
    }
    if(this.state.stage === 'break'){
      this.setState((prevState, props)=>({
        time: prevState.break_time*60
      }));
    }
    return
  }

  handleClick(id){
    console.log(`=>${id}`);
    switch (id) {
      case 'start_stop':
        this.startStopCount();
        break;
      case 'reset':
        this.notification('stop');
        this.resetClock();
        break;
      case 'session-increment':
      case 'session-decrement':
        if(this.state.counting) return;
        this.sessionIncrementDecrement(id);
        break;
      case 'break-increment':
      case 'break-decrement':
        if(this.state.counting) return;
        this.breakIncrementDecrement(id);
        break;
    
      default:
        break;
    }
  }

  render(){
    return(
      <div id='clock'>
        <audio
        id='beep'
        ref={(audio) => {
          this.audioBeep = audio;
        }}
        ></audio>
        <h1>25 + 5 clock</h1>
        <Display 
          time={this.state.time}
          stage={this.state.stage}
        />
        <Controls
          handleClick={this.handleClick}
          counting={this.state.counting}
        />
        <Config 
          handleClick={this.handleClick}
          session_time={this.state.session_time}
          break_time={this.state.break_time}
        />
      </div>
    )
  }
}
function Controls(props){
  return(
    <div
    id='controls'
    >
      <Button
        handleClick={props.handleClick}
        className={(props.counting)?'fa fa-pause':'fa fa-play'}
        id='start_stop'/>
      <Button
        handleClick={props.handleClick}
        className={'fa fa-refresh'}
        id='reset'/>
    </div>
  )
}
function Display(props){
  return(
    <div id='display'>
      <div
      id='time-left'
      >{secondsToTime(props.time)}</div>
      <h3
      id='timer-label'
      >{props.stage}</h3>
    </div>
  )
}
function Config(props){
  return(
  <div id='config'>
    <div>
      <h3
      id='session-label'
      >session time:</h3>
      <div
      className='configBtn'
      id='sessionConfig'>
        <Button
          handleClick={props.handleClick}
          className='fa fa-minus'
          id='session-decrement'/>
        <div
        id='session-length'
        >{props.session_time}</div>
        <Button
          handleClick={props.handleClick}
          className='fa fa-plus'
          id='session-increment'/>
      </div>
    </div>
    <div>
      <h3
      id='break-label'
      >break time:</h3>
      <div
      className='configBtn'
      id='breakConfig'>
        <Button
          handleClick={props.handleClick}
          className='fa fa-minus'
          id='break-decrement'/>
        <div
        id='break-length'
        >{props.break_time}</div>
        <Button
          handleClick={props.handleClick}
          className='fa fa-plus'
          id='break-increment'/>
      </div>
    </div>
  </div>
  )
}
class Button extends React.Component{
  render(){
    return(
    <button
      onClick={()=>this.props.handleClick(this.props.id)}
      className={`button ${this.props.className}`}
      id={this.props.id}
      >
    </button>
    )
  }
}
function App(){
  return(
    <div id='app'>
      <Clock increment={60} unity={1}/>  
    </div>
  )
}
ReactDOM.render(<App />, document.getElementById('root'));
              
            
!
999px

Console