Skip to content

Instantly share code, notes, and snippets.

@sergey-shpak
Last active July 9, 2022 20:39
Show Gist options
  • Save sergey-shpak/9266316d9abd550ddbeac2c88e5a0d22 to your computer and use it in GitHub Desktop.
Save sergey-shpak/9266316d9abd550ddbeac2c88e5a0d22 to your computer and use it in GitHub Desktop.
Hyperapp#2 Html factory
import { html } from 'path/to/html'
import { app } from 'hyperapp#2'
// define any html tag
const { div, span } = html
// following syntax is supported
// tag(attrs)
// tag([child])
// tag(attrs, [child])
// tag(attrs, [textString])
// and your app
app({
init: {},
view: state => div({
class: 'active'
}, [
span([
// no need to use 'text' node
'some text as child node'
])
]),
node: document.body
})
import { h, text } from 'https://unpkg.com/hyperapp'
// Html factory
export const html = new Proxy({}, {
get: (target, name) => (attributes, children = []) => {
const [attr, child] =
Array.isArray(attributes)
? [{}, attributes]
: [attributes, children]
return h(name, attr, child.map(node =>
typeof node === 'string'
? text(node)
: node
))
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment