• Stars
    star
    506
  • Rank 87,236 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 7 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

minimalist virtual dom library

petit-dom

A minimalist virtual DOM library.

  • Supports HTML & SVG elements.
  • Supports Render functions and Fragments.
  • Custom components allows to build your own abstraction around DOM elements.
  • Directives allows you to attach custom behavior to DOM elements.

Installation

The library is provided as a set of ES modules. You can install using npm or yarn and then import from petit-dom (see example below).

$ npm install --save petit-dom

or

$ yarn add petit-dom

Note however no transpiled build is provided. The library will work with all recent versions of Node and major browsers. If you're targeting older platforms, make sure to transpile to the desired ES version.

To run the examples, you can run a local web server (like npm http-server module) from the root folder of the project. Since all example use ES6 modules, you can simply navigate to the example you want and load the desired HTML file.

Usage

If you're using Babel you can use JSX syntax by configuring the jsx runtime

{
  "presets": [
    [
      "@babel/preset-react",
      { "runtime": "automatic", "importSource": "petit-dom" }
    ]
  ]
}
import { render } from "petit-dom";

//  assuming your HTML contains a node with "root" id
const parentNode = document.getElementById("root");

// mount
render(<h1>Hello world!</h1>, parentNode);

// patch
render(<h1>Hello again</h1>, parentNode);

Alternatively you can use the classic Babel transform via /* @jsx h */ on the top. You can also use the raw h function calls if you want, see examples folder for usage.

petit-dom also supports render functions

import { render } from "petit-dom";

function Box(props) {
  return (
    <div>
      <h1>{props.title}</h1>
      <p>{props.children}</p>
    </div>
  );
}

render(<Box title="Fancy box">Put your content here</Box>, parentNode);

render functions behave like React pure components. Patching with the same arguments will not cause any re-rendering. You can also attach a shouldUpdate function to the render function to customize the re-rendering behavior (By default props are tested for shallow equality).

Custom components

Besides HTML/SVG tag names, fragments and render fucntions, the h function also accepts any object with the following signature

{
  mount(self);
  patch(self);
  unmount(self);
}

Each of the 3 functions will be called by the library at the moment suggested by its name.

The self argument which is an aboject holding the following properties:

  • render(...): To create/update DOM content for the component
  • props: the current props passed to the JSX element (or h function)
  • oldProps: the previous props, it's value is undefined inside mount

You can also attach arbitrary properties to the object, they will persist between different invocations.

See examples folder for how to define some custom components.

Directives

You can also attach custom behaviors to DOM nodes. Directives allows you to obtain references to DOM nodes and manage their lifecycle.

A directive is an object with the current interface

{
  mount(domElement, value);
  patch(domElement, newValue, oldValue);
  unmount(element, lastValue);
}

There's an example of a simple log directive in the examples folder.

API

h(type, props, ...children)

Creates a virtual node.

  • type: a string (HTML or SVG tag name), or a custom component (see above)

  • props: in the case of HTML/SVG tags, this corresponds to the attributes/properties to be set in the real DOM node. In the case of components, { ...props, children } is passed to the appropriate component function (mount or patch).

render(vnode, parentDom, options = {})

renders a virtual node into the DOM. The function will initially create a DOM node as specified the virtual node vnode and append it to the children ofparentDOM. Subsequent calls will update the previous DOM node (or replace it if it's a different tag).

Optionally, you can use options to pass custom directives, for example:

  let log = { ... }, // defines a log directive
  render(<Something />, parent, { directives: { log } });

More Repositories

1

focused

Yet another Optics library in JavaScript. Based on the famous lens library from Haskell. Wrapped in a convenient Proxy interface
JavaScript
152
star
2

avenir

Lightweight async library based on lazy futures
JavaScript
123
star
3

adtstream

Streams built using Algebraic Data Types (ADT) and Pure FP (Haskell like) code
JavaScript
120
star
4

cfrp

Attempt to implement (a subset) of [Classic FRP](http://conal.net/papers/icfp97/)
JavaScript
83
star
5

algebraic-effects.js

Implementation of Algebraic effects (https://bit.ly/2Mo7w3Q) based on Generators
JavaScript
55
star
6

pcomb

A set of parser combinators
TypeScript
46
star
7

snabbdom-starter

Simple snabbdom starter repository
JavaScript
38
star
8

snabbdom-todomvc

TodoMVC using snabbdom and Elm architecture
JavaScript
28
star
9

mobx-static-dom

Dynamic UI using static DOM and mobx
JavaScript
27
star
10

elm-arch-with-snabbdom

Elm architecture examples with JavaScript/JSX using Snabbdom
23
star
11

create-a-simple-functional-language

Companion code for https://bit.ly/2WCoR0f
TypeScript
19
star
12

unReact

A prototype implementation of a Pull based FRP in JavaScript
JavaScript
15
star
13

invariant-ui

Created with CodeSandbox
TypeScript
12
star
14

websql.js

the easy way to deal with websql databases
JavaScript
12
star
15

signal

javascript reactive programming
HTML
10
star
16

aos

Some old tutorials I wrote back on 2006 on develpping a Linux style OS Kernel
HTML
5
star
17

elm-examples-js

Elm examples with Snabbdom and JavaScript
JavaScript
5
star
18

redux-fractal-example

JavaScript
3
star
19

oo-tic-tac-toc

TypeScript
2
star
20

petit-dom-custom-components

Created with CodeSandbox
JavaScript
1
star
21

snab-redux

Sanbbdom bindings to Redux
JavaScript
1
star
22

node-litesql

the easy way to deal with sqlite databases in node.js
JavaScript
1
star