Menus
Menus display a list of choices on temporary surfaces.
A Menu displays a list of choices on a temporary surface. It appears when the user interacts with a button, or other control.
Simple Menu
Simple menus open over the anchor element by default (this option can be changed via props). When close to a screen edge, simple menus vertically realign to make sure that all menu items are completely visible.
Choosing an option should immediately ideally commit the option and close the menu.
Disambiguation: In contrast to simple menus, simple dialogs can present additional detail related to the options available for a list item or provide navigational or orthogonal actions related to the primary task. Although they can display the same content, simple menus are preferred over simple dialogs because simple menus are less disruptive to the user’s current context.
<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>
Selected menus
If used for item selection, when opened, simple menus attempt to vertically align the currently selected menu item with the anchor element,
and the initial focus will be placed on the selected menu item.
The currently selected menu item is set using the selected
property (from ListItem).
To use a selected menu item without impacting the initial focus or the vertical positioning of the menu, set the variant
property to menu
.
MenuList composition
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.
The primary responsibility of the MenuList
component is to handle the focus.
Customized menus
Here is an example of customizing the component. You can learn more about this in the overrides documentation page.
The MenuItem
is a wrapper around ListItem
with some additional styles.
You can use the same list composition features with the MenuItem
component:
Max height menus
If the height of a menu prevents all menu items from being displayed, the menu can scroll internally.
Limitations
There is a flexbox bug that prevents text-overflow: ellipsis
from working in a flexbox layout.
You can use the Typography
component with noWrap
to workaround this issue:
<Button aria-controls="fade-menu" aria-haspopup="true" onClick={handleClick}>
Open with fade transition
</Button>
<Menu
id="fade-menu"
anchorEl={anchorEl}
keepMounted
open={open}
onClose={handleClose}
TransitionComponent={Fade}
>
<MenuItem onClick={handleClose}>Profile</MenuItem>
<MenuItem onClick={handleClose}>My account</MenuItem>
<MenuItem onClick={handleClose}>Logout</MenuItem>
</Menu>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam ipsum purus, bibendum sit amet vulputate eget, porta semper ligula. Donec bibendum vulputate erat, ac fringilla mi finibus nec. Donec ac dolor sed dolor porttitor blandit vel vel purus. Fusce vel malesuada ligula. Nam quis vehicula ante, eu finibus est. Proin ullamcorper fermentum orci, quis finibus massa. Nunc lobortis, massa ut rutrum ultrices, metus metus finibus ex, sit amet facilisis neque enim sed neque. Quisque accumsan metus vel maximus consequat. Suspendisse lacinia tellus a libero volutpat maximus.
Complementary projects
For more advanced use cases you might be able to take advantage of:
PopupState helper
There is a 3rd party package material-ui-popup-state
that takes care of menu state for you in most cases.
<PopupState variant="popover" popupId="demo-popup-menu">
{popupState => (
<React.Fragment>
<Button variant="contained" {...bindTrigger(popupState)}>
Open Menu
</Button>
<Menu {...bindMenu(popupState)}>
<MenuItem onClick={popupState.close}>Cake</MenuItem>
<MenuItem onClick={popupState.close}>Death</MenuItem>
</Menu>
</React.Fragment>
)}
</PopupState>