UNPKG

@loke/ui

Version:
136 lines (133 loc) • 6.82 kB
var __create = Object.create; var { getPrototypeOf: __getProtoOf, defineProperty: __defProp, getOwnPropertyNames: __getOwnPropNames, getOwnPropertyDescriptor: __getOwnPropDesc } = Object, __hasOwnProp = Object.prototype.hasOwnProperty; function __accessProp(key) { return this[key]; } var __toESMCache_node, __toESMCache_esm, __toESM = (mod, isNodeMode, target) => { var canCache = mod != null && typeof mod === "object"; if (canCache) { var cache = isNodeMode ? __toESMCache_node ??= /* @__PURE__ */ new WeakMap : __toESMCache_esm ??= /* @__PURE__ */ new WeakMap, cached = cache.get(mod); if (cached) return cached; } target = mod != null ? __create(__getProtoOf(mod)) : {}; let to = isNodeMode || !mod || !mod.__esModule || !__hasOwnProp.call(mod, "default") ? __defProp(target, "default", { value: mod, enumerable: !0 }) : target; if (mod && typeof mod === "object" || typeof mod === "function") { for (let key of __getOwnPropNames(mod)) if (!__hasOwnProp.call(to, key)) __defProp(to, key, { get: __accessProp.bind(mod, key), enumerable: !0 }); } if (canCache) cache.set(mod, to); return to; }, __toCommonJS = (from) => { var entry = (__moduleCache ??= /* @__PURE__ */ new WeakMap).get(from), desc; if (entry) return entry; if (entry = __defProp({}, "__esModule", { value: !0 }), from && typeof from === "object" || typeof from === "function") { for (var key of __getOwnPropNames(from)) if (!__hasOwnProp.call(entry, key)) __defProp(entry, key, { get: __accessProp.bind(from, key), enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return __moduleCache.set(from, entry), entry; }, __moduleCache; var __returnValue = (v) => v; function __exportSetter(name, newValue) { this[name] = __returnValue.bind(null, newValue); } var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: !0, configurable: !0, set: __exportSetter.bind(all, name) }); }; // src/components/avatar/index.ts var exports_avatar = {}; __export(exports_avatar, { Avatar: () => Avatar, AvatarFallback: () => AvatarFallback, AvatarImage: () => AvatarImage, Fallback: () => Fallback, Image: () => Image, Root: () => Root, createAvatarScope: () => createAvatarScope }); module.exports = __toCommonJS(exports_avatar); // src/components/avatar/avatar.tsx var import_context = require("@loke/ui/context"), import_primitive = require("@loke/ui/primitive"), import_use_callback_ref = require("@loke/ui/use-callback-ref"), import_use_layout_effect = require("@loke/ui/use-layout-effect"), import_react = require("react"), jsx_runtime = require("react/jsx-runtime"), AVATAR_NAME = "Avatar", [createAvatarContext, createAvatarScope] = import_context.createContextScope(AVATAR_NAME), [AvatarProvider, useAvatarContext] = createAvatarContext(AVATAR_NAME), Avatar = /* @__PURE__ */ Object.assign(/* @__PURE__ */ import_react.forwardRef((props, forwardedRef) => { let { __scopeAvatar, ...avatarProps } = props, [imageLoadingStatus, setImageLoadingStatus] = import_react.useState("idle"); return /* @__PURE__ */ jsx_runtime.jsx(AvatarProvider, { imageLoadingStatus, scope: __scopeAvatar, setImageLoadingStatus, children: /* @__PURE__ */ jsx_runtime.jsx(import_primitive.Primitive.span, { ...avatarProps, ref: forwardedRef }) }); }), { displayName: AVATAR_NAME }), IMAGE_NAME = "AvatarImage", AvatarImage = /* @__PURE__ */ Object.assign(/* @__PURE__ */ import_react.forwardRef((props, forwardedRef) => { let { __scopeAvatar, src, onLoadingStatusChange, ...imageProps } = props, context = useAvatarContext(IMAGE_NAME, __scopeAvatar), imageLoadingStatus = useImageLoadingStatus(src, { crossOrigin: imageProps.crossOrigin, loadingStatus: context.imageLoadingStatus, referrerPolicy: imageProps.referrerPolicy, setLoadingStatus: context.setImageLoadingStatus }), handleLoadingStatusChange = import_use_callback_ref.useCallbackRef((status) => { onLoadingStatusChange?.(status); }), loadingStatusRef = import_react.useRef(imageLoadingStatus); return import_use_layout_effect.useLayoutEffect(() => { let previousLoadingStatus = loadingStatusRef.current; if (loadingStatusRef.current = imageLoadingStatus, imageLoadingStatus !== previousLoadingStatus) handleLoadingStatusChange(imageLoadingStatus); }, [imageLoadingStatus, handleLoadingStatusChange]), imageLoadingStatus === "loaded" ? /* @__PURE__ */ jsx_runtime.jsx(import_primitive.Primitive.img, { ...imageProps, ref: forwardedRef, src }) : null; }), { displayName: IMAGE_NAME }), FALLBACK_NAME = "AvatarFallback", AvatarFallback = /* @__PURE__ */ Object.assign(/* @__PURE__ */ import_react.forwardRef((props, forwardedRef) => { let { __scopeAvatar, delayMs, ...fallbackProps } = props, context = useAvatarContext(FALLBACK_NAME, __scopeAvatar), [canRender, setCanRender] = import_react.useState(delayMs === void 0); return import_react.useEffect(() => { if (delayMs !== void 0) { let timerId = window.setTimeout(() => setCanRender(!0), delayMs); return () => window.clearTimeout(timerId); } }, [delayMs]), canRender && context.imageLoadingStatus !== "loaded" ? /* @__PURE__ */ jsx_runtime.jsx(import_primitive.Primitive.span, { ...fallbackProps, ref: forwardedRef }) : null; }), { displayName: FALLBACK_NAME }); function useImageLoadingStatus(src, options) { let { crossOrigin, loadingStatus, referrerPolicy, setLoadingStatus } = options; return import_use_layout_effect.useLayoutEffect(() => { if (!src) { setLoadingStatus("error"); return; } let image = new window.Image, handleLoad = (event) => { if (isImageLoadState(event.currentTarget)) setLoadingStatus(getImageLoadingStatus(event.currentTarget)); }, handleError = () => setLoadingStatus("error"); if (image.addEventListener("load", handleLoad), image.addEventListener("error", handleError), referrerPolicy) image.referrerPolicy = referrerPolicy; return image.crossOrigin = crossOrigin ?? null, image.src = src, setLoadingStatus(getImageLoadingStatus(image)), () => { image.removeEventListener("load", handleLoad), image.removeEventListener("error", handleError), setLoadingStatus("idle"); }; }, [src, crossOrigin, referrerPolicy, setLoadingStatus]), loadingStatus; } function isImageLoadState(value) { return value !== null && "complete" in value && "naturalWidth" in value; } function getImageLoadingStatus(image) { if (!image.complete) return "loading"; return image.naturalWidth > 0 ? "loaded" : "error"; } var Root = Avatar, Image = AvatarImage, Fallback = AvatarFallback;