blockdom
v0.9.29
Published
virtual dom library
Downloads
47
Keywords
Readme
blockdom
Probably the fastest virtual dom library in the world!
IMPORTANT: blockdom
is just a proof of concept and a place to experiment some ideas. This is
not intended to be used in a real application, no real support will be given! However, it is
designed to be the rendering engine of the Owl framework!
blockdom
is a very fast virtual dom library. Its main selling
point is that it does not represent DOM element by element, but instead block by
block, where a block is an element with all its static content and some special
tags to indicate dynamic content. This allows blockdom to use cloneNode(true)
on blocks and speed up the diff process, since the vdom tree is much smaller.
It features blocks, supports fragments, manage synthetic event handlers and more.
Note that it is not a framework. It does not even have the concept of components.
blockdom
is intended to be a lower level layer of abstraction, on top of which
other frameworks could be added. See the documentation for a tutorial on that
topic.
How to Install
With Package Managers
Installing using NPM:
npm install blockdom
Installing using Yarn:
yarn add blockdom
With a CDN
https://unpkg.com/blockdom@{VERSION}/dist/blockdom.iife.min.js
For using the latest version:
https://unpkg.com/blockdom/dist/blockdom.iife.min.js
Documentation
- Reference documentation
- Extending blockdom
- Performance Notes
- Tutorial: make your own framework (chapter 1, chapter 2, chapter 3, chapter 4, chapter 5, chapter 6, conclusion)
Examples
Instead of doing something like h('div', {}, [...some children])
, we can
work in blockdom with a larger unit of dom. For example:
// create block types
const block = createBlock(`<div class="some-class"><p>hello</p><blockdom-child-0/></div>`);
const subBlock = createBlock(`<span>some value: <blockdom-text-0/></span>`);
// create a blockdom virtual tree
const tree = block([], [subBlock(["blockdom"])]);
// mount the tree
mount(tree, document.body);
// result:
// <div class="some-class"><p>hello</p><span>some value: blockdom</span></div>
This example shows the mount
function. Here is a more interesting example.
It is a dynamic list of counters, featuring handlers, lists and dynamic content:
const counterBlock = createBlock(`
<div class="counter">
<button block-handler-1="click">Increment</button>
<span>Value: <block-text-0/></span>
</div>`);
const mainBlock = createBlock(`
<div>
<div><button block-handler-0="click">Add a counter</button></div>
<div><block-child-0/></div>
</div>`);
const state = [{ id: 0, value: 3 }];
function addCounter() {
state.push({ value: 0, id: state.length });
update();
}
function incrementCounter(id) {
const counter = state.find((c) => c.id === id);
counter.value++;
update();
}
function render(state) {
const counters = state.map((c) => {
const handler = [incrementCounter, c.id];
return withKey(counterBlock([c.value, handler]), c.id);
});
return mainBlock([addCounter], [list(counters)]);
}
let tree = render(state);
mount(tree, document.body);
function update() {
patch(tree, render(state));
}
Notice that block types are first created, with special attributes or tags such as
<block-text-0 />
or block-handler-1="click"
. What happens is that blockdom
then processes the block template, find all these special tags/attributes and generate
fast functions that will create and/or update these values. The number corresponds
to the index of the data given when the block is constructed.
Also, blockdom supports synthetic handlers (meaning: it only setup one actual
event handler on the body, which is an optimisation). To use this feature, one
can simply use the .synthetic
suffix:
const counterBlock = createBlock(`<button block-handler-1="click.synthetic">Increment</button>`);
It is also possible to setup an handler in capture
mode:
const counterBlock = createBlock(`<button block-handler-1="click.capture">Increment</button>`);
The examples
folder contains the complete code for this example.
About this project
In this section, you will find answers to some questions you may have about this project.
Is this virtual dom used in an actual project? Not yet ready, but it is used in the current work on Owl version 2. The Owl framework 1.x is based on a fork of snabbdom, and as such, does not support fragment. The version 2 is not ready yet, but will be based on
blockdom
.This is not a virtual dom, is it? Yes it is. Well, it depends what you mean by a virtual dom. It is not a representation of the dom tree element by element, but it still is a complete representation of what the dom is looking like. So, yes, in that sense,
blockdom
is a virtual dom.Why would you need a virtual dom, in the first place? It depends on your needs. Clearly, some frameworks can do very well by using other strategies. However, some other frameworks (such as React and owl with their concurrent mode) need the ability to split the rendering process in two phases, so we can choose to commit a rendering (or not if for some reason it is no longer useful). In that case, I do not see how to proceed without a virtual dom.
This sucks. blockdom is useless/slow because of X/Y. Great, please tell me more. I genuinely want to improve this, and helpful criticism is always welcome.
Credits
blockdom
is inspired by many frameworks: snabbdom, then solid, ivi, stage0 and
1more. The people behind these projects are incredible.