Skip to content

Instantly share code, notes, and snippets.

View zaceno's full-sized avatar

Zacharias Enochsson zaceno

View GitHub Profile
/*
Usage:
const HandleSubmit = (state, formdata) => {
// here, formdata will be a js object
// where each of the keys corresponds to
// the name of a form field, and the
// value will be the value of that
// form field
}
@zaceno
zaceno / map.js
Last active November 13, 2019 20:39
Userland implementation of proposed `map` function for Hyperapp
const MEMO = []
const mapSub = (M, [fn, props]) => {
let result = MEMO.filter(
([f, p, r]) => f === fn && shallowEqual(props, p)
).reduce((_, [f, p, r]) => r, null)
if (!result) {
result = mapEffect(M, [fn, props])
MEMO.push([fn, props, result])
@zaceno
zaceno / ha-lifecycle.js
Created October 23, 2019 10:16
Lifecycle hack for hyperapps
(() => {
if (Element.prototype._lifecycle) return
Element.prototype._lifecycle = true
const remove = Element.prototype.removeChild
Element.prototype.removeChild = function(child) {
const ret = remove.apply(this, [child])
child.dispatchEvent(new Event('remove'))
return ret
@zaceno
zaceno / withHead.js
Created October 18, 2019 13:53
withHead hyperapp HOA
const withHead = (() => {
let _setHead = null
let setHead = nodes => {
if (_setHead) return _setHead(nodes)
app({
init: nodes,
view: nodes => nodes,
node: document.head,
middleware: d => (_setHead = d),
})
var sub = function(fn) {
return function(action) {
return [fn, {action: action}]
}
}
var throttledEvent = function(name) {}
var rawEvent = function(name) {
return sub(function(dispatch, props) {
@zaceno
zaceno / slide-transition.js
Created October 11, 2018 19:12
HAV2 sub for FLIP animations
const updatePosition = elem => {
const {top: newY, left: newX} = elem.getBoundingClientRect()
elem._x = newX
elem._y = newY
return [newX, newY]
}
const slide = (elem, {time=300, easing='ease-in-out', delay=0}) => {
const oldX = elem._x
const oldY = elem._y
@zaceno
zaceno / withsubviews.js
Last active October 2, 2018 13:18
HAV2 with subviews
const resolve = (node, state) => {
if (node == null) {
//just return it
} else if (Array.isArray(node)) {
node = node.map(function (n) { return resolve(n, state)})
node = Array.prototype.concat.apply([], node)
node = node.filter(function (n) { return n != null })
} else if (node.name === '$') {
node = resolve(node.props.f(state), state)
} else if (node.children) {
@zaceno
zaceno / hav2-writeup-ex11.html
Created September 11, 2018 19:12
HAV2 Intro - example 11
<!doctype html>
<html>
<head>
<style>
.gauge {
position: relative;
padding: 0;
border: 1px #555 solid;
background-color: #aaa;
@zaceno
zaceno / hav2-writeup-ex10.html
Created September 10, 2018 20:07
HAV2 Intro - example 10
<!doctype html>
<html>
<head>
<style>
.gauge {
position: relative;
padding: 0;
border: 1px #555 solid;
background-color: #aaa;
@zaceno
zaceno / hav2-writeup-ex9.html
Created September 10, 2018 15:49
HAV2 Intro - example 9
<!doctype html>
<html>
<head>
<style>
.gauge {
position: relative;
padding: 0;
border: 1px #555 solid;
background-color: #aaa;