Something went wrong. Try again.
Small functional reactive frontend library
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223import { Signal, effect } from "@preact/signals-core";
// --- Types ---
export type DynamicValue<T> = T | Signal<T> | ((el: HTMLElement) => T);
export type StyleObject = Record<string, DynamicValue<string | number>>;export type StyleProp = DynamicValue<string | StyleObject>;
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<T extends HTMLElement> = { attrs?: Record<string, DynamicValue<string | number | boolean>>; classList?: Record<string, DynamicValue<boolean>>; style?: StyleProp; on?: EventListenersMap;} & { [P in keyof T as P extends "attrs" | "classList" | "style" | "on" ? never : P]?: DynamicValue<T[P]>;};
export type ChildNodeInput = string | number | Node | Signal<any> | ChildNodeInput[];
type ElementBuilderMap = { [K in keyof HTMLElementTagNameMap]: ( props?: ElementProps<HTMLElementTagNameMap[K]>, children?: ChildNodeInput[], ) => HTMLElementTagNameMap[K];};
// --- Helpers ---
export const deferToMacrotask = (cb: () => void) => { setTimeout(cb, 0);};
const isSignal = (val: unknown): val is Signal<any> => { return val instanceof Signal;};
const resolveValue = <T>(val: DynamicValue<T>, 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<string, string | number> = {}; for (const [key, val] of Object.entries(resolved)) { const propVal = resolveValue(val as DynamicValue<string | number>, el); if (propVal !== null && propVal !== undefined) { resolvedObject[key] = propVal; } } Object.assign(el.style, resolvedObject); }};
const setupReactivity = <T>( val: DynamicValue<T>, 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 = <K extends keyof HTMLElementTagNameMap>( tagName: K,): HTMLElementTagNameMap[K] => document.createElement(tagName);
export const h = new Proxy({} as ElementBuilderMap, { get: <K extends keyof HTMLElementTagNameMap>(_: unknown, tagName: K) => { return ( props?: ElementProps<HTMLElementTagNameMap[K]>, 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<any>, 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<any>, el, (val) => { (el as any)[key] = val; }); } }
if (children) { children.forEach((child) => appendChildNode(el, child)); }
return el; }; },});