Merge pull request #490 from snabbdom/remark

docs: introduce remark and some readme tweaks
pull/493/head
Shahar Dawn Or 5 years ago committed by GitHub
commit 5014e65cca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -3,7 +3,7 @@ language: node_js
node_js:
- v12.10.0
script:
- npm test
- run-s test docs check-clean
env:
global:
- BROWSER_STACK_USERNAME='snabbdombot1'

@ -1,8 +1,10 @@
<img src="logo.png" width="356px">
<img alt="Snabbdom" src="logo.png" width="356px">
A virtual DOM library with focus on simplicity, modularity, powerful features
and performance.
* * *
[![License: MIT](https://img.shields.io/badge/License-MIT-brightgreen.svg)](https://opensource.org/licenses/MIT)
[![Build Status](https://travis-ci.org/snabbdom/snabbdom.svg?branch=master)](https://travis-ci.org/snabbdom/snabbdom)
[![npm version](https://badge.fury.io/js/snabbdom.svg)](https://badge.fury.io/js/snabbdom)
@ -12,27 +14,13 @@ and performance.
Thanks to [Browserstack](https://www.browserstack.com/) for providing access to
their great cross-browser testing tools.
## Table of contents
* [Introduction](#introduction)
* [Features](#features)
* [Inline example](#inline-example)
* [Examples](#examples)
* [Core documentation](#core-documentation)
* [Modules documentation](#modules-documentation)
* [Helpers](#helpers)
* [Virtual Node documentation](#virtual-node)
* [Structuring applications](#structuring-applications)
## Why
## 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 way too
bloated, too slow, lacked features, had an API biased towards OOP
and/or lacked features I needed.
## Introduction
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
@ -53,7 +41,7 @@ performance, small size and all the features listed below.
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.
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](#svg).
@ -67,7 +55,7 @@ performance, small size and all the features listed below.
* Template string support using [snabby](https://github.com/jamen/snabby).
* Virtual DOM assertion with [snabbdom-looks-like](https://github.com/jvanbruegge/snabbdom-looks-like)
## Inline example
## Example
```javascript
var snabbdom = require('snabbdom');
@ -101,12 +89,56 @@ patch(vnode, newVnode); // Snabbdom efficiently updates the old view to the new
patch(newVnode, null)
```
## Examples
## More examples
* [Animated reordering of elements](http://snabbdom.github.io/snabbdom/examples/reorder-animation/)
* [Hero transitions](http://snabbdom.github.io/snabbdom/examples/hero/)
* [SVG Carousel](http://snabbdom.github.io/snabbdom/examples/carousel-svg/)
* * *
## Table of contents
* [Core documentation](#core-documentation)
* [`snabbdom.init`](#snabbdominit)
* [`patch`](#patch)
* [`snabbdom/h`](#snabbdomh)
* [`snabbdom/tovnode`](#snabbdomtovnode)
* [Hooks](#hooks)
* [Overview](#overview)
* [Usage](#usage)
* [The `init` hook](#the-init-hook)
* [The `insert` hook](#the-insert-hook)
* [The `remove` hook](#the-remove-hook)
* [The `destroy` hook](#the-destroy-hook)
* [Creating modules](#creating-modules)
* [Modules documentation](#modules-documentation)
* [The class module](#the-class-module)
* [The props module](#the-props-module)
* [The attributes module](#the-attributes-module)
* [The dataset module](#the-dataset-module)
* [The style module](#the-style-module)
* [Custom properties (CSS variables)](#custom-properties-css-variables)
* [Delayed properties](#delayed-properties)
* [Set properties on `remove`](#set-properties-on-remove)
* [Set properties on `destroy`](#set-properties-on-destroy)
* [Eventlisteners module](#eventlisteners-module)
* [SVG](#svg)
* [Classes in SVG Elements](#classes-in-svg-elements)
* [Thunks](#thunks)
* [Virtual Node](#virtual-node)
* [sel : String](#sel--string)
* [data : Object](#data--object)
* [children : Array<vnode>](#children--arrayvnode)
* [text : string](#text--string)
* [elm : Element](#elm--element)
* [key : string | number](#key--string--number)
* [Structuring applications](#structuring-applications)
* [Common errors](#common-errors)
* [Maintenance policy](#maintenance-policy)
* [Pull requests](#pull-requests)
* [Releases](#releases)
## Core documentation
The core of Snabbdom provides only the most essential functionality.
@ -194,18 +226,18 @@ 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 |
| 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`.
@ -462,6 +494,7 @@ h('div', [
```
Each handler is called not only with the given arguments but also with the current event and vnode appended to the argument list. It also supports using multiple listeners per event by specifying an array of handlers:
```javascript
stopPropagation = function(ev) { ev.stopPropagation() }
sendValue = function(func, ev, vnode) { func(vnode.elm.value) }
@ -511,9 +544,7 @@ h('div', [
]);
```
## Helpers
### SVG
## SVG
SVG just works when using the `h` function for creating virtual
nodes. SVG elements are automatically created with the appropriate
@ -529,13 +560,13 @@ var vnode = h('div', [
See also the [SVG example](./examples/svg) and the [SVG Carousel example](./examples/carousel-svg/).
#### Using Classes in SVG Elements
### Classes in SVG Elements
Certain browsers (like IE <=11) [do not support `classList` property in SVG elements](http://caniuse.com/#feat=classlist).
Certain browsers (like IE &lt;=11) [do not support `classList` property in SVG elements](http://caniuse.com/#feat=classlist).
Because the _class_ module internally uses `classList`, it will not work in this case unless you use a [classList polyfill](https://www.npmjs.com/package/classlist-polyfill).
(If you don't want to use a polyfill, you can use the `class` attribute with the _attributes_ module).
### Thunks
## 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
@ -581,22 +612,24 @@ relevant if you are rendering a complicated view that takes
significant computational time to generate.
## Virtual Node
**Properties**
- [sel](#sel--string)
- [data](#data--object)
- [children](#children--array)
- [text](#text--string)
- [elm](#elm--element)
- [key](#key--string--number)
#### sel : String
* [sel](#sel--string)
* [data](#data--object)
* [children](#children--array)
* [text](#text--string)
* [elm](#elm--element)
* [key](#key--string--number)
### sel : String
The `.sel` property of a virtual node is the CSS selector passed to
[`h()`](#snabbdomh) during creation. For example: `h('div#container',
{}, [...])` will create a a virtual node which has `div#container` as
its `.sel` property.
#### data : Object
### data : Object
The `.data` property of a virtual node is the place to add information
for [modules](#modules-documentation) to access and manipulate the
@ -606,6 +639,7 @@ attributes, etc.
The data object is the (optional) second parameter to [`h()`](#snabbdomh)
For example `h('div', {props: {className: 'container'}}, [...])` will produce a virtual node with
```js
{
"props": {
@ -613,9 +647,10 @@ For example `h('div', {props: {className: 'container'}}, [...])` will produce a
}
}
```
as its `.data` object.
#### children : Array<vnode>
### children : Array<vnode>
The `.children` property of a virtual node is the third (optional)
parameter to [`h()`](#snabbdomh) during creation. `.children` is
@ -640,7 +675,7 @@ create a virtual node with
as its `.children` property.
#### text : string
### text : string
The `.text` property is created when a virtual node is created with
only a single child that possesses text and only requires
@ -649,14 +684,14 @@ only a single child that possesses text and only requires
For example: `h('h1', {}, 'Hello')` will create a virtual node with
`Hello` as its `.text` property.
#### elm : Element
### 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](#hooks) as well as
[modules](#modules-documentation).
#### key : string | number
### key : string | number
The `.key` property is created when a key is provided inside of your
[`.data`](#data--object) object. The `.key` property is used to keep
@ -670,7 +705,6 @@ an object, where `.key` is the key and the value is the
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
@ -682,11 +716,11 @@ Here are some approaches to building applications with Snabbdom.
a repository containing several example applications that
demonstrates an architecture that uses Snabbdom.
* [Cycle.js](https://cycle.js.org/)
"A functional and reactive JavaScript framework for cleaner code"
uses Snabbdom
"A functional and reactive JavaScript framework for cleaner code"
uses Snabbdom
* [Vue.js](http://vuejs.org/) use a fork of snabbdom.
* [scheme-todomvc](https://github.com/amirouche/scheme-todomvc/) build
redux-like architecture on top of snabbdom bindings.
redux-like architecture on top of snabbdom bindings.
* [kaiju](https://github.com/AlexGalays/kaiju) -
Stateful components and observables on top of snabbdom
* [Tweed](https://tweedjs.github.io)
@ -710,11 +744,13 @@ using Snabbdom.
## Common errors
```
```text
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 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.
```js
var sharedNode = h('div', {}, 'Selected');
var vnode1 = h('div', [
@ -730,7 +766,9 @@ var vnode2 = h('div', [
patch(container, vnode1);
patch(vnode1, vnode2);
```
You can fix this issue by creating a shallow copy of the object (here with object spread syntax):
```js
var vnode2 = h('div', [
h('div', {}, ['One']),
@ -738,7 +776,9 @@ var vnode2 = h('div', [
h('div', {}, ['Three']),
]);
```
Another solution would be to wrap shared vnodes in a factory function:
```js
var sharedNode = () => h('div', {}, 'Selected');
var vnode1 = h('div', [

@ -5,15 +5,17 @@ Also, the color of each triangle changes when you hover or click/tap it.
I built the build.js using npm and browserify.
In my local copy of the snabbdom project root I did these preparations:
```
```sh
npm install --save-dev babelify
npm install --save-dev babel-preset-es2015
echo '{ "presets": ["es2015"] }' > .babelrc
```
I then built like this:
```
```sh
browserify examples/carousel-svg/script.js -t babelify -o examples/carousel-svg/build.js
```
-- *jk*
\-- _jk_

876
package-lock.json generated

File diff suppressed because it is too large Load Diff

@ -38,10 +38,14 @@
"knuth-shuffle": "^1.0.1",
"mocha": "^5.2.0",
"npm-run-all": "^4.1.5",
"remark-cli": "^7.0.1",
"remark-toc": "^6.0.0",
"typescript": "^3.7.2",
"xyz": "2.1.0"
},
"scripts": {
"docs": "remark . --output",
"check-clean": "git diff --exit-code",
"lint:js": "eslint --ignore-path .gitignore --ignore-pattern /dist --ext .js,.ts .",
"lint:editorconfig": "editorconfig-checker",
"lint": "run-s lint:editorconfig lint:js",
@ -71,10 +75,25 @@
"bugs": {
"url": "https://github.com/paldepind/snabbdom/issues"
},
"remarkConfig": {
"plugins": [
[
"toc",
{
"tight": true
}
]
],
"settings": {
"listItemIndent": "1",
"bullet": "*",
"paddedTable": false
}
},
"homepage": "https://github.com/paldepind/snabbdom#readme",
"husky": {
"hooks": {
"pre-commit": "npm test"
"pre-commit": "run-s docs check-clean test"
}
},
"files": [

Loading…
Cancel
Save