vdom
wybthon.vdom¶
Virtual DOM primitives, diffing, and rendering to real DOM elements.
ErrorBoundary
¶
Suspense
¶
Bases: Component
Render a fallback while one or more resources are loading.
Props
- resources | resource: Resource or list of Resources (objects exposing
.loading.get()) - fallback: VNode | str | callable returning VNode/str
- keep_previous: bool (default False) – when True, keep children visible after first successful load even if a subsequent reload is in-flight.
VNode
dataclass
¶
Virtual node representing an element, text, or component subtree.
h(tag, props=None, *children)
¶
Create a VNode from a tag, props, and children (component-aware).
render(vnode, container)
¶
Render a VNode tree into a container Element or CSS selector.
Public API¶
VNodeh(tag, props=None, *children) -> VNoderender(vnode, container) -> ElementErrorBoundarycomponentSuspensecomponent
Keyed children and diffing¶
h(tag, {"key": key}, ...)assigns a stable identity to a child.- During reconciliation, children are matched by key first, then by type for unkeyed nodes.
- Reorders are applied with minimal DOM moves using a right-to-left pass with a moving anchor.
- Unmatched old nodes are unmounted; unmatched new nodes are mounted at the correct anchor.
Text nodes (fast-path)¶
- When both the old and new nodes are text, the same DOM text node is reused and only
nodeValueis updated. - Lists of plain text children are reconciled efficiently; typically the framework updates text in-place and minimizes DOM moves.
- Example:
from wybthon.vdom import h, render
from wybthon.dom import Element
root = Element(node=document.createElement("div"))
render(h("div", {}, "hello"), root)
render(h("div", {}, "world"), root) # updates the same text node
Suspense¶
Suspense renders a fallback while one or more resources are loading.
- Props:
resourceorresources=[...]fallback– VNode/str/callablekeep_previous=False– keep children visible during subsequent reloads
ErrorBoundary¶
ErrorBoundary catches render errors from its subtree and renders a fallback.
- Props:
fallback– VNode/str/callable. When callable, it is invoked asfallback(error, reset); if the callable only accepts one argument, it is invoked asfallback(error).on_error– optional callback called with the thrown error when the boundary captures it.reset_key– any value; when this value changes, the boundary automatically resets (clears the error) on the next render.-
reset_keys– list/tuple of values; when the tuple of values changes, the boundary automatically resets. -
Methods:
-
reset()– imperative method to clear the current error and attempt re-rendering children. -
Notes:
- If the fallback callable throws, a simple text node "Error rendering fallback" is shown.
- When not in an error state, the boundary renders its
childrenwrapped in adiv.
Prop semantics (style, dataset, value, checked)¶
style: pass a dict of CSS properties using camelCase keys. Keys are converted to kebab-case and applied viastyle.setProperty. On updates, keys that are absent in the new dict are removed withstyle.removeProperty. PassingNoneor a non-dict clears previously set style keys.
python
h("div", {"style": {"backgroundColor": "red", "fontSize": 14}})
# → sets background-color: red; font-size: 14
dataset: pass a dict; entries map todata-*attributes. On updates, keys not present are removed. PassingNoneor a non-dict clears previously setdata-*attributes.
python
h("div", {"dataset": {"id": "x", "role": "button"}})
# → sets data-id="x" data-role="button"
-
value: for form controls, the DOMvalueproperty is set (falling back to thevalueattribute if needed).Nonebecomes "". Removing thevalueprop resets it to "". -
checked: for checkboxes/radios, the DOMcheckedproperty is set when available (falling back to thecheckedattribute). Removing thecheckedprop clears it toFalse.