Giter Site home page Giter Site logo

cel.js's Introduction

cel

Cel.js is a simple HTML Element creation library written in TypeScript.

It allows the developer to specify the structure of an HTML element including its various attributes in a JSON object, and pass that to the Cel constructor to create their HTML element.

It turns code like:

var myElement = document.createElement("div");
myElement.id = "myElement";
myElement.classList.add("these", "are", "my", "classes");
myElement.style.position = "absolute";
myElement.style.width = "480px";
myElement.style.margin = "0 auto";
myElement.style.color = "magenta";

into:

var myElement = cel({
  type: "div",
  id: "myElement",
  classes: ["these", "are", "my", "classes"],
  style: {
    position: "absolute",
    width: "480px",
    margin: "0 auto",
    color: "magenta"
  }
});

I admit that it does not seem like a "space saver" -- in fact it seems like the Cel version has more lines than the sans-Cel version. This is the cost of expressiveness: the ability to specify your HTML element with one function call and a declarative syntax.

This could be used if you are hesitant add jQuery to your project just for the ability to declare HTML elements in JavaScript.

It would excel in a highly dynamic environment (e.g. a Single Page Application), where the UI could be declared using Cel options.

Some code snippets

/* Create a div */
var newDiv = cel({ innerText: "Hello" + " world!"});

/* Create an img */
var newImg = cel({
  type: "img",
  attrs: {
    src: "//placekitten.com/100/100"
  }
});

/* Create a header */
var headerOptions = {
  "type": "div",
  "id": "header",
  "classes": ["fixed-top", "full-width"],
  "children": [
    cel({
      "type": "img",
      "id": "logo",
      "attrs": {
        "src": "//placekitten.com/g/64/64"
      }
    }),
    cel({
      "type": "h1",
      "id": "brand",
      "innerText": "Kittens"
    })
  ]
};
var newHeader = cel(headerOptions);

cel.js's People

Contributors

sea-grass avatar

Stargazers

Pavlo Kuzhel avatar

Watchers

James Cloos avatar  avatar Pavlo Kuzhel avatar  avatar

Forkers

pkuzhel

cel.js's Issues

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.