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 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

              
                <head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
  <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
  <link rel="stylesheet" href="./index.css">
  <script src="https://unpkg.com/react@17/umd/react.development.js" crossorigin></script>
  <script src="https://unpkg.com/react-dom@17/umd/react-dom.development.js" crossorigin></script>
  <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
  <title>clock</title>
</head>

<body>
  <div id="root"></div>
  <script type="text/babel" src="./index.jsx"></script>
</body>

              
            
!

CSS

              
                :root {
  --color1: rgba(100, 200, 200, 0.5);
  --color2: rgba(10, 20, 20, 1);
  --canvas: radial-gradient(ellipse at 50% 50%, #266 0%, #011 100%);
  --back: radial-gradient(
    ellipse at 50% 50%,
    var(--color1) 0%,
    var(--color2) 100%
  );
  --box: 2px 2px 8px 4px #466;
  --text: 1px 4px 6px #0bb;
}

* {
  border-radius: 10px;
  transition: all 0.2s ease-in-out;
  color: #cff;
  text-shadow: var(--text);
  text-align: center;
}

button,
.break-wrapper,
.session-wrapper,
.timer-wrapper {
  background: var(--back);
  box-shadow: var(--box);
}

html {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  text-align: center;
  background: var(--canvas);
}

h1,
h2,
h3,
h4 {
  display: inline-block;
  text-shadow: 1px 4px 6px #000;
  background: var(--back);
  margin: auto;
}

h1 {
  min-width: 12rem;
  font-size: 4rem;
}

h2 {
  max-width: 120px;
  font-size: 2rem;
}

h3 {
  font-size: 2rem;
}

h4 {
  min-width: 4rem;
  font-size: 1.5rem;
  padding: 0.4rem 1rem;
}

.btn {
  margin: 0 0.2rem;
  border-radius: 10px;
}

.title {
  background: none;
}

.label {
  background: none;
  margin-bottom: 1rem;
}

.time {
  background: #122;
}

.container {
  display: box;
  justify-content: center;
  align-items: center;
  text-align: center;
}

.length-container {
  width: 100%;
  display: grid;
  gap: 1rem;
  grid-template-columns: 1fr 1fr;
  margin: auto;
  justify-content: center;
  align-items: center;
  margin-top: 1rem;
}

.break-wrapper {
  padding: 1rem;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.session-wrapper {
  padding: 1rem;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.time-sets {
  display: flex;
  justify-content: space-evenly;
  align-items: center;
}

.time-container {
  width: 50%;
  display: grid;
  grid-template-columns: 1fr;
  margin: auto;
  justify-content: center;
  align-items: center;
  margin-top: 1rem;
}

.timer-wrapper {
  padding: 1rem;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.timer {
  display: flex;
  justify-content: space-evenly;
  align-items: center;
  margin-top: 1rem;
}

#time-left {
  margin-top: 1rem;
}

#break-label,
#session-label,
#timer-label {
  width: 150px;
}

              
            
!

JS

              
                // component 'App' -> element 'root'
function App() {

  // default times = test ? [5/25/25]s : [5/25/25]m;
  const test = false;
  // logging
  const log = (...args) => console.log(...args);

  // constants
  const SECONDS = 60;
  const BREAK   = test ? 10 :  5 * SECONDS;
  const SESSION = test ? 15 : 25 * SECONDS;
  const DISPLAY = test ? 15 : 25 * SECONDS;

  // variables
  const [breakTime, setBreakTime] = React.useState(BREAK);
  const [sessionTime, setSessionTime] = React.useState(SESSION)
  const [displayTime, setDisplayTime] = React.useState(DISPLAY);
  const [timerOn, setTimerOn] = React.useState(false);
  const [isBreak, setIsBreak] = React.useState(false);
  const [sound, setSound] = React.useState(null);

  function updateDisplayTime(time) {
    setDisplayTime(time);
  }

  // displayTime = breakTime
  React.useEffect(() => {
    if (isBreak) {
      updateDisplayTime(breakTime);
    }
  }, [breakTime]);

  // displayTime = sessionTime
  React.useEffect(() => {
    if (!isBreak) {
      updateDisplayTime(sessionTime);
    }
  }, [sessionTime]);

  React.useEffect(() => {
    setSound(document.getElementById('beep'));
  }, []);

  const playSound = () => {
    var audio = sound;
    audio.id = 'beep';
    audio.src = 'https://assets.coderrocketfuel.com/pomodoro-times-up.mp3';
    audio.currentTime = 0;
    audio.play();
  }

  // seconds -> [(m)m]
  const formatTimeMin = (time) => {
    return Math.floor(time / 60);
  }

  // seconds -> [mm:ss]
  const formatTime = (time) => {
    let minutes = Math.floor(time / 60);
    let seconds = time % 60;
    return (
      (minutes < 10 ? "0" + minutes : minutes) +
      ":" +
      (seconds < 10 ? "0" + seconds : seconds)
    );
  }

  const capitalise = (str) => {
    return str.charAt(0).toUpperCase() + str.slice(1);
  }

  // displayTime = !timerOn ? 0 : onBreak ? breakTime : sessionTime
  const changeTime = (change, title) => {
    if (!timerOn) {
      if (title == "break") {
        // breakTime == [1,60]m
        if (breakTime + change <= 0 || breakTime + change > 60 * 60) {
          return;
        }
        setBreakTime(prev => prev + change);
      } else if (title == "session") {
        // sessionTime == [1,60]m
        if (sessionTime + change <= 0 || sessionTime + change > 60 * 60) {
          return;
        }
        setSessionTime(prev => prev + change);
      }
    }
  }

  // start|stop timer: when timer stopped changing breakTime|sessionTime changes displayTime accordingly
  const play = () => {
    // constants
    const TIMESTEP = 1000; // s -> ms [ms]
    const TIMEOUT = 100; // timeout for setDisplayTime [ms]
    // variables
    let date = new Date().getTime();
    let nextDate = Math.round(new Date().getTime() + TIMESTEP);
    // freeze current value of isBreak -> isBreakOn
    let isBreakOn = isBreak;
    // run timer
    if (!timerOn) {
      let interval = setInterval(() => {
        date = new Date().getTime();
        if (date > nextDate) {
          setDisplayTime(prev => {
            // displayTime == 0
            if (prev <= 0 && !isBreakOn) {
              playSound();
              isBreakOn = true;
              setIsBreak(true);
              return breakTime;
            // displayTime == 0
            } else if (prev <= 0 && isBreakOn) {
              playSound();
              isBreakOn = false;
              setIsBreak(false);
              return sessionTime;
            }
            return prev - 1;
          });
          nextDate += TIMESTEP;
        }
      }, TIMEOUT)
      localStorage.clear();
      localStorage.setItem('interval-id', interval);
    } else {
      clearInterval(localStorage.getItem('interval-id'));
    }
    setTimerOn(prev => !prev);
  }

  const reset = () => {
    if (timerOn) {
      // pause timer
      play();
    }
    // stop and rewind audio
    sound.pause();
    sound.currentTime = 0;
    // reset all times
    setBreakTime(BREAK);
    setSessionTime(SESSION);
    setDisplayTime(DISPLAY);
    setIsBreak(false);
  }

  // element 'root'
  return (
    <div className="container">
      {/* sound element */}
      <audio id="beep"></audio>
      <h1 className="title">clock</h1>
      <div className="length-container">
        {/* component 'Length' -> element 'break-length' */}
        <Length
          title={"break"}
          time={breakTime}
          changeTime={changeTime}
          formatTime={formatTimeMin}
          capitalise={capitalise}
        />
        {/* component 'Length' -> element 'session-length' */}
        <Length
          title={"session"}
          time={sessionTime}
          changeTime={changeTime}
          formatTime={formatTimeMin}
          capitalise={capitalise}
        />
      </div>
      {/* element 'timer' */}
      <div className="time-container">
        <div className="timer-wrapper">
          <h2 className="label" id="timer-label">{isBreak ? "Break" : "Session"}</h2>
          <h1 className="time" id="time-left">{formatTime(displayTime)}</h1>
          <div className="timer">
            {/* onClick() -> play() */}
            <button className="btn btn-small deep-purple lighten2" id="start_stop" onClick={() => play()}>
              {timerOn
                // timerOn ? pause-btn : play-btn
                ? (<i className="material-icons">pause_circle_filled</i>)
                : (<i className="material-icons">play_circle_filled</i>)
              }
            </button>
            {/* onClick() -> reset() */}
            <button className="btn btn-small deep-purple lighten2" id="reset" onClick={() => reset()}>
              {/* reset-btn */}
              <i className="material-icons">autorenew</i>
            </button>
          </div>
        </div>
      </div>
    </div>
  )
}

// component 'Length' -> elements 'break-length' & 'session-length'
function Length({ title, changeTime, time, formatTime, capitalise }) {

  // constants
  const DECREMENT = -60; // [s]
  const INCREMENT = +60; // [s]

  // elements 'break-length' & 'session-length'
  return (
    <div className={`${title}-wrapper`}>
      <h2 className="label" id={`${title}-label`}>{capitalise(title)} Length</h2>
      <div className="time-sets">
        {/* decrement breakTime|sessionTime by 1 minute */}
        <button className="btn btn-small deep-purple lighten2" onClick={() => changeTime(DECREMENT, title)}>
          <i className="material-icons" id={`${title}-decrement`}>arrow_downward</i>
        </button>
        <h4 className="time" id={`${title}-length`}>{formatTime(time)}</h4>
        {/* increment breakTime|sessionTime by 1 minute */}
        <button className="btn btn-small deep-purple lighten2" onClick={() => changeTime(INCREMENT, title)}>
          <i className="material-icons" id={`${title}-increment`}>arrow_upward</i>
        </button>
      </div>
    </div>
  )
}

// export component 'App' -> element 'root'
ReactDOM.render(<App />, document.getElementById('root'))

              
            
!
999px

Console