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

dom-elementals

v1.0.1

Published

Easy DOM creation, and identity

Downloads

4

Readme

dom-elementals

Install

npm install --save dom-elementals

About

It's kind of complicated. Let's say it's about making some certain things easier when it comes to browser DOM.

toElement()

import { toElement } from 'dom-elementals';

toElement is the most important thing. It has an extremely overloaded API.

toElement(selector) -> selected element

Simply select a single element from the DOM. This is equivalent to document.querySelector().

toElement(html string) -> new element

Create a DOM element, or DOM DocumentFragment from an html string.

If there are multiple elements in the top level toElement() returns a DocumentFragment. If there is one top level element toElement() will return the DOM element version of that.

toElement(element|fragment) -> element|fragment

If you pass an element, or a DocumentFragment to toElement() you get it back unaltered. This is for composability purposes.

toElement(array) -> fragment

Warning: Infinite circular recursion possible.

Pass an array to toElement(), and it will iterate over that array making elements out of the array values. All array values can be something toElement() accepts. All new elements are combined into a DocumentFragment.

toElement(object) -> new element

Warning: Infinite circular recursion possible.

Pass an object with an element property. That element property will be turned into the element returned by toElement().

Or pass an object like this:

//Create an element
let element = toElement({
    //The tag property is the only required property
    //Here we create a paragraph element
    //tag is equivalent to element.tagName.toLowerCase()
    "tag": "p",
    //Properties in general are set on element
    //All properties that belong to DOM elements are acceptable
    "id": "first-name",
    "textContent": "Tabitha",
    "className": "paragraph-name",
    //Add attributes
    //Just like element.setAttribute(name, value)
    "attributes": {

    },
    //These children will be appended to the element
    "children": [
        //toElement(children[index]) will convert each child
    ],
    //Set element.dataset values
    //To set data-* attributes you might need a polyfill
    "data": {
        "value": 1,
        //Use camelcase, or dash case
        "other-value": 2,
        "camelValue": 3
    },
    //The parent to append element to.
    //This is passed to toElement() as well.
    "parent": "#parent-id-selector",
    //Set the first html of the element
    "head": "<h2>I'm set before anything else</h2>",
    //Set the last html of the element
    "foot": "<footer>I'm set after everything else</footer>",
    //Set styles
    "style": {
        "color": "blue"
    }
});

Create a simple input while setting the value.

let input = toElement({
    tag: 'input',
    value: "I'm a value"
});

toElement(strings, ...values) -> new element

This is an interface to template literals. All values are converted to javascript primitive values (string, number, ...)--even the elements.

You can use it directly:

let value = 'Hello Universe!';
let element = toElement`<p>${value}</p>`;

Or indirectly:

function createElement(strings, ...values){
    return toElement(strings, ...values);
}
let value = 'Hello Cosmos!';
let element = createElement`<p>${value}</p>`;

toHTML()

import { toHTML } from 'dom-elementals';

toHTML(value) can except these values:

  • DOM element
  • object with an element property
  • DocumentFragment

arrayFrom()

arrayFrom(arrayLike) is not really a DOM specific thing, but conversion of array like objects is so common it is included.

setAttributes()

setAttributes(element, object) is used to set all the attributes of a given object on to the element.

isElement()

isElement(value) returns true if value is an element.