61 lines
1.7 KiB
JavaScript
61 lines
1.7 KiB
JavaScript
// src/index.ts
|
|
import { useCallback, useEffect, useRef, useState } from "react";
|
|
import { useSafeLayoutEffect } from "@nextui-org/use-safe-layout-effect";
|
|
function useImage(props = {}) {
|
|
const { loading, src, srcSet, onLoad, onError, crossOrigin, sizes, ignoreFallback } = props;
|
|
const [status, setStatus] = useState("pending");
|
|
useEffect(() => {
|
|
setStatus(src ? "loading" : "pending");
|
|
}, [src]);
|
|
const imageRef = useRef();
|
|
const load = 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;
|
|
}
|
|
};
|
|
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";
|
|
export {
|
|
shouldShowFallbackImage,
|
|
useImage
|
|
};
|