Add additional image fallback loading checks
This commit is contained in:
parent
25533f088c
commit
f2db4cf4d0
@ -20,6 +20,8 @@ export default function ImageWithFallback({
|
|||||||
classNameImage?: string
|
classNameImage?: string
|
||||||
forceFallbackFade?: boolean
|
forceFallbackFade?: boolean
|
||||||
}) {
|
}) {
|
||||||
|
const ref = useRef<HTMLImageElement>(null);
|
||||||
|
|
||||||
const { shouldDebugImageFallbacks } = useAppState();
|
const { shouldDebugImageFallbacks } = useAppState();
|
||||||
|
|
||||||
const [isLoading, setIsLoading] = useState(true);
|
const [isLoading, setIsLoading] = useState(true);
|
||||||
@ -30,15 +32,16 @@ export default function ImageWithFallback({
|
|||||||
const onLoad = useCallback(() => setIsLoading(false), []);
|
const onLoad = useCallback(() => setIsLoading(false), []);
|
||||||
const onError = useCallback(() => setDidError(true), []);
|
const onError = useCallback(() => setDidError(true), []);
|
||||||
|
|
||||||
const isLoadingRef = useRef(isLoading);
|
|
||||||
useEffect(() => { isLoadingRef.current = isLoading; }, [isLoading]);
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const timeout = setTimeout(() => {
|
const timeout = setTimeout(() => {
|
||||||
// If image is still loading after 100ms, force CSS animation
|
// If image is still loading after 50ms, force CSS animation
|
||||||
if (isLoadingRef.current) {
|
if (
|
||||||
|
!ref.current?.complete ||
|
||||||
|
(ref.current?.naturalWidth ?? 0) === 0
|
||||||
|
) {
|
||||||
setFadeFallbackTransition(true);
|
setFadeFallbackTransition(true);
|
||||||
}
|
}
|
||||||
}, 100);
|
}, 50);
|
||||||
return () => clearTimeout(timeout);
|
return () => clearTimeout(timeout);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
@ -59,7 +62,7 @@ export default function ImageWithFallback({
|
|||||||
className,
|
className,
|
||||||
)}
|
)}
|
||||||
>
|
>
|
||||||
<Image {...{
|
<Image ref={ref} {...{
|
||||||
...props,
|
...props,
|
||||||
priority,
|
priority,
|
||||||
className: classNameImage,
|
className: classNameImage,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user