Skip to content

Instantly share code, notes, and snippets.

View ryanmorr's full-sized avatar

Ryan Morr ryanmorr

View GitHub Profile
@ryanmorr
ryanmorr / svg-icon.css
Last active January 16, 2024 21:01
Allow an SVG icon to seamlessly blend with text
/* Courtesy of: https://www.24a11y.com/2018/accessible-svg-icons-with-inline-sprites/ */
.svg-icon {
/* Place the icon on the text baseline. */
position: relative;
top: .125em;
/* Prevent the icon from shrinking inside a flex container. */
flex-shrink: 0;
/* Scale the icon to match the font-size. */
height: 1em;
@ryanmorr
ryanmorr / vertical-rule.css
Created January 16, 2024 20:06
Vertical rule to separate things
.vr {
display: inline-block;
align-self: stretch;
width: 1px;
min-height: 1em;
background-color: currentcolor;
opacity: 0.25;
}
@ryanmorr
ryanmorr / body-shadow.css
Created January 16, 2024 08:02
Add a shadow to the body
body:before {
content: "";
position: fixed;
top: -10px;
left: 0;
width: 100%;
height: 10px;
box-shadow: 0px 0px 10px rgba(0,0,0,.5);
z-index:100;
}
@ryanmorr
ryanmorr / shadows.css
Last active January 29, 2024 07:23
Some shadows I like of increasing depths
.shadow-light {
box-shadow: 0 1.5px 4px rgba(0, 0, 0, 0.23), 0 1.5px 6px rgba(0, 0, 0, 0.12);
}
.shadow-medium {
box-shadow: 0 3px 6px rgba(0,0,0,0.16), 0 3px 6px rgba(0,0,0,0.16);
}
.shadow-heavy {
box-shadow: 0 3px 12px rgba(0, 0, 0, 0.23), 0 3px 12px rgba(0, 0, 0, 0.16);
@ryanmorr
ryanmorr / speech-synthesis.js
Last active January 16, 2024 07:45
JavaScript can talk
const msg = new SpeechSynthesisUtterance();
msg.voiceURI = 'native';
msg.text = 'Hello World';
msg.lang = 'en-US';
speechSynthesis.speak(msg);
@ryanmorr
ryanmorr / callback-middleware-proxy.js
Last active March 29, 2024 06:37
Add middleware to any callback function
function callbackMiddlewareProxy(callback) {
const middleware = [];
const push = middleware.push.bind(middleware);
return new Proxy(callback, {
get: (target, prop) => {
if (prop === 'use') {
return push;
}
return target[prop];
},
@ryanmorr
ryanmorr / tiny-vdom.js
Last active March 29, 2024 06:38
A tiny virtual DOM implementation
function updateAttribute(element, name, newVal, oldVal = null) {
if (name[0] === 'o' && name[1] === 'n') {
name = name.slice(2).toLowerCase();
if (newVal) {
element.addEventListener(name, newVal);
}
if (oldVal) {
element.removeEventListener(name, oldVal);
}
} else if (newVal == null || newVal === false) {
@ryanmorr
ryanmorr / css-json.js
Created January 15, 2024 07:11
Define JSON data within CSS
// I have no idea how practical this is, but I can't help but appreciate hacks like this
// Found it here: https://github.com/uikit/uikit/blob/fc644b5107c47c7242436c782dd935a374ab399e/src/js/util/style.js#L65
const vars = {};
function getCSSVar(name) {
if (!(name in vars)) {
const el = document.createElement('div');
document.documentElement.appendChild(el);
el.classList.add(`var-${name}`);
@ryanmorr
ryanmorr / env.js
Created January 15, 2024 07:03
Are we on the client or the server?
function isBrowser() {
return typeof window != 'undefined' && typeof window.document != 'undefined';
}
function isServer() {
return typeof process != 'undefined' && {}.toString.call(process) == '[object process]';
}
@ryanmorr
ryanmorr / touch-detection.js
Last active March 30, 2024 06:51
Determine if the user has a touch-enabled device
// An aggressive means to detect if the user's device is touch only
// Probably bad practice: https://css-tricks.com/interaction-media-features-and-their-potential-for-incorrect-assumptions/
function isTouch() {
return matchMedia('(hover: none) and (pointer: coarse)').matches;
}
// A more passive approach is to detect if the user's device is merely touch-enabled
function hasTouch() {
return matchMedia('(any-pointer:coarse)').matches;
}