DOM++ · Enhanced Document API

Interaction Patterns

A guided tour from direct DOM control to reactive components, dynamic structure, composition, and adopting existing markup. The original ElementBuilder and ComponentBuilder engine remains the foundation; document enhancement is an adapter, while JSX is only an alternative syntax.

01 · BASIC DOM

Direct element mutation

Foundation: create elements with the builder API, keep references, and update only the nodes that change (01.0). Step up by replacing children and letting reconciliation patch the tree (01.1).

View code (01.0)

View code (01.1)
02 · BASIC DOM + JSX

Familiar JSX syntax

Same direct-mutation foundation as 01, expressed with JSX (compiled to document.createElement() calls).

View code
03 · COMPONENT

Render and rerender

Move from manual updates to a component: document.createComponent renders immediately and returns the element (with .rerender()) so it can be used directly as a child; rerender patches the existing DOM.

View code
04 · MVVM

ViewModel subscription

Separate state from the view: a ViewModel owns the count and notifies subscribers, and the view rerenders in response.

View code
05 · LOCAL STATEFUL IN STATELESS

Local stateful subtree inside a stateless view

Localize rerenders: a stateless view embeds a document.createComponent subtree, and a ViewModel listener rerenders only that local subtree.

View code
06 · CONDITIONAL RENDERING

Add and remove a subtree

After localizing rerenders, control what renders: the panel exists only while confirming is true, and returning null removes it on the next rerender.

View code
07 · KEYED LIST

Stable keys across list changes

Give dynamic lists identity: each item passes a stable key, so append, prepend, reorder, and removal preserve item nodes (changing a tag replaces that node).

View code
08 · FRAGMENTS AND ARRAYS

Fragments and arrays

Composition catalog: compare a wrapper element (<fragment>) with inlined child arrays from fragment-producing forms (<>...</>, elementBuilder.fragment, document.createDocumentFragment(...)), plus array-returning components reconciled per slot on rerender.

View code
09 · KEYED LIST IN FRAGMENT

Keyed list inside a real fragment

Combine 07 and 08: the keyed list lives inside a real fragment array produced by document.createDocumentFragment(...) (inlined, not a <fragment> wrapper) and stable keys still preserve item identity across updates.

View code
10 · SERVER DOM ADOPTION

Attach to existing (server-rendered) markup

Bring it to existing pages: the <ul> is present in the HTML before any script runs (server-rendered). The engine hydrates that node against a keyed descriptor tree, preserving DOM identity and leaving server children not described untouched.

Server-rendered list

This list is plain HTML in the document. Adoption attaches a virtual tree to it.

  • JavaScript will adopt it
  • This list was rendered as HTML
  • Hello from the server

Waiting for adoption...

View code
11 · REACTIVE SERVER MESSENGER

Adopt server markup, then rerender reactively

Combine adoption with components and state: the container is rendered in HTML up front, a component adopts that root with document.hydrateElement(existingNode, ...), then rerenders a keyed message list on Send while keeping the same root attached.

Reactive server messenger

Server-rendered placeholder. The component will adopt and control this root.

  • Welcome! This came from the server.
  • Type below and press Send.

Waiting for component to attach...

View code

document.hydrateElement(existingNode, ...) preserves server markup that is not described in the virtual tree (non-destructive hydration). Use document.createElement(existingNode, ...) when you want reconcile behavior that replaces old children not in the new tree.

12 · CROSS-COMPONENT UPDATE

Components rerender each other directly

document.createComponent returns the element with a .rerender() method, so components can hold direct references to each other and trigger targeted updates without a parent intermediary.

View code