1.3.3 • Published 5 days ago

@thi.ng/rdom v1.3.3

Weekly downloads
99
License
Apache-2.0
Repository
github
Last release
5 days ago

rdom

npm version npm downloads Twitter Follow

This project is part of the @thi.ng/umbrella monorepo.

About

Lightweight, reactive, VDOM-less UI/DOM components with async lifecycle and @thi.ng/hiccup compatible.

From hdom to rdom: Reactive UIs without virtual DOMs

In many ways this package is the direct successor of @thi.ng/hdom, which for several years was my preferred way of building UIs. hdom eschewed using a virtual DOM to represent and maintain a dynamic tree of (UI) components and instead only required a previous and current component tree in @thi.ng/hiccup format (aka nested, plain JS arrays w/ optional support for embedded other JS data types, like ES6 iterables, @thi.ng/api interfaces, etc.) to perform its UI updates. Yet, whilst hiccup trees are plain, simple, user defined data structures, which can be very easily composed without any libraries, hdom itself was still heavily influenced by the general vDOM approach and therefore a centralized update cycle and computing differences between the trees were necessary evils core tasks. In short, hdom allowed the illusion of declarative components with reactive state updates, but had to use a complex and recursive diff to realize those updates.

In contrast, @thi.ng/rdom directly supports embedding reactive values/components in the hiccup tree and compiles them in such a way that their value changes directly target underlying DOM nodes without having to resort to any other intermediate processing (no diffing, vDOM updates etc.). @thi.ng/rdom is entirely vDOM-free. It supports declarative component definitions via @thi.ng/hiccup, @thi.ng/rstream, ES6 classes, direct DOM manipulation (incl. provided helpers) and/or any mixture of these approaches.

Targetted, isolated updates

If a reactive value is used for an element attribute, a value change will trigger an update of only that attribute (there's special handling for event listeners, CSS classes, data attributes and style attribs). If a reactive value is used as (text) body of an element (or an element/component itself), only that body/subtree in the target DOM will be impacted/updated directly...

The package provides an interface IComponent (with a super simple life cycle API), a base component class Component for stubbing and a number of fundamental control constructs & component-wrappers for composing more complex components and to reduce boilerplate for various situations. Whilst targetting a standard JS DOM by default, each component can decide for itself what kind of target data structure (apart from a browser DOM) it manages. rdom components themselves have no mandatory knowledge of a browser DOM. As an example, similar to @thi.ng/hdom-canvas, the @thi.ng/rdom-canvas wrapper provides a component which subscribes to a stream of hiccup-based scene descriptions (trees) and then translates each scene-value into HTML Canvas API draw calls.

Async updates, scheduling & life cycle methods

Since there's no central coordination in rdom (neither explicitly nor implicitly), each component can (and does) update whenever its state value has changed. Likewise, components are free to directly manipulate the DOM through other means, as hinted at earlier. Various rdom control constructs are dispatching component updates via a central scheduler. By default this is only a dummy implementation which processes tasks immediately. However, as usual rdom only relies on the IScheduler interface and so supports other implementations, like RAFScheduler.

The IComponent interface is at the heart of rdom. It defines three lifecycle methods to: .mount(), .unmount() and .update() a component. The first two are always async to allow for more complex component initialization procedures (e.g. preloaders, WASM init, other async ops...). Several of the higher-order controller components/constructs too demand async functions for the same reasons.

Because rdom itself relies for most reactive features, stream composition and reactive value transformations on other packages, i.e. @thi.ng/rstream and @thi.ng/transducers, please consult the docs for these packages to learn more about the available constructs and patterns. Most of rdom only deals with either subscribing to reactive values and/or wrapping/transforming existing subscriptions, either explicitly using the provided control components (e.g. $sub()) or using $compile() to auto-wrap such values embedded in an hiccup tree.

Status

BETA - possibly breaking changes forthcoming

Search or submit any issues for this package

HIC SUNT DRACONES

This is still a young project. Even though most of the overall approach, component lifecycle and API are fairly stable by now (after ~75 commits) and used in production, so far there's only brief documentation and only few public examples. This is being worked & improved on...

@thi.ng/atom integration

For the sake of deduplication of functionality and to keep the number of dependencies to a minimum, direct @thi.ng/atom integration has been removed in favor of using relevant @thi.ng/rstream constructs, which can be used as lightweight adapters, i.e.:

Support packages

Related packages

Installation

yarn add @thi.ng/rdom

ES module import:

<script type="module" src="https://cdn.skypack.dev/@thi.ng/rdom"></script>

Skypack documentation

For Node.js REPL:

# with flag only for < v16
node --experimental-repl-await

> const rdom = await import("@thi.ng/rdom");

Package sizes (gzipped, pre-treeshake): ESM: 3.98 KB

Dependencies

Usage examples

Several demos in this repo's /examples directory are using this package.

A selection:

ScreenshotDescriptionLive demoSource
Probabilistic color theme generatorDemoSource
Color palette generation via dominant color extraction from uploaded imagesDemoSource
Interactive visualization of closest points on ellipsesDemoSource
Parser grammar livecoding editor/playground & codegenDemoSource
Interactive pixel sorting tool using thi.ng/color & thi.ng/pixelDemoSource
Demonstates various rdom usage patternsDemoSource
Dynamically loaded images w/ preloader stateDemoSource
rdom drag & drop exampleDemoSource
rdom & hiccup-canvas interop testDemoSource
Full umbrella repo doc string search w/ paginated resultsDemoSource
rdom powered SVG graph with draggable nodesDemoSource

API

Generated API docs

TODO

Currently, documentation only exists in the form of small examples and various doc strings (incomplete). I'm working to alleviate this situation ASAP... In that respect, PRs are welcome as well!

import { $compile } from "@thi.ng/rdom";
import { reactive } from "@thi.ng/rstream";
import { cycle, map } from "@thi.ng/transducers";

// reactive value
const bg = reactive("gray");

// color options (infinite iterable)
const colors = cycle(["magenta", "yellow", "cyan"]);

// event handler
const nextColor = () => bg.next(<string>colors.next().value);

// define component tree in hiccup syntax, compile & mount component.
// each time `bg` value changes, only subscribed bits will be updated
// i.e. title, the button's `style.background` and its label

// Note: instead of direct hiccup syntax, you could also use the
// element functions provided by https://thi.ng/hiccup-html
$compile([
    "div",
    {},
    // transformed color as title (aka derived view)
    ["h1", {}, bg.transform(map((col) => `Hello, ${col}!`))],
    [
        // tag with Emmet-style ID & classes
        "button#foo.w4.pa3.bn",
        {
            // reactive CSS background property
            style: { background: bg },
            onclick: nextColor,
        },
        // reactive button label
        bg,
    ],
]).mount(document.body);

Authors

Karsten Schmidt

If this project contributes to an academic publication, please cite it as:

@misc{thing-rdom,
  title = "@thi.ng/rdom",
  author = "Karsten Schmidt",
  note = "https://thi.ng/rdom",
  year = 2020
}

License

© 2020 - 2021 Karsten Schmidt // Apache Software License 2.0

1.3.3

5 days ago

1.3.2

6 days ago

1.3.1

9 days ago

1.3.0

11 days ago

1.2.0

20 days ago

1.1.22

23 days ago

1.1.21

1 month ago

1.1.20

1 month ago

1.1.19

1 month ago

1.1.18

1 month ago

1.1.17

1 month ago

1.1.16

1 month ago

1.1.15

2 months ago

1.1.14

2 months ago

1.1.13

2 months ago

1.1.12

2 months ago

1.1.11

2 months ago

1.1.10

2 months ago

1.1.9

2 months ago

1.1.8

2 months ago

1.1.7

2 months ago

1.1.6

2 months ago

1.1.5

2 months ago

1.1.4

2 months ago

1.1.3

2 months ago

1.1.2

3 months ago

1.1.1

3 months ago

1.1.0

3 months ago

1.0.6

3 months ago

1.0.2

3 months ago

1.0.5

3 months ago

1.0.4

3 months ago

1.0.3

3 months ago

1.0.1

3 months ago

1.0.0

3 months ago

0.14.0

4 months ago

0.14.1

4 months ago

0.13.8

4 months ago

0.13.6

4 months ago

0.13.7

4 months ago

0.13.5

5 months ago

0.13.4

5 months ago

0.13.3

5 months ago

0.13.0

6 months ago

0.13.1

5 months ago

0.13.2

5 months ago

0.12.10

7 months ago

0.12.11

7 months ago

0.12.16

6 months ago

0.12.17

6 months ago

0.12.18

6 months ago

0.12.19

6 months ago

0.12.12

7 months ago

0.12.13

7 months ago

0.12.14

6 months ago

0.12.15

6 months ago

0.12.20

6 months ago

0.12.22

6 months ago

0.11.8

9 months ago

0.11.9

9 months ago

0.11.5

10 months ago

0.11.6

9 months ago

0.11.7

9 months ago

0.12.7

8 months ago

0.12.8

8 months ago

0.12.9

7 months ago

0.12.0

8 months ago

0.12.1

8 months ago

0.12.2

8 months ago

0.12.3

8 months ago

0.12.4

8 months ago

0.12.5

8 months ago

0.12.6

8 months ago

0.11.10

9 months ago

0.11.11

9 months ago

0.11.4

11 months ago

0.11.3

12 months ago

0.11.1

1 year ago

0.11.2

1 year ago

0.10.18

1 year ago

0.11.0

1 year ago

0.10.14

1 year ago

0.10.15

1 year ago

0.10.16

1 year ago

0.10.17

1 year ago

0.10.13

1 year ago

0.10.11

1 year ago

0.10.12

1 year ago

0.10.10

1 year ago

0.10.9

1 year ago

0.10.7

1 year ago

0.10.1

1 year ago

0.10.2

1 year ago

0.10.3

1 year ago

0.10.4

1 year ago

0.10.5

1 year ago

0.10.6

1 year ago

0.9.20

1 year ago

0.10.0

1 year ago

0.9.12

2 years ago

0.9.13

2 years ago

0.9.14

2 years ago

0.9.15

2 years ago

0.9.11

2 years ago

0.9.16

2 years ago

0.9.17

2 years ago

0.9.18

1 year ago

0.9.19

1 year ago

0.9.10

2 years ago

0.9.9

2 years ago

0.9.8

2 years ago

0.9.0

2 years ago

0.9.2

2 years ago

0.9.1

2 years ago

0.9.7

2 years ago

0.9.4

2 years ago

0.9.3

2 years ago

0.9.6

2 years ago

0.9.5

2 years ago

0.8.12

2 years ago

0.8.9

2 years ago

0.8.8

2 years ago

0.8.11

2 years ago

0.8.10

2 years ago

0.8.7

2 years ago

0.8.6

2 years ago

0.8.5

2 years ago

0.8.4

2 years ago

0.7.9

2 years ago

0.8.1

2 years ago

0.8.0

2 years ago

0.8.3

2 years ago

0.8.2

2 years ago

0.7.8

2 years ago

0.7.4

3 years ago

0.7.7

3 years ago

0.7.1

3 years ago

0.7.3

3 years ago

0.7.0

3 years ago

0.6.9

3 years ago

0.6.7

3 years ago

0.6.6

3 years ago

0.6.8

3 years ago

0.6.5

3 years ago

0.6.3

3 years ago

0.6.2

3 years ago

0.6.4

3 years ago

0.6.1

3 years ago

0.6.0

3 years ago

0.5.0

3 years ago

0.4.17

3 years ago

0.4.16

3 years ago

0.4.15

3 years ago

0.4.14

3 years ago

0.4.13

3 years ago

0.4.12

3 years ago

0.4.11

3 years ago

0.4.10

3 years ago

0.4.9

3 years ago

0.4.8

3 years ago

0.4.7

3 years ago

0.4.6

3 years ago

0.4.5

3 years ago

0.4.4

3 years ago

0.4.0

3 years ago

0.3.9

3 years ago

0.3.8

3 years ago

0.3.7

3 years ago

0.3.6

3 years ago

0.3.5

3 years ago

0.3.4

3 years ago

0.3.3

3 years ago

0.3.2

3 years ago

0.3.1

3 years ago

0.3.0

3 years ago

0.2.16

3 years ago

0.2.15

3 years ago

0.2.14

4 years ago

0.2.13

4 years ago

0.2.12

4 years ago

0.2.11

4 years ago

0.2.10

4 years ago

0.2.9

4 years ago

0.2.8

4 years ago

0.2.7

4 years ago

0.2.6

4 years ago

0.2.5

4 years ago

0.2.4

4 years ago

0.2.3

4 years ago

0.2.2

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago