Skip to content

Instantly share code, notes, and snippets.

@kyle-west
Last active April 18, 2022 16:37
Show Gist options
  • Select an option

  • Save kyle-west/1e8adb304512e7b21d205da00e817eb8 to your computer and use it in GitHub Desktop.

Select an option

Save kyle-west/1e8adb304512e7b21d205da00e817eb8 to your computer and use it in GitHub Desktop.
A simple DOM node stamping function
window.make = (type, { children = [], ...props } = {}, attrs = {}) => {
let elem = document.createElement(type)
Object.assign(elem, props)
if (children instanceof Array) {
children.forEach(child => elem.appendChild(child))
} else if (children instanceof Node) {
elem.appendChild(children)
} else {
elem.innerText = children
}
Object.entries(attrs).forEach(([k,v]) => elem.setAttribute(k,v))
return elem
}
// usage:
function renderImages (images) {
return make('div', { className: 'masonry',
children: images.map(image => make('img', { src: image.src, className: 'brick' }, { 'aria-hidden': '' }))
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment