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

cre

v0.4.0

Published

A nicer way to create DOM content.

Downloads

83

Readme

cre

Micro-framework for constructing DOM nodes.

cre is, effectively, a better document.createElement, one that will also work as document.createDocumentFragment when given an array of nodes to insert into the fragment, or document.createTextNode when given an array of only one string.

Its interface is similar to FastMail's el function, or crel, which I only found by searching for names I might give this.

Usage

Pardon our dust: This documentation needs some work (it is, after all, a pre-1.0 package): for now, here's a simple example that demonstrates the basic functionality of this module (courtesy this tweet):

import cre from 'https://unpkg.com/[email protected]/cre.js';
function render() {
  document.querySelector('#main').appendChild(cre('h1.big', 'header'));
}
document.addEventListener('DOMContentLoaded', render);

Setting options

cre also supports setting attributes and properties on the created element on creation, as well as adding event listeners, by adding an additional parameter with an object of options.

By default, this options object's own properties will be set accordingly on the created element in its creation, with the following exceptions:

  • namespaceURI and is are only used during element creation and not assigned to the element itself.
  • className and classList receive special handling so that they may be mixed together, along with class specifiers in the "tag name" specifier.
  • Properties of style are applied recursively.
  • attributes applies a series of objects with name and value property pairs to the element (this interface will likely change soon).
  • Properties of on are treated as event names, and addEventListener is called on either the value of the property, or of every value of the array in the property. Listeners that need to be added with options can provide an object with those options as properties, and the listener specified as the listener method of the property.

Effective use

cre works best when you're passing it to appendChild or insertBefore. cre also works best when you use it to clone templatized nodes, rather than constructing new elements from scratch every time (because cloning DOM nodes is faster than creating elements from scratch).

Examples

Here are just a few of the things you can do easily with cre, which would otherwise be somewhat verbose and annoying:

Making template elements

(this example is taken directly from the source code for the Tabalanche Chrome extension)

var templateTabIcon = cre('img.tabicon');
var templateTabLink = cre('a.tablink');
var templateTabListItem = cre('li.tablist-item');
var templateTabStash = cre('div.tabgroup.tabstash');
var templateFlap = cre('div.flap');
var templateTabList = cre('ul.tablist');

Cloning and populating templates

(this example is also taken directly from the source code for the Tabalanche Chrome extension)

function createTabListItem(tab) {
  var tabIcon = cre(templateTabIcon,
    {src: tab.icon || platform.faviconPath(tab.url)});

  var tabLink = cre(templateTabLink, {href: tab.url},
    [tabIcon, tab.title]);

  var listItem = cre(templateTabListItem, [tabLink]);

  /* (event listener code omitted for brevity) */

  return listItem;
}

Appending a text node to an element and manipulating it

var statusMessage = cre([' This message will self-destruct in five seconds.'])
var paragraphs = document.getElementsByTagName('p');
var lastParagraph = paragraphs[paragraphs.length-1];
lastParagraph.appendChild(statusMessage);
setTimeout(function(){
  statusMessage.textContent = ' This message has self-destructed. Have a nice day.'
}, 5000);

Creating a document fragment with spans of text and inserting it

function payItForwardWarning(favorCount) {
  return cre(['You have ', cre('span.favor-count', favorCount*3), ' favors to pay forward'])
}

var alertTicker = document.getElementById('alerts');
var firstMiscThingInTicker = alertTicker.querySelector('.misc');
var freshWarning = payItForwardWarning(user.favorsReceived);
alertTicker.insertBefore(freshWarning, firstMiscThingInTicker);

Using cre for another document

import CreContext from "cre";

const foreignCre = new CreContext(foreignDocument);