Skip to content

Instantly share code, notes, and snippets.

@alexnoz
Created July 12, 2018 13:26
Show Gist options
  • Save alexnoz/f40b58f1871bc01fb5333775dbcef44a to your computer and use it in GitHub Desktop.
Save alexnoz/f40b58f1871bc01fb5333775dbcef44a to your computer and use it in GitHub Desktop.
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