Skip to content

Instantly share code, notes, and snippets.

@v1vendi
Last active September 29, 2023 13:38
Show Gist options
  • Save v1vendi/e4c00cb124ba4dd4cca0da6d58fe21bf to your computer and use it in GitHub Desktop.
Save v1vendi/e4c00cb124ba4dd4cca0da6d58fe21bf to your computer and use it in GitHub Desktop.
Micro react
var createElement = (tag, props, ...children) => {
var el
if (typeof tag === 'string') {
el = document.createElement(tag)
} else if (typeof tag === 'function') {
el = tag()
}
if (children) {
for (const child of children) {
el.append(child)
}
}
for (const name in props) {
if (name === 'className') {
el.className = props.className
} else if (name === 'style') {
setCss(el, props.style)
} else if (name.startsWith('on')) {
el.addEventListener(name.toLowerCase().slice(2), props[name])
} else {
el.setAttribute(name, props[name])
}
}
return el
}
function setCss(el, style) {
if (typeof style === 'string') el.style.cssText = style
for (var key in style) {
el.style.setProperty(key, style[key] == null ? '' : style[key])
}
}
var render = (root, content) => root.append(content)
export { createElement, render }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment