Make sure images load before hiding fallback

This commit is contained in:
Sam Becker 2025-04-27 15:55:30 -05:00
parent a8378fada9
commit 7c6e8371b8

View File

@ -24,7 +24,17 @@ export default function ImageWithFallback({
const [isLoading, setIsLoading] = useState(true);
const [didError, setDidError] = useState(false);
const onLoad = useCallback(() => setIsLoading(false), []);
const onLoad = useCallback(() => {
// Prevent blank flash by waiting for image to load
if (
imgRef.current?.complete &&
(imgRef.current?.naturalWidth ?? 0) > 0
) {
setIsLoading(false);
} else {
setTimeout(() => setIsLoading(false), 100);
}
}, []);
const onError = useCallback(() => setDidError(true), []);
const [hideFallback, setHideFallback] = useState(false);