Menu
Menus display a list of choices on temporary surfaces.
Introduction
Joy UI provides five menu-related components:
MenuButton
: A button that opens a menu. It reuses the styles fromButton
.Menu
: A listbox popup for wrapping the menu items which reuses the styles fromList
.MenuItem
: A menu item which reuses the styles fromListItemButton
.MenuList
: A standalone listbox for composition usage. It also reuses the styles fromList
.Dropdown
: The outermost component that wires a button with a menu. It only provides a context and does not render anything.
<Dropdown>
<MenuButton>
Format
</MenuButton>
<Menu>
<MenuItem>…</MenuItem>
…
</Menu>
</Dropdown>
Playground
Component
After installation, you can start building with this component using the following basic elements:
import Menu from '@mui/joy/Menu';
import MenuButton from '@mui/joy/MenuButton';
import MenuItem from '@mui/joy/MenuItem';
import Dropdown from '@mui/joy/Dropdown';
export default function MyApp() {
return (
<Dropdown>
<MenuButton>Actions</MenuButton>
<Menu>
<MenuItem>Add item</MenuItem>
</Menu>
</Dropdown>
);
}
Basic usage
Place both the Menu and Menu Button inside a Dropdown component. This will wire them together. The Menu Button will toggle the visibility of the menu and act as an anchor for the Menu's popup.
The basic version of the menu opens over the anchor element by default. You can change this via specific positioning props.
When close to the screen's edge, the menu vertically realigns to make sure that all menu items are completely visible. Choosing an option should immediately, and ideally, commit the option and close the menu.
Sizes
The menu component comes with three sizes out of the box: sm
, md
(the default), and lg
.
When specifying a size for the Menu
component, menu items inside of it will inherit the value.
Selected
Use the selected
prop to signal whether a MenuItem
is selected or not.
The menu item uses the same styles as the ListItemButton
.
Icon button menu
To use IconButton component as a menu button, pass it to the root slot of the MenuButton
component.
Use slotProps
to pass props to the IconButton component.
Positioned menu
The Menu
is based on the Base UI Popper
component, which exposes a few placement props.
For example, this is how you'd go for displaying the menu on the bottom-end of the anchor button.
Controlling the open state
By default, the open/close state of the menu is managed internally by the Dropdown component.
To control it programmatically from the outside, apply the Dropdown's open
and onOpenChange
props as shown below:
MenuList
composition
To get full control of the DOM structure, use the MenuList
component.
You can use it to compose any popup-alike component.
The primary responsibility of this component is handling the focus state.
Or display the menu without a popup:
- Category 1
- Action 1
- Action 2
- Action 3
- Action 4
- Action 5
- Action 6
- Action 7
- Action 8
- Action 9
- Action 10
- Category 2
- Action 1
- Action 2
- Action 3
- Action 4
- Action 5
- Action 6
- Action 7
- Action 8
- Action 9
- Action 10
- Category 3
- Action 1
- Action 2
- Action 3
- Action 4
- Action 5
- Action 6
- Action 7
- Action 8
- Action 9
- Action 10
- Category 4
- Action 1
- Action 2
- Action 3
- Action 4
- Action 5
- Action 6
- Action 7
- Action 8
- Action 9
- Action 10
- Category 5
- Action 1
- Action 2
- Action 3
- Action 4
- Action 5
- Action 6
- Action 7
- Action 8
- Action 9
- Action 10
Debugging
To keep the list box open for inspecting elements, enable the Emulate a focused page
option from the Chrome DevTool Rendering tab.
You can also access this option by using command menu and search for it.
Common examples
Apps menu
This example replicates a menu that contain links to other applications.
Menu bar
This example replicates the application menu bar on macOS. It supports mouse and keyboard navigation between menu items.
- File
- Edit
- Selection
Side navigation icons
This example is quite common in dashboard applications where the side navigation is shrunk into icons, and the menu is triggered by hovering them.