ranui
Version:
A framework-agnostic Web Components UI library built on native custom elements, with TypeScript types, light/dark theming, SSR and PWA support.
180 lines (179 loc) • 5.73 kB
JavaScript
var g = typeof document > "u";
function l(t) {
return typeof t != "string" ? String(t) : t.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
}
function c(t) {
return l(t);
}
var p = (t) => typeof t == "string" ? !1 : "tagName" in t && "childrenList" in t, d = (t, e) => {
const s = e.trim();
if (!s) return !1;
if (s.startsWith(".")) return t.classList.contains(s.slice(1));
if (s.startsWith("#")) return t.getAttribute("id") === s.slice(1);
if (s.startsWith("[") && s.endsWith("]")) {
const [n, a] = s.slice(1, -1).trim().split("="), r = (n || "").trim();
if (!r) return !1;
if (a == null) return t.hasAttribute(r);
const i = a.trim().replace(/^['"]|['"]$/g, "");
return t.getAttribute(r) === i;
}
return t.tagName === s.toLowerCase();
}, o = (t, e, s) => {
for (const n of t)
p(n) && (d(n, e) && s.push(n), o(n.childrenList, e, s));
}, u = class {
childrenList = [];
appendChild(t) {
return this.childrenList.push(t), t;
}
serialize() {
return this.childrenList.map((t) => typeof t == "string" ? l(t) : t.serialize()).join("");
}
querySelector(t) {
const e = this.querySelectorAll(t);
return e.length > 0 ? e[0] : null;
}
querySelectorAll(t) {
const e = [];
return o(this.childrenList, t, e), e;
}
}, m = class {
tagName;
attributes = /* @__PURE__ */ new Map();
inlineStyles = /* @__PURE__ */ new Map();
childrenList = [];
shadowRoot = null;
textContent = null;
content;
eventListeners = /* @__PURE__ */ new Map();
style = {
setProperty: (t, e) => {
const s = t.startsWith("--") ? t : t.replace(/([A-Z])/g, "-$1").toLowerCase();
this.inlineStyles.set(s, e);
},
removeProperty: (t) => this.inlineStyles.delete(t)
};
classList = {
add: (...t) => {
const e = this.attributes.get("class") || "", s = /* @__PURE__ */ new Set([...e.split(" ").filter(Boolean), ...t]);
this.attributes.set("class", Array.from(s).join(" "));
},
remove: (...t) => {
const e = (this.attributes.get("class") || "").split(" ").filter((s) => !t.includes(s));
this.attributes.set("class", e.join(" "));
},
toggle: (t) => {
this.classList.contains(t) ? this.classList.remove(t) : this.classList.add(t);
},
contains: (t) => (this.attributes.get("class") || "").split(" ").includes(t)
};
_innerHTML = "";
get innerHTML() {
return this._innerHTML;
}
set innerHTML(t) {
this._innerHTML = t, this.tagName === "template" && this.content && (this.content.childrenList = [t]);
}
constructor(t = "div") {
this.tagName = t.toLowerCase(), this.tagName === "template" && (this.content = new u());
}
get hidden() {
return this.hasAttribute("hidden");
}
set hidden(t) {
t ? this.setAttribute("hidden", "") : this.removeAttribute("hidden");
}
setAttribute(t, e) {
this.attributes.set(t, e);
}
getAttribute(t) {
return this.attributes.get(t) || null;
}
removeAttribute(t) {
this.attributes.delete(t);
}
hasAttribute(t) {
return this.attributes.has(t);
}
appendChild(t) {
return this.childrenList.push(t), t;
}
attachShadow(t) {
return this.shadowRoot = new f(this, t), this.shadowRoot;
}
querySelector(t) {
const e = this.querySelectorAll(t);
return e.length > 0 ? e[0] : null;
}
querySelectorAll(t) {
const e = [];
return o(this.childrenList, t, e), e;
}
addEventListener(t, e) {
const s = this.eventListeners.get(t) || /* @__PURE__ */ new Set();
s.add(e), this.eventListeners.set(t, s);
}
removeEventListener(t, e) {
const s = this.eventListeners.get(t);
s && (s.delete(e), s.size === 0 && this.eventListeners.delete(t));
}
dispatchEvent(t) {
const e = this.eventListeners.get(t.type);
if (!e || e.size === 0) return !0;
for (const s of e) typeof s == "function" ? s.call(this, t) : s.handleEvent(t);
return !t.defaultPrevented;
}
serialize(t) {
typeof this._preSerialize == "function" && this._preSerialize();
const e = t || this._ssrTag || this.tagName, s = Array.from(this.attributes.entries()).map(([i, h]) => ` ${i}="${c(h)}"`).join(""), n = Array.from(this.inlineStyles.entries()).map(([i, h]) => `${i}:${h}`).join(";"), a = n ? ` style="${c(n)}"` : "";
let r = "";
return this.shadowRoot && (r += this.shadowRoot.serialize()), this.textContent !== null ? r += l(this.textContent) : this._innerHTML ? r += this._innerHTML : r += this.childrenList.map((i) => typeof i == "string" ? l(i) : (i instanceof u, i.serialize())).join(""), [
"area",
"base",
"br",
"col",
"embed",
"hr",
"img",
"input",
"link",
"meta",
"param",
"source",
"track",
"wbr"
].includes(e) && !r ? `<${e}${s}${a} />` : `<${e}${s}${a}>${r}</${e}>`;
}
}, f = class {
host;
options;
childrenList = [];
adoptedStyleSheets = [];
constructor(t, e) {
this.host = t, this.options = e;
}
appendChild(t) {
return this.childrenList.push(t), t;
}
querySelector(t) {
const e = this.querySelectorAll(t);
return e.length > 0 ? e[0] : null;
}
querySelectorAll(t) {
const e = [];
return o(this.childrenList, t, e), e;
}
serialize() {
return `<template shadowrootmode="${this.options.mode}"${this.options.delegatesFocus ? " shadowrootdelegatesfocus" : ""}>${this.adoptedStyleSheets.map((t) => `<style>${l(t)}</style>`).join("")}${this.childrenList.map((t) => typeof t == "string" ? l(t) : (t instanceof u, t.serialize())).join("")}</template>`;
}
};
export {
d as a,
g as c,
o as i,
m as n,
l as o,
f as r,
c as s,
u as t
};