我正在使用带有Reaction的MUI.下拉菜单未正确对齐.我正在关注https://mui.com/material-ui/react-menu/#account-menu号文件.

My text code:

//** A styled component **//
const StyledToolbar = styled(Toolbar)({
  display: "flex",
  justifyContent: "space-between",
  backgroundColor: "#1e8e3e",
});


//** States **// 
  const [anchorEl, setAnchorEl] = useState(false);
  const open = Boolean(anchorEl);


//** Actual Menu code **//
      <Menu
        id="account-menu"
        anchorEl={anchorEl}
        keepMounted
        open={open}
        getContentAnchorEl={null}
        onClose={(e) => setAnchorEl(false)}
        onClick={(e) => setAnchorEl(false)}
        anchorOrigin={{ horizontal: 'right', vertical: 'bottom' }}
        transformOrigin={{ horizontal: 'right', vertical: 'top' }}
        PaperProps={{
          elevation: 0,
          sx: {
            overflow: 'visible',
            filter: 'drop-shadow(0px 2px 8px rgba(0,0,0,0.32))',
            mt: 1.5,
            '& .MuiAvatar-root': {
              width: 32,
              height: 32,
              ml: -0.5,
              mr: 1,
            },
            '&:before': {
              content: '""',
              display: 'block',
              position: 'absolute',
              top: 0,
              right: 14,
              width: 10,
              height: 10,
              bgcolor: 'background.paper',
              transform: 'translateY(-50%) rotate(45deg)',
              zIndex: 0,
            },
          },
        }}
      >
        <MenuItem>
          <Typography variant="span">John K.</Typography>
        </MenuItem>
        <Divider />
        <MenuItem>
          <Avatar
            sx={{ bgcolor: green[500], margin: ".5rem", width: 24, height: 24 }}
          />
          Profile
        </MenuItem>
        <MenuItem>
          <ListItemIcon sx={{ color: green[500], margin: ".5rem" }}>
            <Settings fontSize="small" />
          </ListItemIcon>
          Settings
        </MenuItem>
        <Divider />
        <MenuItem onClick={handleSignOut}>
          <ListItemIcon sx={{ color: "#f50057", margin: ".5rem" }}>
            <Logout fontSize="small" />
          </ListItemIcon>
          Logout
        </MenuItem>
        {error && <span className="span">Something went wrong!</span>}
      </Menu>

  • 如果我使用的是anchorOrigin={{ horizontal: 'right', vertical: 'bottom' }},则菜单从窗口底部开始:

enter image description here

  • 如果我使用的是anchorOrigin={{ horizontal: 'right', vertical: 'top' }},则菜单从窗口顶部开始,但从用户配置文件图像的中间开始:

enter image description here

对许多人来说,应用getContentAnchorEl={null}解决了这个问题,但对我来说,它不起作用.我正在使用MUI v5.10.0

推荐答案

您需要指定一个元素作为引用,例如锚点,才能从该元素打开菜单.否则,它将仅显示在页面边缘,具体取决于您在Menu组件上指定的anchorOrigin个属性设置.

<>
      <Button
        id="basic-button"
        aria-controls={open ? "basic-menu" : undefined}
        aria-haspopup="true"
        aria-expanded={open ? "true" : undefined}
        onClick={handleClick}
      >
        Menu
      </Button>
      <Menu
        id="account-menu"
        anchorEl={anchorEl}
        keepMounted
        open={open}
        onClose={(e) => setAnchorEl(false)}
        onClick={(e) => setAnchorEl(false)}
        anchorOrigin={{ horizontal: "right", vertical: "bottom" }}
        transformOrigin={{ horizontal: "right", vertical: "top" }}
        PaperProps={{
          elevation: 0,
          sx: {
            overflow: "visible",
            filter: "drop-shadow(0px 2px 8px rgba(0,0,0,0.32))",
            mt: 1.5,
            "& .MuiAvatar-root": {
              width: 32,
              height: 32,
              ml: -0.5,
              mr: 1
            },
            "&:before": {
              content: '""',
              display: "block",
              position: "absolute",
              top: 0,
              right: 14,
              width: 10,
              height: 10,
              bgcolor: "background.paper",
              transform: "translateY(-50%) rotate(45deg)",
              zIndex: 0
            }
          }
        }}
      >
        <MenuItem>
          <Typography variant="span">John K.</Typography>
        </MenuItem>
        <Divider />
        <MenuItem>
          <Avatar
            sx={{ bgcolor: "green", margin: ".5rem", width: 24, height: 24 }}
          />
          Profile
        </MenuItem>
        <MenuItem>
          <ListItemIcon sx={{ color: "green", margin: ".5rem" }}>
            <Settings fontSize="small" />
          </ListItemIcon>
          Settings
        </MenuItem>
        <Divider />
        <MenuItem
        >
          <ListItemIcon sx={{ color: "#f50057", margin: ".5rem" }}>
            <Logout fontSize="small" />
          </ListItemIcon>
          Logout
        </MenuItem>
      </Menu>
    <>

这是正在工作的codesanbox美元

Reactjs相关问答推荐

react应用程序中的字体不适用于.scss扩展名

Reaction Native:在初始获取后,Reducer变为未定义

安装后未渲染tailwind

后端关闭时如何在REACTION AXIOS拦截器中模拟数据?

未定义发送的数据无法在reactjs中找到原因

拖放 - 将排序保存到数组字段

我可以同时使用 Gatsby 和 NEXT.JS 吗?

单击按钮时如何添加或删除 3d 对象

将水平滚动视图添加到底部导航选项卡

React 测试库包装器组件

VideoSDK api 不使用更新状态

Recharts 笛卡尔网格 - 垂直线和水平线的不同样式

在 Nextjs13 应用程序目录中呈现从 supabase 获取的数据

使用 toast 时挂钩调用无效

React - 拖放 UML

为什么 setState 在 React 中不是异步的?

如何在props 更改时运行自定义挂钩?

React Router 6.4CreateBrowserRouter子元素不显示

如何使用 React Router 在 React 中正确同步 useEffect 和 useLocation 挂钩?

为什么我从另一个组件收到错误?