Skip to content

Instantly share code, notes, and snippets.

@zachgoll
Created October 29, 2022 19:04
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 zachgoll/7a9fa440f68658791b9767c369685675 to your computer and use it in GitHub Desktop.
Save zachgoll/7a9fa440f68658791b9767c369685675 to your computer and use it in GitHub Desktop.
Next.js Image Gallery
import type { NextPage } from 'next';
import Image from 'next/future/image';
import Masonry from 'react-masonry-css';
type ImageEnhanced = {
src: string;
alt: string;
width: number;
height: number;
};
type ImageGalleryPageProps = {
images: ImageEnhanced[];
};
export const getStaticProps: GetStaticProps<
ImageGalleryPageProps
> = async () => {
return {
props: {
images: [
{
src: 'https://some-cdn.someurl.com/image-1.png',
alt: 'test image 1',
width: 1000,
height: 800,
},
{
src: 'https://some-cdn.someurl.com/image-2.png',
alt: 'test image 2',
width: 1000,
height: 800,
},
],
},
};
};
// NOTE: I'm using TailwindCSS here
const ImageGalleryPage: NextPage<ImageGalleryPageProps> = ({ images }) => {
return (
<Masonry>
{images.map((image) => (
<Image
key={image.src}
className="hover:opacity-80 cursor-pointer my-2"
src={image.src}
alt={image.alt}
width={image.width}
height={image.height}
/>
))}
</Masonry>
);
};
export default ImageGalleryPage;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment