97 lines
2.9 KiB
JavaScript
97 lines
2.9 KiB
JavaScript
'use client'
|
|
"use strict";
|
|
var __defProp = Object.defineProperty;
|
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
var __export = (target, all) => {
|
|
for (var name in all)
|
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
};
|
|
var __copyProps = (to, from, except, desc) => {
|
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
for (let key of __getOwnPropNames(from))
|
|
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
}
|
|
return to;
|
|
};
|
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
|
|
// src/use-image.ts
|
|
var use_image_exports = {};
|
|
__export(use_image_exports, {
|
|
shouldShowFallbackImage: () => shouldShowFallbackImage,
|
|
useImage: () => useImage
|
|
});
|
|
module.exports = __toCommonJS(use_image_exports);
|
|
var import_react_use_safe_layout_effect = require("@chakra-ui/react-use-safe-layout-effect");
|
|
var import_react = require("react");
|
|
function useImage(props) {
|
|
const {
|
|
loading,
|
|
src,
|
|
srcSet,
|
|
onLoad,
|
|
onError,
|
|
crossOrigin,
|
|
sizes,
|
|
ignoreFallback
|
|
} = props;
|
|
const [status, setStatus] = (0, import_react.useState)("pending");
|
|
(0, import_react.useEffect)(() => {
|
|
setStatus(src ? "loading" : "pending");
|
|
}, [src]);
|
|
const imageRef = (0, import_react.useRef)();
|
|
const load = (0, import_react.useCallback)(() => {
|
|
if (!src)
|
|
return;
|
|
flush();
|
|
const img = new Image();
|
|
img.src = src;
|
|
if (crossOrigin)
|
|
img.crossOrigin = crossOrigin;
|
|
if (srcSet)
|
|
img.srcset = srcSet;
|
|
if (sizes)
|
|
img.sizes = sizes;
|
|
if (loading)
|
|
img.loading = loading;
|
|
img.onload = (event) => {
|
|
flush();
|
|
setStatus("loaded");
|
|
onLoad == null ? void 0 : onLoad(event);
|
|
};
|
|
img.onerror = (error) => {
|
|
flush();
|
|
setStatus("failed");
|
|
onError == null ? void 0 : onError(error);
|
|
};
|
|
imageRef.current = img;
|
|
}, [src, crossOrigin, srcSet, sizes, onLoad, onError, loading]);
|
|
const flush = () => {
|
|
if (imageRef.current) {
|
|
imageRef.current.onload = null;
|
|
imageRef.current.onerror = null;
|
|
imageRef.current = null;
|
|
}
|
|
};
|
|
(0, import_react_use_safe_layout_effect.useSafeLayoutEffect)(() => {
|
|
if (ignoreFallback)
|
|
return void 0;
|
|
if (status === "loading") {
|
|
load();
|
|
}
|
|
return () => {
|
|
flush();
|
|
};
|
|
}, [status, load, ignoreFallback]);
|
|
return ignoreFallback ? "loaded" : status;
|
|
}
|
|
var shouldShowFallbackImage = (status, fallbackStrategy) => status !== "loaded" && fallbackStrategy === "beforeLoadOrError" || status === "failed" && fallbackStrategy === "onError";
|
|
// Annotate the CommonJS export names for ESM import in node:
|
|
0 && (module.exports = {
|
|
shouldShowFallbackImage,
|
|
useImage
|
|
});
|
|
//# sourceMappingURL=use-image.js.map
|