Skip to content

Commit

Permalink
Update image loading state handler
Browse files Browse the repository at this point in the history
  • Loading branch information
irmantastam committed Jan 24, 2024
1 parent 18d86be commit 6d56dcb
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/components/features/contentful/CtfImage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ interface ImageProps extends Omit<ImageFieldsFragment, '__typename'> {

export const CtfImage = ({ url, width, height, title, nextImageProps, blurHash }: ImageProps) => {
const [isLoading, setIsLoading] = useState(false);
const [isLoaded, setIsLoaded] = useState(false);

useEffect(() => {
setIsLoading(true);
}, []);
setIsLoading(!isLoaded);
}, [isLoaded]);

if (!url || !width || !height) return null;

Expand All @@ -24,11 +25,14 @@ export const CtfImage = ({ url, width, height, title, nextImageProps, blurHash }
width={width}
height={height}
alt={title || ''}
title={title || ''}
placeholder="blur"
blurDataURL={blurHash}
{...nextImageProps}
className={twMerge(nextImageProps?.className, `${isLoading ? 'blur-md ' : ''}transition-all`)}
onLoad={() => setIsLoading(false)}
onLoad={() => {
setIsLoaded(true);
}}
priority
/>
);
Expand Down

0 comments on commit 6d56dcb

Please sign in to comment.