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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@gilchrist/make-element

v1.0.3

Published

A tiny utility for creating DOM elements with nested JS objects

Readme

makeElement() — Tiny DOM tree creation

This module exports one function: makeElement(), which creates DOM Element trees from nested JavaScript objects. It's meant to pair nicely with browser APIs and get element-generating code off the ground quickly.

Function Reference

makeElement(input: string | object | HTMLElement | Array | ...args)

Creates one or more DOM elements from the given input, based on its data type:

  • HTMLElement — returned as-is.

  • String — parsed shorthand for a single element (eg: "div#id.class | Text").

  • Object — full element definition (supports tag, child, children, attributes, style, etc.).

  • Array — multiple definitions, each processed recursively.

  • Multiple arguments — treated like an array, combined into one wrapper <div>.

Returns either a single HTMLElement or an array of HTMLElements, depending on how it is called.

HTMLElement

When makeElement encounters an existing HTMLElement, it adds it to the tree, as-is. This allows you to mix elements created with makeElement and those from browser DOM APIs:

const button = document.createElement("button");
button.textContent = "Click me";

const wrapper = makeElement({
  tag: "div.wrapper",
  child: button,
});

Array or multiple arguments

When makeElement is called with either an array or multiple arguments, it returns an array of multiple elements. If any of those items are arrays, they're wrapped by a <div>.

makeElement([
  "h1 | Hello World",
  { tag: "p", text: "This is a paragraph" },
  ["span|Inline 1", "span|Inline 2"],  // these spans will be in a <div>
]);

Element string

When encountering a string, it is parsed with a simple format to create one element. This format is similar to CSS selectors, but does not assign meaning to whitespace.

| Syntax | Description | Example | | ------------ | -------------------- | ----------------- | | Staring word | Tag name | div | | . | Define a class | div.classname | | # | Sets the ID | section#id-name | | | | Defines text content | h1 | My Heading |

Whitespace Handling

Unlike CSS selectors, element strings are not whitespace-sensitive; classes and IDs can have spaces between them for readability (eg: h1 #main-heading .big .underline | My Page).

note: The text of the element (denoted by a |), also gets trimmed. To preserve whitespace, use an object with a text property.

Object

When makeElement encounters a generic object, it reads its properties to construct the element. The properties are interpreted as follows:

| Property | Description | | ------------------- | ------------------------------------------------------------------------------------------------- | | tag | Element tag string (eg: "div#id.class"). | | class / classes | Additional class names (string or array). | | innerHTML | Raw HTML string. | | text | Sets the element's textContent property. | | style | Object of CSS property/value pairs. | | child | Single nested element (recursively processed). | | children | Array of nested elements (recursively processed). | | onClick | Sets the element's onclick handler. | | onChange | Sets the element's onchange handler. | | events | Object of event handlers. Keys are the name of the event, and values are event handler function (element.addEventListener(key, value)). Handlers are either functions or null, in which case they are skipped. | | attributes | Object of attribute values. | | data | Object of data-* attributes ({ userId: 123 }element.dataset["data-user-id"] = "123"). | | apply | After the element is created, runs a function with the element as an argument (apply(element)). | | bind | Takes an object of functions, and binds and assigns each function into the Element object. | | from | Instead of creating an element, assign properties into an existing element. | | assign | Takes an object, and assigns each property into the Element object using Object.assign(). |

A Longer Example

makeElement({
  // Append elements and assign styles into the document body
  from: document.body,

  style: {
    color:      "#222",
    lineHeight: "1.4",
    margin:     "0",
    fontFamily: "sans-serif",
  },

  child: {
    tag: "main",
    style: {
      maxWidth: "800px",
      margin:   "2em auto",
    },

    children: [
      { tag: "h1 .title | Make Element Example",
        style: {
          borderBottom: "6px double #ccc",
        },
      },

      { tag: "div.card",

        style: {
          padding:       "1em",
          boxShadow:     "2px 10px 24px #0002",
          border:        "1px solid #888",
          display:       "flex",
          flexDirection: "column",
          maxWidth:      "250px",
        },

        children: [
          { tag: "h2 | Card Title",
            style: { margin: "0" },
          },

          { tag: "p",
            text: "Paragraph with text",
            style: {
              fontStyle: "italic",
              margin:    "0.5em 0",
            },
          },

          { tag: "button | Close",
            onClick: function () {
              this.parentElement.remove();
            },
          },
        ],
      },
    ],
  },
});

License

MIT