Skip to content

choojs/hyperx

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

160 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

hyperx

tests

tagged template string virtual dom builder

This module is similar to JSX, but provided as a standards-compliant ES6 tagged template string function.

hyperx works with virtual-dom, react, hyperscript, or any DOM builder with a hyperscript-style API: h(tagName, attrs, children).

You might also want to check out the hyperxify browserify transform to statically compile hyperx into javascript expressions to save sending the hyperx parser down the wire.

compatibility

Template strings are available in: node 4+, chrome 41, firefox 34, edge, opera 28, safari 9

If you're targeting these platforms, there's no need to use a transpiler!

examples

virtual-dom node example

import vdom   from 'virtual-dom'
import hyperx from 'hyperx'

const hx = hyperx(vdom.h)

const title = 'world'
const wow = [ 1, 2, 3 ]
const tree = hx`<div>
  <h1 y="ab${1+2}cd">hello ${title}!</h1>
  ${hx`<i>cool</i>`}
  wow
  ${wow.map(function (w, i) {
    return hx`<b>${w}</b>\n`
  })}
</div>`

console.log(vdom.create(tree).toString())

output:

$ node vdom.js
<div>
  <h1 y="ab3cd">hello world!</h1>
  <i>cool</i>
  wow
  <b>1</b><b>2</b><b>3</b>
</div>

react node example

import React              from 'react'
import { renderToString } from 'react-dom/server'
import hyperx             from 'hyperx'

const hx = hyperx(function createElement (component, properties, children) {
  // Pass children as separate arguments to avoid key warnings
  return React.createElement.apply(null, [component, properties].concat(children))
}, {
  createFragment: function createFragment (children) {
    return React.createElement.apply(null, [React.Fragment, {}].concat(children))
  }
})

const title = 'world'
const wow = [ 1, 2, 3 ]
const frag = hx`
  <tr> <td>row1</td> </tr>
  <tr> <td>row2</td> </tr>
`

const tree = hx`<div>
  <h1 y="ab${1+2}cd">hello ${title}!</h1>
  ${hx`<i>cool</i>`}
  wow
  ${wow.map(function (w, i) {
    return hx`<b>${w}</b>\n`
  })}

  <table>${frag}</table>
</div>`

console.log(renderToString(tree))

hyperscript node example

import h      from 'hyperscript'
import hyperx from 'hyperx'

const hx = hyperx(h)

const title = 'world'
const wow = [1,2,3]
const tree = hx`<div>
  <h1 data-y="ab${1+2}cd">hello ${title}!</h1>
  ${hx`<i>cool</i>`}
  wow
  ${wow.map(function (w) {
    return hx`<b>${w}</b>\n`
  })}
</div>`

console.log(tree.outerHTML)

virtual-dom/main-loop browser example

import vdom   from 'virtual-dom'
import hyperx from 'hyperx'
import main   from 'main-loop'

const hx = hyperx(vdom.h)

const loop = main({ times: 0 }, render, vdom)
document.querySelector('#content').appendChild(loop.target)

function render (state) {
  return hx`<div>
    <h1>clicked ${state.times} times</h1>
    <button onclick=${onclick}>click me!</button>
  </div>`

  function onclick () {
    loop.update({ times: state.times + 1 })
  }
}

react browser example

import React from 'react'
import { render } from 'react-dom'
import hyperx from 'hyperx'

const hx = hyperx(React.createElement)

const App = React.createClass({
  getInitialState: function () { return { n: 0 } },
  render: function () {
    return hx`<div>
      <h1>clicked ${this.state.n} times</h1>
      <button onClick=${this.handleClick}>click me!</button>
    </div>`
  },
  handleClick: function () {
    this.setState({ n: this.state.n + 1 })
  }
})

render(React.createElement(App), document.querySelector('#content'))

console.log example

import hyperx from 'hyperx'

const convertTaggedTemplateOutputToDomBuilder = hyperx(function (tagName, attrs, children) {
  console.log(tagName, attrs, children)
})

convertTaggedTemplateOutputToDomBuilder`<h1>hello world</h1>`

// Running this produces: h1 {} [ 'hello world' ]

api

import hyperx from 'hyperx'

const hx = hyperx(h, opts={})

Return a tagged template function hx from a hyperscript-style factory function h.

Values to use for h:

  • virtual-dom - vdom.h
  • react - React.createElement with parameter children spread
  • hyperscript - hyperscript

Optionally provide:

  • opts.concat(a, b) - custom concatenation function to combine quasiliteral strings with expressions. The h factory function will receive the objects returned by the concatenation function and can make specific use of them. This is useful if you want to implement a pre-processor to generate javascript from hyperx syntax.
  • opts.attrToProp - turn off attribute to property conversions when false
  • opts.createFragment - if your template string has multiple root elements, they will be provided as an array to this function. the return value will then be returned by the template literal

prior art

license

BSD

About

🏷 - tagged template string virtual dom builder

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 23