无法在 Material UI(MUI)选择菜单打开时与屏幕上的任何内容进行交互。

huangapple go评论142阅读模式
英文:

Can't interact with anything on screen while Material UI (MUI) Select Menu is open

问题

I noticed that while a Select component's menu is open, I cannot interact with anything else on the screen. I can't click on other objects, hover events don't trigger, and I can't even scroll until the menu is closed.

Material UI似乎在打开Select菜单时会出现一个不可见的背景,强制用户在与其他内容交互之前关闭菜单。但是,我正在尝试实现一种功能,即在下拉菜单打开时用户仍然可以与应用的其余部分交互(就像Uber网站的主页面中的“公司”下拉菜单一样,您可以在打开时执行其他操作)。

是否有一种方法可以禁用或绕过这个不可见的背景? 我尝试将其他所有内容的z-index设置为高于背景的z-index,但这是一种不稳定的方法,而且不总是有效。

代码/演示: https://codesandbox.io/s/practical-ully-fsfgpj

如果有一种稳健的方法来解决这个问题,那将非常棒。

英文:

I'm using Material UI's Select component in my webapp. I noticed that while a Select component's menu is open, I cannot interact with anything else on the screen. I can't click on other objects, hover events don't trigger, and I can't even scroll until the menu is closed.

It seems material UI has an invisible backdrop that pops up when a Select menu is opened, forcing the user to close that menu before interacting with anything else. However, I am trying to achieve a functionality where the user can still interact with the rest of the app while the dropdown is open (like, for instance, in the main page of Uber's website - the 'Company' dropdown in the nav bar will allow you to do other things while it is open).

Is there a way to disable or work around this invisible backdrop? I tried to set the z-index of everything else to something higher than the z-index of the backdrop, but this is hacky, not sustainable and doesn't always work.

Code / demo: https://codesandbox.io/s/practical-ully-fsfgpj

It would be awesome if there were a robust way to tackle this issue.

答案1

得分: 1

以下是翻译好的内容:

如果您希望使用这样的组件,您不需要使用Select

Select组件使用Menu组件来渲染下拉菜单中的选项列表。

根据文档

菜单组件在内部使用了弹出框(Popover)组件。但是,您可能希望使用不同的定位策略,或者不阻止滚动。为了满足这些需求,我们提供了一个MenuList组件,您可以在此示例中与Popper组合使用。

您可以使用Popper代替Select

例如:

  1. <Button
  2. ref={anchorRef}
  3. id="composition-button"
  4. aria-controls={open ? "composition-menu" : undefined}
  5. aria-expanded={open ? "true" : undefined}
  6. aria-haspopup="true"
  7. onClick={handleToggle}
  8. >
  9. 仪表板
  10. </Button>
  11. <Popper
  12. open={open}
  13. anchorEl={anchorRef.current}
  14. role={undefined}
  15. placement="bottom-start"
  16. transition
  17. disablePortal
  18. >
  19. {({ TransitionProps, placement }) => (
  20. <Grow
  21. {...TransitionProps}
  22. style={{
  23. transformOrigin:
  24. placement === "bottom-start" ? "left top" : "left bottom"
  25. }}
  26. >
  27. <Paper>
  28. <MenuList
  29. autoFocusItem={open}
  30. id="composition-menu"
  31. aria-labelledby="composition-button"
  32. onKeyDown={handleListKeyDown}
  33. >
  34. <MenuItem onClick={handleClose}>个人资料</MenuItem>
  35. <MenuItem onClick={handleClose}>我的帐户</MenuItem>
  36. <MenuItem onClick={handleClose}>注销</MenuItem>
  37. </MenuList>
  38. </Paper>
  39. </Grow>
  40. )}
  41. </Popper>

您可以从上述文档中获取完整的代码。

英文:

You don't need to use Select if you want such a component.

The Select component uses the Menu component to render the list of options in a dropdown menu.

And according to documentation:

> The Menu component uses the Popover component internally. However, you
> might want to use a different positioning strategy, or not blocking
> the scroll. For answering those needs, we expose a MenuList component
> that you can compose, with Popper in this example.

You can use Popper instead of Select.

For example:

  1. &lt;Button
  2. ref={anchorRef}
  3. id=&quot;composition-button&quot;
  4. aria-controls={open ? &quot;composition-menu&quot; : undefined}
  5. aria-expanded={open ? &quot;true&quot; : undefined}
  6. aria-haspopup=&quot;true&quot;
  7. onClick={handleToggle}
  8. &gt;
  9. Dashboard
  10. &lt;/Button&gt;
  11. &lt;Popper
  12. open={open}
  13. anchorEl={anchorRef.current}
  14. role={undefined}
  15. placement=&quot;bottom-start&quot;
  16. transition
  17. disablePortal
  18. &gt;
  19. {({ TransitionProps, placement }) =&gt; (
  20. &lt;Grow
  21. {...TransitionProps}
  22. style={{
  23. transformOrigin:
  24. placement === &quot;bottom-start&quot; ? &quot;left top&quot; : &quot;left bottom&quot;
  25. }}
  26. &gt;
  27. &lt;Paper&gt;
  28. &lt;MenuList
  29. autoFocusItem={open}
  30. id=&quot;composition-menu&quot;
  31. aria-labelledby=&quot;composition-button&quot;
  32. onKeyDown={handleListKeyDown}
  33. &gt;
  34. &lt;MenuItem onClick={handleClose}&gt;Profile&lt;/MenuItem&gt;
  35. &lt;MenuItem onClick={handleClose}&gt;My account&lt;/MenuItem&gt;
  36. &lt;MenuItem onClick={handleClose}&gt;Logout&lt;/MenuItem&gt;
  37. &lt;/MenuList&gt;
  38. &lt;/Paper&gt;
  39. &lt;/Grow&gt;
  40. )}
  41. &lt;/Popper&gt;

You can get the whole code from the above documentation.

huangapple
  • 本文由 发表于 2023年8月5日 06:28:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76839406.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定