Skip to content

Instantly share code, notes, and snippets.

@jed
Created September 23, 2024 01:06
Show Gist options
  • Save jed/e60b602aecdd42e1a2716c042d3b56b6 to your computer and use it in GitHub Desktop.
Save jed/e60b602aecdd42e1a2716c042d3b56b6 to your computer and use it in GitHub Desktop.
minimum viable JSX-to-DOM implementation
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