Revisions
-
VanishMax created this gist
Apr 7, 2022 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,33 @@ // Tag can be string or a function if we parse the functional component type Tag = string | ((props: any, children: any[]) => JSX.Element); // Attributes of the element – object or null type Props = Record<string, string> | null; // Element children – return value from the h() type Children = (Node | string)[]; export const h = (tag: Tag, props: Props, ...children: Children) => { // If tag is a component, call it if (typeof tag === 'function') { return tag({ ... props }, children); } // Create HTML-element with given attributes const el = document.createElement(tag); if (props) { Object.entries(props).forEach(([key, val]) => { if (key === 'className') { el.classList.add(...(val as string || '').trim().split(' ')); return; } el.setAttribute(key, val); }); } // Append child elements into the parent children.forEach((child) => { el.append(child); }); return el; };