diff --git a/src/components/ImageBlurFallback.tsx b/src/components/ImageBlurFallback.tsx index 3f05275b..83292b9b 100644 --- a/src/components/ImageBlurFallback.tsx +++ b/src/components/ImageBlurFallback.tsx @@ -14,76 +14,68 @@ export default function ImageBlurFallback(props: ImageProps) { ...rest } = props; - const [isLoading, setIsLoading] = useState(false); - const [didLoad, setDidLoad] = useState(false); - const [hideBlurPlaceholder, setHideBlurPlaceholder] = useState(false); + const [wasCached, setWasCached] = useState(true); + const [isLoading, setIsLoading] = useState(true); + const [didError, setDidError] = useState(false); + + const [hideBluePlaceholder, setHideBluePlaceholder] = useState(false); const imageClassName = 'object-cover h-full'; const imgRef = useRef(null); useEffect(() => { - const timeout = setTimeout(() => { - // Check if image has already loaded/is cached - // in order to not show blur placeholder on every load - setIsLoading(imgRef.current?.complete !== true); - setDidLoad(imgRef.current?.complete === true); - }, 100); + const timeout = setTimeout( + () => setWasCached(imgRef.current?.complete ?? false), + 100, + ); return () => clearTimeout(timeout); }, []); useEffect(() => { - if (didLoad) { - const timeout = setTimeout(() => - // Hide blurred placeholder after image has faded in after 300ms - setHideBlurPlaceholder(true) - , 500); + if (!isLoading && !didError) { + const timeout = setTimeout(() => { + setHideBluePlaceholder(true); + }, 1000); return () => clearTimeout(timeout); } - }, [didLoad]); + }, [isLoading, didError]); const showPlaceholder = BLUR_ENABLED && props.blurDataURL && - !hideBlurPlaceholder; + !wasCached && + !hideBluePlaceholder; return (
- { - setIsLoading(false); - setDidLoad(true); - }, - onError: () => { - setIsLoading(false); - }, - }} /> {showPlaceholder && } + setIsLoading(false), + onError: () => setDidError(true), + }} />
); }