JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 4
  • Score
    100M100P100Q27048F
  • License MIT

Functionally create elements and compose them to a tree quickly

Package Exports

  • elementx

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (elementx) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

logo

Build status NPM version Dependency Status License Js Standard Style

⚡ Functionally create DOM elements and compose them to a tree quickly.

This module is an alternative to jsx or template strings for those who want to build up their DOM trees using plain function composition.

div([
  h1('.bold', 'elementx'),
  h2('#subtitle', 'Create a DOM tree with ease'),
  button({ href: 'http://ghub.io/elementx' }, 'Open'),
  ul(['simple', 'functional', 'fast'].map(key => li(key)))
])

Features

  • Supports SVG
  • Supports boolean attributes like autofocus: true
  • Weights only 2.52 kB minified and gzipped
  • Pluggable API for hooking into specific attributes and modifying them
  • Functional utilities can be used since it's just functions
  • Can be used with diffing libraries like morphdom or nanomorph for a unidirectional architecture

Installation

> npm install elementx

Usage

const { div, h1, a } = require('elementx')

const tree = div('.container.p2#js-root', [
  h1('.title', 'This is a title'),
  div({ style: 'background-color: red;' }, [
    a({ href: 'http://github.com' }, 'Github')
  ])
])

console.log(tree.outerHTML)
/*
 * ->
 * <div class="full-width p2">
 *   <h1>Some text</h1>
 *   <div style="background-color: red;">
 *     <a href="http://github.com">Github</a>
 *   </div>
 * </div>
 */

Getting Started

Each element in the DOM is exposed as a function when requiring elementx.

const { div, h1, p, button } = require('elementx')

These functions have the following syntax:

tag(selector, 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.

  • selector can be .title to append a class or #id to give the element an id. These can be mixed as you might expect: #id.title.pad.red
  • 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.

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')
// -> or { createElement }

const node = h('h1', 'text')

console.log(node.outerHTML)
/* 
 * ->
 * <h1>text</h1>
 */

Decorating attributes

To process an attribute further you can use the decorate submodule which allows you to hook into them:

create-element.js

const decorate = require('elementx/decorate')

// automatically inlines style objects. Make sure to return the original value
// if you don't modify anything.
module.exports = decorate((attr, value) => {
    case 'style':
      return typeof value !== 'string'
        ? toInlineStyle(value)
        : value
    default:
      return value
})

Differences from hyperscript

This module is a lot smaller because its focused on only creating DOM elements. Feel free to built upon this if you feel like needing any of the following features:

createElement('text') // -> doesn't generate <div>Text</div>

External tools

Tests

Tests are written using JSDOM.

> npm test

License

MIT

The icon in the title was created by Daniel Bruce under the Creative Commons Attribution-Share Alike 3.0 Unported License