HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URLs 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 its URL and the proper URL extension.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<div id="root">
<!-- This element's contents will be replaced with your component. -->
</div>
* {
box-sizing: border-box;
}
body {
font-family: Arial;
background: #ffcfdf;
background: linear-gradient(315deg, #ffcfdf 0%, #b0f3f1 74%);
/* background: linear-gradient(to top right, rgba(0,0,0,0.2), rgba(0,0,0,0.2)), linear-gradient(315deg, #ffcfdf 0%, #b0f3f1 74%); */
min-height: 100vh;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
}
.game {
margin: 0 auto;
/* background: rgba(0,0,0,0.3); */
padding: 20px;
}
.title {
text-align: center;
font-size: 3rem;
letter-spacing: 2px;
margin: 0;
padding-bottom: 30px;
color: #fff;
text-shadow: 0 0 2px black;
}
.help {
color: #333;
margin: 20px 0;
text-align: center;
}
.body {
display: flex;
flex-wrap: wrap;
justify-content: center;
}
.left, .right {
text-align: center;
padding: 20px 0;
width: 250px;
height: 240px;
border: thin solid #ddd;
background: white;
}
.star {
display: inline-block;
margin: 0 15px;
}
.star:after {
content: "\2605";
font-size: 45px;
}
.number {
background-color: #e5e7eb;
color: #fff;
border: none;
width: 45px;
height: 45px;
margin: 10px;
font-size: 1.5rem;
}
.number:focus, .number:active {
outline: none;
border: 1px solid #aaa;
}
.game-done {
position: relative;
top: 30px;
}
.game-done .message {
font-size: 2rem;
margin: 15px;
}
.game-done button {
background-color: #e5e7eb;
color: #fff;
border: none;
height: 45px;
padding: 0 20px;
font-size: 1.2rem;
cursor: pointer;
}
.game-done button:hover {
background-color: #ccc;
}
.game-done button:focus, .game-done button:active {
outline: none;
border: 1px solid #aaa;
}
.timer {
color: #333;
margin: 20px 0;
text-align: center;
}
footer {
padding: 20px 0;
text-align: center;
font-size: 12px;
}
footer a {
color: #333;
}
footer a:hover {
color: #000;
}
const { useEffect, useState } = React;
// improve design
// add start button
const StarsDisplay = props => (
<>
{utils.range(1, props.count).map(starId => (
<div key={starId} className="star" />
))}
</>
);
const PlayNumber = props => (
<button
className="number"
style={{backgroundColor: colors[props.status]}}
onClick={() => props.onClick(props.number, props.status)}
>
{props.number}
</button>
);
const PlayAgain = props => (
<div className="game-done">
<p
className="message"
style={{ color: colors[props.gameStatus]}}
>
{props.gameStatus === 'lost' ? 'Game Over' : 'Nice'}
</p>
<button onClick={props.onClick}>Play Again</button>
</div>
);
const useGameState = timeLimit => {
const [stars, setStars] = useState(utils.random(1, 9));
const [availableNums, setAvailableNums] = useState(utils.range(1, 9));
const [candidateNums, setCandidateNums] = useState([]);
const [secondsLeft, setSecondsLeft] = useState(10);
useEffect(() => {
if (secondsLeft > 0 && availableNums.length > 0) {
const timerId = setTimeout(() => setSecondsLeft(secondsLeft - 1), 1000);
return () => clearTimeout(timerId);
}
}, [secondsLeft]);
const setGameState = (newCandidateNums) => {
if (utils.sum(newCandidateNums) !== stars) {
setCandidateNums(newCandidateNums);
} else {
const newAvailableNums = availableNums.filter(
n => !newCandidateNums.includes(n)
);
setStars(utils.randomSumIn(newAvailableNums, 9));
setAvailableNums(newAvailableNums);
setCandidateNums([]);
}
};
return { stars, availableNums, candidateNums, secondsLeft, setGameState };
};
const Game = props => {
const {
stars,
availableNums,
candidateNums,
secondsLeft,
setGameState,
} = useGameState();
const candidatesAreWrong = utils.sum(candidateNums) > stars;
const gameStatus = availableNums.length === 0
? 'won'
: secondsLeft === 0 ? 'lost' : 'active'
const numberStatus = number => {
if (!availableNums.includes(number)) {
return 'right';
}
if (candidateNums.includes(number)) {
return candidatesAreWrong ? 'wrong' : 'candidate';
}
return 'available';
};
const onNumberClick = (number, currentStatus) => {
if (currentStatus === 'right' || secondsLeft === 0) {
return;
}
const newCandidateNums =
currentStatus === 'available'
? candidateNums.concat(number)
: candidateNums.filter(cn => cn !== number);
setGameState(newCandidateNums);
};
return (
<div className="game">
<h1 className="title"
style={{ color: colors[gameStatus]}}>
St★r M★tch
</h1>
<p className="help">
Pick 1 or more numbers that sum to the number of stars ★
</p>
<div className="body">
<div className="left">
{gameStatus !== 'active' ? (
<PlayAgain onClick={props.startNewGame} gameStatus={gameStatus} />
) : (
<StarsDisplay count={stars} />
)}
</div>
<div className="right">
{utils.range(1, 9).map(number => (
<PlayNumber
key={number}
status={numberStatus(number)}
number={number}
onClick={onNumberClick}
/>
))}
</div>
</div>
<p className="timer">Time Remaining: {secondsLeft}</p>
<footer>Created by <a href="https://remybeumier.be" target="_blank" rel="noreferrer">Rémy Beumier</a></footer>
</div>
);
};
const StarMatch = () => {
const [gameId, setGameId] = useState(1);
return <Game key={gameId} startNewGame={() => setGameId(gameId + 1)}/>;
}
// Color Theme
const colors = {
available: '#e5e7eb',
right: '#9AE6B4',
wrong: '#FEB2B2',
candidate: '#90CDF4',
won: '#9AE6B4',
lost: '#FEB2B2',
};
// Math science
const utils = {
// Sum an array
sum: arr => arr.reduce((acc, curr) => acc + curr, 0),
// create an array of numbers between min and max (edges included)
range: (min, max) => Array.from({length: max - min + 1}, (_, i) => min + i),
// pick a random number between min and max (edges included)
random: (min, max) => min + Math.floor(Math.random() * (max - min + 1)),
// Given an array of numbers and a max...
// Pick a random sum (< max) from the set of all available sums in arr
randomSumIn: (arr, max) => {
const sets = [[]];
const sums = [];
for (let i = 0; i < arr.length; i++) {
for (let j = 0, len = sets.length; j < len; j++) {
const candidateSet = sets[j].concat(arr[i]);
const candidateSum = utils.sum(candidateSet);
if (candidateSum <= max) {
sets.push(candidateSet);
sums.push(candidateSum);
}
}
}
return sums[utils.random(0, sums.length - 1)];
},
};
ReactDOM.render(<StarMatch />, document.getElementById('root'));
Also see: Tab Triggers