Skip to content

Instantly share code, notes, and snippets.

@inodaf
Last active October 9, 2023 21:08
Show Gist options
  • Save inodaf/769de75cb408d5f95fa4af1daef56a10 to your computer and use it in GitHub Desktop.
Save inodaf/769de75cb408d5f95fa4af1daef56a10 to your computer and use it in GitHub Desktop.
const createElement = name =>
document.createElement(String(name).toUpperCase())
const defineAttributes = (VNode, attributes) =>
Object.keys(attributes).forEach(attr =>
VNode.setAttribute(attr, attributes[attr]))
const defineChildren = (VNode, children) =>
Array.isArray(children) && children.forEach(e => VNode.appendChild(e))
const defineProperties = (VNode, props) =>
Object.keys(props).forEach(prop => (VNode[prop] = props[prop]))
export const h = (nodeName, data, children) => {
const DOMNode = createElement(nodeName)
const componentData = { ...data }
defineAttributes(DOMNode, componentData.attrs || {})
delete componentData.attrs
defineChildren(DOMNode, componentData.children || children)
delete componentData.children
defineProperties(DOMNode, componentData)
return DOMNode
}
const Label = text => h('span', {
className: 'ui-label',
textContent: text
})
const Button = props => h('button', {
className: 'ui-button',
onclick: e => props.onclick(e)
}, [
Label(props.label),
...props.slot
])
const Dialog = props => h('article', { className: 'ui-dialog' }, [
h('header', { className: 'ui-dialog__heading' }, [
Label('Remove Bucket')
]),
h('footer', { className: 'ui-dialog__actions' }, [
Button({ label: 'Cancel', onclick: props.oncancel }),
Button({ label: 'Confirm', onclick: props.onconfirm })
])
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment