Last active
September 29, 2023 13:38
-
-
Save v1vendi/e4c00cb124ba4dd4cca0da6d58fe21bf to your computer and use it in GitHub Desktop.
Micro react
This file contains 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
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