Skip to content

Instantly share code, notes, and snippets.

import { useReducer, useRef, useEffect } from "react";
function reducer(state, action) {
switch (action.type) {
// When the hook is first called
case "LOAD": {
return { ...state, status: "loading" };
}
// When LOAD finishes fetching initial remote data
case "RESOLVE_LOAD": {
@oliverjam
oliverjam / safer-autoplay.js
Created January 27, 2021 12:30
A simple Custom Element for autoplaying a video only if the user hasn't opted out of motion
// Wrap around a video you want to autoplay.
// Don't set the autoplay attribute,
// since that can't respect user preferences.
// e.g. <safer-autoplay><video src="vid.mp4"></video></safer-autoplay>
// Sidenote: motion should _really_ be opt-in,
// since lots of users won't know they can set a preference
// "no motion" is the safest default,
// but good luck getting that past your PO/designer ¯\_(ツ)_/¯
function html(strings, ...interpolations) {
return strings
.map((string, i) => {
let value = interpolations[i];
// 0 is falsy but a valid value in HTML
if (value === undefined || value === null || value === false) {
value = "";
}
// join arrays so they aren't stringified with commas
if (Array.isArray(value)) {
@oliverjam
oliverjam / mini-markdown.html
Last active December 6, 2020 15:39
A tiny markdown editor. Paste into your URL bar with `data:text/html,` at the front
<style>
body {
margin: 0;
min-height: 100vh;
display: grid;
grid: none / 1fr 1fr;
gap: 1rem;
}
%23i {
outline: 0;
@oliverjam
oliverjam / simple-pipe.js
Created September 26, 2017 22:30
Simple pipe created by oliverjam - https://repl.it/Lfew/3
// pipe takes an arbitrary number of functions as arguments
// It returns a new function waiting for a value to be passed in
function pipe(...fns) {
return function(val) {
let finalResult;
for (fn of fns) {
// Call each function in turn with val (the first time) or the previous result
finalResult = fn(finalResult || val);
}
return finalResult;
@oliverjam
oliverjam / Functional Fun - Cool Currying.js
Created August 3, 2017 16:34
Functional Fun - Cool Currying created by oliverjam - https://repl.it/Jvni/0
const get = prop => object => object[prop];
const objects = [
{ id: 1 },
{ id: 2 },
{ id: 3 },
];
objects.map(get('id'));
@oliverjam
oliverjam / Functional Fun - Monad Madness.js
Created August 3, 2017 16:31
Functional Fun - Monad Madness created by oliverjam - https://repl.it/Jvn3/0
// inc :: Number -> (Number,String)
const inc = x => y => [y + x, `${inc.name}${x} was called.`];
const inc1 = inc(1);
// dec :: Number -> (Number,String)
const dec = x => y => [y - x, `${dec.name}${x} was called.`];
const dec1 = dec(1);
// unit :: Number -> (Number,String)
const unit = x => [x, ''];
@oliverjam
oliverjam / Recursive loop.js
Created July 31, 2017 09:59
Recursive loop created by oliverjam - https://repl.it/Jpzk/0
function loop(start, end, increment) {
console.log(start);
if (start === end) return;
loop(start + increment, end, increment);
}
loop(0, 10, 1);
@oliverjam
oliverjam / index.js
Created July 18, 2017 14:23
requirebin sketch
const bel = require('bel');
function li(items) {
return bel`<ul>${items.map(item => bel`<li>${item}</li>`)}</ul>`
}
document.body.appendChild(li(['1', '2']));