Minimalistic JavaScript reactive view library (ES2015+ only)
Satori is a minimalistic JavaScript reactive view library that uses ES6 Proxies for data binding (browser support).
Key features:
These qualities also make it useful for quick prototyping — it won’t interfere with your model code, requires almost zero boilerplate and helps you create and reorganize ad hoc components very quickly.
Install the library:
npm i satorijs
Hello, World:
import {Satori} from 'satorijs';
const view = new Satori();
const h = view.h;
const HelloView = name => h('div', null, ['Hello, ', name]);
view.qs('body').appendChild(HelloView('World'));
But this is just a static element. To make something reactive, first make sure that the root object of your model is wrapped in a proxy:
const user = view.proxy({name: 'Mike'});
Then just wrap the reactive part in a function:
const HelloView = user => h('div', null, ['Hello, ', h('span', null, () => user.name)]);
view.qs('body').appendChild(HelloView(user));
// The content of the <span> will be updated automatically
setTimeout(() => {user.name = 'Joe'}, 1000);
Reactivity is based primarily on registering property accesses via proxies, so the properties you want observed have to be accessed inside the wrapper function. This means you can’t just write () => name
— that would be static. Also, you can’t make reactive text nodes — you must have an element. This is why we added a <span>
here.
DOM elements are created using the element factory method:
h(tagName, modifiers, content): Element
There are multiple ways to specify the element content:
Text:
h('div', null, 'Hello')
One child element:
h('div', null, h('div'))
Array of elements and/or strings:
h('div', null, [h('span'), ' ', h('span')])
The view.setElementContent()
method is called under the hood and supports all of the above:
view.setElementContent(element, content)
Element modifiers are passed as the second argument to the element factory:
h('div', {…}, content)
Modifiers are view object methods and can also be applied to any existing DOM elements:
view.content(view.qs('.name'), () => user.name)
view.bind(view.qs('.title'), {model: post, key: 'title'})
You also can use assign()
method to apply multiple modifiers at once to an existing element:
view.assign(view.qs('.clear-completed'), {
show: () => model.completed.length,
on: {click: () => {model.clearCompleted()}},
});
All available modifiers are listed below.
content
Content passed to the element factory is handled as content
modifier internally.
{content: string|Node|[string|Node] | () => string|Node|[string|Node]}
Examples:
h('div', {content: 'Text'})
h('div', {content: h('h1', null, 'Title')})
h('div', {content: [h('strong', null, 10), ' items']})
h('div', {content: () => page.text})
show
Adds display: none
style property when the value is false and removes it, when the value is true.
{show: bool | () => bool}
h('div', {show: false}, 'Text')
h('div', {show: () => items.length}, 'Text')
class
Presence of CSS classes is specified using booleans:
{class: string | [string] | {string: bool | () => bool, …}}
Single static class:
h('div', {class: 'active'})
Static:
h('div', {class: ['active']})
h('div', {class: {active: true}})
h('div', {class: {active: user.active}})
Reactive:
h('div', {class: {active: () => user.active}})
attr
{attr: {string: string | () => string, …}}
h('input', {attr: {type: 'checkbox'}})
prop
{prop: {string: any | () => any, …}}
{prop: {checked: true}}
{prop: {value: ''}}
data
{data: {string: string | () => string, …}}
css
{css: {string: string | () => string, …}}
h('div', {css: {'background-color': 'blue'}})
on
Simply calls addEventHandler()
for each key.
{on: {eventType(event) {…}, …}}
h('div', {on: {click() {alert('Click!')}}})
onCapture
For capturing handlers use onCapture
modifier instead of on
:
{onCapture: {eventType(event) {…}, …}}
keydown
, keyup
{keydown: {[view.Key.*]: (element, event) => …, …}}
{keyup: {[view.Key.*]: (element, event) => …, …}}
h('input', {keydown: {
[view.Key.ENTER]: el => {alert(el.value)},
[view.Key.ESCAPE]: () => {alert('Esc')},
}})
bind
The value of the to
element property gets assigned to the model[key]
on every event specified in on
. The opposite happens on every change of model[key]
. Parameter to
defaults to 'checked'
for checkboxes or 'value'
for anything else, and on
defaults to ['change']
.
{bind: {model: proxy, key: string, to: string, on: string|[string]}}
h('input', {bind: {model: proxy, key: 'title', to: 'value', on: ['keydown', 'keyup']}})
Satori is very fast. It was built with performance in mind and tries to do only what is essential, so there’s actually not much room left for further optimization of typical operations. It updates the UI asynchronously to make bulk updates faster and reflects array modifications with minimal DOM changes. It shows very good results on the TodoMVC benchmark, but I won’t provide you with them due to its controversial nature. In particular, libraries based on virtual DOM, despite performing well in this benchmark, tend to have poor responsiveness when the number of rendered elements is large enough. For instance, simple editing of text in an input field can become irritating because of the delays introduced by rerendering and diffing performed by those libraries on each keystroke.
qs(selector, scope = document)
— alias for scope.querySelector(selector)
qsa(selector, scope = document)
— alias for scope.querySelectorAll(selector)
each(selector, scope = document, func)
— call func
for each element in querySelectorAll()
resultsortCompare(a, b)
— comparer for sorting numbers and strings, for example: ['b', 'a'].sort(sortCompare)
arrayRemove(array, value)
— remove first occurrence of value
from array
Enable logging of all flushes and affected observers in console:
view.logFlushes = true;
Enable logging of all proxy events:
view.logEvents = true;
Highlight all affected DOM elements:
view.highlightUpdates = true;
To inspect registered object observers, just explore the [Symbol(proxyInternals)].observers
property of the proxy object in the developer tools of your browser. Most interesting observer fields are element
and name
. When you hover the element
value, the actual element will usually be highlighted if it is visible.
Apache 2.0.