Skip to content

Instantly share code, notes, and snippets.

@jongacnik
Created August 26, 2022 23:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jongacnik/a702e086d7e4a8ec9985f05262f2599f to your computer and use it in GitHub Desktop.
Save jongacnik/a702e086d7e4a8ec9985f05262f2599f to your computer and use it in GitHub Desktop.
Next/Image Fade In
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