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

@hirojs/dom-build

v1.3.0

Published

Build DOM trees in Javascript

Downloads

8

Readme

dom-build

Build DOM trees in Javascript.

Installation

Install:

npm install --save @hirojs/dom-build

Require:

const D = require('@hirojs/dom-build');

Usage

The general form is:

D(selector, contents...)

Selector defines the type/ID/classlist of the created element and usually takes the form element#id.class1.class2.class3; all components are optional but at least one must be present. If element is omitted, div is assumed. A second selector form, %text, is available for creating text nodes.

Each item in contents can be one of:

  • string or number: appended to element as text
  • Array: all array contents are appended to the element, recursively
  • DOM element or text node: appended to element
  • object: each key-value pair is considered according to the following rules:
    • key == style: if value is string, assigned to element.style.cssText; if object, each key/value pair is attached to element.style
    • key == "properties": value must be an object; each key/value pair is attached directly to element
    • key == "innerHTML": value is assigned to element.innerHTML
    • key == "data": value must be an object; each key/value pair is mapped to a corresponding data- attribute on the element
    • all other key/value pairs are attached as attributes to element, with some special rules:
      • value === true: a no-value boolean attribute is set
      • value === false: no attribute is set
      • typeof(value) == "function": an event listener is attached if key starts with on e.g. onmouseover
      • typeof(value) == "object": attribute value is JSON-encoded

Any falsey values (false, null, undefined) in contents are skipped.

Any functions present in contents will be called to yield a value; if another function is returned, this will be called, and so on, until a non-function value is received.

Example

const D = require('@hirojs/dom-build');

var ui = D('#root.a.b.c',

  "This is a text node", D('br'),
  "This is another text node", D('br'),
  
  D('span',
    // Properties will be attached directly to the object
    { properties: { a: 123 } },

    // Create a text object by concatenating 3 strings
    D('%text',
      'This is an explicit text node; it will be returned.',
      ' Multiple strings ',
      'can be added'
    )
  ),

  D('br'),

  D('a.active',
    { href: "/foo/bar",
      // Added as a click event listener
      onclick: function(evt) { evt.preventDefault(); alert("hello!"); } },
    "Click me! ", [
      D("b", "here's some bold text"),
      " ",
      D("i", "here's some italic text")
    ]
  ),

  // Set style properties.
  // Also note the trailing falsey values are skipped as children for this element
  D("div", {style: {width: 100, height: 100, backgroundColor: 'red'}}, 0, null, void 0, false),

  // Set innerHTML directly
  D("div", {innerHTML: '<b>HERE IS <i>SOME RAW</i> HTML</b>'}),

  // Element is omitted so will be div.data-indicator
  // This will set data-test="100" and data-json='{"foo":"bar"}'
  D(".data-indicator", {data: {test: 100, json:{foo: "bar"}}}, 'the data value is:')
);