简体   繁体   English

控制Material-UI中菜单组件的宽度

[英]Control the width of menu component in Material-UI

I'm trying to implement a menu item with a login form in it.我正在尝试实现一个带有登录表单的菜单项。 It works but the width is too small.它可以工作,但宽度太小。 Is there a way to change it?有没有办法改变它? I couldn't find anything in the documentation about it.我在文档中找不到任何关于它的内容。

import React from 'react';
import Button from '@material-ui/core/Button';
import Menu from '@material-ui/core/Menu';
import MenuItem from '@material-ui/core/MenuItem';

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

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

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

  return (
    <div>
      <Button aria-controls="simple-menu" aria-haspopup="true" onClick={handleClick}>
        Open Menu
      </Button>
      <Menu
        id="simple-menu"
        anchorEl={anchorEl}
        keepMounted
        open={Boolean(anchorEl)}
        onClose={handleClose}
      >
        <MenuItem onClick={handleClose}>Profile</MenuItem>
        <MenuItem onClick={handleClose}>My account</MenuItem>
        <MenuItem onClick={handleClose}>Logout</MenuItem>
      </Menu>
    </div>
  );
}

@norbitrial answer could have side effects. @norbitrial 的答案可能会产生副作用。 In my case i can't close the menu by clicking outside it !就我而言,我无法通过单击外部菜单来关闭菜单!

Better use:更好的使用:

<Menu
  ...
  PaperProps={{  
    style: {  
      width: 350,  
    },  
 }} 

I would go with makeStyles which helps you customize built in components from Material-UI.我将 go 与makeStyles帮助您自定义 Material-UI 的内置组件。 From the documentation (read further here: makeStyles ):从文档(在此处进一步阅读: makeStyles ):

Link a style sheet with a function component using the hook pattern.使用钩子模式将样式表与 function 组件链接。 This hook can be used in a function component.此挂钩可用于 function 组件。 The documentation often calls this returned hook useStyles.文档经常将此返回的钩子称为 useStyles。

The only solution what was working for me is the following:对我有用的唯一解决方案如下:

import { makeStyles } from '@material-ui/core';

// ...

const useStyles = makeStyles({
    customWidth: {
        '& div': {
            // this is just an example, you can use vw, etc.
            width: '350px',
        }
    }
});

// ...

export default function SimpleMenu() {
   // ...
   const classes = useStyles();

   return (
      <Menu
         id="simple-menu"
         anchorEl={anchorEl}
         keepMounted
         open={Boolean(anchorEl)}
         onClose={handleClose}
         className={classes.customWidth}
       >
         <MenuItem onClick={handleClose}>Profile</MenuItem>
         <MenuItem onClick={handleClose}>My account</MenuItem>
         <MenuItem onClick={handleClose}>Logout</MenuItem>
       </Menu>

Additionally just checked the documentation and I even did not find any related property for this purpose, so I would go with the suggested custom solution.另外只是检查了文档,我什至没有找到任何相关的属性,所以我会用建议的自定义解决方案 go 。

You can add classes property to component.您可以将类属性添加到组件。 Define a css file with that class and change the width there.使用该 class 定义一个 css 文件并在那里更改宽度。

Also material-ui brings another way to do it with inline styles with useStyles and makeStyles. material-ui 还带来了另一种方法,使用内联 styles 和 useStyles 和 makeStyles。 check material-ui documentation for that.检查 material-ui 文档。

In MUIv5 there is a straightforward way to handle this by passing the PaperProps argument to menu (menu inherits the arguments from Popover which are documented here ).在 MUIv5 中,有一种直接的方法可以通过将 PaperProps 参数传递给菜单来处理此问题(菜单从 Popover 继承 arguments,此处有文档说明)。

Thus, you could create an 800px wide menu as follows因此,您可以创建一个 800px 宽的菜单,如下所示

<Menu PaperProps={{sx: {width: '800px'}}}>
   ...
</Menu>

The current accepted answer targets every div inside the Menu, which was not ideal in my case.当前接受的答案针对菜单中的每个 div,这在我的情况下并不理想。 The following worked for me:以下对我有用:


const useStyles = makeStyles((theme: Theme) => {
  return createStyles({
    menuRoot: {
      minWidth: "25vw",
    },
  });
});

export default function CustomMenu(props) {
  const classes = useStyles();

  return (
    <Menu
      id="simple-menu"
      anchorEl={anchorEl}
      keepMounted
      open={Boolean(anchorEl)}
      onClose={handleClose}
      transformOrigin={{
        vertical: "bottom",
        horizontal: "left",
      }}
      classes={{
        paper: classes.menuRoot,
      }}
    >
      ...
    </Menu>
  );
}

Control the width of menu component in Material-UI控制Material-UI中菜单组件的宽度

import {  Button,  Menu, MenuItem } from "@material-ui/core";
import React, { useState } from "react";
import style from "./filterBar.module.scss";


const Filterbar = () => {

  const [openMenu, setOpenMenu] = useState(null);
  const handleMenu = () => {
    setOpenMenu(true);
  };

  const closeMenu = ()=>{
      setOpenMenu(false)
  }
  
  return (
    <div>
      <Button onClick={handleMenu}>Filter</Button>
      <Menu open={openMenu} onClose={closeMenu}>
        <MenuItem onClick={closeMenu} >

        <div style={{display:'flex', justifyContent:"space-between", width:"50vw"}}>
            <h6>Brand</h6>
            <h6>Color</h6>
            <h6>Price</h6>
        </div>

        </MenuItem>
      </Menu>
    </div>
  );
};

export default Filterbar;

Assuming we have smth like the following:假设我们有如下内容:

<Menu className={classes.menu} />
 <MenuItem onClick={handleClose}>Profile</MenuItem>
 <Divider component="li" / >
</Menu>

You can fix width in the following way:您可以通过以下方式固定宽度:

const useStyles = makeStyles<Theme>(() => ({
  menu: {
    '&  li': {
      maxWidth: '320px',
    },
  },
}));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM