ranui
Version:
A framework-agnostic Web Components UI library built on native custom elements, with TypeScript types, light/dark theming, SSR and PWA support.
71 lines (70 loc) • 1.84 kB
JavaScript
var i = "ran-theme", s = null, a = null, o = (e) => {
if (e && "style" in e) return e;
if (e && "documentElement" in e) return e.documentElement;
if (!(typeof document > "u"))
return document.documentElement;
}, m = (e) => {
const t = typeof window < "u" && window.matchMedia("(prefers-color-scheme: dark)").matches;
e.setAttribute("data-ran-theme", t ? "dark" : "light"), e.setAttribute("theme", t ? "dark" : "light");
}, l = () => {
s && a && (s.removeEventListener("change", a), s = null, a = null);
}, c = (e, t) => {
const r = o(t);
if (r) {
if (l(), e === "system") {
if (typeof window > "u") return;
m(r), s = window.matchMedia("(prefers-color-scheme: dark)"), a = () => m(r), s.addEventListener("change", a);
try {
localStorage.setItem(i, "system");
} catch {
}
return;
}
r.setAttribute("data-ran-theme", e), r.setAttribute("theme", e);
try {
localStorage.setItem(i, e);
} catch {
}
}
}, h = (e) => {
const t = o(e);
if (!t) return "";
const r = t.getAttribute("data-ran-theme") || t.getAttribute("theme") || "";
if (r === "light" || r === "dark") {
try {
if (localStorage.getItem(i) === "system") return "system";
} catch {
}
return r;
}
return "";
}, u = (e, t, r) => {
const n = o(r);
n && n.style.setProperty(e, String(t));
}, d = (e, t) => {
const r = o(t);
r && r.style.removeProperty(e);
}, f = (e, t) => {
Object.entries(e).forEach(([r, n]) => {
if (n == null) {
d(r, t);
return;
}
u(r, n, t);
});
}, y = (e) => {
if (!(typeof localStorage > "u"))
try {
const t = localStorage.getItem(i);
(t === "light" || t === "dark" || t === "system") && c(t, e);
} catch {
}
};
export {
u as a,
c as i,
h as n,
f as o,
y as r,
d as t
};