import * as React from 'react';
import Box from '@mui/material/Box';
import ImageList from '@mui/material/ImageList';
import ImageListItem from '@mui/material/ImageListItem';
import ImageListItemBar from '@mui/material/ImageListItemBar';
import IconButton from '@mui/material/IconButton';
import VisibilityIcon from '@mui/icons-material/Visibility';
import Dialog from '@mui/material/Dialog';
export default function MasonryImageList({ data }) {
const itemData = data.map((image) => {
return {
img: image.url,
title: image.titre
}
})
return (
<Box id='Mansonry'>
<ImageList variant="masonry" cols={3} gap={8}>
{itemData.map((item, index) => (
<>
<ImageListItem key={item.img}>
<img
srcSet={`${item.img}?w=248&fit=crop&auto=format&dpr=2 2x`}
src={`${item.img}?w=248&fit=crop&auto=format`}
alt={item.title}
loading="lazy"
/>
<ImageListItemBar
title={item.title}
actionIcon={
<IconButton
sx={{ color: 'rgba(255, 255, 255, 0.54)' }}
aria-label={`Agrandir ${item.title}`}
onClick={() => {
**//When clicked, display the image on the MUI Dialog**
}}
>
<VisibilityIcon />
</IconButton>
}
/>
</ImageListItem>
<Dialog onClose={handleClose} open={open}> **//How should i handle the opening and closing of each Dialog for each Image ?**
<img src={`${item.img}`} alt={item.title}
loading="lazy" />
</Dialog>
</>
))}
</ImageList>
</Box>
);
}
The idea is to display the image on front of the screen when the IconButton is clicked. But as I have multiple images, I'm struggling with the onClose and open props of the Dialog component.
You could add a
selectedItemstate that is eithernullto hide the dialogs, or a specificitemto display that item's dialog. ThehandleClosewould reset theselectedItemback tonull.Example:
You could also refactor the code a bit to render only a single
Dialog.Example: