-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from nnivxix/fix/loading-image
Fix/loading image
- Loading branch information
Showing
7 changed files
with
79 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import type { ImgHTMLAttributes, SyntheticEvent } from "react"; | ||
import type { TypeImage } from "@/hooks/useImageFallback"; | ||
import useImageFallback from "@/hooks/useImageFallback"; | ||
|
||
interface ImageProps extends ImgHTMLAttributes<HTMLImageElement> { | ||
src: string; | ||
alt: string; | ||
type: TypeImage; | ||
} | ||
export default function Image({ src, alt, type, ...props }: ImageProps) { | ||
const { fallback } = useImageFallback(type); | ||
const imageFallback = (event: SyntheticEvent<HTMLImageElement, Event>) => { | ||
event.currentTarget.src = fallback; | ||
}; | ||
const imageLoaded = (event: SyntheticEvent<HTMLImageElement, Event>) => { | ||
event.currentTarget.src = src; | ||
}; | ||
|
||
return ( | ||
<img | ||
{...props} | ||
src={fallback} | ||
alt={alt} | ||
onError={imageFallback} | ||
onLoad={imageLoaded} | ||
/> | ||
); | ||
} |
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,26 @@ | ||
import { useEffect, useState } from "react"; | ||
|
||
export type TypeImage = "poster" | "backdrop"; | ||
|
||
const useImageFallback = (type: TypeImage) => { | ||
const [fallback, setFallback] = useState<string>(""); | ||
|
||
useEffect(() => { | ||
switch (type) { | ||
case "poster": | ||
setFallback("/poster-fallback.png"); | ||
break; | ||
case "backdrop": | ||
setFallback("/backdrop-fallback.png"); | ||
break; | ||
default: | ||
setFallback("/poster-fallback.png"); | ||
} | ||
}, [setFallback, type]); | ||
|
||
return { | ||
fallback, | ||
}; | ||
}; | ||
|
||
export default useImageFallback; |
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