HyperScript
Create HyperText with JavaScript, on client or server.
See also mercury is a modular ui framework influenced by hyperscript but much more heavily optimized.
Example
var h =
on the server
you can still use hyperscript on the server, the limitation is that events don't make sense any more, but you can use it to generate html:
console'<h1>hello!</h1>'
h (tag, attrs, [text?, Elements?,...])
Create an HTMLElement
. The first argument must be the tag name, you may use a
fully qualified tagname for building e.g. XML documents: `h('ns:tag').
classes & id
If the tag name is of form name.class1.class2#id
that is a short cut
for setting the class and id.
default tag name
If the tag name begins with a class or id, it defaults to a <div>
.
Attributes
If an {}
object is passed in it will be used to set attributes.
var h =
Note that hyperscript sets properties on the DOM element object, not
attributes on the HTML element. This makes for better consistency across
browsers and a nicer API for booleans. There are some gotchas, however.
Attributes such as colspan
are camel cased to colSpan
, and for
on the
label element is htmlFor
to avoid collision with the language keyword. See the
DOM HTML specification
for details.
events
If an attribute is a function, then it will be registered as an event listener.
var h =
styles
If an attribute has a style property, then that will be handled specially.
var h =
or as a string
var h =
You may pass in attributes in multiple positions, it's no problem!
children - string
If an argument is a string, a TextNode is created in that position.
children - HTMLElement
If a argument is a Node
(or HTMLElement
), for example, the return value of a call to h
thats cool too.
children - null.
This is just ignored.
children - Array
Each item in the array is treated like a ordinary child. (string or HTMLElement) this is useful when you want to iterate over an object:
var h =var obj =a: 'Apple'b: 'Banana'c: 'Cherry'd: 'Durian'e: 'Elder Berry'
Cleaning Up
If you need to clean up a widget created using hyperscript - deregistering all its event handlers and observable listeners, you can use context()
.
var h =var o =var text =// then if you want to remove this widget from the page// to cleanuph
Ecosystem
- html2hscript - Parse HTML into hyperscript
- dom2hscript - Frontend library for parsing HTML into hyperscript using the browser's built in parser.
- html2hscript.herokuapp.com - Online Tool that converts html snippets to hyperscript
- html2hyperscript - Original commandline utility to convert legacy HTML markup into hyperscript
- hyperscript-helpers - write
div(h1('hello')
instead ofh('div', h('h1', 'hello'))
- react-hyperscript - use hyperscript with React.
License
MIT