• Stars
    star
    362
  • Rank 117,671 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 12 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

a small, simple, and fast DOM creation utility

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

More Repositories

1

scroll-into-view

Scrolls an element into view if possible
JavaScript
337
star
2

fastn

Create ultra-lightweight UI components
JavaScript
95
star
3

righto

Lazy eventuals with support for CPS, Promises, and Iterators.
JavaScript
42
star
4

abbott

Abbott breaks promises. Turns them back into CPS style
JavaScript
26
star
5

kgo

A stupidly easy flow control function.
JavaScript
17
star
6

es-twentysixnext

contains both facts and opinions about es-whateverthefuck
JavaScript
14
star
7

doc

Tiny DOM API helpers
JavaScript
11
star
8

flaps

A tiny module for creating android-like swipe-from-the-side menus.
JavaScript
11
star
9

iwantthenbn

I WANT THE NBN!
JavaScript
10
star
10

print-splitter

Splits up a DOM node into a set of pages for printing
HTML
9
star
11

presh

An immutable interpreted expression language.
JavaScript
6
star
12

svgtopng

Converts SVGs to PNGs
JavaScript
6
star
13

ify

ify's stuff
JavaScript
5
star
14

ooze

A model event wrapper. The spiritual successor to gedi
JavaScript
5
star
15

righto-v-promise

A comparison of async solutions done with righto and promises
JavaScript
5
star
16

interact

An interaction unifying library.
JavaScript
4
star
17

enti

A super light-weight key-value 'observable' wrapper that works with references.
JavaScript
4
star
18

rooty

allow root-of-project (closest package.json) requires in node with `^`
JavaScript
4
star
19

turnbull

Takes a perfectly good stream and throttles it down to nearly useless, optionally just failing outright.
JavaScript
3
star
20

parcss

simple css parser
JavaScript
3
star
21

statham

A layer on top of JSON that handles object references
JavaScript
3
star
22

predicty

A super-lightweight auto-complete component.
JavaScript
3
star
23

gaffa-template

A template application for the gaffa framework
JavaScript
2
star
24

evolution

first go at a neural network evolutionary AI.
JavaScript
2
star
25

wtf-rtc

Theoretically a simple wrapper around WebRTC for making data channel connections
JavaScript
2
star
26

uscp

ultra-sonic communication protocol
JavaScript
2
star
27

globalify

globalifies an NPM module.
JavaScript
2
star
28

debug-async

Async debugging challenges
JavaScript
2
star
29

space-tabs

glob-convert spaces/tabs
JavaScript
2
star
30

js-to-doge

very improve, much converter, wow.
JavaScript
2
star
31

how-i-build

How I build software. For explanation to others, and to remind myself.
2
star
32

cyclic-deep-extend

a strict deep-extend that can handle cyclic structures
JavaScript
2
star
33

ohboy

Here I go, reinventing the wheel again..
JavaScript
2
star
34

unbox

get/set/etc into objs via an array of keys
JavaScript
2
star
35

dom-lightning

A hard fork of dom-lite
JavaScript
2
star
36

fastn-virtual-component

fastn components for rendering dom-lite nodes
JavaScript
2
star
37

terrace

automatically layout fixed UI elements based on layers.
JavaScript
2
star
38

potatoscript

how js works explained with potatos
JavaScript
2
star
39

route-tree

A simple named route matcher
JavaScript
2
star
40

to-groups

Take an array of items, and produce an object with groups of its elements based on a hashing function.
JavaScript
2
star
41

can-scroll

Return the number of pixels an element can be scrolled by in all directions
JavaScript
2
star
42

datastore-mock

An in-process mock for google datastore.
JavaScript
2
star
43

automage

Interact with DOM like a human.
JavaScript
2
star
44

request-data

HTML
2
star
45

default-style

simple default style helper for client-side components
JavaScript
1
star
46

quick-click

Makes bad mobile browsers (iOS safari) click quickly
JavaScript
1
star
47

fastn-custom-elements

JavaScript
1
star
48

skeddy

Simple async task scheduler with catch-up for long running tasks
JavaScript
1
star
49

blazon

A small, fast, non-trivial, dependancy-free, non-stick, fully-franked, run-time type checker with an idiomatic API
JavaScript
1
star
50

gaffa-clean

JavaScript
1
star
51

bench

A lightweight JavaScript bench-marking tool
JavaScript
1
star
52

activity-router

application router that supports multiple activities.
JavaScript
1
star
53

dial-roller

infinite rolling dial
JavaScript
1
star
54

color-distance

calculates the distance between two colours
JavaScript
1
star
55

grabetha

JavaScript
1
star
56

Elder

Elder scrolls...
JavaScript
1
star
57

t-bag

Like a toast, but dangling from above..
JavaScript
1
star
58

distinct-values

return a distinct set of values based on a hashing function
JavaScript
1
star
59

dom-to-js

Creates DOM-generating-code, from DOM
JavaScript
1
star
60

crelns

a createElementNS wrapper around crel
JavaScript
1
star
61

rootify

allow root-of-project (closest package.json) requires with `^`
JavaScript
1
star
62

closey

A hopefully fast, simple, quad-tree implementation.
JavaScript
1
star
63

window-location

a window.location mock for testing in node
JavaScript
1
star
64

fredit

a friendly wysiwyg editor.
JavaScript
1
star
65

changelogit

Changelog viewer from commits
JavaScript
1
star
66

jef

Just enough framework
JavaScript
1
star
67

gaffa-three

threejs views for gaffa
JavaScript
1
star
68

css-name

convert a DOM style name to a css name
JavaScript
1
star
69

fastn-dom

A fastn DOM component-renderer
JavaScript
1
star
70

wait-for

Tiny callback counting module to run a function when all watched functions have completed
JavaScript
1
star
71

sassquatch

Watches scss, builds on change.
JavaScript
1
star
72

locale-detector

Detects locales from an Accept-Language string, against a list of known locales.
JavaScript
1
star
73

fancy-props

DOM nodes have an eclectic (horrible) collection of properties and attributes, this library attempts to bring sanity to setting and getting them.
JavaScript
1
star
74

to-datauri

get a data-uri from an image path
JavaScript
1
star
75

idol

auto svg to png converter with caching and stepped sizing.
JavaScript
1
star
76

aquaduck

JavaScript
1
star
77

cyclic-deep-equal

strict deep-equal that can handle cyclic structures.
JavaScript
1
star
78

laidout

Call a callback when an element has been laid out with css
JavaScript
1
star
79

cpjax

callback passing javascript and xtra stuff hopefully not xml
JavaScript
1
star
80

sequelazy

Add lazy execution callbacks to sequelize
JavaScript
1
star
81

double-tap

pop a exit code in dat processes kill
JavaScript
1
star
82

ieify

takes valid code, and makes it more valid for syntactically challenged environments
JavaScript
1
star
83

seedy-random

iny, Very very bad seeded RNG for use in games.
JavaScript
1
star
84

jspect

quickly Inspect a .js export or the contents of a .json file from the command line.
JavaScript
1
star
85

tots

Compositional inheritance - Make little chunks of mashed together things.
JavaScript
1
star
86

lenze

cross-thread data-replication with function-invoking
JavaScript
1
star
87

fixed-fix

makes position fixed elements go generally in the right place in iOS safari
JavaScript
1
star
88

fastn-markdown-component

Markdown renderer for fastn
JavaScript
1
star
89

zack

wrap a task in a tranZacktion.
JavaScript
1
star
90

cherrypick

cherrypick props off an object
JavaScript
1
star
91

fastn-flickr

test app to compare fastn to the react vs vanilla article
JavaScript
1
star
92

lang

A small library for implementing custom languages in.
JavaScript
1
star
93

rest-args

A tiny util for getting all non-named arguments from a function via an arguments object.
JavaScript
1
star
94

picker-component

A flexible component to use where you might use a `<select>` Built with `fastn.js`
JavaScript
1
star