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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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>

<script src="https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js"></script>
              
            
!

CSS

              
                // App.scss
@import url('https://fonts.googleapis.com/css?family=Open+Sans|Raleway:400,500&display=swap');

:root,
* {
    box-sizing: border-box;
}

body {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-around;
    width: 100vw;
    height: 100vh;
    margin: 0;
    padding: 0;
    background-image: linear-gradient(-137deg, #adbcc4 0%, #696d89 100%);
    font-family: 'Raleway', sans-serif;
    font-size: 1.5rem;
    color: #dcdeed;
}

.App {
    width: 350px;
    height: 600px;
    border-radius: 6px;
    background: #34394c;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}

@media (max-width: 499px) {
    .App {
        height: 100%;
        width: 100vw;
        border-radius: 0;
    }

    body {
        background: #34394c;
        touch-action: manipulation;
    }
}

// Pomodoro.scss

.pomodoro {
    height: 100%;
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-around;
    box-sizing: border-box;
}

@media (max-width: 499px) {
    .pomodoro {
        height: 70%;
    }
}

// Title.scss

.title {
    /* Pomodoro Timer: */
    font-weight: 500;
    font-size: 1.5rem;
    letter-spacing: 2.25px;
    text-align: center;
}

// Timer.scss

// set positioning to relative for absolute elements
.wrapper {
    position: relative;
    width: 150px;
    height: 50px;
    display: flex;
    justify-content: center;
    align-items: center;
}

.counter {
    position: absolute;
    z-index: 2;
    font-size: 2rem;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}

.spinner {
    z-index: 1;
    width: 100%;
    height: 100%;
}

.counterType {
    font-size: 1rem;
}

#time-left {
    font-family: 'Open Sans', sans-serif;
}

// Progress.scss

.dot {
    height: 10px;
    width: 10px;
    background-color: #bbb;
    border-radius: 50%;
    display: inline-block;
    margin: 25px 10px;
}

.completed {
    background-color: #f28458;
}

// TimeController.scss

.TimeController {
    display: flex;
    justify-content: space-around;
    width: 80%;
    padding: 1rem 0;
}

.wrapperDisplay {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}

.label {
    font-size: 0.7rem;
    color: #f28458;
    font-size: 1.125;
}

.time {
    display: inline-block;
    font-size: 1.5rem;

    &::after {
        content: ' mins';
    }
}

.controlButton {
    background: #f2f4f1;
    text-align: center;
    width: 3rem;
    height: 3rem;
    border-radius: 100%;
    display: inline-flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    font-size: 1.5rem;
    color: #34394c;
    font-weight: bold;
    text-decoration: none;
    border: none;
}

// ButtonController.js

.ButtonController {
    display: flex;
    justify-content: space-around;
    width: 80%;
    margin: 1rem 0 2rem 0;
}

//Button.scss

.Button {
    background: #f2f4f1;
    text-align: center;
    width: 3rem;
    height: 3rem;
    border-radius: 100%;
    display: inline-flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    font-size: 1.5rem;
    color: #34394c;
    font-weight: bold;
    text-decoration: none;
    border: none;
    &:hover {
        cursor: pointer;
    }
}


              
            
!

JS

              
                

// TimeContext.js

const TimeContext = React.createContext();

const TimeProvider = props => {
    const [timer, setTimer] = React.useState({
        session: 1500,
        break: 300,
        mode: 'session',
        time: { currentTime: 1500, startingTime: 1500 },
        active: false,
        name: 'Pomodoro Timer',
        progress: 0
    });

    return (
        <TimeContext.Provider value={[timer, setTimer]}>
            {props.children}
        </TimeContext.Provider>
    );
};

// App.js

function App() {
  return (
      <TimeProvider>
          <div className="App">
              <Pomodoro />
          </div>
      </TimeProvider>
  );
}



// Pomodoro.js

function Pomodoro() {
    
    const [timer, setTimer] = React.useContext(TimeContext);
    // create ref for the audio
    const beep = React.useRef();
    

    React.useEffect(() => {
        if(timer.active  && timer.time.currentTime > 0){
                const interval = setInterval(() => {
                    setTimer({
                        ...timer, 
                        time: {
                            startingTime: timer.time.startingTime,
                            currentTime: timer.time.currentTime-1
                    }});
                }, 1000);
                return () => clearInterval(interval);
        } else if (timer.time.currentTime === 0) {
                beep.current.play();
                beep.current.currentTime = 0;
                //    setTimeout(() => {
                       if (timer.mode === 'session') {
                           setTimer({
                               ...timer,
                               time: {
                                   currentTime: timer.break,
                                   startingTime: timer.break
                               },
                               mode: 'break',
                           });
                       }
                       if (timer.mode === 'break') {
                           setTimer({
                               ...timer,
                               time: {
                                   currentTime: timer.session,
                                   startingTime: timer.session
                               },
                               mode: 'session',
                               progress: timer.progress+1,
                           });
                       }
                //    }, 2500);
                   
               }
    }, [setTimer, timer]);

    React.useEffect(() => {
        if(timer.playPause){
            beep.current.pause();
            beep.current.currentTime = 0;
        }
    })

     return (
         <div className="pomodoro">
             <Title title={timer.name} />
             <Timer time={timer.time} mode={timer.mode} />

             <Progress progress={timer.progress} />

             <TimeController
                 durationId={timer.session}
                 type="session"
                 label={'Session'}
                 lengthId={'session-length'}
                 labelId={'session-label'}
             />
             <TimeController
                 durationId={timer.break}
                 type="break"
                 label={'Break'}
                 lengthId={'break-length'}
                 labelId={'break-label'}
             />
             <ButtonController playing={timer.active} myRef={beep}/>
             <audio
                 id="beep"
                 preload="auto"
                 src="https://www.soundjay.com/misc/sounds/bell-ringing-05.mp3"
                 ref={beep}
             ></audio>
         </div>
     );
}



// Title.js

const Title = (props) => {
    return (
        <header>
            <h1 className="title">{props.title}</h1>
        </header>
    );
}

// Timer.js

const Timer = (props) => {
    
    function formatTime(time){ 
        let minutes = Math.floor(time / 60);
        if(minutes < 10){
            minutes = "0"+minutes
        }
        let seconds = Math.floor(time - minutes * 60);
        if(seconds < 10){
            seconds = "0"+seconds
        }
        return `${minutes}:${seconds}`;
    }
    
    return (
        <div className="wrapper">
            <div className="counter">
                <span className="counterType" id="timer-label">{props.mode}</span>
                <span id="time-left">{formatTime(props.time.currentTime)}</span>
            </div>
        </div>
    );
}

// Progress.js

const Progress = props => {
    let progress = new Array(4).fill(0).fill(true, 0, props.progress);
    return (
        <div>
            {progress.map((item, index) => (
                <span
                    key={index}
                    className={`dot ${progress[index] ? 'completed' : ''}`}
                ></span>
            ))}
        </div>
    );
};

// TimeController.js

function TimeController(props) {
    const [timer, setTimer] = React.useContext(TimeContext);

    // update session time / break time on click
    const changeTimer = operator => {
        let mode = timer.mode;
        console.log(mode);
        if (timer.mode === props.type) {
            console.log('same mode');
            if (operator === 'decrement' && timer[props.type] > 60) {
                //if the current mode && the type are equal change the currentTime & startingTime
                setTimer({
                    ...timer,
                    [props.type]: timer[props.type] - 60, // change session / break time
                    time: {
                        currentTime: timer[timer.mode] - 60,
                        startingTime: timer[timer.mode] - 60
                    }
                });
            }
            if (operator === 'increment' && timer[props.type] < 3600) {
                setTimer({
                    ...timer,
                    [props.type]: timer[props.type] + 60,
                    time: {
                        currentTime: timer[timer.mode] + 60,
                        startingTime: timer[timer.mode] + 60
                    }
                });
            }
        } else {
            let time = timer.time;
            if (operator === 'decrement' && timer[props.type] > 60) {
                //if the current mode && the type are equal change the currentTime & startingTime
                setTimer({
                    ...timer,
                    [props.type]: timer[props.type] - 60, // change session / break time
                    time: time
                });
            }
            if (operator === 'increment' && timer[props.type] < 3600) {
                setTimer({
                    ...timer,
                    [props.type]: timer[props.type] + 60,
                    time: time
                });
            }
        }
    };

    return (
        <div className="TimeController">
            <Button
                actionClick={() => changeTimer('decrement')}
                className="controlButton"
                buttonId={`${props.type}-decrement`}
            >
                -
            </Button>
            <div className="wrapperDisplay">
                <span id={props.labelId} className="label">
                    {props.label}
                </span>
                <span id={props.lengthId} className="time">
                    {timer[props.type] / 60}
                </span>
            </div>
            <Button
                actionClick={() => changeTimer('increment')}
                className="controlButton"
                buttonId={`${props.type}-increment`}
            >
                +
            </Button>
        </div>
    );
}

// ButtonController.js

function ButtonController(props) {
    const [timer, setTimer] = React.useContext(TimeContext);

    const resetTime = (sessionValue) => {
        console.log(timer);
        setTimer({...timer,
            session: 1500,
            break: 300,
            mode: 'session',
            time: { currentTime: 1500, startingTime: 1500 },
            active: false,
            name: 'Pomodoro Timer',
            progress: 0,
            
        });
        props.myRef.current.pause();
        props.myRef.current.currentTime = 0;
    }

    const setActive = () => {
        setTimer({
            ...timer,
            active: !timer.active
        })
    }
    
    return (
        <div className="ButtonController">
            <Button buttonId="reset" type="reset" actionClick={() => resetTime()}>
                <i className="fas fa-undo-alt"></i>
            </Button>
            <Button buttonId="start_stop" type="play" actionClick={() => setActive()}>
                <i className={`fas ${timer.active ? "fa-pause" : "fa-play"}`}></i>
            </Button>
        </div>
    )
}

//Button.js

function Button(props) {
    return(
        <button id={props.buttonId} className="Button" onClick={() => props.actionClick(props.type)}>
            {props.children}
        </button>
    )
}

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

Console