hyperscript-markup
This is a Babel plugin that transpiles markup into hyperscript. Compatible with any hyperscript library that accepts hyperscript in the form of hyperscriptFunction(tag, attributes, [children])
. Works with React, Mithril, and Hyperapp.
Template Syntax
Views are declared by the magic sequence $>
. This will transpile the template that follows it into hyperscript calls.
You can specify which hyperscript function to call like this:
$>
-- Transpile toReact.createElement()
.$m>
-- Transpile tom()
.$h>
-- Transpile toh()
.$yourFunc>
-- Transpile toyourFunc()
.
const view = $> divintro ul li > 'Some' li > 'List items' ulanother-list item li > item form 'Check this' ReactDOM
Elements
Any html element can be expressed in parentheses:
img
CSS classes can be set using the .
operator:
imgmy-class-namemy-other-class-name
An element id can be set with the +
operator (as # wouldn't be valid haxe syntax):
img+my-id
Attributes can be used inside the selector:
Attributes can also be expressed separately:
A component needs to have it's first letter capitialized:
div
Children
A shortcut for defining one child:
h1 > 'My title'
More than one child can be nested by using indentation:
nav ullinks li > 'Haxe' li > 'Github'
Inline expressions
Strings and template strings are supported.
h1 'A string for example'button ``
Prefix an expression or identifier with the tilde operator ~
to embed an expression without a call to the hyperscript functor.
div ~expression
Would translate to:
React
Conditionals
$if
, $elseif
, and $else
can be used inside templates:
h1 > 'Big' h2 > 'Not that big'$else p > 'Paragraph'
Map
The following syntax can be used for any object (in this case links
) with a map method:
link > linktitle
Map with null check
Using the >>
operator adds a null check prior to map execution.
links >> link > linktitle
Translates to:
if links != null links;else ;
Sample
An example can be found at examples/simple
.
Initial build:
cd examples/simplenpm installnpm run buildcd htdocssee index.html