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.
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!
varvdom=require('virtual-dom')varhyperx=require('hyperx')varhx=hyperx(vdom.h)vartitle='world'varwow=[1,2,3]vartree=hx`<div> <h1 y="ab${1+2}cd">hello ${title}!</h1>${hx`<i>cool</i>`} wow${wow.map(function(w,i){returnhx`<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>
varReact=require('react')vartoString=require('react-dom/server').renderToStringvarhyperx=require('hyperx')varhx=hyperx(functioncreateElement(component,properties,children){// Pass children as separate arguments to avoid key warningsreturnReact.createElement.apply(null,[component,properties].concat(children))},{createFragment: functioncreateFragment(children){returnReact.createElement.apply(null,[React.Fragment,{}].concat(children))}})vartitle='world'varwow=[1,2,3]varfrag=hx` <tr> <td>row1</td> </tr> <tr> <td>row2</td> </tr>`vartree=hx`<div> <h1 y="ab${1+2}cd">hello ${title}!</h1>${hx`<i>cool</i>`} wow${wow.map(function(w,i){returnhx`<b>${w}</b>\n`})} <table>${frag}</table></div>`console.log(toString(tree))varh=require('hyperscript')varhyperx=require('hyperx')varhx=hyperx(h)vartitle='world'varwow=[1,2,3]vartree=hx`<div> <h1 data-y="ab${1+2}cd">hello ${title}!</h1>${hx`<i>cool</i>`} wow${wow.map(function(w){returnhx`<b>${w}</b>\n`})}</div>`console.log(tree.outerHTML)varvdom=require('virtual-dom')varhyperx=require('hyperx')varhx=hyperx(vdom.h)varmain=require('main-loop')varloop=main({times: 0},render,vdom)document.querySelector('#content').appendChild(loop.target)functionrender(state){returnhx`<div> <h1>clicked ${state.times} times</h1> <button onclick=${onclick}>click me!</button> </div>`functiononclick(){loop.update({times: state.times+1})}}varReact=require('react')varrender=require('react-dom').rendervarhyperx=require('hyperx')varhx=hyperx(React.createElement)varApp=React.createClass({getInitialState: function(){return{n: 0}},render: function(){returnhx`<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'))varhyperx=require('hyperx')varconvertTaggedTemplateOutputToDomBuilder=hyperx(function(tagName,attrs,children){console.log(tagName,attrs,children)})convertTaggedTemplateOutputToDomBuilder`<h1>hello world</h1>`// Running this produces: h1 {} [ 'hello world' ]var hyperx = require('hyperx')
Return a tagged template function hx from a hyperscript-style factory function
h.
Values to use for h:
- virtual-dom -
vdom.h - react -
React.createElementwith parameterchildrenspread - hyperscript - hyperscript
Optionally provide:
opts.concat(a, b)- custom concatenation function to combine quasiliteral strings with expressions. Thehfactory 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 whenfalseopts.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
- http://www.2ality.com/2014/07/jsx-template-strings.html?m=1
- http://facebook.github.io/jsx/#why-not-template-literals (respectfully disagree)
BSD
npm install hyperx