Created
June 18, 2017 17:31
-
-
Save monochromer/5750e7221c1e63ca1308ec315b62c233 to your computer and use it in GitHub Desktop.
App with createElement
This file contains 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
const component = function(name, props, children) { | |
let elem = document.createElement(name); | |
Object.keys(props).forEach(key => { | |
if (key === 'style') { | |
Object.assign(elem.style, props[key]) | |
} else { | |
elem.setAttribute(key, props[key]); | |
// elem[key] = props[key]; | |
} | |
}); | |
var children = Array.prototype.slice.call(arguments, 2); | |
if (children && children.length > 0) { | |
children.forEach(child => { | |
if (typeof child === "string") { | |
child = document.createTextNode(child); | |
} | |
elem.appendChild(child); | |
}); | |
}; | |
return elem; | |
}; | |
let App = | |
component('div', { className: 'app' }, | |
component('div', { className: 'app__logo' }), | |
component('span', { className: 'app__text' }, | |
'hello' | |
) | |
); | |
window.onload = () => { | |
document.body.appendChild(App); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment