npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

elementx

v3.1.2

Published

Create complex DOM elements/trees using a functional approach

Downloads

28

Readme

logo

​:zap:​ Create complex DOM elements/trees using a functional approach.

npm version build status test coverage dependency status license js standard style downloads per month

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))
  )
)

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

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')

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.

  • 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)

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>
 */

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)

External tools

Tests

> npm test

License

MIT