如何从左侧而不是右侧打开 Material UI Menu 组件?

use*_*037 2 javascript reactjs material-ui

因此,我使用 Material UI 的菜单组件制作了一个下拉组件,但默认情况下菜单组件在右侧打开。我需要它实际上向左打开。

我尝试过设计它的样式,最终可以让它随着边距移动,但我正在寻找更可靠的东西。老实说,我很惊讶没有任何支撑。

目前,我的下拉菜单像这样打开 - 不好

我希望它从那里开始,只需打开另一个方向。任何帮助表示赞赏!

我的代码如下: 组件

function DropDown({
  dropDownMeta,
  style = { container: {}, icon: {} },
  icon = <MenuIcon style={{ ...style.icon }} />
}: DropDownProps): ReactElement {
  const [menuAnchor, setMenuAnchor] = useState<null | HTMLElement>(null)

  const handleMenuClick = (event: React.MouseEvent<HTMLButtonElement>) => {
    setMenuAnchor(event.currentTarget)
  }

  const handleMenuClose = () => {
    setMenuAnchor(null)
  }
  const classes = useStyles()
  return (
    <Box style={{ ...style.container }}>
      <StyledIconButton
        style={{ ...style.buttonContainer }}
        onClick={handleMenuClick}
      >
        {icon}
      </StyledIconButton>
      <Menu
        anchorEl={menuAnchor}
        keepMounted
        open={Boolean(menuAnchor)}
        onClose={handleMenuClose}
        className={classes.root}
        style={menuStyles as CSSProperties}
        // getContentAnchorEl={null}
        anchorOrigin={{
          vertical: "top",
          horizontal: "left",
        }}
      >
        {dropDownMeta.map((item, i) => {
          const { label, callback, bottomDivider } = item
          return (
            <Box key={`Item${i}`} onClick={handleMenuClose}>
              <StyledMenuItem onClick={callback}>{label}</StyledMenuItem>
              {bottomDivider && <Divider />}
            </Box>
          )
        })}
      </Menu>
    </Box>
  )
}

export default DropDown
Run Code Online (Sandbox Code Playgroud)

风格

dropDown: {
    menu: {
      position: "absolute",
      top: 35
    },
    menuItem: {
      fontSize: 12,
      padding: 5,
      minWidth: 250
    },
    menuIconBtn: {
      fontSize: 31,
      position: "relative",
      top: 1,
      padding: 0
    }
  },
Run Code Online (Sandbox Code Playgroud)

dwm*_*rin 12

您需要transformOrigin向 Menu 组件添加并配置该属性。这没有在 Menu API 文档中显示,但您可以在Popover API文档中阅读相关内容。

transformOriginPopover API 的描述是

这是弹出框的anchorEl 将附加到的锚点上的点。当anchorReference 为“anchorPosition”时,不使用此选项。选项:垂直:[顶部、中心、底部];水平:[左、中、右]。

这个例子只是Material-UI的自定义菜单演示的简化。

import React from "react";
import { Button, Menu, MenuItem } from "@material-ui/core";

export default function CustomizedMenus() {
  const [anchorEl, setAnchorEl] = React.useState(null);

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

  const handleClose = () => {
    setAnchorEl(null);
  };

  return (
    <div style={{ padding: 200 }}>
      <Button variant="contained" onClick={handleClick}>
        Open Menu
      </Button>
      <Menu
        anchorEl={anchorEl}
        keepMounted
        open={!!anchorEl}
        onClose={handleClose}
        getContentAnchorEl={null}
        anchorOrigin={{
          vertical: "bottom",
          horizontal: "center",
        }}
        transformOrigin={{
          vertical: "top",
          horizontal: "right",
        }}
      >
        <MenuItem style={{ backgroundColor: "pink" }}>
          Long bit of text so we can see left/right...
        </MenuItem>
      </Menu>
    </div>
  );
}
Run Code Online (Sandbox Code Playgroud)

单击的菜单项的图像并显示占位符文本

  • Anchor Playground 对于这种配置非常有用 https://material-ui.com/components/popover/#anchor-playground (3认同)