Last active
July 9, 2022 20:39
-
-
Save sergey-shpak/9266316d9abd550ddbeac2c88e5a0d22 to your computer and use it in GitHub Desktop.
Hyperapp#2 Html factory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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