1

I've got a component that opens and closes a Material UI Dialog. At the moment, there is text that is displayed on the page followed by a button that simply opens the dialog. I've created a code sandbox to highlight where I am with this: https://codesandbox.io/s/formdialog-material-demo-forked-cgjgh?file=/demo.js:0-8775

When the dialog is open, is it possible to enable scrolling in the background (i.e. on the main page) so that the user can scroll through the text as an example? I've included the hideBackdrop prop to be true so that the backdrop is hidden, but I'm not sure how to to do it so the scrolling on the main page can be enabled.

3
  • Did you figure out anything, Is there any answer for this? Commented Mar 30, 2022 at 18:43
  • Unfortunately I did not find a solution for this. Commented Mar 31, 2022 at 11:16
  • 1
    @ParvathirajanNatarajan the solution provided by rakram works. Commented Oct 12, 2022 at 8:41

3 Answers 3

5

For me setting "disableScrollLock" property to true solved the problem. I found it under Material UI Modal documentation: https://mui.com/material-ui/api/modal/

<Dialog open={open} 
        onClose={handleClose} 
        hideBackdrop={true}
        disableScrollLock
  >

I tested it in your CodeSandbox example and it worked as it was supposed to. Hope this helps with your problem!

Sign up to request clarification or add additional context in comments.

Comments

1

Please disable overflow:hidden style property of body tag when opening dialog.

2 Comments

Could you give me an example of what you mean?
In global css, please add body{ overflow: auto !important; }
1

you can mange scrolling by using React Hook useEffect.

 const [anchorEl, setAnchorEl] = React.useState(null);

  const handleClick = (event, item) => {
    setAnchorEl(event.currentTarget);
  };

  const open = Boolean(anchorEl);

Now whenever a click will happen "open" will be true OR you might be using some other state method to open the Material UI Dialog by managing state.

So here is the useEffect Method

 useEffect(() => {
    if (open) {
      document.body.style.overflow = "hidden";
    } else {
      document.body.style.overflow = "scroll";
    }
  }, [open]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.