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

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

CSS

              
                :root {
	/* Colors */
	--red: #f8333c;
	--background: #f7f7f2;
	--foreground: #46494c;

	--border-radius: 70px;
	--box-shadow: 8px 8px 13px #cbcbc6, -8px -8px 13px #ffffff;
}

*,
*::before,
*::after {
	box-sizing: border-box;
}

body {
	background-color: var(--background);
	color: var(--foreground);
	display: grid;
	font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
		"Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji",
		"Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";
	font-size: 1rem;
	font-weight: 400;
	height: 100vh;
	line-height: 1.5;
	margin: 0;
	place-items: center;
}

p {
	margin-bottom: 1rem;
	margin-top: 0;
}

button {
	border-radius: 0;
	cursor: pointer;
	font-family: inherit;
	font-size: inherit;
	line-height: inherit;
	margin: 0;
	overflow: visible;
	text-transform: none;
}

button:focus {
	outline: none;
}

.timer__current {
	font-size: 1.25rem;
	text-align: center;
	text-transform: uppercase;
}

.timer__time {
	font-size: 3.052rem;
	text-align: center;
}

.timer__card {
	border-radius: var(--border-radius);
	box-shadow: var(--box-shadow);
	margin-bottom: 1.75rem;
	width: 16rem;
	padding: 2.75rem 1.15rem;
}

.timer__pad {
	display: grid;
	grid-template-areas: "start reset" "plus minus" "change change";
	gap: 1rem;
}

.pad__start {
	grid-area: start;
}

.pad__plus-five {
	grid-area: plus;
}

.pad__minus-five {
	grid-area: minus;
}

.pad__reset {
	grid-area: reset;
}

.pad__change {
	grid-area: change;
}

.btn {
	border: none;
	padding: 1.75rem;
	border-radius: 25px;
	box-shadow: var(--box-shadow);
}

.btn-red {
	color: var(--red);
}

              
            
!

JS

              
                const { useState, useReducer, useContext, useEffect, createContext } = React;
const {render} = ReactDOM;
 
// Context
const actions = {
	increment: "INCREMENT_CURRENT",
	decrement: "DECREMENT_CURRENT",
	reset: "RESET_CURRENT",
	changeSession: "CHANGE_SESSION",
};

const initialContext = {
	current: "work",
	work: {
		name: "work",
		initialTime: 1500,
		timeLeft: 1500
	},
	break: {
		name: "break",
		initialTime: 300,
		timeLeft: 300
	}
};

const reducer = (state, action) => {
	const { current } = state;
	let result;

	switch (action.type) {
		case actions.increment:
			result = state[current].timeLeft + action.payload;
			return {
				...state,
				[current]: {
					...state[current],
					timeLeft: result > 3600 ? 3600 : state[current].timeLeft + action.payload
				}
			};
		case actions.decrement:
			result = state[current].timeLeft - action.payload;
			return {
				...state,
				[current]: {
					...state[current],
					timeLeft: result <= 0 ? 0 : state[current].timeLeft - action.payload
				}
			};
		case actions.reset:
			return {
				...state,
				[current]: {
					...state[current],
					timeLeft: state[current].initialTime
				}
			};
		case actions.changeSession:
			return {
				...state,
				current: current === "work" ? "break" : "work"
			};
		default:
			return {
				...state
			};
	}
};

const Context = createContext(initialContext);

const ContextProvider = ({ children }) => {
	const [state, dispatch] = useReducer(reducer, initialContext);

	return (
		<Context.Provider value={{ state, dispatch }}>{children}</Context.Provider>
	);
};

// App
const formatTime = time => {
	const mins = String(Math.floor(time / 60)).padStart(2, "0");
	const secs = String(Math.ceil(time - mins * 60)).padStart(2, "0");
	return { mins, secs };
};

const TimeLeft = ({ timeLeft }) => {
	const { mins, secs } = formatTime(timeLeft);
	return <p className="timer__time">{`${mins}:${secs}`}</p>;
};

const TimerPad = ({ active, setActive }) => {
	const { state, dispatch } = useContext(Context);

	return (
		<div className="timer__pad">
			<button
				type="button"
				        className={`btn ${active ? 'btn-red' : null} pad__start`}
				onClick={() => setActive(!active)}
			>
				{active ? "Stop" : "Start"}
			</button>
			<button
				type="button"
				className="btn pad__plus-five"
				onClick={() => dispatch({ type: actions.increment, payload: 300 })}
			>
				+5
			</button>
			<button
				type="button"
				className="btn pad__minus-five"
				onClick={() => dispatch({ type: actions.decrement, payload: 300 })}
			>
				-5
			</button>
			<button
				type="button"
				className="btn pad__reset"
				onClick={() => {
					setActive(false);
					dispatch({ type: actions.reset });
				}}
			>
				Reset
			</button>
			<button
				type="button"
				className="btn pad__change"
				onClick={() => {
					setActive(false);
					dispatch({ type: actions.changeSession });
				}}
			>
				Change
			</button>
		</div>
	);
};

const Timer = () => {
	const { state, dispatch } = useContext(Context);
	const [isActive, setIsActive] = useState(false);

	const { current } = state;

	useEffect(() => {
		let interval = undefined;
		if (!isActive) return;

		interval = setInterval(
			() => dispatch({ type: actions.decrement, payload: 1 }),
			1000
		);
		return () => clearInterval(interval);
	}, [state[current].timeLeft, isActive]);

	return (
		<div>
			<div className="timer__card">
				<p className="timer__current">{current}</p>
				<TimeLeft timeLeft={state[current].timeLeft} />
			</div>
			<TimerPad active={isActive} setActive={setIsActive} />
		</div>
	);
};

const app = document.getElementById("app");
render(
	<ContextProvider>
		<Timer />
	</ContextProvider>,
	app
);

              
            
!
999px

Console