Created
August 26, 2022 23:08
-
-
Save jongacnik/a702e086d7e4a8ec9985f05262f2599f to your computer and use it in GitHub Desktop.
Next/Image Fade In
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
import { useState } from "react"; | |
import Image from "next/image"; | |
export default function FadeInImage({ ...props }) { | |
const [isLoaded, setIsLoaded] = useState(false); | |
const { className, src, alt, priority, layout, objectFit, width, height } = | |
props; | |
function onLoadingComplete() { | |
setIsLoaded(true); | |
} | |
return ( | |
<Image | |
className={`transition-opacity ${isLoaded ? "" : "opacity-0"} ${className}`} | |
onLoadingComplete={onLoadingComplete} | |
alt={alt || ""} | |
src={src} | |
priority={priority} | |
layout={layout} | |
objectFit={objectFit} | |
width={width} | |
height={height} | |
/> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment