Last active
April 18, 2022 16:37
-
-
Save kyle-west/1e8adb304512e7b21d205da00e817eb8 to your computer and use it in GitHub Desktop.
A simple DOM node stamping function
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
| 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