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

              
                :root {
	/* COLORS */
	--beige: #f9faf6;
	--green: #1b998b;
	--gray: #2d3047;
	--salmon: #ff9b71;
	--red: #e84855;

	/* SIZE */
	--base: 0.25rem;
}

body {
	background-color: var(--beige);
	font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
		'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
		sans-serif;
	font-size: 16px;
}

.app {
	height: 100vh;
}

.app__header {
	align-items: center;
	background-color: var(--red);
	box-shadow: 5px 5px 5px rgba(12, 12, 12, 0.1);
	color: var(--beige);
	display: grid;
	grid-template-areas: 'title title' 'counter control';
	grid-template-columns: repeat(2, 1fr);
	grid-template-rows: repeat(2, 1fr);
	justify-items: center;
	margin-bottom: calc(var(--base) * 6);
}

.title {
	grid-area: title;
	text-align: center;
	font-weight: 300;
}

.counter {
	grid-area: counter;
	align-items: center;
	display: grid;
	font-size: 1.2rem;
	gap: calc(var(--base) * 5);
	grid-template-columns: repeat(3, auto);
}

.btn {
	background-color: var(--beige);
	border-radius: 50%;
	border: none;
	box-shadow: 5px 5px 5px rgba(12, 12, 12, 0.1);
	color: var(--red);
	cursor: pointer;
	font-weight: bold;
	height: calc(var(--base) * 14);
	transition: box-shadow 0.25s, opacity 0.25s;
	width: calc(var(--base) * 14);
}

.btn:active {
	box-shadow: 0px 0px 0px rgba(12, 12, 12, 0.1);
}

.btn:disabled {
	opacity: 0.5;
	cursor: not-allowed;
	box-shadow: 0px 0px 0px rgba(12, 12, 12, 0.1);
}

.btn:focus {
	outline: none;
}

.control {
	grid-area: control;
}

.control__btn {
	border-radius: calc(var(--base) * 3);
	width: 7rem;
}

.search {
	display: flex;
	place-content: center;
	margin-bottom: calc(var(--base) * 6);
}

.search__input {
	border: 2px solid var(--gray);
	padding: calc(var(--base) * 4) calc(var(--base) * 9);
	padding-left: calc(var(--base) * 3);
	border-radius: calc(var(--base) * 3);
}

.search__input:focus {
	outline: none;
	border: 2px solid var(--red);
}

.length {
	animation: fade 0.5s ease-in-out forwards;
	color: var(--red);
	display: block;
	margin: auto;
	max-width: calc(var(--base) * 172);
	text-align: center;
}

.monkeys {
	margin-top: calc(var(--base) * 6);
	display: grid;
	gap: calc(var(--base) * 20);
	grid-template-columns: repeat(5, 1fr);
	justify-items: center;
	padding: calc(var(--base) * 10);
}

.monkey {
	opacity: 0;
	animation: fade 0.5s ease-in-out forwards;
	background-color: var(--beige);
	border-radius: 50%;
	box-shadow: 5px 5px 5px rgba(12, 12, 12, 0.1);
	cursor: pointer;
	font-size: 2rem;
	margin-bottom: calc(var(--base) * 5);
	padding: calc(var(--base) * 5);
	position: relative;
}

span[aria-label='monkey']::before {
	content: '🐵';
}

.monkey:hover::before {
	content: '🙈';
}

.monkey__badge {
	align-items: center;
	animation: slide 0.5s ease-in-out forwards;
	background-color: var(--beige);
	border-radius: 50%;
	box-shadow: 5px 5px 5px rgba(12, 12, 12, 0.1);
	cursor: pointer;
	display: flex;
	font-size: 1.15rem;
	height: calc(var(--base) * 7);
	justify-content: center;
	line-height: 0;
	opacity: 0;
	padding: calc(var(--base) * 2);
	position: absolute;
	width: calc(var(--base) * 7);
	z-index: 99;
}

.monkey__badge--writing {
	opacity: 1;
	animation: up-and-down 0.7s ease-in-out alternate-reverse infinite;
}

.monkey__match {
	opacity: 0;
	position: absolute;
	transform: translate(1.9rem, -1.15rem);
	z-index: 99;
	transition: opacity 0.7s;
}

.monkey__match--matched {
	opacity: 1;
}

@keyframes fade {
	0% {
		opacity: 0;
	}
	100% {
		opacity: 1;
	}
}

@keyframes slide {
	0% {
		opacity: 0;
		transform: translate(4rem, 0);
	}
	100% {
		opacity: 1;
		transform: translate(4rem, -2.15rem);
	}
}

@keyframes up-and-down {
	0% {
		transform: translate(4rem, -1rem);
	}
	100% {
		transform: translate(4rem, -2.15rem);
	}
}

@media screen and (max-width: 900px) {
	.monkeys {
		grid-template-columns: repeat(4, 1fr);
	}
}

@media screen and (max-width: 700px) {
	.monkeys {
		grid-template-columns: repeat(3, 1fr);
	}
}

@media screen and (max-width: 500px) {
	.monkeys {
		grid-template-columns: repeat(2, 1fr);
	}
}
              
            
!

JS

              
                const {useState, useEffect, createContext, useContext, useReducer} = React;

const actions = {
	addMonkey: 'ADD_MONKEY',
	removeMonkey: 'REMOVE_MONKEY',
	startWriting: 'START_WRITING',
	stopWriting: 'STOP_WRITING',
	updateSearch: 'UPDATE_SEARCH',
};

const initialState = {
	monkeys: 5,
	writing: false,
	searchTerm: '',
};

const reducer = (state, action) => {
	switch (action.type) {
		case actions.addMonkey:
			return {
				...state,
				monkeys: state.monkeys + 1,
			};
		case actions.removeMonkey:
			return {
				...state,
				monkeys: state.monkeys - 1,
			};
		case actions.startWriting:
			return {
				...state,
				writing: true,
			};
		case actions.stopWriting:
			return {
				...state,
				writing: false,
			};
		case actions.updateSearch:
			return {
				...state,
				searchTerm: action.payload,
			};
		default:
			return {
				...state,
			};
	}
};

const Context = createContext(initialState);

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

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

const genRandomString = () => {
	return Math.random()
		.toString(36)
		.replace(/[^a-z]+/g, '')
		.substr(0, 1);
};

const Counter = () => {
	const { state, dispatch } = useContext(Context);

	return (
		<div className="counter">
			<button
				className="btn counter__btn"
				disabled={state.monkeys >= 99}
				onClick={() => dispatch({ type: actions.addMonkey })}
			>
				+
			</button>
			{`${state.monkeys}`.padStart(2, 0)}
			<button
				className="btn counter__btn"
				disabled={state.monkeys <= 1}
				onClick={() => dispatch({ type: actions.removeMonkey })}
			>
				-
			</button>
		</div>
	);
};

const Control = () => {
	const { state, dispatch } = useContext(Context);

	return (
		<div className="control">
			{state.writing ? (
				<button
					className="btn control__btn"
					onClick={() => dispatch({ type: actions.stopWriting })}
				>
					Stop
				</button>
			) : (
				<button
					className="btn control__btn"
					disabled={state.searchTerm === ''}
					onClick={() => dispatch({ type: actions.startWriting })}
				>
					Start
				</button>
			)}
		</div>
	);
};

const Search = () => {
	const { state, dispatch } = useContext(Context);

	const handleChange = (e) => {
		dispatch({
			type: actions.updateSearch,
			payload: e.target.value.toLowerCase().split(' ').join(''),
		});
	};

	return (
		<div className="search">
			<input
				onChange={handleChange}
				value={state.searchTerm}
				className="search__input"
				type="text"
				placeholder="Write something to start searching"
				disabled={state.writing}
			/>
		</div>
	);
};

const Instructions = () => {
	const { state } = useContext(Context);
	const [length, setLength] = useState(0);

	useEffect(() => {
		if (!state.writing) return;

		const interval = setInterval(() => {
			setLength((length) => length + 1);
		}, 250);

		return () => clearInterval(interval);
	}, [state.writing]);

	return (
		<>
			{state.writing || length !== 0 ? (
				<p className="length">{length} letters generated!</p>
			) : (
				<p className="length">
					Monkeys haven't written anything yet! When a monkey writes a text
					similar to the one you are looking for, an{' '}
					<span role="img" aria-label="fire emoji">
						🔥
					</span>
					will appear over it. Click on the monkey to copy the text it has
					generated!
				</p>
			)}
		</>
	);
};

const MonekyMatch = ({ match }) => {
	return (
		<div className={`monkey__match ${match ? 'monkey__match--matched' : ''}`}>
			{match ? (
				<span role="img" aria-label="coffee">
					🔥
				</span>
			) : null}
		</div>
	);
};

const MonkeyBadge = ({ writing }) => {
	return (
		<div className={`monkey__badge ${writing ? 'monkey__badge--writing' : ''}`}>
			{writing ? (
				<span>...</span>
			) : (
				<span role="img" aria-label="coffee">
					☕
				</span>
			)}
		</div>
	);
};

const Monkey = () => {
	const [text, setText] = useState(genRandomString());
	const [match, setMatch] = useState(false);
	const { state, dispatch } = useContext(Context);

	useEffect(() => {
		if (!state.writing) return;

		const interval = setInterval(() => {
			setText((text) => (text += genRandomString()));
		}, 250);

		return () => clearInterval(interval);
	}, [state.writing]);

	useEffect(() => {
		if (state.searchTerm.length >= text || state.searchTerm === '') return;

		setMatch(text.includes(state.searchTerm));
	}, [text, state.searchTerm]);

	return (
		<div
			onClick={() => {
				dispatch({ type: actions.stopWriting });
				copy(text);
			}}
		>
			<MonekyMatch match={match} />
			<MonkeyBadge writing={state.writing} />
			<span className="monkey" role="img" aria-label="monkey"></span>
		</div>
	);
};

const Monkeys = () => {
	const [monkeys, setMonkeys] = useState([]);
	const { state } = useContext(Context);

	useEffect(() => {
		setMonkeys([<Monkey key={0} />]);
	}, []);

	useEffect(() => {
		if (monkeys.length === state.monkeys) return;

		if (monkeys.length < state.monkeys) {
			setMonkeys((monkeys) => [...monkeys, <Monkey key={monkeys.length} />]);
		} else {
			setMonkeys((monkeys) => [...monkeys].slice(0, monkeys.length - 1));
		}
	}, [state.monkeys, monkeys.length]);

	return <div className="monkeys">{monkeys.map((monkey) => monkey)}</div>;
};

const App = () => {
	return (
		<div className="app">
			<header className="app__header">
				<Counter />
				<Control />
			</header>
			<main>
				<Search />
				<Instructions />
				<Monkeys />
			</main>
		</div>
	);
};


ReactDOM.render(
	<ContextProvider>
		<App />
	</ContextProvider>,
	document.getElementById('root')
);

              
            
!
999px

Console