Skip to content

Instantly share code, notes, and snippets.

@lgraubner
Created June 1, 2017 07:25
Show Gist options
  • Save lgraubner/97899c5a367d0846f5c32379480447f7 to your computer and use it in GitHub Desktop.
Save lgraubner/97899c5a367d0846f5c32379480447f7 to your computer and use it in GitHub Desktop.
Simple vdom renderer (by developit)
// generate virtual nodes
function h(nodeName, attributes, ...args) {
// create children array
const children = args.length ? [].concat(...args) : null;
// return node
return { nodeName, attributes, children };
}
function render(vnode) {
// create text node
if (vnode.split) return document.createTextNode(vnode);
// create element
const n = document.createElement(vnode.nodeName);
// set attributes
const a = vnode.attributes || {};
Object.keys(a).forEach(k => n.setAttribute(k, a[k]));
// append children
(vnode.children || []).forEach(c => n.appendChild(render(c)));
return n;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment