Created
September 23, 2024 01:06
-
-
Save jed/e60b602aecdd42e1a2716c042d3b56b6 to your computer and use it in GitHub Desktop.
minimum viable JSX-to-DOM implementation
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
export let Fragment = () => document.createDocumentFragment(); | |
export let jsx = (node, { children = [], ...props }) => | |
jsxs(node, { ...props, children }); | |
export let jsxs = (node, { children, ...props }) => { | |
node = | |
typeof node === "function" ? node(props) : document.createElement(node); | |
for (let name in props) { | |
if (name.startsWith("on")) | |
node.addEventListener(name.slice(2), props[name]); | |
else if (name in node) node[name] = props[name]; | |
else node.setAttribute(name, props[name]); | |
} | |
node.append(...children); | |
return node; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment