diff --git a/dist/dde-with-signals.js b/dist/dde-with-signals.js index 8a34d6d..70b02fe 100644 --- a/dist/dde-with-signals.js +++ b/dist/dde-with-signals.js @@ -1,7 +1,7 @@ //deka-dom-el library is available via global namespace `dde` (()=> { // src/signals-common.js -var k = { +var N = { isSignal(t) { return !1; }, @@ -9,23 +9,23 @@ var k = { return n; } }; -function B(t, e = !0) { - return e ? Object.assign(k, t) : (Object.setPrototypeOf(t, k), t); +function H(t, e = !0) { + return e ? Object.assign(N, t) : (Object.setPrototypeOf(t, N), t); } -function W(t) { - return k.isPrototypeOf(t) && t !== k ? t : k; +function j(t) { + return N.isPrototypeOf(t) && t !== N ? t : N; } // src/helpers.js -var T = (...t) => Object.prototype.hasOwnProperty.call(...t); -function S(t) { +var I = (...t) => Object.prototype.hasOwnProperty.call(...t); +function A(t) { return typeof t > "u"; } function X(t) { let e = typeof t; return e !== "object" ? e : t === null ? "null" : Object.prototype.toString.call(t); } -function q(t, e) { +function P(t, e) { if (!t || !(t instanceof AbortSignal)) return !0; if (!t.aborted) @@ -33,7 +33,7 @@ function q(t, e) { t.removeEventListener("abort", e); }; } -function F(t, e) { +function W(t, e) { let { observedAttributes: n = [] } = t.constructor; return n.reduce(function(r, o) { return r[dt(o)] = e(t, o), r; @@ -54,17 +54,17 @@ var d = { M: globalThis.MutationObserver }; function pt(t, e, n) { - if (Reflect.set(t, e, n), !!S(n)) { + if (Reflect.set(t, e, n), !!A(n)) { if (Reflect.deleteProperty(t, e), t instanceof d.H && t.getAttribute(e) === "undefined") return t.removeAttribute(e); if (Reflect.get(t, e) === "undefined") return Reflect.set(t, e, ""); } } -var O = "__dde_lifecyclesToEvents", _ = "dde:connected", C = "dde:disconnected", M = "dde:attributeChanged"; +var O = "__dde_lifecyclesToEvents", _ = "dde:connected", S = "dde:disconnected", T = "dde:attributeChanged"; // src/dom.js -var A = [{ +var y = [{ get scope() { return d.D.body; }, @@ -72,7 +72,7 @@ var A = [{ prevent: !0 }], x = { get current() { - return A[A.length - 1]; + return y[y.length - 1]; }, get host() { return this.current.host; @@ -82,17 +82,17 @@ var A = [{ return t.prevent = !0, t; }, get state() { - return [...A]; + return [...y]; }, push(t = {}) { - return A.push(Object.assign({}, this.current, { prevent: !1 }, t)); + return y.push(Object.assign({}, this.current, { prevent: !1 }, t)); }, pushRoot() { - return A.push(A[0]); + return y.push(y[0]); }, pop() { - if (A.length !== 1) - return A.pop(); + if (y.length !== 1) + return y.pop(); } }; function Y(...t) { @@ -102,95 +102,82 @@ function lt(t) { return t.append === Y || (t.appendOriginal = t.append, t.append = Y), t; } var $; -function P(t, e, ...n) { - let r = W(this), o = 0, c, i; +function M(t, e, ...n) { + let r = j(this), o = 0, c, u; switch ((Object(e) !== e || r.isSignal(e)) && (e = { textContent: e }), !0) { case typeof t == "function": { o = 1; - let a = (...p) => p.length ? (o === 1 ? n.unshift(...p) : p.forEach((m) => m(i)), void 0) : i; + let a = (...h) => h.length ? (o === 1 ? n.unshift(...h) : h.forEach((m) => m(u)), void 0) : u; x.push({ scope: t, host: a }), c = t(e || void 0); - let h = c instanceof d.F; + let l = c instanceof d.F; if (c.nodeName === "#comment") break; - let v = P.mark({ + let v = M.mark({ type: "component", name: t.name, - host: h ? "this" : "parentElement" + host: l ? "this" : "parentElement" }); - c.prepend(v), h && (i = v); + c.prepend(v), l && (u = v); break; } case t === "#text": - c = j.call(this, d.D.createTextNode(""), e); + c = q.call(this, d.D.createTextNode(""), e); break; case (t === "<>" || !t): - c = j.call(this, d.D.createDocumentFragment(), e); + c = q.call(this, d.D.createDocumentFragment(), e); break; case !!$: - c = j.call(this, d.D.createElementNS($, t), e); + c = q.call(this, d.D.createElementNS($, t), e); break; case !c: - c = j.call(this, d.D.createElement(t), e); + c = q.call(this, d.D.createElement(t), e); } - return lt(c), i || (i = c), n.forEach((a) => a(i)), o && x.pop(), o = 2, c; + return lt(c), u || (u = c), n.forEach((a) => a(u)), o && x.pop(), o = 2, c; } -P.mark = function(t, e = !1) { +M.mark = function(t, e = !1) { t = Object.entries(t).map(([o, c]) => o + `="${c}"`).join(" "); let n = e ? "" : "/", r = d.D.createComment(``); return e && (r.end = d.D.createComment("")), r; }; -function Wt(t, e, n) { - typeof e != "object" && (n = e, e = t); - let r = Symbol.for("default"), o = Array.from(e.querySelectorAll("slot")).reduce((i, a) => Reflect.set(i, a.name || r, a) && i, {}), c = T(o, r); - if (t.append = new Proxy(t.append, { - apply(i, a, h) { - if (h[0] === e) return i.apply(t, h); - if (!h.length) return t; - let v = d.D.createDocumentFragment(); - for (let p of h) { - if (!p || !p.slot) { - c && v.append(p); - continue; - } - let m = p.slot, y = o[m]; - vt(p, "remove", "slot"), y && (bt(y, p, n), Reflect.deleteProperty(o, m)); - } - return c && (o[r].replaceWith(v), Reflect.deleteProperty(o, r)), t.append = i, t; - } - }), t !== e) { - let i = Array.from(t.childNodes); - i.forEach((a) => a.remove()), t.append(...i); - } - return e; -} -function ht(...t) { - return t.filter(Boolean).join(" "); -} -function bt(t, e, n) { - n && n(t, e); - try { - t.replaceWith(j(e, { - className: ht(e.className, t.className), - dataset: { ...t.dataset } - })); - } catch { - t.replaceWith(e); - } -} -function qt(t) { +function Mt(t) { let e = this; return function(...r) { $ = t; - let o = P.call(e, ...r); + let o = M.call(e, ...r); return $ = void 0, o; }; } -var U = /* @__PURE__ */ new WeakMap(), { setDeleteAttr: tt } = d; -function j(t, ...e) { +function jt(t, e = t) { + let n = "\xB9\u2070", r = "\u2713", o = Object.fromEntries( + Array.from(e.querySelectorAll("slot")).filter((c) => !c.name.endsWith(n)).map((c) => [c.name += n, c]) + ); + if (t.append = new Proxy(t.append, { + apply(c, u, a) { + if (a[0] === e) return c.apply(t, a); + for (let l of a) { + let v = (l.slot || "") + n; + try { + bt(l, "remove", "slot"); + } catch { + } + let h = o[v]; + if (!h) return; + v.startsWith(r) || h.childNodes.forEach((m) => m.remove()), h.append(l), h.name = r + v; + } + return t.append = c, t; + } + }), t !== e) { + let c = Array.from(t.childNodes); + t.append(...c); + } + return e; +} +var F = /* @__PURE__ */ new WeakMap(), { setDeleteAttr: tt } = d; +function q(t, ...e) { if (!e.length) return t; - U.set(t, rt(t, this)); + F.set(t, rt(t, this)); for (let [n, r] of Object.entries(Object.assign({}, ...e))) nt.call(this, t, n, r); - return U.delete(t), t; + return F.delete(t), t; } function nt(t, e, n) { let { setRemoveAttr: r, s: o } = rt(t, this), c = this; @@ -198,11 +185,11 @@ function nt(t, e, n) { t, e, n, - (a, h) => nt.call(c, t, a, h) + (a, l) => nt.call(c, t, a, l) ); - let [i] = e; - if (i === "=") return r(e.slice(1), n); - if (i === ".") return et(t, e.slice(1), n); + let [u] = e; + if (u === "=") return r(e.slice(1), n); + if (u === ".") return et(t, e.slice(1), n); if (/(aria|data)([A-Z])/.test(e)) return e = e.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase(), r(e, n); switch (e === "className" && (e = "class"), e) { @@ -214,177 +201,177 @@ function nt(t, e, n) { if (typeof n != "object") break; /* falls through */ case "dataset": - return I(o, n, et.bind(null, t[e])); + return B(o, n, et.bind(null, t[e])); case "ariaset": - return I(o, n, (a, h) => r("aria-" + a, h)); + return B(o, n, (a, l) => r("aria-" + a, l)); case "classList": - return gt.call(c, t, n); + return ht.call(c, t, n); } - return Et(t, e) ? tt(t, e, n) : r(e, n); + return gt(t, e) ? tt(t, e, n) : r(e, n); } function rt(t, e) { - if (U.has(t)) return U.get(t); - let r = (t instanceof d.S ? xt : mt).bind(null, t, "Attribute"), o = W(e); + if (F.has(t)) return F.get(t); + let r = (t instanceof d.S ? Et : vt).bind(null, t, "Attribute"), o = j(e); return { setRemoveAttr: r, s: o }; } -function gt(t, e) { - let n = W(this); - return I( +function ht(t, e) { + let n = j(this); + return B( n, e, (r, o) => t.classList.toggle(r, o === -1 ? void 0 : !!o) ), t; } -function vt(t, e, n, r) { +function bt(t, e, n, r) { return t instanceof d.H ? t[e + "Attribute"](n, r) : t[e + "AttributeNS"](null, n, r); } -function Et(t, e) { +function gt(t, e) { if (!(e in t)) return !1; let n = ot(t, e); - return !S(n.set); + return !A(n.set); } function ot(t, e) { if (t = Object.getPrototypeOf(t), !t) return {}; let n = Object.getOwnPropertyDescriptor(t, e); return n || ot(t, e); } -function I(t, e, n) { +function B(t, e, n) { if (!(typeof e != "object" || e === null)) return Object.entries(e).forEach(function([o, c]) { o && (c = t.processReactiveAttribute(e, o, c, n), n(o, c)); }); } -function mt(t, e, n, r) { - return t[(S(r) ? "remove" : "set") + e](n, r); +function vt(t, e, n, r) { + return t[(A(r) ? "remove" : "set") + e](n, r); } -function xt(t, e, n, r, o = null) { - return t[(S(r) ? "remove" : "set") + e + "NS"](o, n, r); +function Et(t, e, n, r, o = null) { + return t[(A(r) ? "remove" : "set") + e + "NS"](o, n, r); } function et(t, e, n) { - if (Reflect.set(t, e, n), !!S(n)) + if (Reflect.set(t, e, n), !!A(n)) return Reflect.deleteProperty(t, e); } // src/events-observer.js -var D = d.M ? wt() : new Proxy({}, { +var C = d.M ? mt() : new Proxy({}, { get() { return () => { }; } }); -function wt() { - let t = /* @__PURE__ */ new Map(), e = !1, n = (s) => function(u) { - for (let f of u) +function mt() { + let t = /* @__PURE__ */ new Map(), e = !1, n = (i) => function(s) { + for (let f of s) if (f.type === "childList") { - if (p(f.addedNodes, !0)) { - s(); + if (h(f.addedNodes, !0)) { + i(); continue; } - m(f.removedNodes, !0) && s(); + m(f.removedNodes, !0) && i(); } }, r = new d.M(n(a)); return { - observe(s) { - let u = new d.M(n(() => { + observe(i) { + let s = new d.M(n(() => { })); - return u.observe(s, { childList: !0, subtree: !0 }), () => u.disconnect(); + return s.observe(i, { childList: !0, subtree: !0 }), () => s.disconnect(); }, - onConnected(s, u) { - i(); - let f = c(s); - f.connected.has(u) || (f.connected.add(u), f.length_c += 1); + onConnected(i, s) { + u(); + let f = c(i); + f.connected.has(s) || (f.connected.add(s), f.length_c += 1); }, - offConnected(s, u) { - if (!t.has(s)) return; - let f = t.get(s); - f.connected.has(u) && (f.connected.delete(u), f.length_c -= 1, o(s, f)); + offConnected(i, s) { + if (!t.has(i)) return; + let f = t.get(i); + f.connected.has(s) && (f.connected.delete(s), f.length_c -= 1, o(i, f)); }, - onDisconnected(s, u) { - i(); - let f = c(s); - f.disconnected.has(u) || (f.disconnected.add(u), f.length_d += 1); + onDisconnected(i, s) { + u(); + let f = c(i); + f.disconnected.has(s) || (f.disconnected.add(s), f.length_d += 1); }, - offDisconnected(s, u) { - if (!t.has(s)) return; - let f = t.get(s); - f.disconnected.has(u) && (f.disconnected.delete(u), f.length_d -= 1, o(s, f)); + offDisconnected(i, s) { + if (!t.has(i)) return; + let f = t.get(i); + f.disconnected.has(s) && (f.disconnected.delete(s), f.length_d -= 1, o(i, f)); } }; - function o(s, u) { - u.length_c || u.length_d || (t.delete(s), a()); + function o(i, s) { + s.length_c || s.length_d || (t.delete(i), a()); } - function c(s) { - if (t.has(s)) return t.get(s); - let u = { + function c(i) { + if (t.has(i)) return t.get(i); + let s = { connected: /* @__PURE__ */ new WeakSet(), length_c: 0, disconnected: /* @__PURE__ */ new WeakSet(), length_d: 0 }; - return t.set(s, u), u; + return t.set(i, s), s; } - function i() { + function u() { e || (e = !0, r.observe(d.D.body, { childList: !0, subtree: !0 })); } function a() { !e || t.size || (e = !1, r.disconnect()); } - function h() { - return new Promise(function(s) { - (requestIdleCallback || requestAnimationFrame)(s); + function l() { + return new Promise(function(i) { + (requestIdleCallback || requestAnimationFrame)(i); }); } - async function v(s) { - t.size > 30 && await h(); - let u = []; - if (!(s instanceof Node)) return u; + async function v(i) { + t.size > 30 && await l(); + let s = []; + if (!(i instanceof Node)) return s; for (let f of t.keys()) - f === s || !(f instanceof Node) || s.contains(f) && u.push(f); - return u; + f === i || !(f instanceof Node) || i.contains(f) && s.push(f); + return s; } - function p(s, u) { + function h(i, s) { let f = !1; - for (let b of s) { - if (u && v(b).then(p), !t.has(b)) continue; - let N = t.get(b); - N.length_c && (b.dispatchEvent(new Event(_)), N.connected = /* @__PURE__ */ new WeakSet(), N.length_c = 0, N.length_d || t.delete(b), f = !0); + for (let b of i) { + if (s && v(b).then(h), !t.has(b)) continue; + let L = t.get(b); + L.length_c && (b.dispatchEvent(new Event(_)), L.connected = /* @__PURE__ */ new WeakSet(), L.length_c = 0, L.length_d || t.delete(b), f = !0); } return f; } - function m(s, u) { + function m(i, s) { let f = !1; - for (let b of s) - u && v(b).then(m), !(!t.has(b) || !t.get(b).length_d) && ((globalThis.queueMicrotask || setTimeout)(y(b)), f = !0); + for (let b of i) + s && v(b).then(m), !(!t.has(b) || !t.get(b).length_d) && ((globalThis.queueMicrotask || setTimeout)(k(b)), f = !0); return f; } - function y(s) { + function k(i) { return () => { - s.isConnected || (s.dispatchEvent(new Event(C)), t.delete(s)); + i.isConnected || (i.dispatchEvent(new Event(S)), t.delete(i)); }; } } // src/customElement.js -function Jt(t, e, n, r = _t) { +function Ht(t, e, n, r = wt) { x.push({ scope: t, - host: (...i) => i.length ? i.forEach((a) => a(t)) : t + host: (...u) => u.length ? u.forEach((a) => a(t)) : t }), typeof r == "function" && (r = r.call(t, t)); let o = t[O]; - o || yt(t); + o || xt(t); let c = n.call(t, r); - return o || t.dispatchEvent(new Event(_)), e.nodeType === 11 && typeof e.mode == "string" && t.addEventListener(C, D.observe(e), { once: !0 }), x.pop(), e.append(c); + return o || t.dispatchEvent(new Event(_)), e.nodeType === 11 && typeof e.mode == "string" && t.addEventListener(S, C.observe(e), { once: !0 }), x.pop(), e.append(c); } -function yt(t) { +function xt(t) { return J(t.prototype, "connectedCallback", function(e, n, r) { e.apply(n, r), n.dispatchEvent(new Event(_)); }), J(t.prototype, "disconnectedCallback", function(e, n, r) { e.apply(n, r), (globalThis.queueMicrotask || setTimeout)( - () => !n.isConnected && n.dispatchEvent(new Event(C)) + () => !n.isConnected && n.dispatchEvent(new Event(S)) ); }), J(t.prototype, "attributeChangedCallback", function(e, n, r) { let [o, , c] = r; - n.dispatchEvent(new CustomEvent(M, { + n.dispatchEvent(new CustomEvent(T, { detail: [o, c] })), e.apply(n, r); }), t.prototype[O] = !0, t; @@ -393,16 +380,16 @@ function J(t, e, n) { t[e] = new Proxy(t[e] || (() => { }), { apply: n }); } -function _t(t) { - return F(t, (e, n) => e.getAttribute(n)); +function wt(t) { + return W(t, (e, n) => e.getAttribute(n)); } // src/events.js -function Kt(t, e, n) { +function Zt(t, e, n) { return e || (e = {}), function(o, ...c) { n && (c.unshift(o), o = typeof n == "function" ? n() : n); - let i = c.length ? new CustomEvent(t, Object.assign({ detail: c[0] }, e)) : new Event(t, e); - return o.dispatchEvent(i); + let u = c.length ? new CustomEvent(t, Object.assign({ detail: c[0] }, e)) : new Event(t, e); + return o.dispatchEvent(u); }; } function w(t, e, n) { @@ -413,12 +400,12 @@ function w(t, e, n) { var ct = (t) => Object.assign({}, typeof t == "object" ? t : null, { once: !0 }); w.connected = function(t, e) { return e = ct(e), function(r) { - return r.addEventListener(_, t, e), r[O] ? r : r.isConnected ? (r.dispatchEvent(new Event(_)), r) : (q(e.signal, () => D.offConnected(r, t)) && D.onConnected(r, t), r); + return r.addEventListener(_, t, e), r[O] ? r : r.isConnected ? (r.dispatchEvent(new Event(_)), r) : (P(e.signal, () => C.offConnected(r, t)) && C.onConnected(r, t), r); }; }; w.disconnected = function(t, e) { return e = ct(e), function(r) { - return r.addEventListener(C, t, e), r[O] || q(e.signal, () => D.offDisconnected(r, t)) && D.onDisconnected(r, t), r; + return r.addEventListener(S, t, e), r[O] || P(e.signal, () => C.offDisconnected(r, t)) && C.onDisconnected(r, t), r; }; }; var Z = /* @__PURE__ */ new WeakMap(); @@ -427,45 +414,45 @@ w.disconnectedAsAbort = function(t) { let e = new AbortController(); return Z.set(t, e), t(w.disconnected(() => e.abort())), e; }; -var At = /* @__PURE__ */ new WeakSet(); +var _t = /* @__PURE__ */ new WeakSet(); w.attributeChanged = function(t, e) { return typeof e != "object" && (e = {}), function(r) { - if (r.addEventListener(M, t, e), r[O] || At.has(r) || !d.M) return r; - let o = new d.M(function(i) { - for (let { attributeName: a, target: h } of i) - h.dispatchEvent( - new CustomEvent(M, { detail: [a, h.getAttribute(a)] }) + if (r.addEventListener(T, t, e), r[O] || _t.has(r) || !d.M) return r; + let o = new d.M(function(u) { + for (let { attributeName: a, target: l } of u) + l.dispatchEvent( + new CustomEvent(T, { detail: [a, l.getAttribute(a)] }) ); }); - return q(e.signal, () => o.disconnect()) && o.observe(r, { attributes: !0 }), r; + return P(e.signal, () => o.disconnect()) && o.observe(r, { attributes: !0 }), r; }; }; // src/signals-lib.js -var l = "__dde_signal"; -function z(t) { +var p = "__dde_signal"; +function U(t) { try { - return T(t, l); + return I(t, p); } catch { return !1; } } -var H = [], g = /* @__PURE__ */ new WeakMap(); +var z = [], g = /* @__PURE__ */ new WeakMap(); function E(t, e) { if (typeof t != "function") return it(!1, t, e); - if (z(t)) return t; + if (U(t)) return t; let n = it(!0), r = function() { let [o, ...c] = g.get(r); - if (g.set(r, /* @__PURE__ */ new Set([o])), H.push(r), at(n, t()), H.pop(), !c.length) return; - let i = g.get(r); + if (g.set(r, /* @__PURE__ */ new Set([o])), z.push(r), at(n, t()), z.pop(), !c.length) return; + let u = g.get(r); for (let a of c) - i.has(a) || L(a, r); + u.has(a) || R(a, r); }; - return g.set(n[l], r), g.set(r, /* @__PURE__ */ new Set([n])), r(), n; + return g.set(n[p], r), g.set(r, /* @__PURE__ */ new Set([n])), r(), n; } E.action = function(t, e, ...n) { - let r = t[l], { actions: o } = r; + let r = t[p], { actions: o } = r; if (!o || !(e in o)) throw new Error(`'${t}' has no action with name '${e}'!`); if (o[e].apply(r, n), r.skip) return delete r.skip; @@ -475,7 +462,7 @@ E.on = function t(e, n, r = {}) { let { signal: o } = r; if (!(o && o.aborted)) { if (Array.isArray(e)) return e.forEach((c) => t(c, n, r)); - Q(e, n), o && o.addEventListener("abort", () => L(e, n)); + Q(e, n), o && o.addEventListener("abort", () => R(e, n)); } }; E.symbols = { @@ -484,8 +471,8 @@ E.symbols = { }; E.clear = function(...t) { for (let n of t) { - let r = n[l]; - r && (delete n.toJSON, r.onclear.forEach((o) => o.call(r)), e(n, r), delete n[l]); + let r = n[p]; + r && (delete n.toJSON, r.onclear.forEach((o) => o.call(r)), e(n, r), delete n[p]); } function e(n, r) { r.listeners.forEach((o) => { @@ -495,53 +482,53 @@ E.clear = function(...t) { }); } }; -var R = "__dde_reactive"; +var D = "__dde_reactive"; E.el = function(t, e) { - let n = P.mark({ type: "reactive" }, !0), r = n.end, o = d.D.createDocumentFragment(); + let n = M.mark({ type: "reactive" }, !0), r = n.end, o = d.D.createDocumentFragment(); o.append(n, r); - let { current: c } = x, i = {}, a = (h) => { + let { current: c } = x, u = {}, a = (l) => { if (!n.parentNode || !r.parentNode) - return L(t, a); - let v = i; - i = {}, x.push(c); - let p = e(h, function(u, f) { + return R(t, a); + let v = u; + u = {}, x.push(c); + let h = e(l, function(s, f) { let b; - return T(v, u) ? (b = v[u], delete v[u]) : b = f(), i[u] = b, b; + return I(v, s) ? (b = v[s], delete v[s]) : b = f(), u[s] = b, b; }); - x.pop(), Array.isArray(p) || (p = [p]); + x.pop(), Array.isArray(h) || (h = [h]); let m = document.createComment(""); - p.push(m), n.after(...p); - let y; - for (; (y = m.nextSibling) && y !== r; ) - y.remove(); - m.remove(), n.isConnected && St(c.host()); + h.push(m), n.after(...h); + let k; + for (; (k = m.nextSibling) && k !== r; ) + k.remove(); + m.remove(), n.isConnected && yt(c.host()); }; return Q(t, a), ut(t, a, n, e), a(t()), o; }; -function St(t) { - !t || !t[R] || (requestIdleCallback || setTimeout)(function() { - t[R] = t[R].filter(([e, n]) => n.isConnected ? !0 : (L(...e), !1)); +function yt(t) { + !t || !t[D] || (requestIdleCallback || setTimeout)(function() { + t[D] = t[D].filter(([e, n]) => n.isConnected ? !0 : (R(...e), !1)); }); } -var Ot = { +var At = { _set(t) { this.value = t; } }; -function Ct(t) { +function Ot(t) { return function(e, n) { - let r = (...c) => c.length ? e.setAttribute(n, ...c) : K(r), o = ft(r, e.getAttribute(n), Ot); + let r = (...c) => c.length ? e.setAttribute(n, ...c) : K(r), o = ft(r, e.getAttribute(n), At); return t[n] = o, o; }; } var G = "__dde_attributes"; E.observedAttributes = function(t) { - let e = t[G] = {}, n = F(t, Ct(e)); + let e = t[G] = {}, n = W(t, Ot(e)); return w.attributeChanged(function({ detail: o }) { /*! This maps attributes to signals (`S.observedAttributes`). * Investigate `__dde_attributes` key of the element.*/ - let [c, i] = o, a = this[G][c]; - if (a) return E.action(a, "_set", i); + let [c, u] = o, a = this[G][c]; + if (a) return E.action(a, "_set", u); })(t), w.disconnected(function() { /*! This removes all signals mapped to attributes (`S.observedAttributes`). * Investigate `__dde_attributes` key of the element.*/ @@ -549,12 +536,12 @@ E.observedAttributes = function(t) { })(t), n; }; var st = { - isSignal: z, + isSignal: U, processReactiveAttribute(t, e, n, r) { - if (!z(n)) return n; + if (!U(n)) return n; let o = (c) => { if (!t.isConnected) - return L(n, o); + return R(n, o); r(e, c); }; return Q(n, o), ut(n, o, t, e), n(); @@ -563,22 +550,22 @@ var st = { function ut(t, e, ...n) { let { current: r } = x; r.prevent || r.host(function(o) { - o[R] || (o[R] = [], w.disconnected( + o[D] || (o[D] = [], w.disconnected( () => ( /*! * Clears all Signals listeners added in the current scope/host (`S.el`, `assign`, …?). * You can investigate the `__dde_reactive` key of the element. * */ - o[R].forEach(([[c, i]]) => L(c, i, c[l] && c[l].host && c[l].host() === o)) + o[D].forEach(([[c, u]]) => R(c, u, c[p] && c[p].host && c[p].host() === o)) ) - )(o)), o[R].push([[t, e], ...n]); + )(o)), o[D].push([[t, e], ...n]); }); } function it(t, e, n) { let r = t ? () => K(r) : (...o) => o.length ? at(r, ...o) : K(r); return ft(r, e, n, t); } -var Dt = Object.assign(/* @__PURE__ */ Object.create(null), { +var St = Object.assign(/* @__PURE__ */ Object.create(null), { stopPropagation() { this.skip = !0; } @@ -595,13 +582,13 @@ function ft(t, e, n, r = !1) { X(n) !== "[object Object]" && (n = {}); let { onclear: c } = E.symbols; n[c] && (o.push(n[c]), delete n[c]); - let { host: i } = x; - return Reflect.defineProperty(t, l, { + let { host: u } = x; + return Reflect.defineProperty(t, p, { value: { value: e, actions: n, onclear: o, - host: i, + host: u, listeners: /* @__PURE__ */ new Set(), defined: new V().stack, readonly: r @@ -609,65 +596,64 @@ function ft(t, e, n, r = !1) { enumerable: !1, writable: !1, configurable: !0 - }), t.toJSON = () => t(), t.valueOf = () => t[l] && t[l].value, Object.setPrototypeOf(t[l], Dt), t; + }), t.toJSON = () => t(), t.valueOf = () => t[p] && t[p].value, Object.setPrototypeOf(t[p], St), t; } -function Rt() { - return H[H.length - 1]; +function Ct() { + return z[z.length - 1]; } function K(t) { - if (!t[l]) return; - let { value: e, listeners: n } = t[l], r = Rt(); + if (!t[p]) return; + let { value: e, listeners: n } = t[p], r = Ct(); return r && n.add(r), g.has(r) && g.get(r).add(t), e; } function at(t, e, n) { - if (!t[l]) return; - let r = t[l]; + if (!t[p]) return; + let r = t[p]; if (!(!n && r.value === e)) return r.value = e, r.listeners.forEach((o) => o(e)), e; } function Q(t, e) { - if (t[l]) - return t[l].listeners.add(e); + if (t[p]) + return t[p].listeners.add(e); } -function L(t, e, n) { - let r = t[l]; +function R(t, e, n) { + let r = t[p]; if (!r) return; let o = r.listeners.delete(e); if (n && !r.listeners.size) { if (E.clear(t), !g.has(r)) return o; let c = g.get(r); if (!g.has(c)) return o; - g.get(c).forEach((i) => L(i, c, !0)); + g.get(c).forEach((u) => R(u, c, !0)); } return o; } // signals.js -B(st); +H(st); globalThis.dde= { S: E, - assign: j, + assign: q, assignAttribute: nt, chainableAppend: lt, - classListDeclarative: gt, - cn: ht, - createElement: P, - createElementNS: qt, - customElementRender: Jt, - customElementWithDDE: yt, - dispatchEvent: Kt, - el: P, - elNS: qt, - elementAttribute: vt, - isSignal: z, - lifecyclesToEvents: yt, - observedAttributes: _t, + classListDeclarative: ht, + createElement: M, + createElementNS: Mt, + customElementRender: Ht, + customElementWithDDE: xt, + dispatchEvent: Zt, + el: M, + elNS: Mt, + elementAttribute: bt, + isSignal: U, + lifecyclesToEvents: xt, + observedAttributes: wt, on: w, - registerReactivity: B, + registerReactivity: H, scope: x, signal: E, - simulateSlots: Wt + simulateSlots: jt }; })(); \ No newline at end of file diff --git a/dist/dde.js b/dist/dde.js index 7def816..73db7c7 100644 --- a/dist/dde.js +++ b/dist/dde.js @@ -1,7 +1,7 @@ //deka-dom-el library is available via global namespace `dde` (()=> { // src/signals-common.js -var m = { +var C = { isSignal(t) { return !1; }, @@ -9,19 +9,18 @@ var m = { return n; } }; -function G(t, e = !0) { - return e ? Object.assign(m, t) : (Object.setPrototypeOf(t, m), t); +function Z(t, e = !0) { + return e ? Object.assign(C, t) : (Object.setPrototypeOf(t, C), t); } -function L(t) { - return m.isPrototypeOf(t) && t !== m ? t : m; +function S(t) { + return C.isPrototypeOf(t) && t !== C ? t : C; } // src/helpers.js -var q = (...t) => Object.prototype.hasOwnProperty.call(...t); function x(t) { return typeof t > "u"; } -function N(t, e) { +function L(t, e) { if (!t || !(t instanceof AbortSignal)) return !0; if (!t.aborted) @@ -29,19 +28,19 @@ function N(t, e) { t.removeEventListener("abort", e); }; } -function F(t, e) { +function W(t, e) { let { observedAttributes: n = [] } = t.constructor; return n.reduce(function(r, o) { - return r[V(o)] = e(t, o), r; + return r[G(o)] = e(t, o), r; }, {}); } -function V(t) { +function G(t) { return t.replace(/-./g, (e) => e[1].toUpperCase()); } // src/dom-common.js -var a = { - setDeleteAttr: J, +var f = { + setDeleteAttr: V, ssr: "", D: globalThis.document, F: globalThis.DocumentFragment, @@ -49,24 +48,24 @@ var a = { S: globalThis.SVGElement, M: globalThis.MutationObserver }; -function J(t, e, n) { +function V(t, e, n) { if (Reflect.set(t, e, n), !!x(n)) { - if (Reflect.deleteProperty(t, e), t instanceof a.H && t.getAttribute(e) === "undefined") + if (Reflect.deleteProperty(t, e), t instanceof f.H && t.getAttribute(e) === "undefined") return t.removeAttribute(e); if (Reflect.get(t, e) === "undefined") return Reflect.set(t, e, ""); } } -var w = "__dde_lifecyclesToEvents", g = "dde:connected", y = "dde:disconnected", D = "dde:attributeChanged"; +var w = "__dde_lifecyclesToEvents", g = "dde:connected", y = "dde:disconnected", O = "dde:attributeChanged"; // src/dom.js var v = [{ get scope() { - return a.D.body; + return f.D.body; }, - host: (t) => t ? t(a.D.body) : a.D.body, + host: (t) => t ? t(f.D.body) : f.D.body, prevent: !0 -}], S = { +}], D = { get current() { return v[v.length - 1]; }, @@ -91,158 +90,145 @@ var v = [{ return v.pop(); } }; -function $(...t) { +function q(...t) { return this.appendOriginal(...t), this; } -function K(t) { - return t.append === $ || (t.appendOriginal = t.append, t.append = $), t; +function J(t) { + return t.append === q || (t.appendOriginal = t.append, t.append = q), t; } var T; -function j(t, e, ...n) { - let r = L(this), o = 0, c, f; +function k(t, e, ...n) { + let r = S(this), o = 0, c, a; switch ((Object(e) !== e || r.isSignal(e)) && (e = { textContent: e }), !0) { case typeof t == "function": { o = 1; - let d = (...l) => l.length ? (o === 1 ? n.unshift(...l) : l.forEach((E) => E(f)), void 0) : f; - S.push({ scope: t, host: d }), c = t(e || void 0); - let p = c instanceof a.F; + let d = (...h) => h.length ? (o === 1 ? n.unshift(...h) : h.forEach((E) => E(a)), void 0) : a; + D.push({ scope: t, host: d }), c = t(e || void 0); + let p = c instanceof f.F; if (c.nodeName === "#comment") break; - let b = j.mark({ + let b = k.mark({ type: "component", name: t.name, host: p ? "this" : "parentElement" }); - c.prepend(b), p && (f = b); + c.prepend(b), p && (a = b); break; } case t === "#text": - c = O.call(this, a.D.createTextNode(""), e); + c = R.call(this, f.D.createTextNode(""), e); break; case (t === "<>" || !t): - c = O.call(this, a.D.createDocumentFragment(), e); + c = R.call(this, f.D.createDocumentFragment(), e); break; case !!T: - c = O.call(this, a.D.createElementNS(T, t), e); + c = R.call(this, f.D.createElementNS(T, t), e); break; case !c: - c = O.call(this, a.D.createElement(t), e); + c = R.call(this, f.D.createElement(t), e); } - return K(c), f || (f = c), n.forEach((d) => d(f)), o && S.pop(), o = 2, c; + return J(c), a || (a = c), n.forEach((d) => d(a)), o && D.pop(), o = 2, c; } -j.mark = function(t, e = !1) { +k.mark = function(t, e = !1) { t = Object.entries(t).map(([o, c]) => o + `="${c}"`).join(" "); - let n = e ? "" : "/", r = a.D.createComment(``); - return e && (r.end = a.D.createComment("")), r; + let n = e ? "" : "/", r = f.D.createComment(``); + return e && (r.end = f.D.createComment("")), r; }; -function bt(t, e, n) { - typeof e != "object" && (n = e, e = t); - let r = Symbol.for("default"), o = Array.from(e.querySelectorAll("slot")).reduce((f, d) => Reflect.set(f, d.name || r, d) && f, {}), c = q(o, r); - if (t.append = new Proxy(t.append, { - apply(f, d, p) { - if (p[0] === e) return f.apply(t, p); - if (!p.length) return t; - let b = a.D.createDocumentFragment(); - for (let l of p) { - if (!l || !l.slot) { - c && b.append(l); - continue; - } - let E = l.slot, _ = o[E]; - tt(l, "remove", "slot"), _ && (X(_, l, n), Reflect.deleteProperty(o, E)); - } - return c && (o[r].replaceWith(b), Reflect.deleteProperty(o, r)), t.append = f, t; - } - }), t !== e) { - let f = Array.from(t.childNodes); - f.forEach((d) => d.remove()), t.append(...f); - } - return e; -} -function Q(...t) { - return t.filter(Boolean).join(" "); -} -function X(t, e, n) { - n && n(t, e); - try { - t.replaceWith(O(e, { - className: Q(e.className, t.className), - dataset: { ...t.dataset } - })); - } catch { - t.replaceWith(e); - } -} -function gt(t) { +function dt(t) { let e = this; return function(...r) { T = t; - let o = j.call(e, ...r); + let o = k.call(e, ...r); return T = void 0, o; }; } -var P = /* @__PURE__ */ new WeakMap(), { setDeleteAttr: U } = a; -function O(t, ...e) { - if (!e.length) return t; - P.set(t, B(t, this)); - for (let [n, r] of Object.entries(Object.assign({}, ...e))) - z.call(this, t, n, r); - return P.delete(t), t; +function pt(t, e = t) { + let n = "\xB9\u2070", r = "\u2713", o = Object.fromEntries( + Array.from(e.querySelectorAll("slot")).filter((c) => !c.name.endsWith(n)).map((c) => [c.name += n, c]) + ); + if (t.append = new Proxy(t.append, { + apply(c, a, d) { + if (d[0] === e) return c.apply(t, d); + for (let p of d) { + let b = (p.slot || "") + n; + try { + Q(p, "remove", "slot"); + } catch { + } + let h = o[b]; + if (!h) return; + b.startsWith(r) || h.childNodes.forEach((E) => E.remove()), h.append(p), h.name = r + b; + } + return t.append = c, t; + } + }), t !== e) { + let c = Array.from(t.childNodes); + t.append(...c); + } + return e; } -function z(t, e, n) { - let { setRemoveAttr: r, s: o } = B(t, this), c = this; +var N = /* @__PURE__ */ new WeakMap(), { setDeleteAttr: $ } = f; +function R(t, ...e) { + if (!e.length) return t; + N.set(t, H(t, this)); + for (let [n, r] of Object.entries(Object.assign({}, ...e))) + U.call(this, t, n, r); + return N.delete(t), t; +} +function U(t, e, n) { + let { setRemoveAttr: r, s: o } = H(t, this), c = this; n = o.processReactiveAttribute( t, e, n, - (d, p) => z.call(c, t, d, p) + (d, p) => U.call(c, t, d, p) ); - let [f] = e; - if (f === "=") return r(e.slice(1), n); - if (f === ".") return H(t, e.slice(1), n); + let [a] = e; + if (a === "=") return r(e.slice(1), n); + if (a === ".") return F(t, e.slice(1), n); if (/(aria|data)([A-Z])/.test(e)) return e = e.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase(), r(e, n); switch (e === "className" && (e = "class"), e) { case "xlink:href": return r(e, n, "http://www.w3.org/1999/xlink"); case "textContent": - return U(t, e, n); + return $(t, e, n); case "style": if (typeof n != "object") break; /* falls through */ case "dataset": - return M(o, n, H.bind(null, t[e])); + return M(o, n, F.bind(null, t[e])); case "ariaset": return M(o, n, (d, p) => r("aria-" + d, p)); case "classList": - return Y.call(c, t, n); + return K.call(c, t, n); } - return et(t, e) ? U(t, e, n) : r(e, n); + return X(t, e) ? $(t, e, n) : r(e, n); } -function B(t, e) { - if (P.has(t)) return P.get(t); - let r = (t instanceof a.S ? rt : nt).bind(null, t, "Attribute"), o = L(e); +function H(t, e) { + if (N.has(t)) return N.get(t); + let r = (t instanceof f.S ? tt : Y).bind(null, t, "Attribute"), o = S(e); return { setRemoveAttr: r, s: o }; } -function Y(t, e) { - let n = L(this); +function K(t, e) { + let n = S(this); return M( n, e, (r, o) => t.classList.toggle(r, o === -1 ? void 0 : !!o) ), t; } -function tt(t, e, n, r) { - return t instanceof a.H ? t[e + "Attribute"](n, r) : t[e + "AttributeNS"](null, n, r); +function Q(t, e, n, r) { + return t instanceof f.H ? t[e + "Attribute"](n, r) : t[e + "AttributeNS"](null, n, r); } -function et(t, e) { +function X(t, e) { if (!(e in t)) return !1; - let n = I(t, e); + let n = z(t, e); return !x(n.set); } -function I(t, e) { +function z(t, e) { if (t = Object.getPrototypeOf(t), !t) return {}; let n = Object.getOwnPropertyDescriptor(t, e); - return n || I(t, e); + return n || z(t, e); } function M(t, e, n) { if (!(typeof e != "object" || e === null)) @@ -250,213 +236,212 @@ function M(t, e, n) { o && (c = t.processReactiveAttribute(e, o, c, n), n(o, c)); }); } -function nt(t, e, n, r) { +function Y(t, e, n, r) { return t[(x(r) ? "remove" : "set") + e](n, r); } -function rt(t, e, n, r, o = null) { +function tt(t, e, n, r, o = null) { return t[(x(r) ? "remove" : "set") + e + "NS"](o, n, r); } -function H(t, e, n) { +function F(t, e, n) { if (Reflect.set(t, e, n), !!x(n)) return Reflect.deleteProperty(t, e); } // src/events-observer.js -var A = a.M ? ot() : new Proxy({}, { +var m = f.M ? et() : new Proxy({}, { get() { return () => { }; } }); -function ot() { - let t = /* @__PURE__ */ new Map(), e = !1, n = (i) => function(u) { - for (let s of u) - if (s.type === "childList") { - if (l(s.addedNodes, !0)) { - i(); +function et() { + let t = /* @__PURE__ */ new Map(), e = !1, n = (s) => function(u) { + for (let i of u) + if (i.type === "childList") { + if (h(i.addedNodes, !0)) { + s(); continue; } - E(s.removedNodes, !0) && i(); + E(i.removedNodes, !0) && s(); } - }, r = new a.M(n(d)); + }, r = new f.M(n(d)); return { - observe(i) { - let u = new a.M(n(() => { + observe(s) { + let u = new f.M(n(() => { })); - return u.observe(i, { childList: !0, subtree: !0 }), () => u.disconnect(); + return u.observe(s, { childList: !0, subtree: !0 }), () => u.disconnect(); }, - onConnected(i, u) { - f(); - let s = c(i); - s.connected.has(u) || (s.connected.add(u), s.length_c += 1); + onConnected(s, u) { + a(); + let i = c(s); + i.connected.has(u) || (i.connected.add(u), i.length_c += 1); }, - offConnected(i, u) { - if (!t.has(i)) return; - let s = t.get(i); - s.connected.has(u) && (s.connected.delete(u), s.length_c -= 1, o(i, s)); + offConnected(s, u) { + if (!t.has(s)) return; + let i = t.get(s); + i.connected.has(u) && (i.connected.delete(u), i.length_c -= 1, o(s, i)); }, - onDisconnected(i, u) { - f(); - let s = c(i); - s.disconnected.has(u) || (s.disconnected.add(u), s.length_d += 1); + onDisconnected(s, u) { + a(); + let i = c(s); + i.disconnected.has(u) || (i.disconnected.add(u), i.length_d += 1); }, - offDisconnected(i, u) { - if (!t.has(i)) return; - let s = t.get(i); - s.disconnected.has(u) && (s.disconnected.delete(u), s.length_d -= 1, o(i, s)); + offDisconnected(s, u) { + if (!t.has(s)) return; + let i = t.get(s); + i.disconnected.has(u) && (i.disconnected.delete(u), i.length_d -= 1, o(s, i)); } }; - function o(i, u) { - u.length_c || u.length_d || (t.delete(i), d()); + function o(s, u) { + u.length_c || u.length_d || (t.delete(s), d()); } - function c(i) { - if (t.has(i)) return t.get(i); + function c(s) { + if (t.has(s)) return t.get(s); let u = { connected: /* @__PURE__ */ new WeakSet(), length_c: 0, disconnected: /* @__PURE__ */ new WeakSet(), length_d: 0 }; - return t.set(i, u), u; + return t.set(s, u), u; } - function f() { - e || (e = !0, r.observe(a.D.body, { childList: !0, subtree: !0 })); + function a() { + e || (e = !0, r.observe(f.D.body, { childList: !0, subtree: !0 })); } function d() { !e || t.size || (e = !1, r.disconnect()); } function p() { - return new Promise(function(i) { - (requestIdleCallback || requestAnimationFrame)(i); + return new Promise(function(s) { + (requestIdleCallback || requestAnimationFrame)(s); }); } - async function b(i) { + async function b(s) { t.size > 30 && await p(); let u = []; - if (!(i instanceof Node)) return u; - for (let s of t.keys()) - s === i || !(s instanceof Node) || i.contains(s) && u.push(s); + if (!(s instanceof Node)) return u; + for (let i of t.keys()) + i === s || !(i instanceof Node) || s.contains(i) && u.push(i); return u; } - function l(i, u) { - let s = !1; - for (let h of i) { - if (u && b(h).then(l), !t.has(h)) continue; - let C = t.get(h); - C.length_c && (h.dispatchEvent(new Event(g)), C.connected = /* @__PURE__ */ new WeakSet(), C.length_c = 0, C.length_d || t.delete(h), s = !0); + function h(s, u) { + let i = !1; + for (let l of s) { + if (u && b(l).then(h), !t.has(l)) continue; + let A = t.get(l); + A.length_c && (l.dispatchEvent(new Event(g)), A.connected = /* @__PURE__ */ new WeakSet(), A.length_c = 0, A.length_d || t.delete(l), i = !0); } - return s; + return i; } - function E(i, u) { - let s = !1; - for (let h of i) - u && b(h).then(E), !(!t.has(h) || !t.get(h).length_d) && ((globalThis.queueMicrotask || setTimeout)(_(h)), s = !0); - return s; + function E(s, u) { + let i = !1; + for (let l of s) + u && b(l).then(E), !(!t.has(l) || !t.get(l).length_d) && ((globalThis.queueMicrotask || setTimeout)(I(l)), i = !0); + return i; } - function _(i) { + function I(s) { return () => { - i.isConnected || (i.dispatchEvent(new Event(y)), t.delete(i)); + s.isConnected || (s.dispatchEvent(new Event(y)), t.delete(s)); }; } } // src/customElement.js -function mt(t, e, n, r = it) { - S.push({ +function wt(t, e, n, r = rt) { + D.push({ scope: t, - host: (...f) => f.length ? f.forEach((d) => d(t)) : t + host: (...a) => a.length ? a.forEach((d) => d(t)) : t }), typeof r == "function" && (r = r.call(t, t)); let o = t[w]; - o || ct(t); + o || nt(t); let c = n.call(t, r); - return o || t.dispatchEvent(new Event(g)), e.nodeType === 11 && typeof e.mode == "string" && t.addEventListener(y, A.observe(e), { once: !0 }), S.pop(), e.append(c); + return o || t.dispatchEvent(new Event(g)), e.nodeType === 11 && typeof e.mode == "string" && t.addEventListener(y, m.observe(e), { once: !0 }), D.pop(), e.append(c); } -function ct(t) { - return k(t.prototype, "connectedCallback", function(e, n, r) { +function nt(t) { + return P(t.prototype, "connectedCallback", function(e, n, r) { e.apply(n, r), n.dispatchEvent(new Event(g)); - }), k(t.prototype, "disconnectedCallback", function(e, n, r) { + }), P(t.prototype, "disconnectedCallback", function(e, n, r) { e.apply(n, r), (globalThis.queueMicrotask || setTimeout)( () => !n.isConnected && n.dispatchEvent(new Event(y)) ); - }), k(t.prototype, "attributeChangedCallback", function(e, n, r) { + }), P(t.prototype, "attributeChangedCallback", function(e, n, r) { let [o, , c] = r; - n.dispatchEvent(new CustomEvent(D, { + n.dispatchEvent(new CustomEvent(O, { detail: [o, c] })), e.apply(n, r); }), t.prototype[w] = !0, t; } -function k(t, e, n) { +function P(t, e, n) { t[e] = new Proxy(t[e] || (() => { }), { apply: n }); } -function it(t) { - return F(t, (e, n) => e.getAttribute(n)); +function rt(t) { + return W(t, (e, n) => e.getAttribute(n)); } // src/events.js -function Rt(t, e, n) { +function Ct(t, e, n) { return e || (e = {}), function(o, ...c) { n && (c.unshift(o), o = typeof n == "function" ? n() : n); - let f = c.length ? new CustomEvent(t, Object.assign({ detail: c[0] }, e)) : new Event(t, e); - return o.dispatchEvent(f); + let a = c.length ? new CustomEvent(t, Object.assign({ detail: c[0] }, e)) : new Event(t, e); + return o.dispatchEvent(a); }; } -function R(t, e, n) { +function _(t, e, n) { return function(o) { return o.addEventListener(t, e, n), o; }; } -var Z = (t) => Object.assign({}, typeof t == "object" ? t : null, { once: !0 }); -R.connected = function(t, e) { - return e = Z(e), function(r) { - return r.addEventListener(g, t, e), r[w] ? r : r.isConnected ? (r.dispatchEvent(new Event(g)), r) : (N(e.signal, () => A.offConnected(r, t)) && A.onConnected(r, t), r); +var B = (t) => Object.assign({}, typeof t == "object" ? t : null, { once: !0 }); +_.connected = function(t, e) { + return e = B(e), function(r) { + return r.addEventListener(g, t, e), r[w] ? r : r.isConnected ? (r.dispatchEvent(new Event(g)), r) : (L(e.signal, () => m.offConnected(r, t)) && m.onConnected(r, t), r); }; }; -R.disconnected = function(t, e) { - return e = Z(e), function(r) { - return r.addEventListener(y, t, e), r[w] || N(e.signal, () => A.offDisconnected(r, t)) && A.onDisconnected(r, t), r; +_.disconnected = function(t, e) { + return e = B(e), function(r) { + return r.addEventListener(y, t, e), r[w] || L(e.signal, () => m.offDisconnected(r, t)) && m.onDisconnected(r, t), r; }; }; -var W = /* @__PURE__ */ new WeakMap(); -R.disconnectedAsAbort = function(t) { - if (W.has(t)) return W.get(t); +var j = /* @__PURE__ */ new WeakMap(); +_.disconnectedAsAbort = function(t) { + if (j.has(t)) return j.get(t); let e = new AbortController(); - return W.set(t, e), t(R.disconnected(() => e.abort())), e; + return j.set(t, e), t(_.disconnected(() => e.abort())), e; }; -var st = /* @__PURE__ */ new WeakSet(); -R.attributeChanged = function(t, e) { +var ot = /* @__PURE__ */ new WeakSet(); +_.attributeChanged = function(t, e) { return typeof e != "object" && (e = {}), function(r) { - if (r.addEventListener(D, t, e), r[w] || st.has(r) || !a.M) return r; - let o = new a.M(function(f) { - for (let { attributeName: d, target: p } of f) + if (r.addEventListener(O, t, e), r[w] || ot.has(r) || !f.M) return r; + let o = new f.M(function(a) { + for (let { attributeName: d, target: p } of a) p.dispatchEvent( - new CustomEvent(D, { detail: [d, p.getAttribute(d)] }) + new CustomEvent(O, { detail: [d, p.getAttribute(d)] }) ); }); - return N(e.signal, () => o.disconnect()) && o.observe(r, { attributes: !0 }), r; + return L(e.signal, () => o.disconnect()) && o.observe(r, { attributes: !0 }), r; }; }; globalThis.dde= { - assign: O, - assignAttribute: z, - chainableAppend: K, - classListDeclarative: Y, - cn: Q, - createElement: j, - createElementNS: gt, - customElementRender: mt, - customElementWithDDE: ct, - dispatchEvent: Rt, - el: j, - elNS: gt, - elementAttribute: tt, - lifecyclesToEvents: ct, - observedAttributes: it, - on: R, - registerReactivity: G, - scope: S, - simulateSlots: bt + assign: R, + assignAttribute: U, + chainableAppend: J, + classListDeclarative: K, + createElement: k, + createElementNS: dt, + customElementRender: wt, + customElementWithDDE: nt, + dispatchEvent: Ct, + el: k, + elNS: dt, + elementAttribute: Q, + lifecyclesToEvents: nt, + observedAttributes: rt, + on: _, + registerReactivity: Z, + scope: D, + simulateSlots: pt }; })(); \ No newline at end of file diff --git a/dist/esm-with-signals.d.ts b/dist/esm-with-signals.d.ts index 8c1a050..8c053ff 100644 --- a/dist/esm-with-signals.d.ts +++ b/dist/esm-with-signals.d.ts @@ -40,7 +40,7 @@ type AttrsModified= { & Record<`.${string}`, any> type _fromElsInterfaces= Omit; type IsReadonly = - T extends { readonly [P in K]: T[K] } ? true : false; + T extends { readonly [P in K]: T[K] } ? true : false; /** * Just element attributtes * @@ -129,14 +129,9 @@ export function elNS( export { elNS as createElementNS } export function chainableAppend(el: EL): EL; -/** - * Mapper function (optional). Pass for coppying attributes, this is NOT implemented by {@link simulateSlots} itself! - * */ -type simulateSlotsMapper= (body: HTMLSlotElement, el: HTMLElement)=> void; /** Simulate slots for ddeComponents */ export function simulateSlots( root: EL, - mapper?: simulateSlotsMapper ): EL /** * Simulate slots in Custom Elements without using `shadowRoot`. @@ -145,7 +140,7 @@ export function simulateSlots( * */ export function simulateSlots( el: HTMLElement, - body: EL, mapper?: simulateSlotsMapper + body: EL, ): EL export function dispatchEvent(name: keyof DocumentEventMap | string, options?: EventInit): diff --git a/dist/esm-with-signals.js b/dist/esm-with-signals.js index 4fd845d..00ae571 100644 --- a/dist/esm-with-signals.js +++ b/dist/esm-with-signals.js @@ -1,5 +1,5 @@ // src/signals-common.js -var k = { +var N = { isSignal(t) { return !1; }, @@ -7,23 +7,23 @@ var k = { return n; } }; -function B(t, e = !0) { - return e ? Object.assign(k, t) : (Object.setPrototypeOf(t, k), t); +function H(t, e = !0) { + return e ? Object.assign(N, t) : (Object.setPrototypeOf(t, N), t); } -function W(t) { - return k.isPrototypeOf(t) && t !== k ? t : k; +function j(t) { + return N.isPrototypeOf(t) && t !== N ? t : N; } // src/helpers.js -var T = (...t) => Object.prototype.hasOwnProperty.call(...t); -function S(t) { +var I = (...t) => Object.prototype.hasOwnProperty.call(...t); +function A(t) { return typeof t > "u"; } function X(t) { let e = typeof t; return e !== "object" ? e : t === null ? "null" : Object.prototype.toString.call(t); } -function q(t, e) { +function P(t, e) { if (!t || !(t instanceof AbortSignal)) return !0; if (!t.aborted) @@ -31,7 +31,7 @@ function q(t, e) { t.removeEventListener("abort", e); }; } -function F(t, e) { +function W(t, e) { let { observedAttributes: n = [] } = t.constructor; return n.reduce(function(r, o) { return r[dt(o)] = e(t, o), r; @@ -52,17 +52,17 @@ var d = { M: globalThis.MutationObserver }; function pt(t, e, n) { - if (Reflect.set(t, e, n), !!S(n)) { + if (Reflect.set(t, e, n), !!A(n)) { if (Reflect.deleteProperty(t, e), t instanceof d.H && t.getAttribute(e) === "undefined") return t.removeAttribute(e); if (Reflect.get(t, e) === "undefined") return Reflect.set(t, e, ""); } } -var O = "__dde_lifecyclesToEvents", _ = "dde:connected", C = "dde:disconnected", M = "dde:attributeChanged"; +var O = "__dde_lifecyclesToEvents", _ = "dde:connected", S = "dde:disconnected", T = "dde:attributeChanged"; // src/dom.js -var A = [{ +var y = [{ get scope() { return d.D.body; }, @@ -70,7 +70,7 @@ var A = [{ prevent: !0 }], x = { get current() { - return A[A.length - 1]; + return y[y.length - 1]; }, get host() { return this.current.host; @@ -80,17 +80,17 @@ var A = [{ return t.prevent = !0, t; }, get state() { - return [...A]; + return [...y]; }, push(t = {}) { - return A.push(Object.assign({}, this.current, { prevent: !1 }, t)); + return y.push(Object.assign({}, this.current, { prevent: !1 }, t)); }, pushRoot() { - return A.push(A[0]); + return y.push(y[0]); }, pop() { - if (A.length !== 1) - return A.pop(); + if (y.length !== 1) + return y.pop(); } }; function Y(...t) { @@ -100,95 +100,82 @@ function lt(t) { return t.append === Y || (t.appendOriginal = t.append, t.append = Y), t; } var $; -function P(t, e, ...n) { - let r = W(this), o = 0, c, i; +function M(t, e, ...n) { + let r = j(this), o = 0, c, u; switch ((Object(e) !== e || r.isSignal(e)) && (e = { textContent: e }), !0) { case typeof t == "function": { o = 1; - let a = (...p) => p.length ? (o === 1 ? n.unshift(...p) : p.forEach((m) => m(i)), void 0) : i; + let a = (...h) => h.length ? (o === 1 ? n.unshift(...h) : h.forEach((m) => m(u)), void 0) : u; x.push({ scope: t, host: a }), c = t(e || void 0); - let h = c instanceof d.F; + let l = c instanceof d.F; if (c.nodeName === "#comment") break; - let v = P.mark({ + let v = M.mark({ type: "component", name: t.name, - host: h ? "this" : "parentElement" + host: l ? "this" : "parentElement" }); - c.prepend(v), h && (i = v); + c.prepend(v), l && (u = v); break; } case t === "#text": - c = j.call(this, d.D.createTextNode(""), e); + c = q.call(this, d.D.createTextNode(""), e); break; case (t === "<>" || !t): - c = j.call(this, d.D.createDocumentFragment(), e); + c = q.call(this, d.D.createDocumentFragment(), e); break; case !!$: - c = j.call(this, d.D.createElementNS($, t), e); + c = q.call(this, d.D.createElementNS($, t), e); break; case !c: - c = j.call(this, d.D.createElement(t), e); + c = q.call(this, d.D.createElement(t), e); } - return lt(c), i || (i = c), n.forEach((a) => a(i)), o && x.pop(), o = 2, c; + return lt(c), u || (u = c), n.forEach((a) => a(u)), o && x.pop(), o = 2, c; } -P.mark = function(t, e = !1) { +M.mark = function(t, e = !1) { t = Object.entries(t).map(([o, c]) => o + `="${c}"`).join(" "); let n = e ? "" : "/", r = d.D.createComment(``); return e && (r.end = d.D.createComment("")), r; }; -function Wt(t, e, n) { - typeof e != "object" && (n = e, e = t); - let r = Symbol.for("default"), o = Array.from(e.querySelectorAll("slot")).reduce((i, a) => Reflect.set(i, a.name || r, a) && i, {}), c = T(o, r); - if (t.append = new Proxy(t.append, { - apply(i, a, h) { - if (h[0] === e) return i.apply(t, h); - if (!h.length) return t; - let v = d.D.createDocumentFragment(); - for (let p of h) { - if (!p || !p.slot) { - c && v.append(p); - continue; - } - let m = p.slot, y = o[m]; - vt(p, "remove", "slot"), y && (bt(y, p, n), Reflect.deleteProperty(o, m)); - } - return c && (o[r].replaceWith(v), Reflect.deleteProperty(o, r)), t.append = i, t; - } - }), t !== e) { - let i = Array.from(t.childNodes); - i.forEach((a) => a.remove()), t.append(...i); - } - return e; -} -function ht(...t) { - return t.filter(Boolean).join(" "); -} -function bt(t, e, n) { - n && n(t, e); - try { - t.replaceWith(j(e, { - className: ht(e.className, t.className), - dataset: { ...t.dataset } - })); - } catch { - t.replaceWith(e); - } -} -function qt(t) { +function Mt(t) { let e = this; return function(...r) { $ = t; - let o = P.call(e, ...r); + let o = M.call(e, ...r); return $ = void 0, o; }; } -var U = /* @__PURE__ */ new WeakMap(), { setDeleteAttr: tt } = d; -function j(t, ...e) { +function jt(t, e = t) { + let n = "\xB9\u2070", r = "\u2713", o = Object.fromEntries( + Array.from(e.querySelectorAll("slot")).filter((c) => !c.name.endsWith(n)).map((c) => [c.name += n, c]) + ); + if (t.append = new Proxy(t.append, { + apply(c, u, a) { + if (a[0] === e) return c.apply(t, a); + for (let l of a) { + let v = (l.slot || "") + n; + try { + bt(l, "remove", "slot"); + } catch { + } + let h = o[v]; + if (!h) return; + v.startsWith(r) || h.childNodes.forEach((m) => m.remove()), h.append(l), h.name = r + v; + } + return t.append = c, t; + } + }), t !== e) { + let c = Array.from(t.childNodes); + t.append(...c); + } + return e; +} +var F = /* @__PURE__ */ new WeakMap(), { setDeleteAttr: tt } = d; +function q(t, ...e) { if (!e.length) return t; - U.set(t, rt(t, this)); + F.set(t, rt(t, this)); for (let [n, r] of Object.entries(Object.assign({}, ...e))) nt.call(this, t, n, r); - return U.delete(t), t; + return F.delete(t), t; } function nt(t, e, n) { let { setRemoveAttr: r, s: o } = rt(t, this), c = this; @@ -196,11 +183,11 @@ function nt(t, e, n) { t, e, n, - (a, h) => nt.call(c, t, a, h) + (a, l) => nt.call(c, t, a, l) ); - let [i] = e; - if (i === "=") return r(e.slice(1), n); - if (i === ".") return et(t, e.slice(1), n); + let [u] = e; + if (u === "=") return r(e.slice(1), n); + if (u === ".") return et(t, e.slice(1), n); if (/(aria|data)([A-Z])/.test(e)) return e = e.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase(), r(e, n); switch (e === "className" && (e = "class"), e) { @@ -212,177 +199,177 @@ function nt(t, e, n) { if (typeof n != "object") break; /* falls through */ case "dataset": - return I(o, n, et.bind(null, t[e])); + return B(o, n, et.bind(null, t[e])); case "ariaset": - return I(o, n, (a, h) => r("aria-" + a, h)); + return B(o, n, (a, l) => r("aria-" + a, l)); case "classList": - return gt.call(c, t, n); + return ht.call(c, t, n); } - return Et(t, e) ? tt(t, e, n) : r(e, n); + return gt(t, e) ? tt(t, e, n) : r(e, n); } function rt(t, e) { - if (U.has(t)) return U.get(t); - let r = (t instanceof d.S ? xt : mt).bind(null, t, "Attribute"), o = W(e); + if (F.has(t)) return F.get(t); + let r = (t instanceof d.S ? Et : vt).bind(null, t, "Attribute"), o = j(e); return { setRemoveAttr: r, s: o }; } -function gt(t, e) { - let n = W(this); - return I( +function ht(t, e) { + let n = j(this); + return B( n, e, (r, o) => t.classList.toggle(r, o === -1 ? void 0 : !!o) ), t; } -function vt(t, e, n, r) { +function bt(t, e, n, r) { return t instanceof d.H ? t[e + "Attribute"](n, r) : t[e + "AttributeNS"](null, n, r); } -function Et(t, e) { +function gt(t, e) { if (!(e in t)) return !1; let n = ot(t, e); - return !S(n.set); + return !A(n.set); } function ot(t, e) { if (t = Object.getPrototypeOf(t), !t) return {}; let n = Object.getOwnPropertyDescriptor(t, e); return n || ot(t, e); } -function I(t, e, n) { +function B(t, e, n) { if (!(typeof e != "object" || e === null)) return Object.entries(e).forEach(function([o, c]) { o && (c = t.processReactiveAttribute(e, o, c, n), n(o, c)); }); } -function mt(t, e, n, r) { - return t[(S(r) ? "remove" : "set") + e](n, r); +function vt(t, e, n, r) { + return t[(A(r) ? "remove" : "set") + e](n, r); } -function xt(t, e, n, r, o = null) { - return t[(S(r) ? "remove" : "set") + e + "NS"](o, n, r); +function Et(t, e, n, r, o = null) { + return t[(A(r) ? "remove" : "set") + e + "NS"](o, n, r); } function et(t, e, n) { - if (Reflect.set(t, e, n), !!S(n)) + if (Reflect.set(t, e, n), !!A(n)) return Reflect.deleteProperty(t, e); } // src/events-observer.js -var D = d.M ? wt() : new Proxy({}, { +var C = d.M ? mt() : new Proxy({}, { get() { return () => { }; } }); -function wt() { - let t = /* @__PURE__ */ new Map(), e = !1, n = (s) => function(u) { - for (let f of u) +function mt() { + let t = /* @__PURE__ */ new Map(), e = !1, n = (i) => function(s) { + for (let f of s) if (f.type === "childList") { - if (p(f.addedNodes, !0)) { - s(); + if (h(f.addedNodes, !0)) { + i(); continue; } - m(f.removedNodes, !0) && s(); + m(f.removedNodes, !0) && i(); } }, r = new d.M(n(a)); return { - observe(s) { - let u = new d.M(n(() => { + observe(i) { + let s = new d.M(n(() => { })); - return u.observe(s, { childList: !0, subtree: !0 }), () => u.disconnect(); + return s.observe(i, { childList: !0, subtree: !0 }), () => s.disconnect(); }, - onConnected(s, u) { - i(); - let f = c(s); - f.connected.has(u) || (f.connected.add(u), f.length_c += 1); + onConnected(i, s) { + u(); + let f = c(i); + f.connected.has(s) || (f.connected.add(s), f.length_c += 1); }, - offConnected(s, u) { - if (!t.has(s)) return; - let f = t.get(s); - f.connected.has(u) && (f.connected.delete(u), f.length_c -= 1, o(s, f)); + offConnected(i, s) { + if (!t.has(i)) return; + let f = t.get(i); + f.connected.has(s) && (f.connected.delete(s), f.length_c -= 1, o(i, f)); }, - onDisconnected(s, u) { - i(); - let f = c(s); - f.disconnected.has(u) || (f.disconnected.add(u), f.length_d += 1); + onDisconnected(i, s) { + u(); + let f = c(i); + f.disconnected.has(s) || (f.disconnected.add(s), f.length_d += 1); }, - offDisconnected(s, u) { - if (!t.has(s)) return; - let f = t.get(s); - f.disconnected.has(u) && (f.disconnected.delete(u), f.length_d -= 1, o(s, f)); + offDisconnected(i, s) { + if (!t.has(i)) return; + let f = t.get(i); + f.disconnected.has(s) && (f.disconnected.delete(s), f.length_d -= 1, o(i, f)); } }; - function o(s, u) { - u.length_c || u.length_d || (t.delete(s), a()); + function o(i, s) { + s.length_c || s.length_d || (t.delete(i), a()); } - function c(s) { - if (t.has(s)) return t.get(s); - let u = { + function c(i) { + if (t.has(i)) return t.get(i); + let s = { connected: /* @__PURE__ */ new WeakSet(), length_c: 0, disconnected: /* @__PURE__ */ new WeakSet(), length_d: 0 }; - return t.set(s, u), u; + return t.set(i, s), s; } - function i() { + function u() { e || (e = !0, r.observe(d.D.body, { childList: !0, subtree: !0 })); } function a() { !e || t.size || (e = !1, r.disconnect()); } - function h() { - return new Promise(function(s) { - (requestIdleCallback || requestAnimationFrame)(s); + function l() { + return new Promise(function(i) { + (requestIdleCallback || requestAnimationFrame)(i); }); } - async function v(s) { - t.size > 30 && await h(); - let u = []; - if (!(s instanceof Node)) return u; + async function v(i) { + t.size > 30 && await l(); + let s = []; + if (!(i instanceof Node)) return s; for (let f of t.keys()) - f === s || !(f instanceof Node) || s.contains(f) && u.push(f); - return u; + f === i || !(f instanceof Node) || i.contains(f) && s.push(f); + return s; } - function p(s, u) { + function h(i, s) { let f = !1; - for (let b of s) { - if (u && v(b).then(p), !t.has(b)) continue; - let N = t.get(b); - N.length_c && (b.dispatchEvent(new Event(_)), N.connected = /* @__PURE__ */ new WeakSet(), N.length_c = 0, N.length_d || t.delete(b), f = !0); + for (let b of i) { + if (s && v(b).then(h), !t.has(b)) continue; + let L = t.get(b); + L.length_c && (b.dispatchEvent(new Event(_)), L.connected = /* @__PURE__ */ new WeakSet(), L.length_c = 0, L.length_d || t.delete(b), f = !0); } return f; } - function m(s, u) { + function m(i, s) { let f = !1; - for (let b of s) - u && v(b).then(m), !(!t.has(b) || !t.get(b).length_d) && ((globalThis.queueMicrotask || setTimeout)(y(b)), f = !0); + for (let b of i) + s && v(b).then(m), !(!t.has(b) || !t.get(b).length_d) && ((globalThis.queueMicrotask || setTimeout)(k(b)), f = !0); return f; } - function y(s) { + function k(i) { return () => { - s.isConnected || (s.dispatchEvent(new Event(C)), t.delete(s)); + i.isConnected || (i.dispatchEvent(new Event(S)), t.delete(i)); }; } } // src/customElement.js -function Jt(t, e, n, r = _t) { +function Ht(t, e, n, r = wt) { x.push({ scope: t, - host: (...i) => i.length ? i.forEach((a) => a(t)) : t + host: (...u) => u.length ? u.forEach((a) => a(t)) : t }), typeof r == "function" && (r = r.call(t, t)); let o = t[O]; - o || yt(t); + o || xt(t); let c = n.call(t, r); - return o || t.dispatchEvent(new Event(_)), e.nodeType === 11 && typeof e.mode == "string" && t.addEventListener(C, D.observe(e), { once: !0 }), x.pop(), e.append(c); + return o || t.dispatchEvent(new Event(_)), e.nodeType === 11 && typeof e.mode == "string" && t.addEventListener(S, C.observe(e), { once: !0 }), x.pop(), e.append(c); } -function yt(t) { +function xt(t) { return J(t.prototype, "connectedCallback", function(e, n, r) { e.apply(n, r), n.dispatchEvent(new Event(_)); }), J(t.prototype, "disconnectedCallback", function(e, n, r) { e.apply(n, r), (globalThis.queueMicrotask || setTimeout)( - () => !n.isConnected && n.dispatchEvent(new Event(C)) + () => !n.isConnected && n.dispatchEvent(new Event(S)) ); }), J(t.prototype, "attributeChangedCallback", function(e, n, r) { let [o, , c] = r; - n.dispatchEvent(new CustomEvent(M, { + n.dispatchEvent(new CustomEvent(T, { detail: [o, c] })), e.apply(n, r); }), t.prototype[O] = !0, t; @@ -391,16 +378,16 @@ function J(t, e, n) { t[e] = new Proxy(t[e] || (() => { }), { apply: n }); } -function _t(t) { - return F(t, (e, n) => e.getAttribute(n)); +function wt(t) { + return W(t, (e, n) => e.getAttribute(n)); } // src/events.js -function Kt(t, e, n) { +function Zt(t, e, n) { return e || (e = {}), function(o, ...c) { n && (c.unshift(o), o = typeof n == "function" ? n() : n); - let i = c.length ? new CustomEvent(t, Object.assign({ detail: c[0] }, e)) : new Event(t, e); - return o.dispatchEvent(i); + let u = c.length ? new CustomEvent(t, Object.assign({ detail: c[0] }, e)) : new Event(t, e); + return o.dispatchEvent(u); }; } function w(t, e, n) { @@ -411,12 +398,12 @@ function w(t, e, n) { var ct = (t) => Object.assign({}, typeof t == "object" ? t : null, { once: !0 }); w.connected = function(t, e) { return e = ct(e), function(r) { - return r.addEventListener(_, t, e), r[O] ? r : r.isConnected ? (r.dispatchEvent(new Event(_)), r) : (q(e.signal, () => D.offConnected(r, t)) && D.onConnected(r, t), r); + return r.addEventListener(_, t, e), r[O] ? r : r.isConnected ? (r.dispatchEvent(new Event(_)), r) : (P(e.signal, () => C.offConnected(r, t)) && C.onConnected(r, t), r); }; }; w.disconnected = function(t, e) { return e = ct(e), function(r) { - return r.addEventListener(C, t, e), r[O] || q(e.signal, () => D.offDisconnected(r, t)) && D.onDisconnected(r, t), r; + return r.addEventListener(S, t, e), r[O] || P(e.signal, () => C.offDisconnected(r, t)) && C.onDisconnected(r, t), r; }; }; var Z = /* @__PURE__ */ new WeakMap(); @@ -425,45 +412,45 @@ w.disconnectedAsAbort = function(t) { let e = new AbortController(); return Z.set(t, e), t(w.disconnected(() => e.abort())), e; }; -var At = /* @__PURE__ */ new WeakSet(); +var _t = /* @__PURE__ */ new WeakSet(); w.attributeChanged = function(t, e) { return typeof e != "object" && (e = {}), function(r) { - if (r.addEventListener(M, t, e), r[O] || At.has(r) || !d.M) return r; - let o = new d.M(function(i) { - for (let { attributeName: a, target: h } of i) - h.dispatchEvent( - new CustomEvent(M, { detail: [a, h.getAttribute(a)] }) + if (r.addEventListener(T, t, e), r[O] || _t.has(r) || !d.M) return r; + let o = new d.M(function(u) { + for (let { attributeName: a, target: l } of u) + l.dispatchEvent( + new CustomEvent(T, { detail: [a, l.getAttribute(a)] }) ); }); - return q(e.signal, () => o.disconnect()) && o.observe(r, { attributes: !0 }), r; + return P(e.signal, () => o.disconnect()) && o.observe(r, { attributes: !0 }), r; }; }; // src/signals-lib.js -var l = "__dde_signal"; -function z(t) { +var p = "__dde_signal"; +function U(t) { try { - return T(t, l); + return I(t, p); } catch { return !1; } } -var H = [], g = /* @__PURE__ */ new WeakMap(); +var z = [], g = /* @__PURE__ */ new WeakMap(); function E(t, e) { if (typeof t != "function") return it(!1, t, e); - if (z(t)) return t; + if (U(t)) return t; let n = it(!0), r = function() { let [o, ...c] = g.get(r); - if (g.set(r, /* @__PURE__ */ new Set([o])), H.push(r), at(n, t()), H.pop(), !c.length) return; - let i = g.get(r); + if (g.set(r, /* @__PURE__ */ new Set([o])), z.push(r), at(n, t()), z.pop(), !c.length) return; + let u = g.get(r); for (let a of c) - i.has(a) || L(a, r); + u.has(a) || R(a, r); }; - return g.set(n[l], r), g.set(r, /* @__PURE__ */ new Set([n])), r(), n; + return g.set(n[p], r), g.set(r, /* @__PURE__ */ new Set([n])), r(), n; } E.action = function(t, e, ...n) { - let r = t[l], { actions: o } = r; + let r = t[p], { actions: o } = r; if (!o || !(e in o)) throw new Error(`'${t}' has no action with name '${e}'!`); if (o[e].apply(r, n), r.skip) return delete r.skip; @@ -473,7 +460,7 @@ E.on = function t(e, n, r = {}) { let { signal: o } = r; if (!(o && o.aborted)) { if (Array.isArray(e)) return e.forEach((c) => t(c, n, r)); - Q(e, n), o && o.addEventListener("abort", () => L(e, n)); + Q(e, n), o && o.addEventListener("abort", () => R(e, n)); } }; E.symbols = { @@ -482,8 +469,8 @@ E.symbols = { }; E.clear = function(...t) { for (let n of t) { - let r = n[l]; - r && (delete n.toJSON, r.onclear.forEach((o) => o.call(r)), e(n, r), delete n[l]); + let r = n[p]; + r && (delete n.toJSON, r.onclear.forEach((o) => o.call(r)), e(n, r), delete n[p]); } function e(n, r) { r.listeners.forEach((o) => { @@ -493,53 +480,53 @@ E.clear = function(...t) { }); } }; -var R = "__dde_reactive"; +var D = "__dde_reactive"; E.el = function(t, e) { - let n = P.mark({ type: "reactive" }, !0), r = n.end, o = d.D.createDocumentFragment(); + let n = M.mark({ type: "reactive" }, !0), r = n.end, o = d.D.createDocumentFragment(); o.append(n, r); - let { current: c } = x, i = {}, a = (h) => { + let { current: c } = x, u = {}, a = (l) => { if (!n.parentNode || !r.parentNode) - return L(t, a); - let v = i; - i = {}, x.push(c); - let p = e(h, function(u, f) { + return R(t, a); + let v = u; + u = {}, x.push(c); + let h = e(l, function(s, f) { let b; - return T(v, u) ? (b = v[u], delete v[u]) : b = f(), i[u] = b, b; + return I(v, s) ? (b = v[s], delete v[s]) : b = f(), u[s] = b, b; }); - x.pop(), Array.isArray(p) || (p = [p]); + x.pop(), Array.isArray(h) || (h = [h]); let m = document.createComment(""); - p.push(m), n.after(...p); - let y; - for (; (y = m.nextSibling) && y !== r; ) - y.remove(); - m.remove(), n.isConnected && St(c.host()); + h.push(m), n.after(...h); + let k; + for (; (k = m.nextSibling) && k !== r; ) + k.remove(); + m.remove(), n.isConnected && yt(c.host()); }; return Q(t, a), ut(t, a, n, e), a(t()), o; }; -function St(t) { - !t || !t[R] || (requestIdleCallback || setTimeout)(function() { - t[R] = t[R].filter(([e, n]) => n.isConnected ? !0 : (L(...e), !1)); +function yt(t) { + !t || !t[D] || (requestIdleCallback || setTimeout)(function() { + t[D] = t[D].filter(([e, n]) => n.isConnected ? !0 : (R(...e), !1)); }); } -var Ot = { +var At = { _set(t) { this.value = t; } }; -function Ct(t) { +function Ot(t) { return function(e, n) { - let r = (...c) => c.length ? e.setAttribute(n, ...c) : K(r), o = ft(r, e.getAttribute(n), Ot); + let r = (...c) => c.length ? e.setAttribute(n, ...c) : K(r), o = ft(r, e.getAttribute(n), At); return t[n] = o, o; }; } var G = "__dde_attributes"; E.observedAttributes = function(t) { - let e = t[G] = {}, n = F(t, Ct(e)); + let e = t[G] = {}, n = W(t, Ot(e)); return w.attributeChanged(function({ detail: o }) { /*! This maps attributes to signals (`S.observedAttributes`). * Investigate `__dde_attributes` key of the element.*/ - let [c, i] = o, a = this[G][c]; - if (a) return E.action(a, "_set", i); + let [c, u] = o, a = this[G][c]; + if (a) return E.action(a, "_set", u); })(t), w.disconnected(function() { /*! This removes all signals mapped to attributes (`S.observedAttributes`). * Investigate `__dde_attributes` key of the element.*/ @@ -547,12 +534,12 @@ E.observedAttributes = function(t) { })(t), n; }; var st = { - isSignal: z, + isSignal: U, processReactiveAttribute(t, e, n, r) { - if (!z(n)) return n; + if (!U(n)) return n; let o = (c) => { if (!t.isConnected) - return L(n, o); + return R(n, o); r(e, c); }; return Q(n, o), ut(n, o, t, e), n(); @@ -561,22 +548,22 @@ var st = { function ut(t, e, ...n) { let { current: r } = x; r.prevent || r.host(function(o) { - o[R] || (o[R] = [], w.disconnected( + o[D] || (o[D] = [], w.disconnected( () => ( /*! * Clears all Signals listeners added in the current scope/host (`S.el`, `assign`, …?). * You can investigate the `__dde_reactive` key of the element. * */ - o[R].forEach(([[c, i]]) => L(c, i, c[l] && c[l].host && c[l].host() === o)) + o[D].forEach(([[c, u]]) => R(c, u, c[p] && c[p].host && c[p].host() === o)) ) - )(o)), o[R].push([[t, e], ...n]); + )(o)), o[D].push([[t, e], ...n]); }); } function it(t, e, n) { let r = t ? () => K(r) : (...o) => o.length ? at(r, ...o) : K(r); return ft(r, e, n, t); } -var Dt = Object.assign(/* @__PURE__ */ Object.create(null), { +var St = Object.assign(/* @__PURE__ */ Object.create(null), { stopPropagation() { this.skip = !0; } @@ -593,13 +580,13 @@ function ft(t, e, n, r = !1) { X(n) !== "[object Object]" && (n = {}); let { onclear: c } = E.symbols; n[c] && (o.push(n[c]), delete n[c]); - let { host: i } = x; - return Reflect.defineProperty(t, l, { + let { host: u } = x; + return Reflect.defineProperty(t, p, { value: { value: e, actions: n, onclear: o, - host: i, + host: u, listeners: /* @__PURE__ */ new Set(), defined: new V().stack, readonly: r @@ -607,62 +594,61 @@ function ft(t, e, n, r = !1) { enumerable: !1, writable: !1, configurable: !0 - }), t.toJSON = () => t(), t.valueOf = () => t[l] && t[l].value, Object.setPrototypeOf(t[l], Dt), t; + }), t.toJSON = () => t(), t.valueOf = () => t[p] && t[p].value, Object.setPrototypeOf(t[p], St), t; } -function Rt() { - return H[H.length - 1]; +function Ct() { + return z[z.length - 1]; } function K(t) { - if (!t[l]) return; - let { value: e, listeners: n } = t[l], r = Rt(); + if (!t[p]) return; + let { value: e, listeners: n } = t[p], r = Ct(); return r && n.add(r), g.has(r) && g.get(r).add(t), e; } function at(t, e, n) { - if (!t[l]) return; - let r = t[l]; + if (!t[p]) return; + let r = t[p]; if (!(!n && r.value === e)) return r.value = e, r.listeners.forEach((o) => o(e)), e; } function Q(t, e) { - if (t[l]) - return t[l].listeners.add(e); + if (t[p]) + return t[p].listeners.add(e); } -function L(t, e, n) { - let r = t[l]; +function R(t, e, n) { + let r = t[p]; if (!r) return; let o = r.listeners.delete(e); if (n && !r.listeners.size) { if (E.clear(t), !g.has(r)) return o; let c = g.get(r); if (!g.has(c)) return o; - g.get(c).forEach((i) => L(i, c, !0)); + g.get(c).forEach((u) => R(u, c, !0)); } return o; } // signals.js -B(st); +H(st); export { E as S, - j as assign, + q as assign, nt as assignAttribute, lt as chainableAppend, - gt as classListDeclarative, - ht as cn, - P as createElement, - qt as createElementNS, - Jt as customElementRender, - yt as customElementWithDDE, - Kt as dispatchEvent, - P as el, - qt as elNS, - vt as elementAttribute, - z as isSignal, - yt as lifecyclesToEvents, - _t as observedAttributes, + ht as classListDeclarative, + M as createElement, + Mt as createElementNS, + Ht as customElementRender, + xt as customElementWithDDE, + Zt as dispatchEvent, + M as el, + Mt as elNS, + bt as elementAttribute, + U as isSignal, + xt as lifecyclesToEvents, + wt as observedAttributes, w as on, - B as registerReactivity, + H as registerReactivity, x as scope, E as signal, - Wt as simulateSlots + jt as simulateSlots }; diff --git a/dist/esm.d.ts b/dist/esm.d.ts index 451dab9..f004308 100644 --- a/dist/esm.d.ts +++ b/dist/esm.d.ts @@ -40,7 +40,7 @@ type AttrsModified= { & Record<`.${string}`, any> type _fromElsInterfaces= Omit; type IsReadonly = - T extends { readonly [P in K]: T[K] } ? true : false; + T extends { readonly [P in K]: T[K] } ? true : false; /** * Just element attributtes * @@ -129,14 +129,9 @@ export function elNS( export { elNS as createElementNS } export function chainableAppend(el: EL): EL; -/** - * Mapper function (optional). Pass for coppying attributes, this is NOT implemented by {@link simulateSlots} itself! - * */ -type simulateSlotsMapper= (body: HTMLSlotElement, el: HTMLElement)=> void; /** Simulate slots for ddeComponents */ export function simulateSlots( root: EL, - mapper?: simulateSlotsMapper ): EL /** * Simulate slots in Custom Elements without using `shadowRoot`. @@ -145,7 +140,7 @@ export function simulateSlots( * */ export function simulateSlots( el: HTMLElement, - body: EL, mapper?: simulateSlotsMapper + body: EL, ): EL export function dispatchEvent(name: keyof DocumentEventMap | string, options?: EventInit): diff --git a/dist/esm.js b/dist/esm.js index 4427d6d..78da6a9 100644 --- a/dist/esm.js +++ b/dist/esm.js @@ -1,5 +1,5 @@ // src/signals-common.js -var m = { +var C = { isSignal(t) { return !1; }, @@ -7,19 +7,18 @@ var m = { return n; } }; -function G(t, e = !0) { - return e ? Object.assign(m, t) : (Object.setPrototypeOf(t, m), t); +function Z(t, e = !0) { + return e ? Object.assign(C, t) : (Object.setPrototypeOf(t, C), t); } -function L(t) { - return m.isPrototypeOf(t) && t !== m ? t : m; +function S(t) { + return C.isPrototypeOf(t) && t !== C ? t : C; } // src/helpers.js -var q = (...t) => Object.prototype.hasOwnProperty.call(...t); function x(t) { return typeof t > "u"; } -function N(t, e) { +function L(t, e) { if (!t || !(t instanceof AbortSignal)) return !0; if (!t.aborted) @@ -27,19 +26,19 @@ function N(t, e) { t.removeEventListener("abort", e); }; } -function F(t, e) { +function W(t, e) { let { observedAttributes: n = [] } = t.constructor; return n.reduce(function(r, o) { - return r[V(o)] = e(t, o), r; + return r[G(o)] = e(t, o), r; }, {}); } -function V(t) { +function G(t) { return t.replace(/-./g, (e) => e[1].toUpperCase()); } // src/dom-common.js -var a = { - setDeleteAttr: J, +var f = { + setDeleteAttr: V, ssr: "", D: globalThis.document, F: globalThis.DocumentFragment, @@ -47,24 +46,24 @@ var a = { S: globalThis.SVGElement, M: globalThis.MutationObserver }; -function J(t, e, n) { +function V(t, e, n) { if (Reflect.set(t, e, n), !!x(n)) { - if (Reflect.deleteProperty(t, e), t instanceof a.H && t.getAttribute(e) === "undefined") + if (Reflect.deleteProperty(t, e), t instanceof f.H && t.getAttribute(e) === "undefined") return t.removeAttribute(e); if (Reflect.get(t, e) === "undefined") return Reflect.set(t, e, ""); } } -var w = "__dde_lifecyclesToEvents", g = "dde:connected", y = "dde:disconnected", D = "dde:attributeChanged"; +var w = "__dde_lifecyclesToEvents", g = "dde:connected", y = "dde:disconnected", O = "dde:attributeChanged"; // src/dom.js var v = [{ get scope() { - return a.D.body; + return f.D.body; }, - host: (t) => t ? t(a.D.body) : a.D.body, + host: (t) => t ? t(f.D.body) : f.D.body, prevent: !0 -}], S = { +}], D = { get current() { return v[v.length - 1]; }, @@ -89,158 +88,145 @@ var v = [{ return v.pop(); } }; -function $(...t) { +function q(...t) { return this.appendOriginal(...t), this; } -function K(t) { - return t.append === $ || (t.appendOriginal = t.append, t.append = $), t; +function J(t) { + return t.append === q || (t.appendOriginal = t.append, t.append = q), t; } var T; -function j(t, e, ...n) { - let r = L(this), o = 0, c, f; +function k(t, e, ...n) { + let r = S(this), o = 0, c, a; switch ((Object(e) !== e || r.isSignal(e)) && (e = { textContent: e }), !0) { case typeof t == "function": { o = 1; - let d = (...l) => l.length ? (o === 1 ? n.unshift(...l) : l.forEach((E) => E(f)), void 0) : f; - S.push({ scope: t, host: d }), c = t(e || void 0); - let p = c instanceof a.F; + let d = (...h) => h.length ? (o === 1 ? n.unshift(...h) : h.forEach((E) => E(a)), void 0) : a; + D.push({ scope: t, host: d }), c = t(e || void 0); + let p = c instanceof f.F; if (c.nodeName === "#comment") break; - let b = j.mark({ + let b = k.mark({ type: "component", name: t.name, host: p ? "this" : "parentElement" }); - c.prepend(b), p && (f = b); + c.prepend(b), p && (a = b); break; } case t === "#text": - c = O.call(this, a.D.createTextNode(""), e); + c = R.call(this, f.D.createTextNode(""), e); break; case (t === "<>" || !t): - c = O.call(this, a.D.createDocumentFragment(), e); + c = R.call(this, f.D.createDocumentFragment(), e); break; case !!T: - c = O.call(this, a.D.createElementNS(T, t), e); + c = R.call(this, f.D.createElementNS(T, t), e); break; case !c: - c = O.call(this, a.D.createElement(t), e); + c = R.call(this, f.D.createElement(t), e); } - return K(c), f || (f = c), n.forEach((d) => d(f)), o && S.pop(), o = 2, c; + return J(c), a || (a = c), n.forEach((d) => d(a)), o && D.pop(), o = 2, c; } -j.mark = function(t, e = !1) { +k.mark = function(t, e = !1) { t = Object.entries(t).map(([o, c]) => o + `="${c}"`).join(" "); - let n = e ? "" : "/", r = a.D.createComment(``); - return e && (r.end = a.D.createComment("")), r; + let n = e ? "" : "/", r = f.D.createComment(``); + return e && (r.end = f.D.createComment("")), r; }; -function bt(t, e, n) { - typeof e != "object" && (n = e, e = t); - let r = Symbol.for("default"), o = Array.from(e.querySelectorAll("slot")).reduce((f, d) => Reflect.set(f, d.name || r, d) && f, {}), c = q(o, r); - if (t.append = new Proxy(t.append, { - apply(f, d, p) { - if (p[0] === e) return f.apply(t, p); - if (!p.length) return t; - let b = a.D.createDocumentFragment(); - for (let l of p) { - if (!l || !l.slot) { - c && b.append(l); - continue; - } - let E = l.slot, _ = o[E]; - tt(l, "remove", "slot"), _ && (X(_, l, n), Reflect.deleteProperty(o, E)); - } - return c && (o[r].replaceWith(b), Reflect.deleteProperty(o, r)), t.append = f, t; - } - }), t !== e) { - let f = Array.from(t.childNodes); - f.forEach((d) => d.remove()), t.append(...f); - } - return e; -} -function Q(...t) { - return t.filter(Boolean).join(" "); -} -function X(t, e, n) { - n && n(t, e); - try { - t.replaceWith(O(e, { - className: Q(e.className, t.className), - dataset: { ...t.dataset } - })); - } catch { - t.replaceWith(e); - } -} -function gt(t) { +function dt(t) { let e = this; return function(...r) { T = t; - let o = j.call(e, ...r); + let o = k.call(e, ...r); return T = void 0, o; }; } -var P = /* @__PURE__ */ new WeakMap(), { setDeleteAttr: U } = a; -function O(t, ...e) { - if (!e.length) return t; - P.set(t, B(t, this)); - for (let [n, r] of Object.entries(Object.assign({}, ...e))) - z.call(this, t, n, r); - return P.delete(t), t; +function pt(t, e = t) { + let n = "\xB9\u2070", r = "\u2713", o = Object.fromEntries( + Array.from(e.querySelectorAll("slot")).filter((c) => !c.name.endsWith(n)).map((c) => [c.name += n, c]) + ); + if (t.append = new Proxy(t.append, { + apply(c, a, d) { + if (d[0] === e) return c.apply(t, d); + for (let p of d) { + let b = (p.slot || "") + n; + try { + Q(p, "remove", "slot"); + } catch { + } + let h = o[b]; + if (!h) return; + b.startsWith(r) || h.childNodes.forEach((E) => E.remove()), h.append(p), h.name = r + b; + } + return t.append = c, t; + } + }), t !== e) { + let c = Array.from(t.childNodes); + t.append(...c); + } + return e; } -function z(t, e, n) { - let { setRemoveAttr: r, s: o } = B(t, this), c = this; +var N = /* @__PURE__ */ new WeakMap(), { setDeleteAttr: $ } = f; +function R(t, ...e) { + if (!e.length) return t; + N.set(t, H(t, this)); + for (let [n, r] of Object.entries(Object.assign({}, ...e))) + U.call(this, t, n, r); + return N.delete(t), t; +} +function U(t, e, n) { + let { setRemoveAttr: r, s: o } = H(t, this), c = this; n = o.processReactiveAttribute( t, e, n, - (d, p) => z.call(c, t, d, p) + (d, p) => U.call(c, t, d, p) ); - let [f] = e; - if (f === "=") return r(e.slice(1), n); - if (f === ".") return H(t, e.slice(1), n); + let [a] = e; + if (a === "=") return r(e.slice(1), n); + if (a === ".") return F(t, e.slice(1), n); if (/(aria|data)([A-Z])/.test(e)) return e = e.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase(), r(e, n); switch (e === "className" && (e = "class"), e) { case "xlink:href": return r(e, n, "http://www.w3.org/1999/xlink"); case "textContent": - return U(t, e, n); + return $(t, e, n); case "style": if (typeof n != "object") break; /* falls through */ case "dataset": - return M(o, n, H.bind(null, t[e])); + return M(o, n, F.bind(null, t[e])); case "ariaset": return M(o, n, (d, p) => r("aria-" + d, p)); case "classList": - return Y.call(c, t, n); + return K.call(c, t, n); } - return et(t, e) ? U(t, e, n) : r(e, n); + return X(t, e) ? $(t, e, n) : r(e, n); } -function B(t, e) { - if (P.has(t)) return P.get(t); - let r = (t instanceof a.S ? rt : nt).bind(null, t, "Attribute"), o = L(e); +function H(t, e) { + if (N.has(t)) return N.get(t); + let r = (t instanceof f.S ? tt : Y).bind(null, t, "Attribute"), o = S(e); return { setRemoveAttr: r, s: o }; } -function Y(t, e) { - let n = L(this); +function K(t, e) { + let n = S(this); return M( n, e, (r, o) => t.classList.toggle(r, o === -1 ? void 0 : !!o) ), t; } -function tt(t, e, n, r) { - return t instanceof a.H ? t[e + "Attribute"](n, r) : t[e + "AttributeNS"](null, n, r); +function Q(t, e, n, r) { + return t instanceof f.H ? t[e + "Attribute"](n, r) : t[e + "AttributeNS"](null, n, r); } -function et(t, e) { +function X(t, e) { if (!(e in t)) return !1; - let n = I(t, e); + let n = z(t, e); return !x(n.set); } -function I(t, e) { +function z(t, e) { if (t = Object.getPrototypeOf(t), !t) return {}; let n = Object.getOwnPropertyDescriptor(t, e); - return n || I(t, e); + return n || z(t, e); } function M(t, e, n) { if (!(typeof e != "object" || e === null)) @@ -248,210 +234,209 @@ function M(t, e, n) { o && (c = t.processReactiveAttribute(e, o, c, n), n(o, c)); }); } -function nt(t, e, n, r) { +function Y(t, e, n, r) { return t[(x(r) ? "remove" : "set") + e](n, r); } -function rt(t, e, n, r, o = null) { +function tt(t, e, n, r, o = null) { return t[(x(r) ? "remove" : "set") + e + "NS"](o, n, r); } -function H(t, e, n) { +function F(t, e, n) { if (Reflect.set(t, e, n), !!x(n)) return Reflect.deleteProperty(t, e); } // src/events-observer.js -var A = a.M ? ot() : new Proxy({}, { +var m = f.M ? et() : new Proxy({}, { get() { return () => { }; } }); -function ot() { - let t = /* @__PURE__ */ new Map(), e = !1, n = (i) => function(u) { - for (let s of u) - if (s.type === "childList") { - if (l(s.addedNodes, !0)) { - i(); +function et() { + let t = /* @__PURE__ */ new Map(), e = !1, n = (s) => function(u) { + for (let i of u) + if (i.type === "childList") { + if (h(i.addedNodes, !0)) { + s(); continue; } - E(s.removedNodes, !0) && i(); + E(i.removedNodes, !0) && s(); } - }, r = new a.M(n(d)); + }, r = new f.M(n(d)); return { - observe(i) { - let u = new a.M(n(() => { + observe(s) { + let u = new f.M(n(() => { })); - return u.observe(i, { childList: !0, subtree: !0 }), () => u.disconnect(); + return u.observe(s, { childList: !0, subtree: !0 }), () => u.disconnect(); }, - onConnected(i, u) { - f(); - let s = c(i); - s.connected.has(u) || (s.connected.add(u), s.length_c += 1); + onConnected(s, u) { + a(); + let i = c(s); + i.connected.has(u) || (i.connected.add(u), i.length_c += 1); }, - offConnected(i, u) { - if (!t.has(i)) return; - let s = t.get(i); - s.connected.has(u) && (s.connected.delete(u), s.length_c -= 1, o(i, s)); + offConnected(s, u) { + if (!t.has(s)) return; + let i = t.get(s); + i.connected.has(u) && (i.connected.delete(u), i.length_c -= 1, o(s, i)); }, - onDisconnected(i, u) { - f(); - let s = c(i); - s.disconnected.has(u) || (s.disconnected.add(u), s.length_d += 1); + onDisconnected(s, u) { + a(); + let i = c(s); + i.disconnected.has(u) || (i.disconnected.add(u), i.length_d += 1); }, - offDisconnected(i, u) { - if (!t.has(i)) return; - let s = t.get(i); - s.disconnected.has(u) && (s.disconnected.delete(u), s.length_d -= 1, o(i, s)); + offDisconnected(s, u) { + if (!t.has(s)) return; + let i = t.get(s); + i.disconnected.has(u) && (i.disconnected.delete(u), i.length_d -= 1, o(s, i)); } }; - function o(i, u) { - u.length_c || u.length_d || (t.delete(i), d()); + function o(s, u) { + u.length_c || u.length_d || (t.delete(s), d()); } - function c(i) { - if (t.has(i)) return t.get(i); + function c(s) { + if (t.has(s)) return t.get(s); let u = { connected: /* @__PURE__ */ new WeakSet(), length_c: 0, disconnected: /* @__PURE__ */ new WeakSet(), length_d: 0 }; - return t.set(i, u), u; + return t.set(s, u), u; } - function f() { - e || (e = !0, r.observe(a.D.body, { childList: !0, subtree: !0 })); + function a() { + e || (e = !0, r.observe(f.D.body, { childList: !0, subtree: !0 })); } function d() { !e || t.size || (e = !1, r.disconnect()); } function p() { - return new Promise(function(i) { - (requestIdleCallback || requestAnimationFrame)(i); + return new Promise(function(s) { + (requestIdleCallback || requestAnimationFrame)(s); }); } - async function b(i) { + async function b(s) { t.size > 30 && await p(); let u = []; - if (!(i instanceof Node)) return u; - for (let s of t.keys()) - s === i || !(s instanceof Node) || i.contains(s) && u.push(s); + if (!(s instanceof Node)) return u; + for (let i of t.keys()) + i === s || !(i instanceof Node) || s.contains(i) && u.push(i); return u; } - function l(i, u) { - let s = !1; - for (let h of i) { - if (u && b(h).then(l), !t.has(h)) continue; - let C = t.get(h); - C.length_c && (h.dispatchEvent(new Event(g)), C.connected = /* @__PURE__ */ new WeakSet(), C.length_c = 0, C.length_d || t.delete(h), s = !0); + function h(s, u) { + let i = !1; + for (let l of s) { + if (u && b(l).then(h), !t.has(l)) continue; + let A = t.get(l); + A.length_c && (l.dispatchEvent(new Event(g)), A.connected = /* @__PURE__ */ new WeakSet(), A.length_c = 0, A.length_d || t.delete(l), i = !0); } - return s; + return i; } - function E(i, u) { - let s = !1; - for (let h of i) - u && b(h).then(E), !(!t.has(h) || !t.get(h).length_d) && ((globalThis.queueMicrotask || setTimeout)(_(h)), s = !0); - return s; + function E(s, u) { + let i = !1; + for (let l of s) + u && b(l).then(E), !(!t.has(l) || !t.get(l).length_d) && ((globalThis.queueMicrotask || setTimeout)(I(l)), i = !0); + return i; } - function _(i) { + function I(s) { return () => { - i.isConnected || (i.dispatchEvent(new Event(y)), t.delete(i)); + s.isConnected || (s.dispatchEvent(new Event(y)), t.delete(s)); }; } } // src/customElement.js -function mt(t, e, n, r = it) { - S.push({ +function wt(t, e, n, r = rt) { + D.push({ scope: t, - host: (...f) => f.length ? f.forEach((d) => d(t)) : t + host: (...a) => a.length ? a.forEach((d) => d(t)) : t }), typeof r == "function" && (r = r.call(t, t)); let o = t[w]; - o || ct(t); + o || nt(t); let c = n.call(t, r); - return o || t.dispatchEvent(new Event(g)), e.nodeType === 11 && typeof e.mode == "string" && t.addEventListener(y, A.observe(e), { once: !0 }), S.pop(), e.append(c); + return o || t.dispatchEvent(new Event(g)), e.nodeType === 11 && typeof e.mode == "string" && t.addEventListener(y, m.observe(e), { once: !0 }), D.pop(), e.append(c); } -function ct(t) { - return k(t.prototype, "connectedCallback", function(e, n, r) { +function nt(t) { + return P(t.prototype, "connectedCallback", function(e, n, r) { e.apply(n, r), n.dispatchEvent(new Event(g)); - }), k(t.prototype, "disconnectedCallback", function(e, n, r) { + }), P(t.prototype, "disconnectedCallback", function(e, n, r) { e.apply(n, r), (globalThis.queueMicrotask || setTimeout)( () => !n.isConnected && n.dispatchEvent(new Event(y)) ); - }), k(t.prototype, "attributeChangedCallback", function(e, n, r) { + }), P(t.prototype, "attributeChangedCallback", function(e, n, r) { let [o, , c] = r; - n.dispatchEvent(new CustomEvent(D, { + n.dispatchEvent(new CustomEvent(O, { detail: [o, c] })), e.apply(n, r); }), t.prototype[w] = !0, t; } -function k(t, e, n) { +function P(t, e, n) { t[e] = new Proxy(t[e] || (() => { }), { apply: n }); } -function it(t) { - return F(t, (e, n) => e.getAttribute(n)); +function rt(t) { + return W(t, (e, n) => e.getAttribute(n)); } // src/events.js -function Rt(t, e, n) { +function Ct(t, e, n) { return e || (e = {}), function(o, ...c) { n && (c.unshift(o), o = typeof n == "function" ? n() : n); - let f = c.length ? new CustomEvent(t, Object.assign({ detail: c[0] }, e)) : new Event(t, e); - return o.dispatchEvent(f); + let a = c.length ? new CustomEvent(t, Object.assign({ detail: c[0] }, e)) : new Event(t, e); + return o.dispatchEvent(a); }; } -function R(t, e, n) { +function _(t, e, n) { return function(o) { return o.addEventListener(t, e, n), o; }; } -var Z = (t) => Object.assign({}, typeof t == "object" ? t : null, { once: !0 }); -R.connected = function(t, e) { - return e = Z(e), function(r) { - return r.addEventListener(g, t, e), r[w] ? r : r.isConnected ? (r.dispatchEvent(new Event(g)), r) : (N(e.signal, () => A.offConnected(r, t)) && A.onConnected(r, t), r); +var B = (t) => Object.assign({}, typeof t == "object" ? t : null, { once: !0 }); +_.connected = function(t, e) { + return e = B(e), function(r) { + return r.addEventListener(g, t, e), r[w] ? r : r.isConnected ? (r.dispatchEvent(new Event(g)), r) : (L(e.signal, () => m.offConnected(r, t)) && m.onConnected(r, t), r); }; }; -R.disconnected = function(t, e) { - return e = Z(e), function(r) { - return r.addEventListener(y, t, e), r[w] || N(e.signal, () => A.offDisconnected(r, t)) && A.onDisconnected(r, t), r; +_.disconnected = function(t, e) { + return e = B(e), function(r) { + return r.addEventListener(y, t, e), r[w] || L(e.signal, () => m.offDisconnected(r, t)) && m.onDisconnected(r, t), r; }; }; -var W = /* @__PURE__ */ new WeakMap(); -R.disconnectedAsAbort = function(t) { - if (W.has(t)) return W.get(t); +var j = /* @__PURE__ */ new WeakMap(); +_.disconnectedAsAbort = function(t) { + if (j.has(t)) return j.get(t); let e = new AbortController(); - return W.set(t, e), t(R.disconnected(() => e.abort())), e; + return j.set(t, e), t(_.disconnected(() => e.abort())), e; }; -var st = /* @__PURE__ */ new WeakSet(); -R.attributeChanged = function(t, e) { +var ot = /* @__PURE__ */ new WeakSet(); +_.attributeChanged = function(t, e) { return typeof e != "object" && (e = {}), function(r) { - if (r.addEventListener(D, t, e), r[w] || st.has(r) || !a.M) return r; - let o = new a.M(function(f) { - for (let { attributeName: d, target: p } of f) + if (r.addEventListener(O, t, e), r[w] || ot.has(r) || !f.M) return r; + let o = new f.M(function(a) { + for (let { attributeName: d, target: p } of a) p.dispatchEvent( - new CustomEvent(D, { detail: [d, p.getAttribute(d)] }) + new CustomEvent(O, { detail: [d, p.getAttribute(d)] }) ); }); - return N(e.signal, () => o.disconnect()) && o.observe(r, { attributes: !0 }), r; + return L(e.signal, () => o.disconnect()) && o.observe(r, { attributes: !0 }), r; }; }; export { - O as assign, - z as assignAttribute, - K as chainableAppend, - Y as classListDeclarative, - Q as cn, - j as createElement, - gt as createElementNS, - mt as customElementRender, - ct as customElementWithDDE, - Rt as dispatchEvent, - j as el, - gt as elNS, - tt as elementAttribute, - ct as lifecyclesToEvents, - it as observedAttributes, - R as on, - G as registerReactivity, - S as scope, - bt as simulateSlots + R as assign, + U as assignAttribute, + J as chainableAppend, + K as classListDeclarative, + k as createElement, + dt as createElementNS, + wt as customElementRender, + nt as customElementWithDDE, + Ct as dispatchEvent, + k as el, + dt as elNS, + Q as elementAttribute, + nt as lifecyclesToEvents, + rt as observedAttributes, + _ as on, + Z as registerReactivity, + D as scope, + pt as simulateSlots }; diff --git a/docs/components/examples/signals/dom-attrs.js b/docs/components/examples/signals/dom-attrs.js index 88b3ba2..0941766 100644 --- a/docs/components/examples/signals/dom-attrs.js +++ b/docs/components/examples/signals/dom-attrs.js @@ -4,7 +4,7 @@ const count= S(0); import { el } from "deka-dom-el"; document.body.append( el("p", S(()=> "Currently: "+count())), - el("p", { classList: { red: S(()=> count()%2 === 0) }, dataset: { count }, textContent: "Attributes example" }) + el("p", { classList: { red: S(()=> count()%2 === 0) }, dataset: { count }, textContent: "Attributes example" }), ); document.head.append( el("style", ".red { color: red; }") diff --git a/examples/components/webComponent.js b/examples/components/webComponent.js index 75dc6bf..57581d3 100644 --- a/examples/components/webComponent.js +++ b/examples/components/webComponent.js @@ -34,7 +34,9 @@ export class CustomHTMLTestElement extends HTMLElement{ text(test), text(name), text(preName), - el("button", { type: "button", textContent: "pre-name", onclick: ()=> preName("Ahoj") }) + el("button", { type: "button", textContent: "pre-name", onclick: ()=> preName("Ahoj") }), + " | ", + el("slot", { className: "test", name: "test" }), ); } test= "A"; diff --git a/examples/index.js b/examples/index.js index bcfdfa1..8767717 100644 --- a/examples/index.js +++ b/examples/index.js @@ -15,7 +15,10 @@ document.body.append( el("h1", "Experiments:"), el(fullNameComponent), el(todosComponent), - el(CustomHTMLTestElement.tagName, { name: "attr" }), + el(CustomHTMLTestElement.tagName, { name: "attr" }).append( + el("span", { textContent: "test", slot: "test" }), + el("span", { textContent: "test", slot: "test" }), + ), el(thirdParty), el(CustomSlottingHTMLElement.tagName, { onclick: ()=> toggle(!toggle()) }).append( el("strong", { slot: "name", textContent: "Honzo" }), diff --git a/index.d.ts b/index.d.ts index 46b06f3..6365503 100644 --- a/index.d.ts +++ b/index.d.ts @@ -129,14 +129,9 @@ export function elNS( export { elNS as createElementNS } export function chainableAppend(el: EL): EL; -/** - * Mapper function (optional). Pass for coppying attributes, this is NOT implemented by {@link simulateSlots} itself! - * */ -type simulateSlotsMapper= (body: HTMLSlotElement, el: HTMLElement)=> void; /** Simulate slots for ddeComponents */ export function simulateSlots( root: EL, - mapper?: simulateSlotsMapper ): EL /** * Simulate slots in Custom Elements without using `shadowRoot`. @@ -145,7 +140,7 @@ export function simulateSlots( * */ export function simulateSlots( el: HTMLElement, - body: EL, mapper?: simulateSlotsMapper + body: EL, ): EL export function dispatchEvent(name: keyof DocumentEventMap | string, options?: EventInit): diff --git a/src/dom.js b/src/dom.js index 714e6a7..b8f9026 100644 --- a/src/dom.js +++ b/src/dom.js @@ -25,7 +25,7 @@ export const scope= { return scopes.pop(); }, }; -// following chainableAppend implementation is OK as the ElementPrototype.append description already is { writable: true, enumerable: true, configurable: true } // editorconfig-checker-disable-line +//NOTE: following chainableAppend implementation is OK as the ElementPrototype.append description already is { writable: true, enumerable: true, configurable: true } // editorconfig-checker-disable-line function append(...els){ this.appendOriginal(...els); return this; } export function chainableAppend(el){ if(el.append===append) return el; el.appendOriginal= el.append; el.append= append; return el; @@ -36,7 +36,6 @@ export function createElement(tag, attributes, ...addons){ const s= signals(this); let scoped= 0; let el, el_host; - //TODO Array.isArray(tag) ⇒ set key (cache els) if(Object(attributes)!==attributes || s.isSignal(attributes)) attributes= { textContent: attributes }; switch(true){ @@ -81,58 +80,7 @@ createElement.mark= function(attrs, is_open= false){ return out; }; export { createElement as el }; - -import { hasOwn } from "./helpers.js"; -/** @param {HTMLElement} element @param {HTMLElement} [root] */ -export function simulateSlots(element, root, mapper){ - if(typeof root!=="object"){ - mapper= root; - root= element; - } - const _default= Symbol.for("default"); - const slots= Array.from(root.querySelectorAll("slot")) - .reduce((out, curr)=> Reflect.set(out, curr.name || _default, curr) && out, {}); - const has_d= hasOwn(slots, _default); - element.append= new Proxy(element.append, { - apply(orig, _, els){ - if(els[0]===root) return orig.apply(element, els); - if(!els.length) return element; - - const d= env.D.createDocumentFragment(); - for(const el of els){ - if(!el || !el.slot){ if(has_d) d.append(el); continue; } - const name= el.slot; - const slot= slots[name]; - elementAttribute(el, "remove", "slot"); - if(!slot) continue; - simulateSlotReplace(slot, el, mapper); - Reflect.deleteProperty(slots, name); - } - if(has_d){ - slots[_default].replaceWith(d); - Reflect.deleteProperty(slots, _default); - } - element.append= orig; //TODO: better memory management, but non-native behavior! - return element; - } - }); - if(element!==root){ - const els= Array.from(element.childNodes); - els.forEach(el=> el.remove()); - element.append(...els); - } - return root; -} -export function cn(...s){ return s.filter(Boolean).join(" "); } -function simulateSlotReplace(slot, element, mapper){ - if(mapper) mapper(slot, element); - try{ slot.replaceWith(assign(element, { - className: cn(element.className, slot.className), - dataset: { ...slot.dataset } })); } - catch(_){ slot.replaceWith(element); } -} - -//const namespaceHelper= ns=> ns==="svg" ? "http://www.w3.org/2000/svg" : ns; +//TODO?: const namespaceHelper= ns=> ns==="svg" ? "http://www.w3.org/2000/svg" : ns; export function createElementNS(ns){ const _this= this; return function createElementNSCurried(...rest){ @@ -144,6 +92,38 @@ export function createElementNS(ns){ } export { createElementNS as elNS }; +/** @param {HTMLElement} element @param {HTMLElement} [root] */ +export function simulateSlots(element, root= element){ + const mark_e= "¹⁰", mark_s= "✓"; //NOTE: Markers to identify slots processed by this function. Also “prevents” native behavior as it is unlikely to use these in names. // editorconfig-checker-disable-line + const slots= Object.fromEntries( + Array.from(root.querySelectorAll("slot")) + .filter(s => !s.name.endsWith(mark_e)) + .map(s => [(s.name += mark_e), s])); + element.append= new Proxy(element.append, { + apply(orig, _, els){ + if(els[0]===root) return orig.apply(element, els); + for(const el of els){ + const name= (el.slot||"")+mark_e; + try{ elementAttribute(el, "remove", "slot"); } catch(_error){} + const slot= slots[name]; + if(!slot) return; + if(!name.startsWith(mark_s)) slot.childNodes.forEach(c=> c.remove()); + slot.append(el); + slot.name= mark_s+name; + //TODO?: el.dispatchEvent(new CustomEvent("dde:slotchange", { detail: slot })); + } + element.append= orig; //TODO?: better memory management, but non-native behavior! + return element; + } + }); + if(element!==root){ + const els= Array.from(element.childNodes); + //els.forEach(el=> el.remove()); + element.append(...els); + } + return root; +} + const assign_context= new WeakMap(); const { setDeleteAttr }= env; export function assign(element, ...attributes){ @@ -168,11 +148,11 @@ export function assignAttribute(element, key, value){ key= key.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase(); return setRemoveAttr(key, value); } - if("className"===key) key= "class";//just optimalization, `isPropSetter` returns false immediately + if("className"===key) key= "class";//NOTE: just optimalization, this makes `isPropSetter` returns false immediately // editorconfig-checker-disable-line switch(key){ case "xlink:href": return setRemoveAttr(key, value, "http://www.w3.org/1999/xlink"); - case "textContent": //just optimalization, its part of Node ⇒ deep for `isPropSetter` + case "textContent": //NOTE: just optimalization, this makes `isPropSetter` returns false immediately (as its part of Node ⇒ deep for `isPropSetter`) // editorconfig-checker-disable-line return setDeleteAttr(element, key, value); case "style": if(typeof value!=="object") break; @@ -206,7 +186,7 @@ export function elementAttribute(element, op, key, value){ return element[op+"AttributeNS"](null, key, value); } import { isUndef } from "./helpers.js"; -//TODO add cache? `(Map/Set)` +//TODO: add cache? `(Map/Set)` function isPropSetter(el, key){ if(!(key in el)) return false; const des= getPropDescriptor(el, key); diff --git a/src/events-observer.js b/src/events-observer.js index 5dc81a6..846340e 100644 --- a/src/events-observer.js +++ b/src/events-observer.js @@ -82,12 +82,12 @@ function connectionsChangesObserverConstructor(){ is_observing= false; observer.disconnect(); } - //TODO remount support? + //TODO: remount support? function requestIdle(){ return new Promise(function(resolve){ (requestIdleCallback || requestAnimationFrame)(resolve); }); } async function collectChildren(element){ - if(store.size > 30)//TODO limit? + if(store.size > 30)//TODO?: limit await requestIdle(); const out= []; if(!(element instanceof Node)) return out; diff --git a/src/observables-lib.js b/src/observables-lib.js index a019133..1fbae47 100644 --- a/src/observables-lib.js +++ b/src/observables-lib.js @@ -58,7 +58,7 @@ signal.on= function on(s, listener, options= {}){ if(Array.isArray(s)) return s.forEach(s=> on(s, listener, options)); addSignalListener(s, listener); if(as) as.addEventListener("abort", ()=> removeSignalListener(s, listener)); - //TODO cleanup when signal removed + //TODO: cleanup when signal removed }; signal.symbols= { //signal: mark, diff --git a/src/signals-lib.js b/src/signals-lib.js index a019133..1fbae47 100644 --- a/src/signals-lib.js +++ b/src/signals-lib.js @@ -58,7 +58,7 @@ signal.on= function on(s, listener, options= {}){ if(Array.isArray(s)) return s.forEach(s=> on(s, listener, options)); addSignalListener(s, listener); if(as) as.addEventListener("abort", ()=> removeSignalListener(s, listener)); - //TODO cleanup when signal removed + //TODO: cleanup when signal removed }; signal.symbols= { //signal: mark,