Git Product home page Git Product logo

bel's Introduction

NPM version build status Downloads js-standard-style experimental

A simple library for composable DOM elements using tagged template strings.

If you're looking for a higher level front end framework, try yo-yo. Or even higher than that, try choo.

usage

For a more in depth tutorial on getting started, please check out the wiki.

A Simple Element

Create an element:

// list.js
var bel = require('bel')

module.exports = function (items) {
  return bel`<ul>
    ${items.map(function (item) {
      return bel`<li>${item}</li>`
    })}
  </ul>`
}

Then pass data to it and add to the DOM:

// app.js
var createList = require('./list.js')
var list = createList([
  'grizzly',
  'polar',
  'brown'
])
document.body.appendChild(list)

Data Down, Actions Up

// list.js
var bel = require('bel')

// The DOM is built by the data passed in
module.exports = function (items, onselected) {
  function render () {
    return bel`<ul>
    ${items.map(function (item) {
      return bel`<li>${button(item.id, item.label)}</li>`
    })}
    </ul>`
  }
  function button (id, label) {
    return bel`<button onclick=${function () {
      // Then action gets sent up
      onselected(id)
    }}>${label}</button>`
  }
  var element = render()
  return element
}
// app.js
var bel = require('bel')
var morphdom = require('morphdom')
var list = require('./list.js')

module.exports = function (bears) {
  function onselected (id) {
    // When a bear is selected, rerender with the newly selected item
    // This will use DOM diffing to render, sending the data back down again
    morphdom(element, render(id))
  }
  function render (selected) {
    return bel`<div className="app">
      <h1>Selected: ${selected}</h1>
      ${list(bears, onselected)}
    </div>`
  }
  // On first render, we haven't selected anything
  var element = render('none')
  return element
}

use with/without hyperx

hyperx is built into bel but there may be times when you wish to use your own version or implementation of hyperx. Or if you prefer to create elements using bel without using tagged template literals:

var createElement = require('bel').createElement
var hyperx = require('hyperx')
var bel = hyperx(createElement)

var element = bel`<div class="heading">Hello!</div>`

// ...

var sameElement = createElement('div', { className: 'heading' }, ['Hello!'])

use yo-yoify to build

Transform bel template strings into pure and fast document calls with browserify.

e.g. browserify entry.js -g yo-yoify -o bundle.js

See also

note

Please use yo-yoify which will transform any Function.caller into plain strings until an alternative solution to identify element creators is implemented.

yo-yoify can resolve the error like below:

TypeError: Function.caller used to retrieve strict caller

or

TypeError: access to strict mode caller function is censored

security

bel sets attributes with element.setAttribute() and element.setAttributeNS(), and creates text nodes with document.createTextNode(). These approaches mitigate some Cross-Site Scripting (XSS) attacks. You should still code carefully every time you put content from users in the DOM.

similar projects

  • vel minimal virtual-dom library
  • base-element An element authoring library for creating standalone and performant elements
  • virtual-dom A Virtual DOM and diffing algorithm
  • hyperscript Create HyperText with JavaScript.

license

(c) 2016 Kyle Robinson Young. MIT License

bel's People

Contributors

chromakode avatar kemitchell avatar shama avatar stevemao avatar tgfjt avatar tornqvist avatar yoshuawuyts avatar

Stargazers

 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.