Skip to content

Instantly share code, notes, and snippets.

View tomhodgins's full-sized avatar
😍
Writing CSS

Tommy Hodgins tomhodgins

😍
Writing CSS
View GitHub Profile
@tomhodgins
tomhodgins / various-ways-to-do-async.js
Created April 11, 2020 01:15
A variety of ways to express asynchronous stuff in JS
// In an async IIFE with await
(async () => {
const response = await fetch('http://api.open-notify.org/astros.json')
const json = await response.json()
console.log(json)
})();
// Using .then()
fetch('http://api.open-notify.org/astros.json')
@tomhodgins
tomhodgins / on-atttributes.js
Created April 10, 2020 22:22
Replace on:click="reference" to addEventListener('click', reference) if given a DOM node and an object of named functions like {reference: event => alert(1)}
function onAttributes(node = document.createElement('span'), functions = {}) {
if (
node.attributes
&& node.attributes.length
) {
Array.from(node.attributes)
.filter(({name}) => name.startsWith('on:'))
.forEach(({name, value}) => {
node.addEventListener(
name.replace(/^on:/, '').toLowerCase(),
<script type=module>
import computedVariables from 'https://unpkg.com/computed-variables/index.es.js';
computedVariables(
'--python',
(value, event, rule) => {
if ('brython' in window === false) {
const tag = document.createElement('script');
tag.onload = callback;
@--important {
.mobile-hero,
.desktop-hero {
background-image: none;
}
.home-hero .button {
width: 100%;
max-width: 500px;
font-size: 1.75rem;
}
function html(strings, ...things) {
function stringify(object = '') {
if (Array.isArray(object)) {
return object.map(stringify).join('')
}
if (object instanceof DocumentFragment) {
return Array.from(object.childNodes)
.map(node => node.outerHTML || node.nodeValue)
.join('')
// Tagged template function for parsing a template string as CSS stylesheet
// …you can remove async from this if you replace the dynamic import with:
// import {parse} from 'https://unpkg.com/cssomtools'
async function css(strings, ...things) {
const {parse} = await import('https://unpkg.com/cssomtools')
return parse(
strings.reduce((styles, string, index) => styles + things[index - 1] + string)
)
}
@tomhodgins
tomhodgins / parsing-JSON-from-dom-and-cssom.html
Created March 21, 2020 22:02
Storing every type JSON supports in CSSOM using CSS custom properties, and in DOM using data attributes. Then JavaScript can access the string values of these properties and attributes, and JSON.parse() can return the result not as a string but as the actual JS object it represents
<style>
json- {
--string: "Hello World";
--number: 1e3;
--object: {"hello": "world"};
--array: [1, 2, 3];
--true: true;
--false: false;
--null: null;
}
function object2microxml(object) {
function consume(object) {
// Null
if (object === null) {
return ['null', {}, ['null']]
}
// Array
if (object instanceof Array) {
return ['array', {}, object.map(function(child) { return consume(child) })]
@tomhodgins
tomhodgins / font-demo.js
Created March 9, 2020 21:47
run this in your browser's console to display all font-family properties used on the site styled in that font-family
{
const fonts = new Set()
document.querySelectorAll('*').forEach(tag => fonts.add(getComputedStyle(tag).fontFamily))
console.log(fonts)
document.body.insertAdjacentHTML('beforeEnd', `
<font-demo>
<style>
/* Either external library of these helper rules exist */
/* Or can be added to any stylesheet */
--big {
font-size: xx-large;
}
--red {
color: #c00;
background: white;