-
-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: PreviewGroup support fallback (#266)
* feat: PreviewGroup support fallback * fix: optimize code
- Loading branch information
Showing
6 changed files
with
122 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { useEffect, useRef, useState } from 'react'; | ||
import { isImageValid } from '../util'; | ||
|
||
type ImageStatus = 'normal' | 'error' | 'loading'; | ||
|
||
export default function useStatus({ | ||
src, | ||
isCustomPlaceholder, | ||
fallback, | ||
}: { | ||
src: string; | ||
isCustomPlaceholder?: boolean; | ||
fallback?: string; | ||
}) { | ||
const [status, setStatus] = useState<ImageStatus>(isCustomPlaceholder ? 'loading' : 'normal'); | ||
const isLoaded = useRef(false); | ||
|
||
const isError = status === 'error'; | ||
|
||
// https://github.com/react-component/image/pull/187 | ||
useEffect(() => { | ||
isImageValid(src).then(isValid => { | ||
if (!isValid) { | ||
setStatus('error'); | ||
} | ||
}); | ||
}, [src]); | ||
|
||
useEffect(() => { | ||
if (isCustomPlaceholder && !isLoaded.current) { | ||
setStatus('loading'); | ||
} else if (isError) { | ||
setStatus('normal'); | ||
} | ||
}, [src]); | ||
|
||
const onLoad = () => { | ||
setStatus('normal'); | ||
}; | ||
|
||
const getImgRef = (img?: HTMLImageElement) => { | ||
isLoaded.current = false; | ||
if (status !== 'loading') { | ||
return; | ||
} | ||
if (img?.complete && (img.naturalWidth || img.naturalHeight)) { | ||
isLoaded.current = true; | ||
onLoad(); | ||
} | ||
}; | ||
|
||
const srcAndOnload = isError && fallback ? { src: fallback } : { onLoad, src }; | ||
|
||
return [getImgRef, srcAndOnload, status] as const; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1bb2309
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
image – ./
image-react-component.vercel.app
image-git-master-react-component.vercel.app
image-sepia.vercel.app