ranui
Version:
A framework-agnostic Web Components UI library built on native custom elements, with TypeScript types, light/dark theming, SSR and PWA support.
615 lines (614 loc) • 17.2 kB
JavaScript
import "./ssr-DSGwPtEd.js";
var R = class {
_events = /* @__PURE__ */ new Map();
tap = (e, t) => {
this._events.get(e) && e !== /* @__PURE__ */ Symbol.for("NEW_LISTENER") && this.call(/* @__PURE__ */ Symbol.for("NEW_LISTENER"), e);
const r = this._events.get(e) || /* @__PURE__ */ new Set();
return typeof t == "function" ? r.add({
name: e,
callback: t
}) : r.add(t), this._events.set(e, r), this;
};
call = (e, ...t) => ((this._events.get(e) || /* @__PURE__ */ new Set()).forEach((r) => {
const { callback: a } = r;
a(...t);
}), this);
callSync = async (e, ...t) => {
const r = this._events.get(e) || /* @__PURE__ */ new Set();
for (const a of r) {
const { callback: s } = a;
await s(...t);
}
return this;
};
once = (e, t) => {
let r;
if (typeof t == "function") r = {
name: e,
callback: (...a) => {
t(...a), this.off(e, r);
},
initialCallback: t
};
else {
const { callback: a } = t;
r = {
name: e,
callback: (...s) => {
a(...s), this.off(e, r);
},
initialCallback: a
};
}
return this.tap(e, r), this;
};
off = (e, t) => {
const r = [...this._events.get(e) || /* @__PURE__ */ new Set()].filter((a) => {
if (typeof t == "function") return a.callback !== t && a.initialCallback !== t;
{
const { callback: s } = t;
return a.callback !== s && a.initialCallback !== s;
}
});
return this._events.set(e, new Set(r)), this;
};
}, y = (e = 8) => Math.random().toString(36).substring(2, e + 2), L = (e = "") => {
if (e.length === 0) return 0;
if (e.endsWith("%")) {
const t = Number(e.replace("%", ""));
return t > 1 ? t / 100 : t;
}
return Number(e);
}, B = (e, t = 0, r = 1) => Math.min(r, Math.max(t, e)), C = (e) => typeof e == "function";
function h(e) {
return e < 10 ? `0${e}` : e;
}
var O = (e) => {
if (e === 0) return "00:00";
if (!e) return "";
const t = Math.trunc(e / 3600), r = Math.trunc(e % 3600 / 60), a = h(Math.trunc(e - 3600 * t - 60 * r));
return t === 0 ? `${h(r)}:${a}` : `${h(t)}:${h(r)}:${a}`;
}, $ = () => {
if (typeof window < "u") {
const e = navigator.userAgent.toLowerCase();
return /ipad|ipod/.test(e) ? "ipad" : /android/.test(e) ? "android" : /iphone/.test(e) ? "iphone" : "pc";
}
return "pc";
}, E = typeof window < "u", T = () => {
if (!E) return !1;
const e = window.navigator.userAgent;
return !!/Android|webOS|iPhone|iPod|iPad|BlackBerry/i.test(e);
}, U = (e) => {
const t = e.backingStorePixelRatio || e.webkitBackingStorePixelRatio || e.mozBackingStorePixelRatio || e.msBackingStorePixelRatio || e.oBackingStorePixelRatio || 1;
return (E && window.devicePixelRatio || 1) / t;
}, A = 12e4, M = "__ranuts_bridge__", q = "default", m = new class {
bridges = /* @__PURE__ */ new Set();
attachedWindow = null;
handleMessage = (e) => {
for (const t of Array.from(this.bridges)) t.receive(e);
};
add(e) {
this.bridges.add(e), typeof window < "u" && this.attachedWindow !== window && (window.addEventListener("message", this.handleMessage), this.attachedWindow = window);
}
remove(e) {
this.bridges.delete(e), this.bridges.size === 0 && this.attachedWindow && (this.attachedWindow.removeEventListener("message", this.handleMessage), this.attachedWindow = null);
}
}(), P = class {
targetWindow;
targetOrigin;
messageHandlers;
pendingRequests;
channel;
senderId;
available;
constructor(e, t = "*", r = q) {
this.targetOrigin = t, this.channel = r, this.senderId = y(12), this.messageHandlers = /* @__PURE__ */ new Map(), this.pendingRequests = /* @__PURE__ */ new Map(), this.available = typeof window < "u", this.available ? (this.targetWindow = e ?? window, m.add(this)) : this.targetWindow = void 0;
}
post(e, t) {
const r = {
...t,
__bridge: M,
channel: this.channel,
senderId: this.senderId
};
e.postMessage(r, this.targetOrigin);
}
receive = (e) => {
if (!this.available || this.targetOrigin !== "*" && e.origin !== this.targetOrigin || e.source && e.source !== this.targetWindow) return;
const t = e.data;
if (!t || typeof t != "object" || t.__bridge !== "__ranuts_bridge__" || (t.channel ?? "default") !== this.channel) return;
const { type: r, payload: a, id: s, isResponse: o, isError: w, senderId: d } = t;
if (o && s) {
const c = this.pendingRequests.get(s);
c && (w ? c.reject(new Error(typeof a == "string" ? a : "Bridge request failed")) : c.resolve(a), this.pendingRequests.delete(s));
return;
}
if (d && d === this.senderId || typeof r != "string" || !this.messageHandlers.has(r)) return;
const v = this.messageHandlers.get(r);
if (C(v)) {
const c = e.source ?? this.targetWindow;
Promise.resolve(v(a)).then((l) => {
s && this.post(c, {
type: r,
payload: l,
id: s,
isResponse: !0
});
}).catch((l) => {
s && this.post(c, {
type: r,
payload: l instanceof Error ? l.message : String(l),
id: s,
isResponse: !0,
isError: !0
});
});
}
};
on = (e, t) => {
this.messageHandlers.set(e, t);
};
off = (e) => {
this.messageHandlers.delete(e);
};
send = async (e, t) => {
if (!this.available) return Promise.reject(/* @__PURE__ */ new Error("PostMessageBridge is unavailable outside a browser environment"));
const r = y(10);
return new Promise((a, s) => {
const o = setTimeout(() => {
this.pendingRequests.has(r) && (this.pendingRequests.delete(r), s(/* @__PURE__ */ new Error("Request timeout")));
}, A), w = (d) => {
clearTimeout(o), s(d);
};
this.pendingRequests.set(r, {
resolve: (d) => {
clearTimeout(o), a(d);
},
reject: w
});
try {
this.post(this.targetWindow, {
type: e,
payload: t,
id: r
});
} catch (d) {
this.pendingRequests.delete(r), w(d instanceof Error ? d : /* @__PURE__ */ new Error("Failed to post message"));
}
});
};
broadcast = (e) => {
if (this.available) try {
this.post(this.targetWindow, {
type: e.type,
payload: e.payload
});
} catch {
}
};
destroy = () => {
m.remove(this), this.messageHandlers.clear(), this.pendingRequests.forEach((e) => {
e.reject(/* @__PURE__ */ new Error("Bridge destroyed"));
}), this.pendingRequests.clear();
};
}, _ = class u {
static instance;
bridges = /* @__PURE__ */ new Map();
constructor() {
}
static getInstance() {
return u.instance || (u.instance = new u()), u.instance;
}
connectClient = ({ id: t, targetOrigin: r, targetWindow: a, channel: s }) => {
const o = new P(a, r, s);
if (t || (t = y(10)), this.bridges.has(t)) throw new Error(`Bridge ${t} already exists`);
return this.bridges.set(t, o), {
bridge: o,
id: t
};
};
getClient = (t) => this.bridges.get(t);
removeClient = (t) => {
const r = this.bridges.get(t);
r && (r.destroy(), this.bridges.delete(t));
};
removeAllClient = () => {
this.bridges.forEach((t) => {
t.destroy();
}), this.bridges.clear();
};
broadcast = (t) => {
this.bridges.forEach((r) => {
r.broadcast(t);
});
};
sendTo = (t, r, a) => {
const s = this.getClient(t);
return s ? s.send(r, a) : Promise.reject(/* @__PURE__ */ new Error(`Bridge ${t} not found`));
};
}, F = _.getInstance(), H = new R(), i = [
1116352408,
1899447441,
3049323471,
3921009573,
961987163,
1508970993,
2453635748,
2870763221,
3624381080,
310598401,
607225278,
1426881987,
1925078388,
2162078206,
2614888103,
3248222580,
3835390401,
4022224774,
264347078,
604807628,
770255983,
1249150122,
1555081692,
1996064986,
2554220882,
2821834349,
2952996808,
3210313671,
3336571891,
3584528711,
113926993,
338241895,
666307205,
773529912,
1294757372,
1396182291,
1695183700,
1986661051,
2177026350,
2456956037,
2730485921,
2820302411,
3259730800,
3345764771,
3516065817,
3600352804,
4094571909,
275423344,
430227734,
506948616,
659060556,
883997877,
958139571,
1322822218,
1537002063,
1747873779,
1955562222,
2024104815,
2227730452,
2361852424,
2428436474,
2756734187,
3204031479,
3329325298
], n = class {
highOrder;
lowOrder;
constructor(e, t) {
this.highOrder = e, this.lowOrder = t;
}
}, x = [
new n(i[0], 3609767458),
new n(i[1], 602891725),
new n(i[2], 3964484399),
new n(i[3], 2173295548),
new n(i[4], 4081628472),
new n(i[5], 3053834265),
new n(i[6], 2937671579),
new n(i[7], 3664609560),
new n(i[8], 2734883394),
new n(i[9], 1164996542),
new n(i[10], 1323610764),
new n(i[11], 3590304994),
new n(i[12], 4068182383),
new n(i[13], 991336113),
new n(i[14], 633803317),
new n(i[15], 3479774868),
new n(i[16], 2666613458),
new n(i[17], 944711139),
new n(i[18], 2341262773),
new n(i[19], 2007800933),
new n(i[20], 1495990901),
new n(i[21], 1856431235),
new n(i[22], 3175218132),
new n(i[23], 2198950837),
new n(i[24], 3999719339),
new n(i[25], 766784016),
new n(i[26], 2566594879),
new n(i[27], 3203337956),
new n(i[28], 1034457026),
new n(i[29], 2466948901),
new n(i[30], 3758326383),
new n(i[31], 168717936),
new n(i[32], 1188179964),
new n(i[33], 1546045734),
new n(i[34], 1522805485),
new n(i[35], 2643833823),
new n(i[36], 2343527390),
new n(i[37], 1014477480),
new n(i[38], 1206759142),
new n(i[39], 344077627),
new n(i[40], 1290863460),
new n(i[41], 3158454273),
new n(i[42], 3505952657),
new n(i[43], 106217008),
new n(i[44], 3606008344),
new n(i[45], 1432725776),
new n(i[46], 1467031594),
new n(i[47], 851169720),
new n(i[48], 3100823752),
new n(i[49], 1363258195),
new n(i[50], 3750685593),
new n(i[51], 3785050280),
new n(i[52], 3318307427),
new n(i[53], 3812723403),
new n(i[54], 2003034995),
new n(i[55], 3602036899),
new n(i[56], 1575990012),
new n(i[57], 1125592928),
new n(i[58], 2716904306),
new n(i[59], 442776044),
new n(i[60], 593698344),
new n(i[61], 3733110249),
new n(i[62], 2999351573),
new n(i[63], 3815920427),
new n(3391569614, 3928383900),
new n(3515267271, 566280711),
new n(3940187606, 3454069534),
new n(4118630271, 4000239992),
new n(116418474, 1914138554),
new n(174292421, 2731055270),
new n(289380356, 3203993006),
new n(460393269, 320620315),
new n(685471733, 587496836),
new n(852142971, 1086792851),
new n(1017036298, 365543100),
new n(1126000580, 2618297676),
new n(1288033470, 3409855158),
new n(1501505948, 4234509866),
new n(1607167915, 987167468),
new n(1816402316, 1246189591)
], D = [
new n(0, 1),
new n(0, 32898),
new n(2147483648, 32906),
new n(2147483648, 2147516416),
new n(0, 32907),
new n(0, 2147483649),
new n(2147483648, 2147516545),
new n(2147483648, 32777),
new n(0, 138),
new n(0, 136),
new n(0, 2147516425),
new n(0, 2147483658),
new n(0, 2147516555),
new n(2147483648, 139),
new n(2147483648, 32905),
new n(2147483648, 32771),
new n(2147483648, 32770),
new n(2147483648, 128),
new n(0, 32778),
new n(2147483648, 2147483658),
new n(2147483648, 2147516545),
new n(2147483648, 32896),
new n(0, 2147483649),
new n(2147483648, 2147516424)
], j = (e, t = 500) => {
let r;
return (...a) => {
clearTimeout(r), r = setTimeout(() => {
e.apply(void 0, a);
}, t);
};
}, G = (e, t) => {
if (typeof document > "u") return;
const r = e.classList;
r.contains(t) || r.add(t);
}, z = (e, t) => {
if (typeof document > "u") return;
const r = e.classList;
r.contains(t) && r.remove(t);
}, V = () => {
let e = 0, t = null;
return function(r, a = 300) {
return function(...s) {
const o = Date.now();
o - e >= a ? (t != null && (clearTimeout(t), t = null), e = o, r.apply(this, s)) : t ??= window.setTimeout(() => {
e = Date.now(), r.apply(this, s), t = null;
}, a - (o - e));
};
};
}, f = /* @__PURE__ */ new Map([
[100, "Continue"],
[101, "Switching Protocols"],
[102, "Processing"],
[103, "Early Hints"],
[200, "OK"],
[201, "Created"],
[202, "Accepted"],
[203, "Non-Authoritative Information"],
[204, "No Content"],
[205, "Reset Content"],
[206, "Partial Content"],
[207, "Multi-Status"],
[208, "Already Reported"],
[226, "IM Used"],
[300, "Multiple Choices"],
[301, "Moved Permanently"],
[302, "Found"],
[303, "See Other"],
[304, "Not Modified"],
[305, "Use Proxy"],
[307, "Temporary Redirect"],
[308, "Permanent Redirect"],
[400, "Bad Request"],
[401, "Unauthorized"],
[402, "Payment Required"],
[403, "Forbidden"],
[404, "Not Found"],
[405, "Method Not Allowed"],
[406, "Not Acceptable"],
[407, "Proxy Authentication Required"],
[408, "Request Timeout"],
[409, "Conflict"],
[410, "Gone"],
[411, "Length Required"],
[412, "Precondition Failed"],
[413, "Payload Too Large"],
[414, "URI Too Long"],
[415, "Unsupported Media Type"],
[416, "Range Not Satisfiable"],
[417, "Expectation Failed"],
[418, "I'm a Teapot"],
[421, "Misdirected Request"],
[422, "Unprocessable Entity"],
[423, "Locked"],
[424, "Failed Dependency"],
[425, "Too Early"],
[426, "Upgrade Required"],
[428, "Precondition Required"],
[429, "Too Many Requests"],
[431, "Request Header Fields Too Large"],
[451, "Unavailable For Legal Reasons"],
[500, "Internal Server Error"],
[501, "Not Implemented"],
[502, "Bad Gateway"],
[503, "Service Unavailable"],
[504, "Gateway Timeout"],
[505, "HTTP Version Not Supported"],
[506, "Variant Also Negotiates"],
[507, "Insufficient Storage"],
[508, "Loop Detected"],
[509, "Bandwidth Limit Exceeded"],
[510, "Not Extended"],
[511, "Network Authentication Required"]
]), K = {
message: f,
code: (function(e) {
const t = /* @__PURE__ */ new Map();
for (const [r, a] of e) t.set(a.toLowerCase(), r);
return t;
})(f),
codes: (function(e) {
const t = [];
for (const [r, a] of e) t.push(r);
return t;
})(f),
redirect: {
300: !0,
301: !0,
302: !0,
303: !0,
305: !0,
307: !0,
308: !0
},
empty: {
204: !0,
205: !0,
304: !0
},
retry: {
502: !0,
503: !0,
504: !0
}
}, k = [
!1,
"false",
null,
void 0
], X = (e) => {
const t = e.hasAttribute("disabled"), r = e.getAttribute("disabled");
return !!(t && !k.includes(r));
}, g = /* @__PURE__ */ new Map(), p = /* @__PURE__ */ new Map(), N = (e, t) => {
if (!(typeof document > "u") && t) {
if (typeof CSSStyleSheet < "u") try {
if (!g.has(t)) {
const a = new CSSStyleSheet();
a.replaceSync(t), g.set(t, a);
}
const r = g.get(t);
e.adoptedStyleSheets.includes(r) || (e.adoptedStyleSheets = [...e.adoptedStyleSheets, r]);
return;
} catch {
}
if (!e.querySelector("style[data-ranui]")) {
const r = document.createElement("style");
r.setAttribute("data-ranui", ""), r.textContent = t, e.appendChild(r);
}
}
}, W = (e, t) => {
if (!(typeof document > "u") && t) {
if (typeof CSSStyleSheet < "u") try {
if (!p.has(t)) {
const a = new CSSStyleSheet();
a.replaceSync(t), p.set(t, a);
}
const r = p.get(t);
e.adoptedStyleSheets.includes(r) || (e.adoptedStyleSheets = [...e.adoptedStyleSheets, r]);
return;
} catch {
}
if (!Array.from(e.querySelectorAll("style[data-ranui-sheet]")).some((r) => r.textContent === t)) {
const r = document.createElement("style");
r.setAttribute("data-ranui-sheet", ""), r.textContent = t, e.appendChild(r);
}
}
}, b = /* @__PURE__ */ new WeakMap(), S = "@media (prefers-reduced-motion: reduce){*,*::before,*::after{animation-duration:.01ms !important;animation-iteration-count:1 !important;transition-duration:.01ms !important;scroll-behavior:auto !important}}", Y = (e, t = "", r = { mode: "closed" }) => {
const a = e.shadowRoot || b.get(e) || e.attachShadow(r);
return b.set(e, a), N(a, t ? `${t}
${S}` : S), a;
}, J = (e, t, r) => {
const a = e.querySelector(t);
if (a) return a;
const s = r();
return e.appendChild(s), s;
}, Q = (e, t, r = "") => e.getAttribute(t) ?? r, Z = (e, t, r, a = {}) => {
if (a.removeEmpty && !r) {
e.removeAttribute(t);
return;
}
e.setAttribute(t, r ?? "");
}, ee = (e, t, r, a = {}) => {
if (r) {
e.setAttribute(t, ""), a.aria && e.setAttribute(`aria-${a.aria}`, "true");
return;
}
e.removeAttribute(t), a.aria && e.removeAttribute(`aria-${a.aria}`);
}, te = (e, t, r, a, s) => {
if (r !== "sheet" || a === s) return;
const o = e.getAttribute("sheet") || "";
o && W(t, o);
};
export {
V as _,
Z as a,
X as c,
T as d,
U as f,
B as g,
O as h,
ee as i,
G as l,
L as m,
Y as n,
te as o,
z as p,
Q as r,
k as s,
J as t,
j as u,
$ as v,
R as y
};