ranui
Version:
A framework-agnostic Web Components UI library built on native custom elements, with TypeScript types, light/dark theming, SSR and PWA support.
52 lines (51 loc) • 1.45 kB
JavaScript
import { localStorageGetItem as o, localStorageSetItem as a, watchMediaQuery as u } from "ranuts/utils";
var s = "ran-theme", l = "(prefers-color-scheme: dark)", i = null, m = (e) => {
if (e && "style" in e) return e;
if (e && "documentElement" in e) return e.documentElement;
if (!(typeof document > "u"))
return document.documentElement;
}, h = (e, t) => {
e.setAttribute("data-ran-theme", t ? "dark" : "light"), e.setAttribute("theme", t ? "dark" : "light");
}, d = () => {
i?.(), i = null;
}, c = (e, t) => {
const r = m(t);
if (r) {
if (d(), e === "system") {
if (typeof window > "u") return;
i = u(l, (n) => h(r, n)), a(s, "system");
return;
}
r.setAttribute("data-ran-theme", e), r.setAttribute("theme", e), a(s, e);
}
}, T = (e) => {
const t = m(e);
if (!t) return "";
const r = t.getAttribute("data-ran-theme") || t.getAttribute("theme") || "";
return r === "light" || r === "dark" ? o(s) === "system" ? "system" : r : "";
}, f = (e, t, r) => {
const n = m(r);
n && n.style.setProperty(e, String(t));
}, y = (e, t) => {
const r = m(t);
r && r.style.removeProperty(e);
}, E = (e, t) => {
Object.entries(e).forEach(([r, n]) => {
if (n == null) {
y(r, t);
return;
}
f(r, n, t);
});
}, A = (e) => {
const t = o(s);
(t === "light" || t === "dark" || t === "system") && c(t, e);
};
export {
f as a,
c as i,
T as n,
E as o,
A as r,
y as t
};