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

[Modal or Dialog]: Modal Background Overlap When Used in map() Without Unique id #44457

Open
Bittukr7479 opened this issue Nov 18, 2024 · 2 comments
Labels
component: modal This is the name of the generic UI component, not the React module! package: material-ui Specific to @mui/material status: waiting for maintainer These issues haven't been looked at yet by a maintainer

Comments

@Bittukr7479
Copy link

Bittukr7479 commented Nov 18, 2024

Steps to reproduce

Screenshot 2024-11-18 171430

Current behavior

When using the Modal component inside a map() function without unique state or IDs:

  1. Clicking the "Open Modal" button for any item in the list opens all modals in the map() simultaneously.

  2. The Modal's backdrop (BackdropComponent) becomes darker or fully black, with each modal stacking a new backdrop.

  3. This occurs regardless of whether only one modal is intended to open.

Expected behavior

  1. Only the modal for the clicked item should open.
  2. The BackdropComponent should appear once and not stack or become overly dark, even when multiple modals are rendered.
  3. There should be no visual overlap or stacking issues with the modal's backdrop.

Context

I am trying to render multiple modals dynamically using a map() function for a list of items. Each modal should work independently. However, the issue arises when:

  1. The same state or ID is used for all modals.
  2. No unique identifier is provided for the Modal.

Your environment

npx @mui/envinfo
  Don't forget to mention which browser you used.
  Output from `npx @mui/envinfo` goes here.

Search keywords: Dialog Modal

@Bittukr7479 Bittukr7479 added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Nov 18, 2024
@sai6855
Copy link
Contributor

sai6855 commented Nov 18, 2024

Please provide a github or sandbox link that shows the problem, the description are very unclear

@sai6855 sai6855 added status: waiting for author Issue with insufficient information component: modal This is the name of the generic UI component, not the React module! package: material-ui Specific to @mui/material and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Nov 18, 2024
@Bittukr7479
Copy link
Author

Bittukr7479 commented Nov 18, 2024

Exapmple code without UniqueID (issue):

import React, { useState } from 'react';
import { Box, Button, Modal } from '@mui/material';

const ModalIssueWithoutUniqueID = () => {
  const items = [1, 2, 3]; // Example data
  const [open, setOpen] = useState(false);

  const handleOpen = () => setOpen(true);
  const handleClose = () => setOpen(false);

  return (
    <div>
      {items.map((item) => (
        <div key={item}>
          <Button onClick={handleOpen}>Open Modal {item}</Button>
          <Modal
            open={open}
            onClose={handleClose}
            aria-labelledby="modal-title"
            aria-describedby="modal-description"
          >
            <Box
              sx={{
                position: 'absolute',
                top: '50%',
                left: '50%',
                transform: 'translate(-50%, -50%)',
                width: 300,
                bgcolor: 'background.paper',
                boxShadow: 24,
                p: 4,
              }}
            >
              <h2 id="modal-title">Modal {item}</h2>
              <p id="modal-description">
                This is the content of modal {item}.
              </p>
            </Box>
          </Modal>
        </div>
      ))}
    </div>
  );
};


export default ModalIssueWithoutUniqueID;

Exapmple code with UniqueID (alternative solution):

import React, { useState } from 'react';
import { Box, Button, Modal } from '@mui/material';

const ModalIssueExample = () => {
  const items = [1, 2, 3]; // Example data
  const [openStates, setOpenStates] = useState({});

  const handleOpen = (id) => {
    setOpenStates((prev) => ({ ...prev, [id]: true }));
  };

  const handleClose = (id) => {
    setOpenStates((prev) => ({ ...prev, [id]: false }));
  };

  return (
    <div>
      {items.map((item) => (
        <div key={item}>
          <Button onClick={() => handleOpen(item)}>Open Modal {item}</Button>
          <Modal
            open={!!openStates[item]}
            onClose={() => handleClose(item)}
            aria-labelledby={`modal-title-${item}`}
            aria-describedby={`modal-description-${item}`}
          >
            <Box
              sx={{
                position: 'absolute',
                top: '50%',
                left: '50%',
                transform: 'translate(-50%, -50%)',
                width: 300,
                bgcolor: 'background.paper',
                boxShadow: 24,
                p: 4,
              }}
            >
              <h2 id={`modal-title-${item}`}>Modal {item}</h2>
              <p id={`modal-description-${item}`}>
                This is the content of modal {item}.
              </p>
            </Box>
          </Modal>
        </div>
      ))}
    </div>
  );
};

export default ModalIssueExample;

@github-actions github-actions bot added status: waiting for maintainer These issues haven't been looked at yet by a maintainer and removed status: waiting for author Issue with insufficient information labels Nov 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: modal This is the name of the generic UI component, not the React module! package: material-ui Specific to @mui/material status: waiting for maintainer These issues haven't been looked at yet by a maintainer
Projects
None yet
Development

No branches or pull requests

2 participants