1
1
import React from "react" ;
2
- import { ImageProps } from "next/image" ;
2
+ import Image , { ImageProps , StaticImageData } from "next/image" ;
3
3
import styled , { css } from "styled-components" ;
4
4
5
5
interface FallbackImgImageProps extends ImageProps {
6
- fallbackSrc : string ;
6
+ fallbackSrc : string | StaticImageData ;
7
7
alt : string ;
8
8
sizes ?: string ;
9
9
rounded ?: boolean ;
@@ -20,19 +20,24 @@ const FallbackImgImage = ({
20
20
rounded,
21
21
...props
22
22
} : FallbackImgImageProps ) => {
23
- const [ imgSrc , setImgSrc ] = React . useState < string > ( "" ) ;
23
+ const [ isError , setIsError ] = React . useState ( false ) ;
24
24
25
- React . useEffect ( ( ) => {
26
- setImgSrc ( src ) ;
27
- } , [ src ] ) ;
28
-
29
- return (
25
+ return isError ? (
30
26
< StyledImage
31
- src = { imgSrc }
27
+ src = { src }
28
+ { ...props }
29
+ alt = { alt }
30
+ isshouldhide = { isShouldHide }
31
+ onError = { ( ) => setIsError ( true ) }
32
+ isrounded = { rounded ?. toString ( ) }
33
+ />
34
+ ) : (
35
+ < StyledFallbackImage
36
+ src = { fallbackSrc }
32
37
{ ...props }
33
38
alt = { alt }
34
39
isshouldhide = { isShouldHide }
35
- onError = { ( ) => setImgSrc ( fallbackSrc ) }
40
+ onError = { ( ) => setIsError ( true ) }
36
41
isrounded = { rounded ?. toString ( ) }
37
42
/>
38
43
) ;
@@ -55,4 +60,21 @@ const StyledImage = styled.img<{
55
60
` }
56
61
` ;
57
62
63
+ const StyledFallbackImage = styled ( Image ) < {
64
+ isshouldhide ?: boolean ;
65
+ isrounded ?: string ;
66
+ } > `
67
+ height: auto;
68
+ ${ ( { isshouldhide } ) =>
69
+ isshouldhide &&
70
+ css `
71
+ display : none;
72
+ ` }
73
+ ${ ( { isrounded } ) =>
74
+ isrounded === "true" &&
75
+ css `
76
+ border-radius : 50% ;
77
+ ` }
78
+ ` ;
79
+
58
80
export default FallbackImgImage ;
0 commit comments