This gist offers another build-step-less way to construct Hyperapp views by using the powers of JavaScript proxies (see Proxy - JavaScript | MDN) and the ability to "create" dedicated hyperscript functions per needed tag on the fly by destructuring the imported proxy. Text nodes are implicit - just place a string as a child. Dashed custom elements and web components are also supported - just camel-case them when destructuring. Lists of children can be passed as an array or as individual arguments, or as a mix of both.
import dom from "./dom.js"
const { main, h1, input, ul, button, myCustomElement, myWebComponent } = dom
The signature of the destructured hyperscript functions is flexible. See the following examples:
// just a `<br>`
br()
// a `<span>` with a simple text node
strong('I am 💪')
// render `<main id="app" class="--running"></main>`
main({ id: 'app', class: { '--running': true } })
// render `<ul><li>one</li><li>two</li><li>three</li></ul>`
ul(li('one'), li('two'), li('three'))
// render `<ul lang="sme"><li>okta</li><li>guokte</li><li>golbma</li></ul>`
ul({ lang: 'sme' }, li('okta'), li('guokte'), li('golbma'))
// equivalent ways to declare children (all render `<ol><li>Firstly</li><li>Secondly</li><li>Thirdly</li></ol>`):
ol(li('Firstly'), li('Secondly'), li('Thirdly'))
ol([ li('Firstly'), li('Secondly'), li('Thirdly') ])
ol(li('Firstly'), [ li('Secondly'), li('Thirdly') ])
ol(li('Firstly'), [ li('Secondly'), [ li('Thirdly') ] ])
const items = [ li('Firstly'), li('Secondly'), li('Thirdly') ]
ol(...items)
// render `<p>Hello, <em>Sven</em>!</p>` for `state = { name: 'Sven' }`
p('Hello, ', em(state.name), '!')
// render `<my-custom-element></my-custom-element>` and `<my-web-component></my-web-component>`
myCustomElement()
myWebComponent()
// using the imported `dom` proxy/object
dom.hr() // renders `<hr>`
dom['my-web-component']() // renders `<my-web-component></my-web-component>`
dom[buttonOrLink](attrs, 'Click me') // renders `<a href='#'>Click me</a>` for `buttonOrLink = 'a'` and `attrs = { href: '#' }`