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

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

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

例如:

<Button
  ref={anchorRef}
  id="composition-button"
  aria-controls={open ? "composition-menu" : undefined}
  aria-expanded={open ? "true" : undefined}
  aria-haspopup="true"
  onClick={handleToggle}
>
  仪表板
</Button>
<Popper
  open={open}
  anchorEl={anchorRef.current}
  role={undefined}
  placement="bottom-start"
  transition
  disablePortal
>
  {({ TransitionProps, placement }) => (
    <Grow
      {...TransitionProps}
      style={{
        transformOrigin:
          placement === "bottom-start" ? "left top" : "left bottom"
      }}
    >
      <Paper>
        <MenuList
          autoFocusItem={open}
          id="composition-menu"
          aria-labelledby="composition-button"
          onKeyDown={handleListKeyDown}
        >
          <MenuItem onClick={handleClose}>个人资料</MenuItem>
          <MenuItem onClick={handleClose}>我的帐户</MenuItem>
          <MenuItem onClick={handleClose}>注销</MenuItem>
        </MenuList>
      </Paper>
    </Grow>
  )}
</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:

&lt;Button
  ref={anchorRef}
  id=&quot;composition-button&quot;
  aria-controls={open ? &quot;composition-menu&quot; : undefined}
  aria-expanded={open ? &quot;true&quot; : undefined}
  aria-haspopup=&quot;true&quot;
  onClick={handleToggle}
&gt;
  Dashboard
&lt;/Button&gt;
&lt;Popper
  open={open}
  anchorEl={anchorRef.current}
  role={undefined}
  placement=&quot;bottom-start&quot;
  transition
  disablePortal
&gt;
  {({ TransitionProps, placement }) =&gt; (
    &lt;Grow
      {...TransitionProps}
      style={{
        transformOrigin:
          placement === &quot;bottom-start&quot; ? &quot;left top&quot; : &quot;left bottom&quot;
      }}
    &gt;
      &lt;Paper&gt;
        &lt;MenuList
          autoFocusItem={open}
          id=&quot;composition-menu&quot;
          aria-labelledby=&quot;composition-button&quot;
          onKeyDown={handleListKeyDown}
        &gt;
          &lt;MenuItem onClick={handleClose}&gt;Profile&lt;/MenuItem&gt;
          &lt;MenuItem onClick={handleClose}&gt;My account&lt;/MenuItem&gt;
          &lt;MenuItem onClick={handleClose}&gt;Logout&lt;/MenuItem&gt;
        &lt;/MenuList&gt;
      &lt;/Paper&gt;
    &lt;/Grow&gt;
  )}
&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:

确定