英文:
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:
<Button
ref={anchorRef}
id="composition-button"
aria-controls={open ? "composition-menu" : undefined}
aria-expanded={open ? "true" : undefined}
aria-haspopup="true"
onClick={handleToggle}
>
Dashboard
</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}>Profile</MenuItem>
<MenuItem onClick={handleClose}>My account</MenuItem>
<MenuItem onClick={handleClose}>Logout</MenuItem>
</MenuList>
</Paper>
</Grow>
)}
</Popper>
You can get the whole code from the above documentation.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论