Skip to content

Commit

Permalink
fix image
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubinquitous committed Mar 5, 2024
1 parent 75c18d1 commit a1c85df
Showing 1 changed file with 32 additions and 10 deletions.
42 changes: 32 additions & 10 deletions src/components/atoms/FallbackImgImage.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import React from "react";
import { ImageProps } from "next/image";
import Image, { ImageProps, StaticImageData } from "next/image";
import styled, { css } from "styled-components";

interface FallbackImgImageProps extends ImageProps {
fallbackSrc: string;
fallbackSrc: string | StaticImageData;
alt: string;
sizes?: string;
rounded?: boolean;
Expand All @@ -20,19 +20,24 @@ const FallbackImgImage = ({
rounded,
...props
}: FallbackImgImageProps) => {
const [imgSrc, setImgSrc] = React.useState<string>("");
const [isError, setIsError] = React.useState(false);

React.useEffect(() => {
setImgSrc(src);
}, [src]);

return (
return isError ? (
<StyledImage
src={imgSrc}
src={src}
{...props}
alt={alt}
isshouldhide={isShouldHide}
onError={() => setIsError(true)}
isrounded={rounded?.toString()}
/>
) : (
<StyledFallbackImage
src={fallbackSrc}
{...props}
alt={alt}
isshouldhide={isShouldHide}
onError={() => setImgSrc(fallbackSrc)}
onError={() => setIsError(true)}
isrounded={rounded?.toString()}
/>
);
Expand All @@ -55,4 +60,21 @@ const StyledImage = styled.img<{
`}
`;

const StyledFallbackImage = styled(Image)<{
isshouldhide?: boolean;
isrounded?: string;
}>`
height: auto;
${({ isshouldhide }) =>
isshouldhide &&
css`
display: none;
`}
${({ isrounded }) =>
isrounded === "true" &&
css`
border-radius: 50%;
`}
`;

export default FallbackImgImage;

0 comments on commit a1c85df

Please sign in to comment.