Include build ESM

This commit is contained in:
2026-04-04 14:24:32 +02:00
parent 80493ded82
commit 087a5a3c6e
5 changed files with 515 additions and 36 deletions

97
dist/sigpro.js vendored
View File

@@ -241,8 +241,12 @@
content = props;
props = {};
}
const el = document.createElement(tag), _sanitize = (key, val) => (key === "src" || key === "href") && String(val).toLowerCase().includes("javascript:") ? "#" : val;
const svgTags = ["svg", "path", "circle", "rect", "line", "polyline", "polygon", "g", "defs", "text", "tspan", "use"];
const isSVG = svgTags.includes(tag);
const el = isSVG ? document.createElementNS("http://www.w3.org/2000/svg", tag) : document.createElement(tag);
const _sanitize = (key, val) => (key === "src" || key === "href") && String(val).toLowerCase().includes("javascript:") ? "#" : val;
el._cleanups = new Set;
const boolAttrs = ["disabled", "checked", "required", "readonly", "selected", "multiple", "autofocus"];
for (let [key, val] of Object.entries(props)) {
if (key === "ref") {
typeof val === "function" ? val(el) : val.current = el;
@@ -265,13 +269,38 @@
} else if (isSignal) {
el._cleanups.add($watch(() => {
const currentVal = _sanitize(key, val());
if (key === "class")
if (key === "class") {
el.className = currentVal || "";
else
currentVal == null ? el.removeAttribute(key) : el.setAttribute(key, currentVal);
} else if (boolAttrs.includes(key)) {
if (currentVal) {
el.setAttribute(key, "");
el[key] = true;
} else {
el.removeAttribute(key);
el[key] = false;
}
} else {
if (currentVal == null) {
el.removeAttribute(key);
} else if (isSVG && typeof currentVal === "number") {
el.setAttribute(key, currentVal);
} else {
el.setAttribute(key, currentVal);
}
}
}));
} else {
el.setAttribute(key, _sanitize(key, val));
if (boolAttrs.includes(key)) {
if (val) {
el.setAttribute(key, "");
el[key] = true;
} else {
el.removeAttribute(key);
el[key] = false;
}
} else {
el.setAttribute(key, _sanitize(key, val));
}
}
}
const append = (child) => {
@@ -286,7 +315,7 @@
el._cleanups.add($watch(() => {
const res = child(), next = (Array.isArray(res) ? res : [res]).map((i) => i?._isRuntime ? i.container : i instanceof Node ? i : document.createTextNode(i ?? ""));
nodes.forEach((n) => {
sweep(n);
sweep?.(n);
n.remove();
});
next.forEach((n) => marker.parentNode?.insertBefore(n, marker));
@@ -298,65 +327,73 @@
append(content);
return el;
};
var $if = (condition, thenVal, otherwiseVal = null) => {
var $if = (condition, thenVal, otherwiseVal = null, transition = null) => {
const marker = document.createTextNode("");
const container = $html("div", { style: "display:contents" }, [marker]);
let current = null, last = null;
$watch(() => {
const state = !!(typeof condition === "function" ? condition() : condition);
if (state !== last) {
last = state;
if (state === last)
return;
last = state;
if (current && !state && transition?.out) {
transition.out(current.container, () => {
current.destroy();
current = null;
});
} else {
if (current)
current.destroy();
current = null;
}
if (state || !state && otherwiseVal) {
const branch = state ? thenVal : otherwiseVal;
if (branch) {
current = _view(() => typeof branch === "function" ? branch() : branch);
container.insertBefore(current.container, marker);
if (state && transition?.in)
transition.in(current.container);
}
}
});
return container;
};
$if.not = (condition, thenVal, otherwiseVal) => $if(() => !(typeof condition === "function" ? condition() : condition), thenVal, otherwiseVal);
var $for = (source, render, keyFn) => {
const marker = document.createComment("sigpro-for-end");
var $for = (source, render, keyFn, tag = "div", props = { style: "display:contents" }) => {
const marker = document.createTextNode("");
const container = $html(tag, props, [marker]);
let cache = new Map;
$watch(() => {
const items = (typeof source === "function" ? source() : source) || [];
const parent = marker.parentNode;
if (!parent)
return;
const newCache = new Map;
const newOrder = [];
for (let i = 0;i < items.length; i++) {
const item = items[i];
const key = keyFn ? keyFn(item, i) : i;
let cached = cache.get(key);
if (!cached) {
const view = _view(() => render(item, i));
const node = view.container.firstElementChild || view.container.firstChild;
cached = { node, destroy: view.destroy };
let run = cache.get(key);
if (!run) {
run = _view(() => render(item, i));
} else {
cache.delete(key);
}
newCache.set(key, cached);
newOrder.push(cached.node);
newCache.set(key, run);
newOrder.push(key);
}
cache.forEach((c) => {
c.destroy();
c.node.remove();
cache.forEach((run) => {
run.destroy();
run.container.remove();
});
let currentAnchor = marker;
let anchor = marker;
for (let i = newOrder.length - 1;i >= 0; i--) {
const node = newOrder[i];
if (node.nextSibling !== currentAnchor) {
parent.insertBefore(node, currentAnchor);
const run = newCache.get(newOrder[i]);
if (run.container.nextSibling !== anchor) {
container.insertBefore(run.container, anchor);
}
currentAnchor = node;
anchor = run.container;
}
cache = newCache;
});
return marker;
return container;
};
var $router = (routes) => {
const sPath = $(window.location.hash.replace(/^#/, "") || "/");