Created
June 1, 2017 07:25
-
-
Save lgraubner/97899c5a367d0846f5c32379480447f7 to your computer and use it in GitHub Desktop.
Simple vdom renderer (by developit)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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