Created
July 12, 2018 13:26
-
-
Save alexnoz/f40b58f1871bc01fb5333775dbcef44a to your computer and use it in GitHub Desktop.
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
function createElement (name, attrs, ...children) { | |
name = name.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase() | |
const $el = document.createElement(name) | |
if (attrs) { | |
for (const [attr, val] of Object.entries(attrs)) | |
$el.setAttribute(attr, val) | |
} | |
const fragment = document.createDocumentFragment() | |
for (let child of children) { | |
if (typeof child === 'string') | |
child = document.createTextNode(child) | |
fragment.appendChild(child) | |
} | |
$el.appendChild(fragment) | |
return $el | |
} | |
export default [ | |
'xTabs', | |
'span', | |
'div', | |
'li', | |
'a', | |
'p' | |
// ... | |
].reduce((obj, name) => { | |
obj[name] = (attrs, ...children) => createElement(name, attrs, ...children) | |
return obj | |
}, {}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment