Skip to content

Instantly share code, notes, and snippets.

View ryanmorr's full-sized avatar

Ryan Morr ryanmorr

View GitHub Profile
@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 / 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 / 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 / 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 / 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 / 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 / is-node-list.js
Last active January 17, 2024 08:07
Detect a DOM nodelist or the like
function isNodeList(obj) {
const type = {}.toString.call(obj).slice(8, -1);
switch(type) {
case 'NodeList':
case 'HTMLCollection':
case 'HTMLOptionsCollection':
case 'HTMLFormControlsCollection':
case 'RadioNodeList':
case 'StyleSheetList':
return true;
@ryanmorr
ryanmorr / at-rule-support.js
Last active January 22, 2024 19:44
Check if the browser supports some cutting edge at-rules
function supportsContainerQueries() {
return !!window.CSSContainerRule;
}
function supportsCustomPropertyDefinitions() {
return !!window.CSSPropertyRule;
}
function supportsScopedCSS() {
return !!window.CSSScopeRule;
@ryanmorr
ryanmorr / timezone.js
Last active January 26, 2024 16:52
Get the timezone of the client
// Get the IANA timezone
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
// My result
console.log(timezone); //=> "America/Toronto"
@ryanmorr
ryanmorr / is-mobile.js
Last active January 26, 2024 18:11
Experimental new API to detect mobile devices
// Spec: https://wicg.github.io/ua-client-hints
// Support: https://caniuse.com/mdn-api_navigator_useragentdata
const isMobile = navigator.userAgentData.mobile; //=> true/false