:zap: Create complex DOM elements/trees using a functional approach.
This module provides an alternative to JSX or template strings for those who want to build up their DOM trees using plain function composition.
const { div, h1, h2, button, ul, li } = require ( 'elementx' )
div (
h1 ({ class: 'bold' }, 'elementx' ),
h2 ({ id: 'subtitle' }, 'Create a DOM tree with ease' ),
button ({ href: 'http://ghub.io/elementx' }, 'Open' ),
ul (
[ 'simple' , 'functional' , 'fast' ]
. map (key => li (key))
)
) copy
Features
Universal - Works in Node and Browser
SVG Support - Supports creating SVG Elements
Functional - Since it's just function composition we can arbitrarily compose them
Small Only 1.99 kB minified and gzipped
Interoperability Can be used with diffing libraries like morphdom , nanomorph or anyhting that uses the DOM
Installation
> npm install elementx copy
Example
const { div, h1, a } = require ( 'elementx' )
const node = div (
h1 ({ class: 'title' }, 'This is a title' ),
div ({ class: 'bg-red' },
a ({ href: 'http://github.com' }, 'Github' )
)
)
// mount the tree to the DOM
document.body. appendChild (node)
console. log (tree.outerHTML)
/*
* ->
* <div class="title">
* <h1>This is a title</h1>
* <div class="bg-red">
* <a href="http://github.com">Github</a>
* </div>
* </div>
*/
Getting Started
Each HTML tag is exposed as a function when requiring elementx.
// using destructuring
const { div, h1, p, button } = require ( 'elementx' ) copy
These functions have the following syntax:
tag (tagName, attributes, children) copy
All arguments are optional with at least one argument needing to be present . This kind of function overloading allows you to iterate on your DOM structure really fast and reduce visual noise.
tagName any valid html tag
attributes is an object of dom attributes: { href: '#header' }
children can be a string for a text node or an array of nodes
Lifecycle hooks
This module aims to be just the element creation layer. It can be used with any view framework using DOM as their base element abstraction for diffing. Some libraries like this include choo or inu .
SVG
SVG works as expected. Sets the appropriate namespace for both elements and attributes. All SVG tags can only be created with the h-helper:
const { svg } = require ( 'elementx' )
const node = svg ({
viewBox: '0 0 0 32 32' ,
fill: 'currentColor' ,
height: '32px' ,
width: '32px'
}, [
h ( 'use' , { 'xlink:href' : '#my-id' })
])
document.body. appendChild (node) copy
Use without helper functions
Sometimes you need to fall back to the traditional createElement(tag, attributes, children) (aliased to h), for example svg tags.
const h = require ( 'elementx' )
const node = h ( 'h1' , 'text' )
console. log (node.outerHTML)
/*
* ->
* <h1>text</h1>
*/ copy
Events
All HTML DOM Events can be attached. The casing of the event name doesn't matter (onClick, onclick, ONCLICK etc.)
const node = h. button ({
onClick : () => console. log ( 'button has been clicked' )
})
document.body. appendChild (node) copy
html-to-hyperscript - Web-Service to convert HTML to hyperscript
Tests
License
MIT