Skip to content

Instantly share code, notes, and snippets.

View zaceno's full-sized avatar

Zacharias Enochsson zaceno

View GitHub Profile
@zaceno
zaceno / tetris.elm
Created December 18, 2016 01:36
Simple Tetris in elm.
import Html exposing (Html)
import Html.Attributes exposing (style)
import Svg exposing (Svg, svg, rect)
import Svg.Attributes exposing (x, y, width, height, fill, viewBox)
import Array exposing (Array)
import Keyboard exposing (presses, KeyCode)
import Random
import Time exposing (Time, second)
main = Html.program
@zaceno
zaceno / gist:239e384dd914f1cb83a4d4b36af25ea2
Last active July 14, 2020 06:41
progressive possible future hyperapp app example
/*
In the following examples I will show how an app could be written in
progressively improved potential hyperapp versions
It may not always look like an improvement. I'd have to use an *actually*
complicated app as an example to really prove the benefit. So just use
your imagination :)
But first: the basic app I will be successively improving on. This should
@zaceno
zaceno / gist:83286dfd2a18ebbd3b4f1d7cb5810a0c
Created June 30, 2017 09:57
Proposal for "component" concept in Hyperapp
A "component": (state, actions, components, props, children) -> vnode
Where:
- state: the state of the app a component belongs to
- actions: the actions-collection of the app the component belongs to
- components: the components-collection of the app the component belongs to (will contain the component itself)
- props: {...}
- children: [vnodes]
Instantiate app with:
@zaceno
zaceno / gist:cc42cae952ae5ae1fe5812d50543401d
Created June 30, 2017 22:42
Hyperapp: Embed app in view
function embed (opts) {
return {
tag: 'div',
children: [],
data: {
key: opts.key,
oncreate: el => setTimeout(_ => {
const fakeRoot = document.createElement('div')
opts.root = fakeRoot
app(opts)
@zaceno
zaceno / gist:d53da7140ccfabfc77ab664593145789
Created August 16, 2017 14:24
scoped widgets and mixins for hyperapp
const scopedUpdate = (scope, state, update) => val => update({[scope]: Object.assign(state[scope], val)})
const scopedAction = function (scope, fn) {
return function (state, actions, data) {
return function (update) {
const retVal = fn(state[scope], actions[scope], data)
const sup = scopedUpdate(scope, state, update)
if (typeof retVal === 'function') return retVal(sup)
else return sup(retVal)
@zaceno
zaceno / gist:44e62b885140ef35aa35a2a36cf0f773
Created August 17, 2017 19:02
Hyperapp 0.11.0 dist file
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n(e.hyperapp={})}(this,function(e){"use strict";function n(e,n){var t,o=[];for(r=arguments.length;r-- >2;)a.push(arguments[r]);for(;a.length;)if(Array.isArray(t=a.pop()))for(r=t.length;r--;)a.push(t[r]);else null!=t&&!0!==t&&!1!==t&&("number"==typeof t&&(t+=""),o.push(t));return"string"==typeof e?{tag:e,data:n||{},children:o}:e(n,o)}function t(e){function n(e){for(x=v(w,x,h,h=i("render",k)(p,y),g=!g);e=o.pop();)e()}function t(){k&&!g&&requestAnimationFrame(n,g=!g)}function r(e){return e&&(e=i("update",u(p,e)))&&t(p=e),p}function a(e,n,t){Object.keys(n||[]).map(function(o){var u=n[o],f=t?t+"."+o:o;"function"==typeof u?e[o]=function(e){i("action",{name:f,data:e});var n=i("resolve",u(p,y,e));return"function"==typeof n?n(r):r(n)}:a(e[o]||(e[o]={}),u,f)})}function i(e,n){return(m[e]||[]).map(function(e){var t=e(p,y,n);null!=t&&(n=t)}),n}function u(e,n){var t={};for(var r in e)t
@zaceno
zaceno / gist:54297f9c3a3d2443bfb3678427b7b37a
Last active August 25, 2017 07:47
Stateful Hyperapp Components
/*
This function lets you define components you can use in your views, which are
stateful, i e, they embed their own hyperapp app.
The idea is to let you build apps according to what I, for lack of better terms,
call the "Vue architecture" -- where your app is built up of a tree of stateful
components. Parent components communicate with children by the props passed in
the view. Children may communicate back to parents via events the parents listen
to.
@zaceno
zaceno / gist:7803d2038b523b1d4b85e1ab70dbaf7e
Last active January 22, 2018 22:15
stateful components blog post

A frequently asked question

Just about every new user of hyperapp at some point asks the question: "How can I make components with local state?"

In React, for example, there are object-components with render methods, and .setState(). In Vue.js, every component has its .props. Such components encapsulate state and behavior locally, keeping it out of the rest of the app (because it's only relevant internally to the component).

But Hyperapp's design is clear: there is one single state store, and components are purely functional. So components with local state are off the table ... right?

@zaceno
zaceno / array-actions.js
Created February 15, 2018 12:24
Helper for making hyperapp actions that operate on an array
window.arrayActions = (name, actions) => {
const mapList = `_al_${name}`
const mapEach = `_ae_${name}`
const mapItem = `_ai_${name}`
const defs = actions({
list: fn => (_, actions) => {actions[mapList](fn)},
each: fn => (_, actions) => {actions[mapEach](fn)},
item: (i,fn) => (_, actions) => {actions[mapItem]([i, fn])},
})
defs[mapList] = fn => state => ({[name]: fn(state[name])})
@zaceno
zaceno / logo.svg
Last active June 25, 2018 22:44
zxdom logo svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.