laif-ds
Version:
Design System di Laif con componenti React basati su principi di Atomic Design
77 lines (76 loc) • 2.65 kB
JavaScript
"use client";
import * as i from "react";
import { createContextScope as C } from "../../react-context/dist/index.js";
import { useCallbackRef as h } from "../../react-use-callback-ref/dist/index.js";
import { useLayoutEffect as f } from "../../react-use-layout-effect/dist/index.js";
import { Primitive as m } from "../../react-primitive/dist/index.js";
import { useIsHydrated as _ } from "../../react-use-is-hydrated/dist/index.js";
import { jsx as l } from "react/jsx-runtime";
var v = "Avatar", [y] = C(v), [x, A] = y(v), L = i.forwardRef(
(a, e) => {
const { __scopeAvatar: o, ...r } = a, [n, t] = i.useState("idle");
return /* @__PURE__ */ l(
x,
{
scope: o,
imageLoadingStatus: n,
onImageLoadingStatusChange: t,
children: /* @__PURE__ */ l(m.span, { ...r, ref: e })
}
);
}
);
L.displayName = v;
var S = "AvatarImage", E = i.forwardRef(
(a, e) => {
const { __scopeAvatar: o, src: r, onLoadingStatusChange: n = () => {
}, ...t } = a, u = A(S, o), s = N(r, t), d = h((c) => {
n(c), u.onImageLoadingStatusChange(c);
});
return f(() => {
s !== "idle" && d(s);
}, [s, d]), s === "loaded" ? /* @__PURE__ */ l(m.img, { ...t, ref: e, src: r }) : null;
}
);
E.displayName = S;
var I = "AvatarFallback", R = i.forwardRef(
(a, e) => {
const { __scopeAvatar: o, delayMs: r, ...n } = a, t = A(I, o), [u, s] = i.useState(r === void 0);
return i.useEffect(() => {
if (r !== void 0) {
const d = window.setTimeout(() => s(!0), r);
return () => window.clearTimeout(d);
}
}, [r]), u && t.imageLoadingStatus !== "loaded" ? /* @__PURE__ */ l(m.span, { ...n, ref: e }) : null;
}
);
R.displayName = I;
function p(a, e) {
return a ? e ? (a.src !== e && (a.src = e), a.complete && a.naturalWidth > 0 ? "loaded" : "loading") : "error" : "idle";
}
function N(a, { referrerPolicy: e, crossOrigin: o }) {
const r = _(), n = i.useRef(null), t = r ? (n.current || (n.current = new window.Image()), n.current) : null, [u, s] = i.useState(
() => p(t, a)
);
return f(() => {
s(p(t, a));
}, [t, a]), f(() => {
const d = (w) => () => {
s(w);
};
if (!t) return;
const c = d("loaded"), g = d("error");
return t.addEventListener("load", c), t.addEventListener("error", g), e && (t.referrerPolicy = e), typeof o == "string" && (t.crossOrigin = o), () => {
t.removeEventListener("load", c), t.removeEventListener("error", g);
};
}, [t, o, e]), u;
}
var H = L, j = E, B = R;
export {
L as Avatar,
R as AvatarFallback,
E as AvatarImage,
B as Fallback,
j as Image,
H as Root
};