import { Signal, effect } from "@preact/signals-core"; // --- Types --- export type DynamicValue = T | Signal | ((el: HTMLElement) => T); export type StyleObject = Record>; export type StyleProp = DynamicValue; export type EventListenersMap = { [E in keyof HTMLElementEventMap]?: ( ev: HTMLElementEventMap[E] extends infer Ev ? (Ev extends Event ? any : Ev) : never ) => void; } & { [customEvent: string]: (ev: any) => void; }; export type ElementProps = { attrs?: Record>; classList?: Record>; style?: StyleProp; on?: EventListenersMap; } & { [P in keyof T as P extends "attrs" | "classList" | "style" | "on" ? never : P]?: DynamicValue; }; export type ChildNodeInput = string | number | Node | Signal | ChildNodeInput[]; type ElementBuilderMap = { [K in keyof HTMLElementTagNameMap]: ( props?: ElementProps, children?: ChildNodeInput[], ) => HTMLElementTagNameMap[K]; }; // --- Helpers --- export const deferToMacrotask = (cb: () => void) => { setTimeout(cb, 0); }; const isSignal = (val: unknown): val is Signal => { return val instanceof Signal; }; const resolveValue = (val: DynamicValue, el: HTMLElement): T => { if (typeof val === "function") { return (val as (el: HTMLElement) => T)(el); } if (isSignal(val)) { return val.value; } return val as T; }; const applyStyles = (el: HTMLElement, styleProp: StyleProp) => { const resolved = resolveValue(styleProp, el); if (typeof resolved === "string") { el.style.cssText = resolved; } else if (resolved && typeof resolved === "object") { el.removeAttribute("style"); const resolvedObject: Record = {}; for (const [key, val] of Object.entries(resolved)) { const propVal = resolveValue(val as DynamicValue, el); if (propVal !== null && propVal !== undefined) { resolvedObject[key] = propVal; } } Object.assign(el.style, resolvedObject); } }; const setupReactivity = ( val: DynamicValue, el: Node, callback: (latest: T) => void ) => { if (isSignal(val)) { deferToMacrotask(() => { let initialRun = true; // effect() automatically tracks dependency on val.value // and returns a dispose function const dispose = effect(() => { const latest = val.value; // Access .value to subscribe if (!el.isConnected) { dispose(); // Teardown effect when DOM element is unmounted return; } // Skip running callback on initial sync pass since element was already configured if (initialRun) { initialRun = false; return; } callback(latest); }); }); } }; const appendChildNode = (parent: HTMLElement, child: ChildNodeInput) => { if (child === null || child === undefined) return; if (Array.isArray(child)) { child.forEach((c) => appendChildNode(parent, c)); return; } if (isSignal(child)) { const textNode = document.createTextNode(String(child.value ?? "")); setupReactivity(child, textNode, (val) => { textNode.textContent = val ?? ""; }); parent.appendChild(textNode); return; } if (child instanceof Node) { parent.appendChild(child); return; } parent.appendChild(document.createTextNode(String(child))); }; // --- Core Proxy --- const _h = ( tagName: K, ): HTMLElementTagNameMap[K] => document.createElement(tagName); export const h = new Proxy({} as ElementBuilderMap, { get: (_: unknown, tagName: K) => { return ( props?: ElementProps, children?: ChildNodeInput[], ): HTMLElementTagNameMap[K] => { const el = _h(tagName); if (props) { const { attrs, classList, style, on, ...restProps } = props; // Synchronous Setup if (on) { for (const [eventName, listener] of Object.entries(on)) { if (typeof listener === "function") { el.addEventListener(eventName, listener as EventListener); } } } if (attrs) { for (const [key, value] of Object.entries(attrs)) { const initialVal = resolveValue(value, el); if (initialVal !== false) { el.setAttribute(key, initialVal === true ? "" : String(initialVal)); } } } if (classList) { for (const [key, condition] of Object.entries(classList)) { const initialCond = Boolean(resolveValue(condition, el)); el.classList.toggle(key, initialCond); } } if (style) { applyStyles(el, style); } for (const [key, value] of Object.entries(restProps)) { const initialVal = resolveValue(value as DynamicValue, el); (el as any)[key] = initialVal; } // Deferred Reactivity Setup if (attrs) { for (const [key, value] of Object.entries(attrs)) { setupReactivity(value, el, (val) => { if (val === false) { el.removeAttribute(key); } else { el.setAttribute(key, val === true ? "" : String(val)); } }); } } if (classList) { for (const [key, condition] of Object.entries(classList)) { setupReactivity(condition, el, (val) => { el.classList.toggle(key, Boolean(val)); }); } } if (style) { setupReactivity(style, el, () => { applyStyles(el, style); }); } for (const [key, value] of Object.entries(restProps)) { setupReactivity(value as DynamicValue, el, (val) => { (el as any)[key] = val; }); } } if (children) { children.forEach((child) => appendChildNode(el, child)); } return el; }; }, });