libreria-renderizado-npm
Version:
Una librería básica para gestionar el estado y JSX
48 lines (47 loc) • 1.35 kB
JavaScript
function u(o) {
let t = o;
const r = /* @__PURE__ */ new Set();
function n(s) {
s !== t && (t = s, r.forEach((f) => f()));
}
function e() {
return t;
}
function i(s) {
if (typeof s != "function")
throw new Error("El listener debe ser una función");
return r.add(s), () => r.delete(s);
}
return [e, n, i];
}
function a(o, t, ...r) {
if (typeof o != "string")
throw new Error("El tipo del elemento debe ser una cadena");
const n = document.createElement(o);
return t && Object.keys(t).forEach((e) => {
if (e.startsWith("on")) {
const i = e.substring(2).toLowerCase();
typeof t[e] == "function" ? n.addEventListener(i, t[e]) : console.warn(`El manejador del evento ${i} no es una función.`);
} else e === "className" ? n.classList.add(t[e]) : n.setAttribute(e, t[e]);
}), r.forEach((e) => {
typeof e == "string" ? n.appendChild(document.createTextNode(e)) : e instanceof HTMLElement && n.appendChild(e);
}), n;
}
function c(o, t) {
if (!t || !(t instanceof HTMLElement))
throw new Error("El contenedor debe ser un elemento DOM válido");
function r() {
const { element: e } = o();
t.innerHTML = "", t.appendChild(e);
}
r();
const n = o().subscribe(r);
return () => {
r(), n();
};
}
export {
u as createState,
a as jsx,
c as render
};