Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does anybody use with next/image? it doesn't work. #84

Open
nwatab opened this issue Mar 31, 2022 · 4 comments
Open

Does anybody use with next/image? it doesn't work. #84

nwatab opened this issue Mar 31, 2022 · 4 comments

Comments

@nwatab
Copy link

nwatab commented Mar 31, 2022

All images are shown in full screen and overlaid.

import Image from 'next/image';
import Masonry from 'react-masonry-css'

<Masonry>
{
  imgs.map(img => (
    <Image
      key={img.id}
      src={img.src}
      layout='fill'
    />
  ))
}
</Masonry>
@ivenuss
Copy link

ivenuss commented May 6, 2022

Next.js fill layout requires relative position parent. But this won't kinda resolve the issue.

@SanjivG10
Copy link

I don't think it works with NextJS image as NextJS Image is kind of fixed and optimized by NextJS itself. I always prefer to use plain old tag when I use react-masonry.

@nwatab
Copy link
Author

nwatab commented Jun 27, 2022

Hi. This works.

index.tsx

import React from 'react'
import Masonry, { MasonryProps } from 'react-masonry-css'
import styles from "./index.module.css"

const breakpointColumnsObj = {
  default: 4,
  1024: 3,
  768: 2,
  // 640: 1
};

type MasonryGridProps = {
  breakpointCols?: MasonryProps['breakpointCols']
}

export const MasonryGrid: React.FC<MasonryGridProps> = ({children, breakpointCols=breakpointColumnsObj}) => (
  <Masonry
    breakpointCols={breakpointCols}
    className={styles.container}
    columnClassName={styles.column}
  >
    {children}
  </Masonry>
)

index.module.css

.container {
  display: -webkit-box; /* Not needed if autoprefixing */
  display: -ms-flexbox; /* Not needed if autoprefixing */
  display: flex;
  margin-left: -1px; /* gutter size offset */
  width: auto;
}
.column {
  padding-left: 1px; /* gutter size */
  background-clip: padding-box;
}

.column > span { /* change div to reference your elements you put in <Masonry> */
  background: grey;
  margin-bottom: 1px;
}

I'm using it with next/Image

import Image from 'next/image'
...
  <MasonryGrid>
    {images.map((img, index) => (
        <Image
          src={img.url}
          width={640}
          height={640 * img.height / img.width}
          loader={replaceWithYourLoader}
        />
    ))}
  </MasonryGrid>

@mbritton
Copy link

mbritton commented Sep 3, 2023

"use client";

import styles from "@/styles/slices/ImageGallery/ImageGallery.module.scss";
import Image from "next/image";
import Masonry from "react-masonry-css";
import ImageOverlay from "@/components/ImageOverlay";

/**
 * @typedef {import("@prismicio/client").Content.ImageGallerySlice} ImageGallerySlice
 * @typedef {import("@prismicio/react").SliceComponentProps<ImageGallerySlice>} ImageGalleryProps
 * @param {ImageGalleryProps}
 */
const ImageGallery = ({ slice }) => {
  const imageChildren = slice.items.map((item) => {
    return (
      <div
        key={`${item + item.stair_photo.url}`}
        className={styles.imageGalleryItem}
      >
        <Image fill alt={item.stair_photo.alt} src={item.stair_photo.url} />
      </div>
    );
  });

  return (
    <div>
      <ImageOverlay imageProps={slice} />
      <div className={styles.container}>
        <div className={styles.sectionTitle}>
          {slice.primary.section_title[0].text}
        </div>
        <Masonry
          breakpointCols={3}
          className={styles.myMasonryGrid}
          columnClassName={styles.myMasonryGridColumn}
        >
          {imageChildren}
        </Masonry>
      </div>
    </div>
  );
};

export default ImageGallery;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants