function createVNode(type, props, children) {
return {
type,
props,
children,
_dom: null,
};
}
// The mount function takes a vnode and mounts it to an existing "parent" DOM node
function mount(vnode, parentDom) {
let domNode;
if (typeof vnode === "string" || typeof vnode === "number") {
// When the node is a string or number, we can insert a plain text node.
domNode = document.createTextNode(vnode);
} else {
if (typeof vnode.type === "function") {
// If the vnode type is a function, we can assume it is a component.
domNode = createComponentNode(vnode, parentDom);
} else {
// For "regular" vnodes, we create a HTMLElement of the node's type.
domNode = document.createElement(vnode.type);
}
// Store the DOM node on the VDOM node for future updates.
vnode._dom = domNode;
if (vnode.props) {
for (const prop in vnode.props) {
setProperty(domNode, prop, vnode.props[prop]);
}
}
if (vnode.children) {
// Any children go through the same process recursively until we have
// mounted the whole tree.
vnode.children.forEach((child) => mount(child, domNode));
}
}
// When we're finished, append the new DOM node to the parent.
parentDom.appendChild(domNode);
return domNode;
}
function setProperty(domNode, prop, value) {
if (prop === "style") {
if (typeof value === "string") {
domNode.style.cssText = value;
} else {
for (const styleProp in value) {
domNode.style[styleProp] = value[styleProp];
}
}
} else {
domNode[prop] = value;
}
}
// Create a component node for a component vnode
function createComponentNode(vnode, parentDom) {
const...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.