Skip to content

Instantly share code, notes, and snippets.

@tornqvist
tornqvist / example-memo.js
Last active May 19, 2020 19:02
A series of examples illustrating a new component API I've been working on in conjunction with changes to nanohtml
var { html, render } = require('nanohtml')
var { Component, memo, onupdate } = require('nanohtml/component')
// This example illustrates how memo can be used to maintain form state.
// On first render, getInitialValues is called and read values either
// from the fields argument or from local storage.
// Whenever a field changes its value is persisted to local storage.
var Form = Component(function (fields, values = memo(getInitialValues)) {
var update = onupdate(function (fields, values) {
@tornqvist
tornqvist / index.js
Created January 4, 2019 12:17
Lazy Choo component using split-require
var Choo = require('choo')
var html = require('choo/html')
var LazyColor = require('./lazy-color')
var app = new Choo()
app.use(store)
app.route('/', view)
app.mount('body')
@tornqvist
tornqvist / greeting.js
Last active August 1, 2018 13:51
Hypercomponent – experimental module for writing JSX-like markup with Nanocomponent
var html = require('nanohtml')
var Hypercomponent = require('hypercomponent')
class Greeting extends Hypercomponent {
update (strings, ...args) {
return true
}
Foo (attrs, children) {
if (attrs.bold) children = html`<strong>${children}</strong>`
@tornqvist
tornqvist / index.js
Last active March 7, 2018 10:44
Choo async render issue
var fetch = require('node-fetch')
var html = require('choo/html')
var choo = require('choo')
var app = choo()
app.route('/', main)
app.use(store)
function main (state, emit) {
if (!state.data) {
@tornqvist
tornqvist / button.js
Last active December 18, 2017 12:33
Choo cache thing
var html = require('choo/html')
var cache = require('choo/cache')
var Nanocomponent = require('nanocomponent')
class Button extends Nanocomponent {
// this is where you'd implement custom gc logic
static discard (instance) {
return !instance.element
}
@tornqvist
tornqvist / index.js
Created November 3, 2017 08:06
requirebin sketch
var html = require('bel')
var assert = require('assert')
var morph = require('nanomorph')
var Nanocomponent = require('nanocomponent')
function Component (name) {
Nanocomponent.call(this, name)
}
Component.prototype = Object.create(Nanocomponent.prototype)
@tornqvist
tornqvist / index.js
Created November 3, 2017 08:06
requirebin sketch
var html = require('bel')
var assert = require('assert')
var morph = require('nanomorph')
var Nanocomponent = require('nanocomponent')
function Component (name) {
Nanocomponent.call(this, name)
}
Component.prototype = Object.create(Nanocomponent.prototype)
@tornqvist
tornqvist / index.js
Created November 3, 2017 08:06
requirebin sketch
var html = require('bel')
var assert = require('assert')
var morph = require('nanomorph')
var Nanocomponent = require('nanocomponent')
function Component (name) {
Nanocomponent.call(this, name)
}
Component.prototype = Object.create(Nanocomponent.prototype)
@tornqvist
tornqvist / index.js
Last active October 31, 2017 12:38
requirebin sketch
var html = require('bel')
var assert = require('assert')
var Nanocomponent = require('nanocomponent')
function Component (name) {
Nanocomponent.call(this, name)
this.cache = {}
}
Component.prototype = Object.create(Nanocomponent.prototype)
@tornqvist
tornqvist / index.js
Created August 2, 2017 12:14
requirebin sketch
const html = require('bel');
const string = require('pelo');
document.body.innerHTML = string`
<input disabled=${ false } placeholder="Wut, I'm disabled?" />
`;
document.body.appendChild(html`
<input disabled=${ false } placeholder="I'm not disabled!" />
`);