Home

Awesome

Open Source Love npm version Downloads

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

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.

Credits

blockdom is inspired by many frameworks: snabbdom, then solid, ivi, stage0 and 1more. The people behind these projects are incredible.