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

              
                <main id="app" @keyup="handleKeyPress">
	<div class="container">
		<p id="quote" :class="{ 'strike': strikeout, 'highlight': puzzleComplete }">
			<span v-for="word in splitQuote">
				<template v-for="letter in word">{{ isRevealed(letter) }}</template>
			</span>
			<small v-if="gameOver">
				—{{ quoteAuthor }}
			</small>
		</p>

		<div class="status">
			<h2>Strikes:</h2>
			<ul class="status">
				<li v-for="strike in strikes">{{ strike.icon }}</li>
			</ul>
		</div>

		<div id="button-board">
			<button
				v-for="letter in letters"
				@click="guess(letter)"
				:class="{'strike': badGuesses.includes(letter), 'highlight': guesses.includes(letter)}"
				:disabled="guesses.includes(letter) || gameOver"
			>
				<span class="letter" :class="{'riser': guesses.includes(letter)}">{{ letter }}</span>
				<span class="background"></span>
			</button>
		</div>
		
		<div class="status">
			<p>{{ message }}</p>
		</div>

		<button id="new-game" @click="newGame" :class="{'highlight': gameOver}">New game</button>
	</div>
</>
              
            
!

CSS

              
                $lighterGray: #d7dade;
$lightGray: #a7a8aa;
$darkGray: #53565a;
$mediumGray: #888b8d;
$black: #101820;
$white: #fff;
$yellow: #ffd100;
$orange: #ff6a13;
$red: #e4002b;
$lightBlue: #7ba7bc;
$darkBlue: #34657f;

$easing: cubic-bezier(0.5, 0, 0.5, 1);
$breakpoint: 600px;

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

*:focus {
	outline: none;
	box-shadow: 0 0 0 4px $yellow;
}

html {
	font-size: 1.125em;
	line-height: 1.5;
	
	@media(min-width: 1200px) {
		font-size: 1.375em;
	}
}

body {
	margin: 0;
	min-height: 100vh;
	font-size: 1.5em;
	font-family: "Rubik Mono One", "Pathway Gothic One";
	color: $darkGray;
	background: lighten($lightBlue, 35%);
}

.container {
	width: 100%;
	max-width: 36rem;
	margin: auto;
	padding: 1rem;
	
	@media(min-width: $breakpoint) {
		padding: 2rem;
	}
}

#quote {
	letter-spacing: 0.1em;
	margin: 0 0 2rem;
	font-size: 1.25rem;
	line-height: 1.2em;
	background: lighten($lightBlue, 30%);
	border: 2px solid $darkGray;
	padding: 1rem 1rem 2rem;
	box-shadow: 4px 4px 0 0 $lightBlue;
	position: relative;
	
	@media(min-width: $breakpoint) {
		font-size: 2rem;
		margin: 1em 0 4rem;
		padding: 2rem 2rem 3rem;
	}
	
	&.strike {
		color: $lighterGray;
		background-color: $red;
	}
	
	&.highlight {
		background-color: $yellow;
	}

	span {
		margin-right: 0.75em;
		display: inline-block;
	}
	
	small {
		font-size: .5em;
		position: absolute;
		bottom: 0.5em;
		left: 2em;
	}
}

#button-board {
	display: grid;
	grid-template-columns: repeat(auto-fit, minmax(1.5em, 1fr));
	grid-gap: 0.5rem;
	
	@media (min-width: $breakpoint) {
		grid-template-columns: repeat(auto-fit, minmax(2em, 1fr));
	}
}

button {
	display: flex;
	align-items: center;
	justify-content: center;
	font-family: inherit;
	padding: 0.4em .5em 0.3em .5em;
	width: auto;
	line-height: 1;
	border: 2px solid $darkGray;
	color: inherit;
	background: lighten($lightBlue, 30%);
	font-size: .75em;
	box-shadow: 4px 4px 0 0 $lightBlue;
	position: relative;
	overflow: hidden;
	transform-origin: bottom right;
	
	@media(min-width: $breakpoint) {
		font-size: 1em;
	}
	
	.background {
		content: '';
		height: 100%;
		width: 100%;
		background: $yellow;
		position: absolute;
		z-index: 0;
		bottom: 0;
		left: 0;
		transform: scaleY(0);
		transform-origin: bottom;
		transition: transform .2s cubic-bezier(.75, 0, .25, 1);
	} 
	
	&:hover:not(:active):not([disabled]),
	&:focus:not(:active):not([disabled]) {
		transform: translate(-1px, -1px);
		box-shadow: 6px 6px 0 0 $lightBlue;
	}
	
	&:not([disabled]):active {
		transform: translate(2px, 2px);
		box-shadow: 0 0 0 0 $lightBlue;
	}
	
	&.highlight .background {
		transform: scaleY(1);
	}
	
	&.strike {
		color: $lighterGray;
		.background {
			background-color: $red;
		}
	}
	
	&#new-game {
		margin-top: 2rem;
		
		@media(min-width: $breakpoint) {
			margin-top: 4rem;
		}
	}
	
	.letter {
		positon: relative;
		z-index: 2;
		color: inherit;
	}
	
	&[disabled]:not(.highlight) {
		border-color: $lightGray;
		
		.letter {
			color: $lightGray;
		}
	}
}

.status {
	display: flex;
	flex-wrap: wrap;
	list-style-type: none;
	align-items: center;
	margin: 1rem 0;

	h2 {
		font-size: 1rem;
		margin: 0;
	}

	ul {
		display: flex;
		margin: 0;
		padding: 0;

		li {
			margin-left: 0.25em;
		}
	}
	
	p {
		font-size: 1rem;
		width: 100%;
		margin: 0;
	}
}

.riser {
	animation: rise .3s $easing;
	display: block;
}

@keyframes rise {
	0% {
		transform: translateY(0);
	}
	44.9% {
		transform: translateY(-1.5em);
	}
	45% {
		transform: translateY(1.5em);
	}
	55% {
		transform: translateY(1.5em);
	}
	0% {
		transform: translateY(0);
	}
}
              
            
!

JS

              
                // Change this if you want the possibility of longer or shorter puzzles.
const maxLength = 40 // (Typically, the lower this number, the harder the puzzle.)

//Change this if you want more or fewer strikes allowed
const allowedStrikes = 3 //If you set this and maxLength both too high, the puzzle will be impossible to lose.

const defaultStrikes = new Array(allowedStrikes).fill({ icon: '⚪', guess: '' })				

const app = new Vue({
	el: "#app",
	data: () => ({
		letters: Array.from('ABCDEFGHIJKLMNOPQRSTUVWXYZ'),
		quotes: [], //Filled by the mounted hook
		currentQuote: '', //Filled by the mounted hook
		quoteAuthor: '',
		guesses: [],
		strikes: [...defaultStrikes],
		gameOver: false
	}),
	mounted() {
		fetch('https://type.fit/api/quotes')
			.then((response) => response.json())
			.then((fetchedQuotes) => {
				fetchedQuotes = fetchedQuotes.filter(quote => quote.text.length <= maxLength) // Get rid of any quotes that are too long
				this.quotes = fetchedQuotes
				this.pickAQuote()
			})
	},
	methods: {
		//Can enter guesses with a keyboard, but it doesn't work super great because you need to be focusing a non-disabled element to use it currently. Needs some refinement.
		handleKeyPress(e) {
			const key = e.key.toUpperCase()		
			if(key.length === 1 && key.match(/[a-zA-Z]/) && !this.guesses.includes(key)) {
				console.log(key)
				this.guess(key)
			}
		},
		pickAQuote() {
			console.log(this.quotes)
			const random = Math.floor(Math.random() * this.quotes.length)
			this.currentQuote = this.quotes[random].text.toUpperCase()
			this.quoteAuthor = this.quotes[random].author
		},
		//The function that turns unguessed letters into blank spots
		isRevealed(letter) {
			if (!letter.match(/[a-zA-Z\s]/)) {
				return letter
			}
			return this.guesses.includes(letter) || this.gameOver ? letter : '_'
		},
		//Handles the guess and all possible results
		guess(letter) {
			console.log(letter)
			this.guesses.push(letter)
			if (!this.currentQuote.includes(letter)) {
				this.strikes.pop()
				this.strikes = [{ icon: '🚫', guess: letter }, ...this.strikes]
			}
			if (this.strikeout || this.puzzleComplete) {
				this.gameOver = true
				if(this.puzzleComplete) fireEmAll();
			}
		},
		newGame() {
			const confirmation = confirm('End this game and start a new one?')
			if (!confirmation) return
			this.pickAQuote()
			this.guesses = []
			this.strikes = [...defaultStrikes]
			this.gameOver = false
		}
	},
	computed: {
		splitQuote() {
			return this.currentQuote.split(' ')
		},
		badGuesses() {
			return this.strikes.filter(s => s.guess).map(s => s.guess)
		},
		strikeout() {
			return this.badGuesses.length >= allowedStrikes
		},
		puzzleComplete() {
			return this.unrevealed === 0
		},
		unrevealed() {
			return [...this.currentQuote].filter(letter => {
				return letter.match(/[a-zA-Z]/) && !this.guesses.includes(letter)
			}).length
		},
		message() {
			if(!this.gameOver) {
				return '☝️ Pick a letter'
			} else if (this.strikeout) {
				return '❌ You lost this round. Try again?'
			} else if (this.puzzleComplete) {
				return 'ğŸŽ‰ You win!'
			}
			//You can never be too safe ¯\_(ツ)_/¯
			return '😬 Unforeseen error state, maybe try a new game?'
		}
	}
})


//Confetti! ğŸŽ‰
//All the below code is just for the confetti. Could've brought it into Vue but didn't seem like there was any real reason to. Library is linked in the HTML tab's header settings.
let count = 200;
let defaults = {
	origin: { y: 0.5 },
	colors: ['#ffd100', '#a7a8aa', '#ff6a13', '#e4002b', '#7ba7bc', '#34657f']
};

const fire = (particleRatio, opts) => {
	confetti(
		Object.assign({}, defaults, opts, {
			particleCount: Math.floor(count * particleRatio)
		})
	);
};

const fireEmAll = () => {
	fire(0.25, {
		spread: 26,
		startVelocity: 55
	});
	fire(0.2, {
		spread: 60
	});
	fire(0.35, {
		spread: 100,
		decay: 0.91
	});
	fire(0.1, {
		spread: 120,
		startVelocity: 25,
		decay: 0.92
	});
	fire(0.1, {
		spread: 120,
		startVelocity: 45
	});
};
              
            
!
999px

Console