package
Create complex DOM elements/trees using a functional approach
npm install elementxGitHub-flavored package documentation.
Across all versions.
Download trend unavailable.
Runtime metadata.
Tagged topics.
Latest published Mar 20, 2017
latest

: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))
)
)1.99 kB minified and gzipped> npm install elementxconst { 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>
*/Each HTML tag is exposed as a function when requiring elementx.
// using destructuring
const { div, h1, p, button } = require('elementx')These functions have the following syntax:
tag(tagName, attributes, children)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.
{ href: '#header' }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 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)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>
*/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)> npm testEstimated unpacked size across 5 packages.
78.0 kB
Includes dependencies.
3 direct · 2 outdated