• Stars
    star
    1,010
  • Rank 45,519 (Top 0.9 %)
  • Language
    JavaScript
  • License
    BSD 2-Clause "Sim...
  • Created almost 9 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🏷 - tagged template string virtual dom builder

hyperx

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

var vdom = require('virtual-dom')
var hyperx = require('hyperx')
var hx = hyperx(vdom.h)

var title = 'world'
var wow = [1,2,3]
var 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

var React = require('react')
var toString = require('react-dom/server').renderToString
var hyperx = require('hyperx')
var 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))
  }
})

var title = 'world'
var wow = [1,2,3]
var frag = hx`
  <tr> <td>row1</td> </tr>
  <tr> <td>row2</td> </tr>
`
var 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(toString(tree))

hyperscript node example

var h = require('hyperscript')
var hyperx = require('hyperx')
var hx = hyperx(h)

var title = 'world'
var wow = [1,2,3]
var 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

var vdom = require('virtual-dom')
var hyperx = require('hyperx')
var hx = hyperx(vdom.h)

var main = require('main-loop')
var 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

var React = require('react')
var render = require('react-dom').render
var hyperx = require('hyperx')
var hx = hyperx(React.createElement)

var 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

var hyperx = require('hyperx')

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

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

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

api

var hyperx = require('hyperx')

var 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

install

npm install hyperx

More Repositories

1

choo

πŸš‚πŸš‹ - sturdy 4kb frontend framework
JavaScript
6,776
star
2

bankai

πŸš‰ - friendly web compiler
JavaScript
1,088
star
3

nanomorph

πŸš… - Hyper fast diffing algorithm for real DOM nodes
JavaScript
726
star
4

nanohtml

πŸ‰ HTML template strings for the Browser with support for Server Side Rendering in Node.
JavaScript
687
star
5

nanographql

Tiny graphQL client library
JavaScript
421
star
6

nanocomponent

πŸšƒ - create performant HTML components
JavaScript
366
star
7

wayfarer

πŸ‘“ composable trie based router
JavaScript
332
star
8

choo-handbook

πŸš‚βœ‹πŸ“– - Learn the choo framework through a set of exercises
HTML
268
star
9

nanobus

🚎 - Tiny message bus
JavaScript
225
star
10

awesome-choo

πŸŒ… Awesome things related with choo framework
197
star
11

create-choo-app

🚞 - create a fresh choo application
JavaScript
181
star
12

nanostate

🚦- Small Finite State Machines
JavaScript
170
star
13

nanorouter

πŸ›€ - Small frontend router
JavaScript
116
star
14

nanocomponent-adapters

πŸ”Œ - Convert a nanocomponent to a component for your favourite API or library (web components, (p)react, angular)
JavaScript
96
star
15

choop

πŸš‚βš›οΈ - choo architecture for preact
JavaScript
93
star
16

on-idle

😴 - Detect when the browser is idle
JavaScript
82
star
17

nanologger

πŸ“œ - Cute browser logs
JavaScript
80
star
18

nanoanimation

πŸ‘¨β€πŸŽ¨ - Safety wrapper around the Web Animation API
JavaScript
72
star
19

nanoraf

🎞 - Only call RAF when needed
JavaScript
71
star
20

choo-devtools

πŸ’Ό - Expose a choo instance on the window
JavaScript
53
star
21

nanoquery

πŸ“‡ - Tiny querystring module
JavaScript
49
star
22

nanotask

Microtask queue scheduler for the browser
JavaScript
47
star
23

choo-log

πŸ“ƒ - Development logger for choo
JavaScript
47
star
24

nanoscheduler

Schedule work to be completed when the user agent is idle.
JavaScript
46
star
25

website

πŸš‡ - Hyper Train Transfer Protocol (HTTP)
JavaScript
46
star
26

nanohref

β›“ - Tiny href click handler library
JavaScript
41
star
27

nanotick

process.nextTick() batching utility
JavaScript
37
star
28

choo-store

Lightweight state structure for choo apps.
JavaScript
37
star
29

nanotiming

⏲ - Small timing library
JavaScript
35
star
30

create-choo-electron

:electron: - Create a fresh Choo Electron application
JavaScript
29
star
31

object-change-callsite

Determine the callsite of an object change using Proxies
JavaScript
27
star
32

choo-reload

⛽️ - Livereloading package for choo
JavaScript
27
star
33

on-performance

Listen for performance timeline events
JavaScript
26
star
34

nanobeacon

Small navigator.sendBeacon wrapper
JavaScript
25
star
35

choo-service-worker

πŸ‘· - Service worker loader for choo
JavaScript
24
star
36

choo-scaffold

πŸ— - Scaffold out files for a Choo project
JavaScript
24
star
37

choo-notification

Web Notification plugin for Choo
JavaScript
22
star
38

nanobounce

Smol debounce package
JavaScript
19
star
39

choo-choo

πŸŽ“ learn choo from the command line!
JavaScript
19
star
40

nanomount

Mount a DOM tree on a target node
JavaScript
19
star
41

choo-redirect

🎬 - Redirect a view to another view
JavaScript
19
star
42

persist-storage

πŸ—„ - Enable persistent storage in the browser
JavaScript
19
star
43

nanohistory

Small browser history library
JavaScript
14
star
44

choo-hooks

🎣 - Hook into Choo's events and timings
JavaScript
12
star
45

nanolocation

πŸ“- Small window.location library
JavaScript
10
star
46

discuss

🎭 – Discuss project organization, initiatives, and anything else!
8
star
47

nanocache

Cache Nanocomponents.
JavaScript
7
star
48

bankai-website

JavaScript
6
star
49

choo-umd

πŸ™ˆ - umd build for choo framework
HTML
3
star