What is the Virtual DOM?
The Virtual DOM (VDOM) is a lightweight JavaScript representation of the actual DOM. Instead of directly manipulating the browser’s DOM (which is slow), GlyphUI creates a virtual tree of objects that mirrors the structure of your UI. When your application’s state changes, GlyphUI:- Creates a new Virtual DOM tree
- Compares it with the previous tree (diffing)
- Calculates the minimal set of changes needed
- Updates only the necessary parts of the real DOM
DOM Types
GlyphUI defines three fundamental types of virtual nodes inpackages/runtime/src/h.js:4-8:
Text Nodes
Represent plain text content in your UI. Automatically created when you pass strings as children.Element Nodes
Represent HTML elements with a tag, props, and children.Fragment Nodes
Group multiple children without adding an extra element to the DOM.The h() Function
Theh() function (short for “hyperscript”) is the core of creating Virtual DOM nodes. It takes three arguments:
- tag (string): The HTML tag name (
'div','span','button', etc.) - props (object): Element attributes, event listeners, and special properties
- children (array): Child elements (can be
h()calls, strings, or null values)
Strings in the children array are automatically converted to text nodes using the internal
hString() function.Basic Example
Here’s how to create a simple button element:Special Props
Theh() function recognizes several special props from packages/runtime/src/h.js:14-19:
Event Listeners (on)
Attach event handlers using an object where keys are event names:
CSS Classes (class)
Can be a string or array of strings:
Inline Styles (style)
Provide CSS properties as an object:
Keys (key)
Unique identifiers for efficient list rendering (covered in the Rendering page):
Complex Example
Here’s a more realistic example from the hello-world demo:Helper Functions
GlyphUI provides additional helper functions for creating specific node types:hString()
Explicitly creates a text virtual node:hFragment()
Creates a fragment node that groups children without adding a wrapper element:- You need to return multiple root elements from a component
- You want to avoid unnecessary DOM nesting
- You’re working with components that expect specific child structures
Example from Counter App
Fromexamples/counter/counter.js:24-31:
Virtual Node Structure
When you callh(), it creates a plain JavaScript object from packages/runtime/src/h.js:29-36:
Why Virtual DOM?
Performance Benefits
Performance Benefits
Direct DOM manipulation is expensive. Every change can trigger reflows and repaints. The Virtual DOM batches updates and minimizes actual DOM operations.
Declarative Programming
Declarative Programming
You describe what the UI should look like for any given state, and GlyphUI handles the updates. No need to manually track and update individual DOM nodes.
Cross-platform Potential
Cross-platform Potential
The Virtual DOM abstraction makes it easier to render to different targets (DOM, Canvas, Native, etc.) by changing the rendering layer.
Next Steps
Now that you understand the Virtual DOM structure, learn about:- Rendering - How GlyphUI mounts and patches the DOM
- Components - Building reusable UI with components
- API Reference - Complete
h()function API documentation