GithubHelp home page GithubHelp logo

tensai75 / crel Goto Github PK

View Code? Open in Web Editor NEW

This project forked from korynunn/crel

0.0 2.0 0.0 84 KB

a small, simple, and fast DOM creation utility

License: MIT License

JavaScript 98.93% HTML 1.07%

crel's Introduction

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 DOM with it is tedious.

crel.js makes the process easier.

Inspiration was taken from https://github.com/joestelmach/laconic, but crel wont screw with your bad in-DOM event listeners, and is smaller, faster, etc...

Usage

Signature:

crel(tagName/dom element [, 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

For browserify:

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

For standard script tag style:

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

To make some DOM:

Example:

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

// Do something with 'element'

You can create attributes with dashes, or reserved keywords, but using strings for the objects keys:

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

You can pass an already available element to crel, and it will be the target of the attributes/child elements

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

You can assign child elements to variables during creation:

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

You could probably use crel to rearrange existing dom..

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:

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

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

Browser support

Crel works in everything (as far as I know), but of course...

IE SUPPORT

If you require this library to work in IE7, add the following after declaring crel.

var testDiv = document.createElement('div'),
    testLabel = document.createElement('label');

testDiv.setAttribute('class', 'a');
testDiv['className'] !== 'a' ? crel.attrMap['class'] = 'className':undefined;
testDiv.setAttribute('name','a');
testDiv['name'] !== 'a' ? crel.attrMap['name'] = function(element, value){
    element.id = value;
}:undefined;


testLabel.setAttribute('for', 'a');
testLabel['htmlFor'] !== 'a' ? crel.attrMap['for'] = 'htmlFor':undefined;

Goals

Easy to use

Tiny

less than 1K minified about 500 bytes gzipped

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

crel's People

Contributors

ap avatar korynunn avatar marianoguerra avatar mauricebutler avatar parameme avatar pdehaan avatar ritschwumm avatar uniphil avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.