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

              
                .keyboard
	.row
		.key.orange(data-code="Escape" data-macro="orange")
		//- - for(var i = 0; i < 12; i++)
		each val in '1234567890'.split('').map(n => 'Digit' + n).concat('Minus', 'Equal')
			.key(data-code=val)
		.key.size-4.grey.stretch(data-code="Backspace")
		.key.red(data-macro="red")
	.row
		.key.size-2.grey.stretch(data-code="Tab")
		//- - for(var i = 0; i < 12; i++)
		each val in 'QWERTYUIOP'.split('').map(n => 'Key' + n).concat('BracketLeft', 'BracketRight')
			.key(data-code=val)
		.key.size-2.grey.stretch(data-code="Backslash")
		.key.yellow(data-macro="yellow")
	.row
		.key.size-3.grey.stretch(data-code="CapsLock")
		//- - for(var i = 0; i < 11; i++)
		each val in 'ASDFGHJKL'.split('').map(n => 'Key' + n).concat('Semicolon', 'Quote')
			.key(data-code=val)
		.key.size-5.green.stretch(data-code="Enter" data-macro="green:Enter")
		.key.green(data-macro="green")
	.row
		.key.size-5.yellow.stretch(data-code="ShiftLeft" data-macro="yellow:ShiftLeft")
		//- - for(var i = 0; i < 10; i++)
		each val in 'ZXCVBNM'.split('').map(n => 'Key' + n).concat('Comma', 'Period', 'Slash')
			.key(data-code=val)
		.key.size-3.yellow.stretch(data-code="ShiftRight" data-macro="yellow:ShiftRight")
		.key(data-code="ArrowUp")
		.key.blue(data-macro="blue")
	.row
		.key.size-6.red(data-code="ControlLeft" data-macro="red:ControlLeft")
		.key.size-6.purple(data-code="MetaLeft" data-macro="purple:MetaLeft")
		.key.size-6.blue(data-code="AltLeft" data-macro="blue:AltLeft")
		.key.size-7.empty(data-code="Space" data-macro="default:Space")
		.key.size-2.blue.stretch(data-code="AltRight" data-macro="blue:AltRight")
		.key.size-2.purple.stretch(data-code="ContextMenu" data-macro="purple:ContextMenu")
		.key(data-code="ArrowLeft")
		.key(data-code="ArrowDown")
		.key(data-code="ArrowRight")
#footer
	a(href="https://dribbble.com/shots/14735602-Minimal-keyboard" target="_blank")
		#dribbble
              
            
!

CSS

              
                // Inspred by "Minimal keyboard" by Dibyajyoti Mishra
// https://dribbble.com/shots/14735602-Minimal-keyboard
// https://cdn.dribbble.com/users/4964399/screenshots/14735602/media/bfa7790a2b2a9abe08de761862c5207f.png

:root {
	--gap: 15px;
	--key-width: 50px;
}

body {
	margin: 0;
	overflow: hidden;
	background-color: hsl(0, 0%, 4%);
	height: 100vh;
	display: flex;
	align-items: center;
	justify-content: center;
	user-select: none;
}

.keyboard {
	--rot-x: 20deg;
	--rot-y: 0deg;
	display: inline-flex;
	flex-direction: column;
	padding: var(--gap);
	border-radius: 17.5px;
	transform-style: preserve-3d;
	perspective: 1200px;
	transform: perspective(1200px) rotateX(var(--rot-x)) rotateY(var(--rot-y));
	transition: 100ms transform;
}

.row {
	display: flex;
	transform-style: preserve-3d;
	
	& + & { margin-top: var(--gap); }
}

.keyboard,
.key {
	background: hsl(0, 0%, 8%);
	border: 2px solid hsla(0, 0%, 25%, 0.2);
	box-shadow:
		-5px -5px 10px hsl(0%, 0%, 100%, 0.01),
		-2px -2px  6px hsl(0%, 0%, 100%, 0.03),
		 2px  2px  5px hsl(0, 0%, 0%, 0.3),
		 5px  5px 10px hsl(0, 0%, 0%, 0.3);
}

.key {
	--width: var(--key-width);
	--color: hsl(0, 0%, 85%);
	--size: 1;
	--size-2: 1.654545; // calc((50 + 15 + 25 - 7.5) / 50); // 1.65
	--size-3: 1.981818; // calc((50 + 15 + 25 - 7.5 + 15) / 50); // 1.95
	--size-4: 2.290909; // calc((50 + 15 + 25 * 2) / 50); // 2.3
	--size-5: 2.618181; // calc((50 + 15 * 2 + 25 * 2) / 50); // 2.6 | Left shift
	--size-6: 1.327272;
	// --size-7: 7.781818;
	--size-7: 7.8;
	display: flex;
	align-items: center;
	justify-content: center;
	width: calc(var(--width) * var(--size));
	height: var(--width);
	border-radius: 7.5px;
	flex-shrink: 0;
	box-sizing: border-box;
	transform: translate3D(0, 0, 30px);
	transition: 100ms;
	cursor: pointer;
	
	&:hover { transform: translate3D(0, 0, 35px); }
	&:active,
	&[data-selected]:not([data-selected="false"]) {
		transform: translate3D(0, 0, 25px);
		border-color: var(--color);
		box-shadow:
			-5px -5px 10px hsl(0%, 0%, 100%, 0.01),
			-2px -2px  6px hsl(0%, 0%, 100%, 0.03),
			 2px  2px  5px hsl(0, 0%, 0%, 0.3),
			 5px  5px 10px hsl(0, 0%, 0%, 0.3),
			 0px  2px 8px var(--color),
			 0px  5px 20px var(--color);
	}
	& + & { margin-left: var(--gap); }
	&:before {
		display: block;
		content: '';
		width: 15px;
		height: 15px;
		border-radius: 7.5px;
		// margin: 17.5px 0 0 17.5px;
		background-color: var(--color);
	}
	&.stretch {
		// justify-content: stretch;
		padding: 0 25px;
		&:before { width: 100%; }
	}
	&.empty:before { display: none; }
	&.size-2 { --size: var(--size-2); }
	&.size-3 { --size: var(--size-3); }
	&.size-4 { --size: var(--size-4); }
	&.size-5 { --size: var(--size-5); }
	&.size-6 { --size: var(--size-6); }
	&.size-7 { --size: var(--size-7); }
	&.grey   { --color: hsl(  0,  0%, 50%); }
	&.red    { --color: hsl(  0, 80%, 60%); }
	&.orange { --color: hsl( 25, 80%, 60%); }
	&.yellow { --color: hsl( 50, 80%, 60%); }
	&.green  { --color: hsl(150, 80%, 60%); }
	&.blue   { --color: hsl(210, 80%, 60%); }
	&.purple { --color: hsl(270, 80%, 60%); }
	&[data-color="grey"]   { --color: hsl(  0,  0%, 50%); }
	&[data-color="red"]    { --color: hsl(  0, 80%, 60%); }
	&[data-color="orange"] { --color: hsl( 25, 80%, 60%); }
	&[data-color="yellow"] { --color: hsl( 50, 80%, 60%); }
	&[data-color="green"]  { --color: hsl(150, 80%, 60%); }
	&[data-color="blue"]   { --color: hsl(210, 80%, 60%); }
	&[data-color="purple"] { --color: hsl(270, 80%, 60%); }
}

////////////////////////////////

a {
	text-decoration: none;
	color: inherit;
}
#footer {
	height: 12vh;
	font-size: 1vh;
	
	#dribbble {
		border-radius: 2vh;
		position: absolute;
		bottom: 4vh;
		right: 4vh;
		transition: 300ms cubic-bezier(0.7, 0, 0.3, 1);
		padding-left: 1.5vh;

		&:before,
		&:after {
			vertical-align: middle;
			transition: inherit;
		}
		&:before {
			display: inline;
			content: 'View original Dribbble';
			font-size: 2vh;
			opacity: 0;
			transform: translate3D(-200px, 0, 0);
		}
		&:after {
			content: '';
			display: inline-block;
			width: 4vh;
			height: 4vh;
			margin-left: 1vh;
			background-image: url(https://alca.tv/static/u/82fde61b-28ef-4f17-976e-8f1abb5a1165.png);
			background-size: contain;
			background-position: center;
		}
		&.hover,
		&:hover {
			background: hsl(337, 78%, 61%);

			&:before {
				opacity: 1;
				transform: translate3D(0, 0, 0);
				transition-delay: 50ms;
			}
			&:after {
				filter: saturate(0%) contrast(200%) brightness(200%) invert(100%);
			}
		}
	}
}
              
            
!

JS

              
                const keyboard = document.querySelector('.keyboard');
const rotation = { x: 20, y: 0 };
// const empty = document.querySelector('.empty');
// empty.style.color = 'white';
let animating = [];
let animatingColor;
const keysDown = new Set();
const keyCodeToEle = new Map();
const allKeys = [ ...document.querySelectorAll('.key') ];
allKeys.forEach(ele => {
	const { code } = ele.dataset;
	if(code) {
		keyCodeToEle.set(code, ele);
	}
});
const capsLockKeyIndex = allKeys.indexOf(keyCodeToEle.get('CapsLock'));
const arrowKeyIndexes = [ 'Up', 'Left', 'Down', 'Right' ].map(n => allKeys.indexOf(keyCodeToEle.get(`Arrow${n}`)));
const macroKeys = [ document.querySelector('[data-code="Escape"]'), ...document.querySelectorAll('[data-macro]') ];
const allKeyBounds = allKeys.map(n => n.getBoundingClientRect());
const furthestKeys = {};
for(const macro of macroKeys) {
	const index = allKeys.indexOf(macro);
	const color = macro.dataset.macro;
	furthestKeys[color] = 0;
	const macroBounds = allKeyBounds[index];
	for(let i = 0; i < allKeys.length; i++) {
		const ele = allKeys[i];
		if(macro === ele) continue;
		const eleBounds = allKeyBounds[i];
		const d = dist(
			macroBounds.x + macroBounds.width * 0.5, macroBounds.y + macroBounds.height * 0.5,
			eleBounds.x + eleBounds.width * 0.5, eleBounds.y + eleBounds.height * 0.5
		);
		ele.macro = ele.macro || {};
		ele.macro[color] = d;
		if(d > furthestKeys[color]) {
			furthestKeys[color] = d;
		}
	}
}
function animateMacro(ele) {
	const { macro } = ele.dataset;
	const [ color, id ]  = macro.split(':');
	if(!keysDown.has(capsLockKeyIndex)) {
		if([ 'Space', 'ShiftLeft' ].includes(id)) {
			return;
		}
	}
	animating.push({ time: performance.now(), macro, color });
	// animating.push({ time: performance.now(), color: macro.split(':')[0] });
	if(animating.length === 1) {
		_draw();
	}
}
macroKeys.forEach(ele => {
	ele.addEventListener('click', () => animateMacro(ele));
});
function setKeyState(code, state) {
	const ele = keyCodeToEle.get(code);
	if(ele) {
		if(state) {
			keysDown.add(allKeys.indexOf(ele));
			ele.dataset.selected = 'true';
		}
		else {
			ele.dataset.selected = 'false';
			keysDown.delete(allKeys.indexOf(ele));
			if(macroKeys.includes(ele)) {
				animateMacro(ele);
			}
		}
		const [ up, left, down, right ] = arrowKeyIndexes.map(n => keysDown.has(n));
		if(up) rotation.x += 1;
		if(left) rotation.y += -1;
		if(down) rotation.x += -1;
		if(right) rotation.y += 1;
		keyboard.style.setProperty('--rot-x', `${rotation.x}deg`);
		keyboard.style.setProperty('--rot-y', `${rotation.y}deg`);
	}
}
window.addEventListener('keydown', e => {
	if(e.code.startsWith('F') && !isNaN(e.code.slice(1))) {
	   return;
	}
	e.preventDefault();
	setKeyState('CapsLock', e.getModifierState('CapsLock'));
	if(e.code === 'CapsLock') {
		return;
	}
	// if(animating.length) return;
	// empty.textContent = e.code;
	// const ele = document.querySelector(`[data-code=${e.code}]`);
	setKeyState(e.code, true);
});
window.addEventListener('keyup', e => {
	e.preventDefault();
	setKeyState('CapsLock', e.getModifierState('CapsLock'));
	if(e.code === 'CapsLock') {
		return;
	}
	// if(animating.length) return;
	// empty.textContent = e.code;
	// const ele = document.querySelector(`[data-code=${e.code}]`);
	setKeyState(e.code, false);
});
window.addEventListener('blur', e => {
	if(animating.length) animating.splice(0);
	for(const ele of document.querySelectorAll('[data-selected="true"], [data-color]')) {
		const index = allKeys.indexOf(ele);
		if(index !== capsLockKeyIndex) {
			ele.dataset.selected = 'false';
			ele.dataset.color = '';
			keysDown.delete(index);
		}
	}
});

function distSq(x1, y1, x2, y2) {
	const _x = x2 - x1, _y = y2 - y1;
	return _x * _x + _y * _y;
}
function dist(x1, y1, x2, y2) {
	const d = distSq(x1, y1, x2, y2);
	if(d === 0) return 0;
	return Math.sqrt(d);
}

function _draw(e) {
	draw(e);
	if(animating.length) {
		requestAnimationFrame(_draw);
	}
	else {
		for(const ele of document.querySelectorAll('[data-selected="true"], [data-color]')) {
			ele.dataset.selected = 'false';
			ele.dataset.color = '';
		}
	}
}

function draw(e) {
	if(!animating.length) return;
	const actions = Array(allKeys.length).fill(false);
	keysDown.forEach(i => actions[i] = true);
	const dilation = 100;
	for(let i = animating.length - 1; i >= 0; i--) {
		const a = animating[i];
		const time = e - a.time;
		const duration = furthestKeys[a.macro] + dilation;
		if(time >= duration) {
			animating.splice(i, 1);
			return;
		}
		for(let keyIndex = 0; keyIndex < allKeys.length; keyIndex++) {
			const key = allKeys[keyIndex];
			const d = key.macro[a.macro];
			const t = Math.abs(time - d);
			if(t < dilation && !actions[keyIndex]) {
				actions[keyIndex] = a.color;
			}
		}
	}
	for(let i = 0; i < actions.length; i++) {
		const key = allKeys[i];
		if(actions[i]) {
			key.dataset.selected = 'true';
			if(typeof actions[i] === 'string') {
				key.dataset.color = actions[i];
			}
		}
		else {
			key.dataset.color = '';
			key.dataset.selected = 'false';
		}
	}
}
              
            
!
999px

Console