Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
S

snabbdom

> 编程语言
Open source

A virtual DOM library with focus on simplicity, modularity, powerful features and performance.

12.4K stars0 likes2 views
WebsiteGitHub

About

A virtual DOM library with focus on simplicity, modularity, powerful features and performance.

A virtual DOM library with a focus on simplicity, modularity, powerful features and performance.


Thanks to Browserstack for providing access to their great cross-browser testing tools.


English | 简体中文 | Hindi

Introduction

Virtual DOM is awesome. It allows us to express our application's view as a function of its state. But existing solutions were way too bloated, too slow, lacked features, had an API biased towards OOP , and/or lacked features I needed.

Snabbdom consists of an extremely simple, performant, and extensible core that is only ≈ 200 SLOC. It offers a modular architecture with rich functionality for extensions through custom modules. To keep the core simple, all non-essential functionality is delegated to modules.

You can mold Snabbdom into whatever you desire! Pick, choose, and customize the functionality you want. Alternatively you can just use the default extensions and get a virtual DOM library with high performance, small size, and all the features listed below.

Features

  • Core features
    • About 200 SLOC – you could easily read through the entire core and fully understand how it works.
    • Extendable through modules.
    • A rich set of hooks available, both per vnode and globally for modules, to hook into any part of the diff and patch process.
    • Splendid performance. Snabbdom is among the fastest virtual DOM libraries.
    • Patch function with a function signature equivalent to a reduce/scan function. Allows for easier integration with a FRP library.
  • Features in modules
    • h function for easily creating virtual DOM nodes.
    • SVG just works with the h helper.
    • Features for doing complex CSS animations.
    • Powerful event listener functionality.
    • Thunks to optimize the diff and patch process even further.
    • JSX support, including TypeScript types
  • Third party features
    • Server-side HTML output provided by snabbdom-to-html.
    • Compact virtual DOM creation with snabbdom-helpers.
    • Template string support using snabby.
    • Virtual DOM assertion with snabbdom-looks-like

Example

…

More examples

  • Animated reordering of elements
  • Hero transitions
  • SVG Carousel

Table of contents

  • Core documentation
    • init
    • patch
      • Unmounting
    • h
    • fragment (experimental)
    • toVNode
    • Hooks
      • Overview
      • Usage
      • The init hook
      • The insert hook
      • The remove hook
      • The destroy hook
    • Creating modules
  • Modules documentation
    • The class module
    • The props module
    • The attributes module
    • The dataset module
    • The style module
      • Custom properties (CSS variables)
      • Delayed properties
      • Set properties on remove
      • Set properties on destroy
    • The eventlisteners module
  • SVG
    • Classes in SVG Elements
  • Thunks
  • JSX
    • TypeScript
    • Babel
  • Virtual Node
    • sel : String
    • data : Object
    • children : Array<vnode>
    • text : string
    • elm : Element
    • key : string | number
  • Structuring applications
  • Related packages
  • Common errors
  • Opportunity for community feedback

Core documentation

The core of Snabbdom provides only the most essential functionality. It is designed to be as simple as possible while still being fast and extendable.

init

The core exposes only one single function init. This init takes a list of modules and returns a patch function that uses the specified set of modules.

javascript
import { classModule, styleModule } from "snabbdom";

const patch = init([classModule, styleModule]);

patch

The patch function returned by init takes two arguments. The first is a DOM element or a vnode representing the current view. The second is a vnode representing the new, updated view.

If a DOM element with a parent is passed, newVnode will be turned into a DOM node, and the passed element will be replaced by the created DOM node. If an old vnode is passed, Snabbdom will efficiently modify it to match the description in the new vnode.

Any old vnode passed must be the resulting vnode from a previous call to patch. This is necessary since Snabbdom stores information in the vnode. This makes it possible to implement a simpler and more performant architecture. This also avoids the creation of a new old vnode tree.

javascript
patch(oldVnode, newVnode);

Unmounting

While there is no API specifically for removing a VNode tree from its mount point element, one way of almost achieving this is providing a comment VNode as the second argument to patch, such as:

javascript
patch(
  oldVnode,
  h("!", {
    hooks: {
      post: () => {
        /* patch complete */
      }
    }
  })
);

Of course, then there is still a single comment node at the mount point.

h

It is recommended that you use h to create vnodes. It accepts a tag/selector as a string, an optional data object, and an optional string or an array of children.

javascript
import { h } from "snabbdom";

const vnode = h("div#container", { style: { color: "#000" } }, [
  h("h1.primary-title", "Headline"),
  h("p", "A paragraph")
]);

fragment (experimental)

Caution: This feature is currently experimental and must be opted in. Its API may be changed without a major version bump.

javascript
const patch = init(modules, undefined, {
  experimental: {
    fragments: true
  }
});

Creates a virtual node that will be converted to a document fragment containing the given children.

javascript
import { fragment, h } from "snabbdom";

const vnode = fragment(["I am", h("span", [" a", " fragment"])]);

toVNode

Converts a DOM node into a virtual node. Especially good for patching over pre-existing, server-side generated HTML content.

javascript
import {
  init,
  styleModule,
  attributesModule,
  h,
  toVNode
} from "snabbdom";

const patch = init([
  // Initialize a `patch` function with the modules used by `toVNode`
  attributesModule // handles attributes from the DOM node
  datasetModule, // handles `data-*` attributes from the DOM node
]);

const newVNode = h("div", { style: { color: "#000" } }, [
  h("h1", "Headline"),
  h("p", "A paragraph"),
  h("img", { attrs: { src: "sunrise.png", alt: "morning sunrise" } })
]);

patch(toVNode(document.querySelector(".container")), newVNode);

Hooks

Hooks are a way to hook into the lifecycle of DOM nodes. Snabbdom offers a rich selection of hooks. Hooks are used both by modules to extend Snabbdom, and in normal code for executing arbitrary code at desired points in the life of a virtual node.

Overview

Name Triggered when Arguments to callback
pre the patch process begins none
init a vnode has been added vnode
create a DOM element has been created based on a vnode emptyVnode, vnode
insert an element has been inserted into the DOM vnode
prepatch an element is about to be patched oldVnode, vnode
update an element is being updated oldVnode, vnode
postpatch an element has been patched oldVnode, vnode
destroy an element is directly or indirectly being removed vnode
remove an element is directly being removed from the DOM vnode, removeCallback
post the patch process is done none

The following hooks are available for modules: pre, create, update, destroy, remove, post.

The following hooks are available in the hook property of individual elements: init, create, insert, prepatch, update, postpatch, destroy, remove.

Usage

To use hooks, pass them as an object to hook field of the data object argument.

javascript
h("div.row", {
  key: movie.rank,
  hook: {
    insert: (vnode) => {
      movie.elmHeight = vnode.elm.offsetHeight;
    }
  }
});

The init hook

This hook is invoked during the patch process when a new virtual node has been found. The hook is called before Snabbdom has processed the node in any way. I.e., before it has created a DOM node based on the vnode.

The insert hook

This hook is invoked once the DOM element for a vnode has been inserted into the document and the rest of the patch cycle is done. This means that you can do DOM measurements (like using getBoundingClientRect in this hook safely, knowing that no elements will be changed afterwards that could affect the position of the inserted elements.

The remove hook

Allows you to hook into the removal of an element. The hook is called once a vnode is to be removed from the DOM. The handling function receives both the vnode and a callback. You can control and delay the removal with the callback. The callback should be invoked once the hook is done doing its business, and the element will only be removed once all remove hooks have invoked their callback.

The hook is only triggered when an element is to be removed from its parent – not if it is the child of an element that is removed. For that, see the destroy hook.

The destroy hook

This hook is invoked on a virtual node when its DOM element is removed from the DOM or if its parent is being removed from the DOM.

To see the difference between this hook and the remove hook, consider an example.

javascript
const vnode1 = h("div", [h("div", [h("span", "Hello")])]);
const vnode2 = h("div", []);
patch(container, vnode1);
patch(vnode1, vnode2);

Here destroy is triggered for both the inner div element and the span element it contains. remove, on the other hand, is only triggered on the div element because it is the only element being detached from its parent.

You can, for instance, use remove to trigger an animation when an element is being removed and use the destroy hook to additionally animate the disappearance of the removed element's children.

Creating modules

Modules work by registering global listeners for hooks. A module is simply a dictionary mapping hook names to functions.

javascript
const myModul

Issues· 86 open

View all issuesOpen on GitHub
  • #1114

    Slow type checking with snabbdom 3.6+

    Updated Jul 7, 2026
  • #137

    attachto module is not documented

    type:bugaffects:usergood first issueUpdated Jun 28, 2026
  • #1135

    Element inserts into DOM in wrong location when replacing empty fragment

    Updated Dec 21, 2025
  • #1126

    Fix style.ts for headless environments

    Updated May 12, 2025
  • #1129

    snabbdom needs to support custom elements

    Updated Mar 31, 2025
  • #1128

    Is there any way to detect whether a render task caused repaint on the browser or not ?

    Updated Feb 10, 2025
  • #1125

    JSX.IntrinsicElements type error in editor

    Updated Jan 22, 2025
  • #1121

    VNodeStyle type checking error for "delayed" and "remove" styles

    Updated Nov 24, 2024
  • #1118

    I want to know the origin of the two-ended diff algorithm

    Updated May 29, 2024
  • #1112

    Attribute behavior should be improved

    Updated May 18, 2024

> Tags

TypeScripthacktoberfestsnabbdomvirtual-dom-library

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 28, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言