Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

snabbdom

snabbdom167.4kMIT3.6.2TypeScript support: included

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

virtual, dom, vdom, light, kiss, performance

readme

Snabbdom

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


License: MIT Build Status npm version npm downloads Join the chat at https://gitter.im/snabbdom/snabbdom

Donate to our collective

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
  • Third party features

Example

import {
  init,
  classModule,
  propsModule,
  styleModule,
  eventListenersModule,
  h
} from "snabbdom";

const patch = init([
  // Init patch function with chosen modules
  classModule, // makes it easy to toggle classes
  propsModule, // for setting properties on DOM elements
  styleModule, // handles styling on elements with support for animations
  eventListenersModule // attaches event listeners
]);

const container = document.getElementById("container");

const vnode = h(
  "div#container.two.classes",
  { on: { click: () => console.log("div clicked") } },
  [
    h("span", { style: { fontWeight: "bold" } }, "This is bold"),
    " and this is just normal text",
    h("a", { props: { href: "/foo" } }, "I'll take you places!")
  ]
);
// Patch into empty DOM element – this modifies the DOM as a side effect
patch(container, vnode);

const newVnode = h(
  "div#container.two.classes",
  { on: { click: () => console.log("updated div clicked") } },
  [
    h(
      "span",
      { style: { fontWeight: "normal", fontStyle: "italic" } },
      "This is now italic type"
    ),
    " and this is still just normal text",
    h("a", { props: { href: "/bar" } }, "I'll take you places!")
  ]
);
// Second `patch` invocation
patch(vnode, newVnode); // Snabbdom efficiently updates the old view to the new state

More examples


Table of contents

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.

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.

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:

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.

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 an major version bump.

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

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

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.

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.

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.

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.

const myModule = {
  create: function (oldVnode, vnode) {
    // invoked whenever a new virtual node is created
  },
  update: function (oldVnode, vnode) {
    // invoked whenever a virtual node is updated
  }
};

With this mechanism you can easily augment the behaviour of Snabbdom. For demonstration, take a look at the implementations of the default modules.

Modules documentation

This describes the core modules. All modules are optional. JSX examples assume you're using the jsx pragma provided by this library.

The class module

The class module provides an easy way to dynamically toggle classes on elements. It expects an object in the class data property. The object should map class names to booleans that indicate whether or not the class should stay or go on the vnode.

h("a", { class: { active: true, selected: false } }, "Toggle");

In JSX, you can use class like this:

<div class={{ foo: true, bar: true }} />
// Renders as: <div class="foo bar"></div>

The props module

Allows you to set properties on DOM elements.

h("a", { props: { href: "/foo" } }, "Go to Foo");

In JSX, you can use props like this:

<input props={{ name: "foo" }} />
// Renders as: <input name="foo" /> with input.name === "foo"

Properties can only be set. Not removed. Even though browsers allow addition and deletion of custom properties, deletion will not be attempted by this module. This makes sense, because native DOM properties cannot be removed. And if you are using custom properties for storing values or referencing objects on the DOM, then please consider using data-* attributes instead. Perhaps via the dataset module.

The attributes module

Same as props, but set attributes instead of properties on DOM elements.

h("a", { attrs: { href: "/foo" } }, "Go to Foo");

In JSX, you can use attrs like this:

<div attrs={{ "aria-label": "I'm a div" }} />
// Renders as: <div aria-label="I'm a div"></div>

Attributes are added and updated using setAttribute. In case of an attribute that had been previously added/set and is no longer present in the attrs object, it is removed from the DOM element's attribute list using removeAttribute.

In the case of boolean attributes (e.g. disabled, hidden, selected ...), the meaning doesn't depend on the attribute value (true or false) but depends instead on the presence/absence of the attribute itself in the DOM element. Those attributes are handled differently by the module: if a boolean attribute is set to a falsy value (0, -0, null, false,NaN, undefined, or the empty string ("")), then the attribute will be removed from the attribute list of the DOM element.

The dataset module

Allows you to set custom data attributes (data-*) on DOM elements. These can then be accessed with the HTMLElement.dataset property.

h("button", { dataset: { action: "reset" } }, "Reset");

In JSX, you can use dataset like this:

<div dataset={{ foo: "bar" }} />
// Renders as: <div data-foo="bar"></div>

The style module

The style module is for making your HTML look slick and animate smoothly. At its core it allows you to set CSS properties on elements.

h(
  "span",
  {
    style: {
      border: "1px solid #bada55",
      color: "#c0ffee",
      fontWeight: "bold"
    }
  },
  "Say my name, and every colour illuminates"
);

In JSX, you can use style like this:

<div
  style={{
    border: "1px solid #bada55",
    color: "#c0ffee",
    fontWeight: "bold"
  }}
/>
// Renders as: <div style="border: 1px solid #bada55; color: #c0ffee; font-weight: bold"></div>

Custom properties (CSS variables)

CSS custom properties (aka CSS variables) are supported, they must be prefixed with --

h(
  "div",
  {
    style: { "--warnColor": "yellow" }
  },
  "Warning"
);

Delayed properties

You can specify properties as being delayed. Whenever these properties change, the change is not applied until after the next frame.

h(
  "span",
  {
    style: {
      opacity: "0",
      transition: "opacity 1s",
      delayed: { opacity: "1" }
    }
  },
  "Imma fade right in!"
);

This makes it easy to declaratively animate the entry of elements.

The all value of transition-property is not supported.

Set properties on remove

Styles set in the remove property will take effect once the element is about to be removed from the DOM. The applied styles should be animated with CSS transitions. Only once all the styles are done animating will the element be removed from the DOM.

h(
  "span",
  {
    style: {
      opacity: "1",
      transition: "opacity 1s",
      remove: { opacity: "0" }
    }
  },
  "It's better to fade out than to burn away"
);

This makes it easy to declaratively animate the removal of elements.

The all value of transition-property is not supported.

Set properties on destroy

h(
  "span",
  {
    style: {
      opacity: "1",
      transition: "opacity 1s",
      destroy: { opacity: "0" }
    }
  },
  "It's better to fade out than to burn away"
);

The all value of transition-property is not supported.

The eventlisteners module

The event listeners module gives powerful capabilities for attaching event listeners.

You can attach a function to an event on a vnode by supplying an object at on with a property corresponding to the name of the event you want to listen to. The function will be called when the event happens and will be passed to the event object that belongs to it.

function clickHandler(ev) {
  console.log("got clicked");
}
h("div", { on: { click: clickHandler } });

In JSX, you can use on like this:

<div on={{ click: clickHandler }} />

Snabbdom allows swapping event handlers between renders. This happens without actually touching the event handlers attached to the DOM.

Note, however, that you should be careful when sharing event handlers between vnodes, because of the technique this module uses to avoid re-binding event handlers to the DOM. (And in general, sharing data between vnodes is not guaranteed to work, because modules are allowed to mutate the given data).

In particular, you should not do something like this:

// Does not work
const sharedHandler = {
  change: function (e) {
    console.log("you chose: " + e.target.value);
  }
};
h("div", [
  h("input", {
    props: { type: "radio", name: "test", value: "0" },
    on: sharedHandler
  }),
  h("input", {
    props: { type: "radio", name: "test", value: "1" },
    on: sharedHandler
  }),
  h("input", {
    props: { type: "radio", name: "test", value: "2" },
    on: sharedHandler
  })
]);

For many such cases, you can use array-based handlers instead (described above). Alternatively, simply make sure each node is passed unique on values:

// Works
const sharedHandler = function (e) {
  console.log("you chose: " + e.target.value);
};
h("div", [
  h("input", {
    props: { type: "radio", name: "test", value: "0" },
    on: { change: sharedHandler }
  }),
  h("input", {
    props: { type: "radio", name: "test", value: "1" },
    on: { change: sharedHandler }
  }),
  h("input", {
    props: { type: "radio", name: "test", value: "2" },
    on: { change: sharedHandler }
  })
]);

SVG

SVG just works when using the h function for creating virtual nodes. SVG elements are automatically created with the appropriate namespaces.

const vnode = h("div", [
  h("svg", { attrs: { width: 100, height: 100 } }, [
    h("circle", {
      attrs: {
        cx: 50,
        cy: 50,
        r: 40,
        stroke: "green",
        "stroke-width": 4,
        fill: "yellow"
      }
    })
  ])
]);

See also the SVG example and the SVG Carousel example.

Classes in SVG Elements

Certain browsers (like IE <=11) do not support classList property in SVG elements. Because the class module internally uses classList, it will not work in this case unless you use a classList polyfill. (If you don't want to use a polyfill, you can use the class attribute with the attributes module).

Thunks

The thunk function takes a selector, a key for identifying a thunk, a function that returns a vnode and a variable amount of state parameters. If invoked, the render function will receive the state arguments.

thunk(selector, key, renderFn, [stateArguments])

The renderFn is invoked only if the renderFn is changed or [stateArguments] array length or its elements are changed.

The key is optional. It should be supplied when the selector is not unique among the thunks siblings. This ensures that the thunk is always matched correctly when diffing.

Thunks are an optimization strategy that can be used when one is dealing with immutable data.

Consider a simple function for creating a virtual node based on a number.

function numberView(n) {
  return h("div", "Number is: " + n);
}

The view depends only on n. This means that if n is unchanged, then creating the virtual DOM node and patching it against the old vnode is wasteful. To avoid the overhead we can use the thunk helper function.

function render(state) {
  return thunk("num", numberView, [state.number]);
}

Instead of actually invoking the numberView function this will only place a dummy vnode in the virtual tree. When Snabbdom patches this dummy vnode against a previous vnode, it will compare the value of n. If n is unchanged it will simply reuse the old vnode. This avoids recreating the number view and the diff process altogether.

The view function here is only an example. In practice thunks are only relevant if you are rendering a complicated view that takes significant computational time to generate.

JSX

Note that JSX fragments are still experimental and must be opted in. See fragment section for details.

TypeScript

Add the following options to your tsconfig.json:

{
  "compilerOptions": {
    "jsx": "react",
    "jsxFactory": "jsx",
    "jsxFragmentFactory": "Fragment"
  }
}

Then make sure that you use the .tsx file extension and import the jsx function and the Fragment function at the top of the file:

import { Fragment, jsx, VNode } from "snabbdom";

const node: VNode = (
  <div>
    <span>I was created with JSX</span>
  </div>
);

const fragment: VNode = (
  <>
    <span>JSX fragments</span>
    are experimentally supported
  </>
);

Babel

Add the following options to your babel configuration:

{
  "plugins": [
    [
      "@babel/plugin-transform-react-jsx",
      {
        "pragma": "jsx",
        "pragmaFrag": "Fragment"
      }
    ]
  ]
}

Then import the jsx function and the Fragment function at the top of the file:

import { Fragment, jsx } from "snabbdom";

const node = (
  <div>
    <span>I was created with JSX</span>
  </div>
);

const fragment = (
  <>
    <span>JSX fragments</span>
    are experimentally supported
  </>
);

Virtual Node

Properties

sel : string

The sel property specifies the HTML element of the vnode, optionally its id prefixed by a #, and zero or more classes each prefixed by a .. The syntax is inspired by CSS selectors. Here are a few examples:

  • div#container.bar.baz – A div element with the id container and the classes bar and baz.
  • li – A li element with no id nor classes.
  • button.alert.primarybutton element with the two classes alert and primary.

The selector is meant to be static, that is, it should not change over the lifetime of the element. To set a dynamic id use the props module and to set dynamic classes use the class module.

Since the selector is static, Snabbdom uses it as part of a vnodes identity. For instance, if the two child vnodes

[h("div#container.padding", children1), h("div.padding", children2)];

are patched against

[h("div#container.padding", children2), h("div.padding", children1)];

then Snabbdom uses the selector to identify the vnodes and reorder them in the DOM tree instead of creating new DOM element. This use of selectors avoids the need to specify keys in many cases.

data : Object

The .data property of a virtual node is the place to add information for modules to access and manipulate the real DOM element when it is created; Add styles, CSS classes, attributes, etc.

The data object is the (optional) second parameter to h()

For example h('div', {props: {className: 'container'}}, [...]) will produce a virtual node with

({
  props: {
    className: "container"
  }
});

as its .data object.

children : Array<vnode>

The .children property of a virtual node is the third (optional) parameter to h() during creation. .children is simply an Array of virtual nodes that should be added as children of the parent DOM node upon creation.

For example h('div', {}, [ h('h1', {}, 'Hello, World') ]) will create a virtual node with

[
  {
    sel: "h1",
    data: {},
    children: undefined,
    text: "Hello, World",
    elm: Element,
    key: undefined
  }
];

as its .children property.

text : string

The .text property is created when a virtual node is created with only a single child that possesses text and only requires document.createTextNode() to be used.

For example: h('h1', {}, 'Hello') will create a virtual node with Hello as its .text property.

elm : Element

The .elm property of a virtual node is a pointer to the real DOM node created by snabbdom. This property is very useful to do calculations in hooks as well as modules.

key : string | number

The .key property is created when a key is provided inside of your .data object. The .key property is used to keep pointers to DOM nodes that existed previously to avoid recreating them if it is unnecessary. This is very useful for things like list reordering. A key must be either a string or a number to allow for proper lookup as it is stored internally as a key/value pair inside of an object, where .key is the key and the value is the .elm property created.

If provided, the .key property must be unique among sibling elements.

For example: h('div', {key: 1}, []) will create a virtual node object with a .key property with the value of 1.

Structuring applications

Snabbdom is a low-level virtual DOM library. It is unopinionated with regards to how you should structure your application.

Here are some approaches to building applications with Snabbdom.

  • functional-frontend-architecture – a repository containing several example applications that demonstrate an architecture that uses Snabbdom.
  • Cycle.js – "A functional and reactive JavaScript framework for cleaner code" uses Snabbdom
  • Vue.js use a fork of snabbdom.
  • scheme-todomvc build redux-like architecture on top of snabbdom bindings.
  • kaiju - Stateful components and observables on top of snabbdom
  • Tweed – An Object Oriented approach to reactive interfaces.
  • Cyclow - "A reactive frontend framework for JavaScript" uses Snabbdom
  • Tung – A JavaScript library for rendering HTML. Tung helps to divide HTML and JavaScript development.
  • sprotty - "A web-based diagramming framework" uses Snabbdom.
  • Mark Text - "Realtime preview Markdown Editor" build on Snabbdom.
  • puddles - "Tiny vdom app framework. Pure Redux. No boilerplate." - Built with :heart: on Snabbdom.
  • Backbone.VDOMView - A Backbone View with VirtualDOM capability via Snabbdom.
  • Rosmaro Snabbdom starter - Building user interfaces with state machines and Snabbdom.
  • Pureact - "65 lines implementation of React incl Redux and hooks with only one dependency - Snabbdom"
  • Snabberb - A minimalistic Ruby framework using Opal and Snabbdom for building reactive views.
  • WebCell - Web Components engine based on JSX & TypeScript

Be sure to share it if you're building an application in another way using Snabbdom.

Packages related to snabbdom should be tagged with the snabbdom keyword and published on npm. They can be found using the query string keywords:snabbdom.

Common errors

Uncaught NotFoundError: Failed to execute 'insertBefore' on 'Node':
    The node before which the new node is to be inserted is not a child of this node.

The reason for this error is the reusing of vnodes between patches (see code example), snabbdom stores actual dom nodes inside the virtual dom nodes passed to it as performance improvement, so reusing nodes between patches is not supported.

const sharedNode = h("div", {}, "Selected");
const vnode1 = h("div", [
  h("div", {}, ["One"]),
  h("div", {}, ["Two"]),
  h("div", {}, [sharedNode])
]);
const vnode2 = h("div", [
  h("div", {}, ["One"]),
  h("div", {}, [sharedNode]),
  h("div", {}, ["Three"])
]);
patch(container, vnode1);
patch(vnode1, vnode2);

You can fix this issue by creating a shallow copy of the object (here with object spread syntax):

const vnode2 = h("div", [
  h("div", {}, ["One"]),
  h("div", {}, [{ ...sharedNode }]),
  h("div", {}, ["Three"])
]);

Another solution would be to wrap shared vnodes in a factory function:

const sharedNode = () => h("div", {}, "Selected");
const vnode1 = h("div", [
  h("div", {}, ["One"]),
  h("div", {}, ["Two"]),
  h("div", {}, [sharedNode()])
]);

Opportunity for community feedback

Pull requests that the community may care to provide feedback on should be merged after such an opportunity of a few days was provided.

changelog

3.6.1 (2024-01-30)

Bug Fixes

3.6.0 (2024-01-20)

Note: Do to improvements to the JSX typings the minimum supported TypeScript version is now 4.1.

Bug Fixes

Features

  • optimise detection of data and svg names (#1058) (71813ff)

3.5.1 (2022-06-22)

Bug Fixes

3.5.0 (2022-04-26)

Features

  • notify hooks and modules about textContent changes (c230eb8)

3.4.0 (2022-03-20)

Bug Fixes

  • use array access instead of string.startsWith (515bc97)

Features

Bug Fixes

3.3.0 (2022-01-17)

Features

3.2.0 (2021-12-13)

Bug Fixes

  • assert that fragment functions are implemented (df0f031)

Features

  • allow DocumentFragment as a container (3505879)
  • support DocumentFragment (7e86386), closes #560

3.1.0 (2021-09-03)

Features

  • allow String and Number objects to be used as children (#979) (19d1d29), closes #977

3.0.3 (2021-06-04)

Bug Fixes

  • build and test code before release (b8707ad)

3.0.2 (2021-06-01)

Bug Fixes

  • avoid className check to handle SVG elements within shadow DOM (#966) (95fa8ad), closes #965

3.0.1 (2021-03-28)

Bug Fixes

  • reference type declarations in package.json (995b983)

3.0.0 (2021-03-28)

Bug Fixes

Features

  • add JSX namespace to jsx factory (5d5fc5a)
  • allow symbols as keys (#954) (ad80c6e), closes #124
  • export everything from 'snabbdom' (7af7e3f), closes #913 #748
  • mark the snabbdom package as side effect free (8524013)
  • offer esm and commonjs bundles (ebf6915)

BREAKING CHANGES

  • Snabbdom does not export the hero module any more. If you require this module, copy the code from examples/hero/hero.js and add it to your project
  • The imports of snabbdom functions have changed. Every file in the project had to be imported on its own, e.g.
import { h } from 'snabbdom/h'
import { VNode } from 'snabbdom/vnode'

Now, the main snabbdom package exports all of the public API like

import { h, VNode } from 'snabbdom'

This means consumers of the snabbdom package need to update their imports. The change makes the use of the exports field in package.json unnecessary, which caused issues for TypeScript users

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

2.1.0 (2020-09-14)

Features

  • eventlisteners: add types for VNode in listener (63b1b6c), closes #796
  • eventlisteners: relax custom event listener type (15ce059), closes #850

2.0.0 (2020-09-10)

⚠ BREAKING CHANGES

  • eventlisteners: loaded/carrying event listeners are no longer supported.

Features

  • eventlisteners: add missing mult. listeners type (5a89efe), closes #794
  • eventlisteners: remove loaded listeners feature (6e0ff8e), closes #802 #802

Bug Fixes

  • deps: add regenertor-runtime to devDeps (2a2964c), closes #813
  • docs: gitter badge url (7e19849)
  • examples: example import paths (8111f62), closes #761
  • examples: totalHeight 0 on remove last element reorder animation (afa77c0)
  • package: remove directories field (c7a2a93)
  • package: update urls paldepind -> snabbdom (f94185a), closes #775

1.0.1 (2020-06-18)

User facing changes

  • package: fix ./snabbdom related files and exports fields errors (89b917b)

1.0.0 (2020-06-18)

⚠ BREAKING CHANGES

  • exports: The main export path, 'snabbdom' was replaced with the export path 'snabbdom/init'. This new export path includes only the named export init.
  • exports: No default exports exist. All exports are named.
  • exports: the import path snabbdom/snabbdom.bundle is removed.
  • typescript: Types exported by this package have re-declared the global Element.setAttribute and Element.setAttributeNS to accept number and boolean for the value parameter. This change removes that re-declaration and thus the only valid value is string. If your code provides number and/or boolean, then it may now fail to compile.
  • props: props module does not attempt to delete node properties. This may affect you if you are using the props module to add non-native (custom) properties to DOM nodes. Instead, it is recommended to use data-* attributes. https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes
  • CommonJS modules are no longer provided.
  • import paths in ES modules include file name extensions.
  • Compiled to ES2015 (was ES5).
  • UMD bundles are no longer provided.

Internal changes

  • commitlint: add type auto and scope deps for renovate (b56a0ac)
  • commitlint: fix and enable in CI (f8cf5cc), closes #662
  • deps: update dependency @typescript-eslint/eslint-plugin to v3.3.0 (9448e42)
  • deps: update dependency tsconfigs to v5 (eb1ec8c)
  • deps: update dependency typescript to v3.9.5 (5e24b20)
  • docs: lint code examples (41cb359)
  • eslint: lint cjs files (d581217)
  • format: sort file lists (e77615b), closes #673
  • git: ignore each test artifact specifically (b34e9a9)
  • package: consistent values in files field (6fe56f8), closes #672
  • relic: remove @types/assert (2846189)
  • typescript: package and tests are two projects (8a71211)
  • vscode: eslint.validate short forms (ba3e85b)
  • vscode: use workspace typescript (eabbd2f)

User facing changes

  • docs: enable eslint rule array-bracket-spacing (77e54e9)
  • docs: enable eslint rule import/first (17cf7ae)
  • docs: enable eslint rule import/newline-after-import (cd3a5cf)
  • docs: enable eslint rule indent (e2861bb)
  • docs: enable eslint rule key-spacing (349b686)
  • docs: enable eslint rule max-statements-per-line (a128a23)
  • docs: enable eslint rule no-multi-spaces (8179381), closes #692
  • docs: enable eslint rule object-curly-spacing (8b8fbd5)
  • docs: enable eslint rule quote-props (37512fe)
  • docs: enable eslint rule quotes (2d455b5)
  • docs: enable eslint rule semi (f4e7885)
  • docs: enable eslint rule space-before-blocks (9f2d2d7)
  • docs: enable eslint rule space-before-function-paren (23e7b87)
  • docs: enable eslint rules object-*-newline (9a45b5b)
  • docs: fix wrong module import paths (3b6baee), closes #691
  • docs: provide a release changelog (616df35), closes #670
  • exports: main export provided (3becd84), closes #682
  • exports: only named exports (fefd141), closes #522 #523
  • exports: relative values in exports field (187088e), closes #674
  • exports: remove package.json main field (3122eec), closes #680
  • exports: remove the /snabbdom.bundle path (c862993)
  • exports: replaced main export path with init (09f2d1c), closes #522
  • package: no module field (2b30e25), closes #681
  • props: do not attempt to delete node properties (6f316c1), closes #623 #283 #415 #307 #151 #416
  • typescript: do not redeclare Element.setAttribute(NS) (0620b5e), closes #615
  • do not provide UMD bundles (8e24bbf), closes #498 #514 #481
  • only esm and correct import paths (dad44f0), closes #516 #437 #263

v0.7.2 - 2018-09-02

Bugfixes

  • Improvements to TypeScript types #364. Thanks to @gfmio.
  • In some cases and browsers the style module would cause elements to not be removed correctly #367. Thanks to @jvanbruegge for fixing this tricky bug.

v0.7.0 - 2017-07-27

Breaking change

The way Snabbdom handles boolean attributes in the attributes module has been changed. Snabbdom no longer maintains a list of known boolean attributes. Not relying on such a list means that custom boolean attributes are supported, that performance is slightly better, and that the list doesn't have to be kept up to date.

Whether or not to set a boolean attribute is now determined by looking at the value specified for the attribute. If the value is a boolean (i.e. strictly equal to true or false) it will be handled as a boolean attribute. If you're currently setting boolean attributes with booleans then this change does not affect you.

h("div", {
  attrs: {
    foo: true, // will be set a boolean attribute since `true` is a boolean
    bar: "baz" // will set a normal attribute
  }
});

The example above will result in the HTML: <div foo bar="baz" />. Even if bar is actually a boolean attribute. So for instance h("input", { attrs: { required: 12 } }) will no longer set a boolean attribute correctly.

Previously h("input", { attrs: { required: 0 } }) would result in the HTML <input> since required was a know boolean attribute and 0 is falsey. Per the new behavior the HTML will be <input required="0">. To accomidate for the change always give boolean values for boolean attributes.

Bugfixes

  • toVNode now handles DocumentFragment which makes it possible to patch into a fragment. #320. Thanks to @staltz.
  • Custom boolean attributes are handled correctly. #314. Thanks to @caridy.
  • Type improvement. VNode key property can be undefined #290. Thanks to @yarom82.
  • Data attributes are checked for existence before deleting. Old behavior caused error in Safari. #313. Thanks to @FeliciousX.

Performance improvements

  • New handling of boolean attributes. #314. Thanks to @caridy.

v0.6.9 - 2017-05-19

Bug fixes

v0.6.8 - 2017-05-16

Bug fixes

Performance improvements

v0.6.6 - 2017-03-07

Bug fixes

v0.6.5 - 2017-02-25

This is a patch version with a few bug fixes.

Bug fixes

v0.6.4 - 2017-02-09

This version adds some features such as support for comment nodes and better server-side/client-side rendering collaboration, besides some bug fixes.

New features

Add ability to create comment nodes. https://github.com/snabbdom/snabbdom/issues/142 Thanks to @pedrosland

Example:

h("!", "Will show as a comment");

will be rendered on the DOM as

<!-- Will show as a comment -->

Introduce toVNode() to reconstruct root element as vnode. https://github.com/snabbdom/snabbdom/issues/167 Thanks to @staltz

Useful for client-side rendering over existing HTML that was rendered server-side with snabbdom.

Example:

import { toVNode } from "snabbdom/tovnode";
// ...
patch(toVNode(element), vnode);

Will deep-convert the element to a VNode, this way allowing existing HTML to not be ignored by the patch process.

Bug fixes

v0.6.3 - 2017-01-16

Bugfixes

  • Fix the export of the Module interface for TypeScript projects depending on snabbdom.

v0.6.2 - 2017-01-16

Bugfixes

  • Fix the export of the Hooks interface for TypeScript projects depending on snabbdom.

v0.6.1 - 2017-01-05

The biggest change in this release is that the Snabbdom source code has been ported to TypeScript. The work has been primarily done by @staltz. This brings much improved support for using Snabbdom in TypeScript projects.

Note: This release contains breaking changes. See below.

New features

  • Complete TypeScript support. Thanks to @staltz.
  • Support for CSS variables. #195. Thanks to @jlesquembre.
  • Allow h(sel, data, node) and h(sel, node) shortcut notations in the h function. #196. That is, instead of h('div', [child]) one can now do h('div', child). Thanks to @AlexGalays.

Bugfixes

  • Fix custom element creation when tag name begins with 'svg'. #213. Thanks to @tdumitrescu.
  • Fix bug related to updating one child with same key but different selector. #188. Thanks to @zhulongzheng.
  • Strings can be used as children inside SVG elements. #208. Thanks to @jbucaran and @jbucaran.
  • Use parentNode fixing bug in IE 11. #210. Thanks to @aronallen.

Breaking changes

The TypeScript rewrite uses the import and export features introduced in ECMAScript 2015. Unfortunately the ES imports have no analogy to the CommonJS pattern of setting module.exports. This means that the Snabbdom modules that previously used this feature now have to be imported in a slightly different way.

/* eslint-disable no-redeclare */
const h = require("snabbdom/h"); // The old way
const h = require("snabbdom/h").h; // The new way
const h = require("snabbdom/h").default; // Alternative new way
const { h } = require("snabbdom/h"); // Using destructuring
/* eslint-enable no-redeclare */

v0.6.0 - 2017-01-05

Deprecated. Use version 0.6.1 instead.

v0.5.0 - 2016-05-16

Breaking change

This release contains a new thunk implementation that solves many issues with the old thunk implementation. The thunk API has changed slightly. Please see the thunks section in the readme.