UNPKG

3.2 kBMarkdownView Raw
1# [bel](https://en.wikipedia.org/wiki/Bel_(mythology))
2
3[![NPM version][npm-image]][npm-url]
4[![build status][travis-image]][travis-url]
5[![Downloads][downloads-image]][downloads-url]
6[![js-standard-style][standard-image]][standard-url]
7![experimental][experimental-image]
8
9A simple library for composable DOM elements using [tagged template strings](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals).
10
11## usage
12
13For a more in depth tutorial on getting started, please [check out the wiki](https://github.com/shama/bel/wiki).
14
15### A Simple Element
16
17Create an element:
18
19```js
20// list.js
21var bel = require('bel')
22
23module.exports = function (items) {
24 return bel`<ul>
25 ${items.map(function (item) {
26 return bel`<li>${item}</li>`
27 })}
28 </ul>`
29}
30```
31
32Then pass data to it and add to the DOM:
33
34```js
35// app.js
36var createList = require('./list.js')
37var list = createList([
38 'grizzly',
39 'polar',
40 'brown'
41])
42document.body.appendChild(list)
43```
44
45### Data Down, Actions Up
46
47```js
48// list.js
49var bel = require('bel')
50
51// The DOM is built by the data passed in
52module.exports = function (items, onselected) {
53 function render () {
54 return bel`<ul>
55 ${items.map(function (item) {
56 return bel`<li>${button(item.id, item.label)}</li>`
57 })}
58 </ul>`
59 }
60 function button (id, label) {
61 return bel`<button onclick=${function () {
62 // Then action gets sent up
63 onselected(id)
64 }}>${label}</button>`
65 }
66 var element = render()
67 return element
68}
69```
70
71```js
72// app.js
73var bel = require('bel')
74var list = require('./list.js')
75
76module.exports = function (bears) {
77 function onselected (id) {
78 // When a bear is selected, rerender with the newly selected item
79 // This will use DOM diffing to render, sending the data back down again
80 element.update(render(id))
81 }
82 function render (selected) {
83 return bel`<div className="app">
84 <h1>Selected: ${selected}</h1>
85 ${list(bears, onselected)}
86 </div>`
87 }
88 // On first render, we haven't selected anything
89 var element = render('none')
90 return element
91}
92```
93
94## similar projects
95
96* [vel](https://github.com/yoshuawuyts/vel)
97 minimal virtual-dom library
98* [base-element](https://github.com/shama/base-element)
99 An element authoring library for creating standalone and performant elements
100* [virtual-dom](https://github.com/Matt-Esch/virtual-dom)
101 A Virtual DOM and diffing algorithm
102* [hyperscript](https://github.com/dominictarr/hyperscript)
103 Create HyperText with JavaScript.
104
105# license
106(c) 2016 Kyle Robinson Young. MIT License
107
108[npm-image]: https://img.shields.io/npm/v/bel.svg?style=flat-square
109[npm-url]: https://npmjs.org/package/bel
110[travis-image]: https://img.shields.io/travis/shama/bel/master.svg?style=flat-square
111[travis-url]: https://travis-ci.org/shama/bel
112[downloads-image]: http://img.shields.io/npm/dm/vel.svg?style=flat-square
113[downloads-url]: https://npmjs.org/package/bel
114[standard-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square
115[standard-url]: https://github.com/feross/standard
116[experimental-image]: https://img.shields.io/badge/stability-experimental-orange.svg?style=flat-square