Skip to content

Instantly share code, notes, and snippets.

@Tomotoes
Last active August 18, 2019 07:15
Show Gist options
  • Save Tomotoes/4f52b1cf10450ad2cb84f6140e94a09e to your computer and use it in GitHub Desktop.
Save Tomotoes/4f52b1cf10450ad2cb84f6140e94a09e to your computer and use it in GitHub Desktop.
Generate DOM using proxy.
const dom = new Proxy({},{
get(target,property){
return function (attrs={},...children){
const el = document.createElement(property)
for(let prop of Object.keys(attrs)){
el.setAttribute(prop,attrs[prop])
}
console.log(property) // a li li li ul div
for(let child of children){
if(typeof child === 'string'){
child = document.createTextNode(child)
}
el.appendChild(child)
}
return el
}
}
})
const el = dom.div({},
'Hello, my name is ',
dom.a({href: '//example.com'}, 'Mark'),
'. I like:',
dom.ul({},
dom.li({}, 'The web'),
dom.li({}, 'Food'),
dom.li({}, '…actually that\'s it')
)
)
document.body.appendChild(el)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment