https://github.com/bredele/grout
:sweat_drops: virtual dom
https://github.com/bredele/grout
Last synced: 1 day ago
JSON representation
:sweat_drops: virtual dom
- Host: GitHub
- URL: https://github.com/bredele/grout
- Owner: bredele
- License: mit
- Created: 2015-01-21T23:17:04.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2015-12-09T05:54:33.000Z (over 10 years ago)
- Last Synced: 2025-03-14T09:16:59.728Z (over 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 40 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 23
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# grout
Grout is a declarative way to create DOM elements and efficiently update them whenever data changes. Also called virtual-dom it is the perfect companion for [brick](http://github.com/brickjs) and also works server side with nodejs.
```js
var inception = dom('ul', [
dom('li', 'Hello'),
dom('li', '${name}')
]);
inception({
name: 'bredele'
});
```
[see live]()
Both [brick](http://github.com/brickjs) and grout promote clean and maintainable rendering logic. Their speed as well as their combined weight (only 3kb) makes them ideal for desktop and mobile.
## Learn it in 3
### create
Create a DOM element
```js
var btn = dom('button');
btn();
// =>
```
with a text content
```js
var btn = dom('button', 'Hello world!');
btn();
// => Hello world!
```
and quickly append multiple DOM nodes.
```js
var inception = dom('ul', [
dom('li', 'first item'),
dom('li', 'second item')
]);
inception();
```
Grout keeps manual DOM manipulation out of your application code.
### attributes
Create attributes
```js
var btn = dom('button', null, {
id: 'btn',
class: 'dark'
});
btn();
// =>
```
add some inline styles
```js
var btn = dom('button', null, {
id: 'btn',
style: {
background: 'red'
}
});
btn();
// =>
```
and attach event listeners
```js
var btn = dom('button', null, {
id: 'btn',
onclick: function() {
// do something
}
});
btn();
```
it is that easy!
### observable
Bind a DOM element with some data
```js
var btn = dom('button', 'Hello ${name}!');
btn({
name: 'bredele'
});
// => Hello bredele
```
and update it whenever the data changes
```js
btn({
name: 'olivier'
});
// => Hello olivier
```
It is blazing fast and works with every DOM nodes
```js
var btn = dom('button', 'Hello ${name}', {
class: '${type}'
});
btn({
name: 'olivier',
type: 'developer'
});
// => Hello olivier
```
## Advanced
### Use with [datastore](http://github.com/bredele/datastore)
```js
var store = new Store();
var btn = dom('button', '${name}');
btn(store);
store.set('name', 'bredele');
// => bredele
store.set('name', 'olivier');
// => olivier
```
### Use with [brick](http://github.com/bredele/brickjs)
```js
var list = brick(
dom('ul', [
dom('li', 'hello ${name}'),
dom('li', [
dom('button', 'welcome!', {
class: 'btn ${repo}'
})
])
])
);
list.set({
name: 'olivier',
repo: 'grout'
});
```
## License
The MIT License (MIT)
Copyright (c) 2014 Olivier Wietrich
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.