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

crel

v4.2.1

Published

A small, simple, and fast DOM creation utility

Downloads

33,055

Readme

crel

NPM version Downloads

What

A small, simple, and fast DOM creation utility

Why

Writing HTML is stupid. It's slow, messy, and should not be done in JavaScript.

The best way to make DOM elements is via document.createElement, but making lots of them with it is tedious.

Crel makes this process easier.

Inspiration was taken from laconic, but Crel wont screw with your bad in-DOM event listeners, and it's smaller, faster, etc...

Changelog

View changelog

Installing

For browserify:

npm i crel
let crel = require('crel');

For AMD:

require.config({paths: { crel: 'https://cdnjs.cloudflare.com/ajax/libs/crel/4.0.1/crel.min' }});
require(['crel'], (crel) => {
    // Your code
});

Using ES6+:

import crel from 'crel';

For standard script tag style:

<script src="crel.min.js"></script>

Usage

Syntax:

// Returns a DOM element
crel(tagName / domElement, attributes, child1, child2, childN);

where childN may be:

  • a DOM element,
  • a string, which will be inserted as a textNode,
  • null, which will be ignored, or
  • an Array containing any of the above

Examples

let element = crel('div',
    crel('h1', 'Crello World!'),
    crel('p', 'This is crel'),
    crel('input', { type: 'number' })
);

// Do something with 'element'

You can add attributes that have dashes or reserved keywords in the name, by using strings for the objects keys:

crel('div', { 'class': 'thing', 'data-attribute': 'majigger' });

You can define custom functionality for certain keys seen in the attributes object:

crel.attrMap['on'] = (element, value) => {
    for (let eventName in value) {
        element.addEventListener(eventName, value[eventName]);
    }
};
// Attaches an onClick event to the img element
crel('img', { on: {
    click: () => {
        console.log('Clicked');
    }
}});

You can pass already available elements to Crel to modify their attributes / add child elements to them

crel(document.body, crel('h1', 'Page title'));

You can assign child elements to variables during creation:

let button;
let wrapper = crel('div',
    button  = crel('button')
);

You could probably use Crel to rearrange existing DOM elements..

crel(someDiv, crel(someOtherDiv, anotherOne));

But don't.

Proxy support

If you are using Crel in an environment that supports Proxies, you can also use the new API:

let crel = require('crel').proxy;

let element = crel.div(
    crel.h1('Crello World!'),
    crel.p('This is crel'),
    crel.input({ type: 'number' })
);

Browser support

Crel uses ES6 features, so it'll work in all evergreen browsers.

Goals

Easy to use & Tiny

Less than 1K minified, about 500 bytes gzipped === Smal

Fast

Crel is fast. Depending on what browser you use, it is up there with straight document.createElement calls: http://jsperf.com/dom-creation-libs/10

License

MIT