Skip to main content

Menu

Please refer to MUI's official docs for more details on component's usage guide and API documentation.

Basic Menu

Manage anchorEl and open props with the help of a state and onClose prop with the help of a function in Menu component.


// React Imports
import { useState } from 'react'
import type { MouseEvent } from 'react'

// MUI Imports
import Menu from '@mui/material/Menu'
import Button from '@mui/material/Button'
import MenuItem from '@mui/material/MenuItem'

const MenuBasic = () => {
// States
const [anchorEl, setAnchorEl] = useState<HTMLElement | null>(null)

const handleClick = (event: MouseEvent<HTMLButtonElement>) => {
setAnchorEl(event.currentTarget)
}

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

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

export default MenuBasic
Selected Menu

Manage selected prop with the help of a state in MenuItem component to select an item.


// React Imports
import { useState } from 'react'
import type { MouseEvent } from 'react'

// MUI Imports
import List from '@mui/material/List'
import Menu from '@mui/material/Menu'
import ListItem from '@mui/material/ListItem'
import MenuItem from '@mui/material/MenuItem'
import ListItemText from '@mui/material/ListItemText'
import ListItemButton from '@mui/material/ListItemButton'

const options = [
'Show some love to MUI',
'Show all notification content',
'Hide sensitive notification content',
'Hide all notification content'
]

const MenuSelected = () => {
// States
const [selectedIndex, setSelectedIndex] = useState<number>(1)
const [anchorEl, setAnchorEl] = useState<HTMLElement | null>(null)

const handleClickListItem = (event: MouseEvent<HTMLElement>) => {
setAnchorEl(event.currentTarget)
}

const handleMenuItemClick = (event: MouseEvent<HTMLElement>, index: number) => {
setAnchorEl(null)
setSelectedIndex(index)
}

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

return (
<>
<List component='nav' className='p-0' aria-label='Device settings'>
<ListItem
disablePadding
aria-haspopup='true'
aria-controls='lock-menu'
onClick={handleClickListItem}
aria-label='when device is locked'
>
<ListItemButton>
<ListItemText primary='When device is locked' secondary={options[selectedIndex]} />
</ListItemButton>
</ListItem>
</List>
<Menu id='lock-menu' anchorEl={anchorEl} keepMounted open={Boolean(anchorEl)} onClose={handleClose}>
{options.map((option, index) => (
<MenuItem
key={option}
disabled={index === 0}
selected={index === selectedIndex}
onClick={event => handleMenuItemClick(event, index)}
>
{option}
</MenuItem>
))}
</Menu>
</>
)
}

export default MenuSelected
MenuList Composition

Use a different positioning strategy and not blocking the page scroll by using MenuList and Popper components.


// React Imports
import { useEffect, useRef, useState } from 'react'
import type { KeyboardEvent, MouseEvent, TouchEvent } from 'react'

// MUI Imports
import Fade from '@mui/material/Fade'
import Paper from '@mui/material/Paper'
import Button from '@mui/material/Button'
import Popper from '@mui/material/Popper'
import MenuList from '@mui/material/MenuList'
import MenuItem from '@mui/material/MenuItem'
import ClickAwayListener from '@mui/material/ClickAwayListener'

const MenuComposition = () => {
// States
const [open, setOpen] = useState<boolean>(false)
const anchorRef = useRef<HTMLButtonElement | null>(null)

const handleToggle = () => {
setOpen(prevOpen => !prevOpen)
}

const handleClose = (event: MouseEvent | TouchEvent): void => {
if (anchorRef.current && anchorRef.current.contains(event.target as HTMLElement)) {
return
}
setOpen(false)
}

const handleListKeyDown = (event: KeyboardEvent) => {
if (event.key === 'Tab') {
event.preventDefault()
setOpen(false)
} else if (event.key === 'Escape') {
setOpen(false)
}
}

// return focus to the button when we transitioned from !open -> open
const prevOpen = useRef(open)

useEffect(() => {
if (prevOpen.current === true && open === false) {
anchorRef.current!.focus()
}

prevOpen.current = open
}, [open])

return (
<>
<Button
ref={anchorRef}
variant='outlined'
aria-haspopup='true'
onClick={handleToggle}
id='composition-button'
aria-expanded={open ? 'true' : undefined}
aria-controls={open ? 'composition-menu' : undefined}
>
Open Menu
</Button>
<Popper
transition
open={open}
disablePortal
role={undefined}
placement='bottom-start'
anchorEl={anchorRef.current}
className='z-[var(--mui-zIndex-modal)]'
popperOptions={{
modifiers: [
{
name: 'flip',
options: {
enabled: true,
boundary: 'window'
}
}
]
}}
>
{({ TransitionProps, placement }) => (
<Fade
{...TransitionProps}
style={{ transformOrigin: placement === 'bottom-start' ? 'left top' : 'left bottom' }}
>
<Paper className='shadow-lg mbs-0.5'>
<ClickAwayListener onClickAway={() => setOpen(false)}>
<MenuList autoFocusItem={open} id='composition-menu' onKeyDown={handleListKeyDown}>
<MenuItem onClick={handleClose}>Profile</MenuItem>
<MenuItem onClick={handleClose}>My account</MenuItem>
<MenuItem onClick={handleClose}>Logout</MenuItem>
</MenuList>
</ClickAwayListener>
</Paper>
</Fade>
)}
</Popper>
</>
)
}

export default MenuComposition
Customized Menu

Use styled hook to customize your menu.


// React Imports
import { useState } from 'react'
import type { MouseEvent } from 'react'

// MUI Imports
import Button from '@mui/material/Button'
import { styled } from '@mui/material/styles'
import ListItemIcon from '@mui/material/ListItemIcon'
import ListItemText from '@mui/material/ListItemText'
import MuiMenu from '@mui/material/Menu'
import MuiMenuItem from '@mui/material/MenuItem'
import type { MenuProps } from '@mui/material/Menu'
import type { MenuItemProps } from '@mui/material/MenuItem'

// Styled Menu component
const Menu = styled(MuiMenu)<MenuProps>(({ theme }) => ({
'& .MuiMenu-paper': {
border: `1px solid ${theme.palette.divider}`
}
}))

// Styled MenuItem component
const MenuItem = styled(MuiMenuItem)<MenuItemProps>(({ theme }) => ({
'&:focus': {
backgroundColor: theme.palette.primary.main,
'& .MuiListItemIcon-root, & .MuiListItemText-primary': {
color: theme.palette.common.white
}
}
}))

const MenuCustomized = () => {
// States
const [anchorEl, setAnchorEl] = useState<HTMLElement | null>(null)

const handleClick = (event: MouseEvent<HTMLElement>) => {
setAnchorEl(event.currentTarget)
}

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

return (
<>
<Button variant='outlined' aria-haspopup='true' onClick={handleClick} aria-controls='customized-menu'>
Open Menu
</Button>
<Menu
keepMounted
elevation={0}
anchorEl={anchorEl}
id='customized-menu'
onClose={handleClose}
open={Boolean(anchorEl)}
anchorOrigin={{
vertical: 'bottom',
horizontal: 'center'
}}
transformOrigin={{
vertical: 'top',
horizontal: 'center'
}}
>
<MenuItem>
<ListItemIcon>
<i className='ri-send-plane-2-line text-xl' />
</ListItemIcon>
<ListItemText primary='Sent mail' />
</MenuItem>
<MenuItem>
<ListItemIcon>
<i className='ri-mail-open-line text-xl' />
</ListItemIcon>
<ListItemText primary='Drafts' />
</MenuItem>
<MenuItem>
<ListItemIcon>
<i className='ri-inbox-archive-line text-xl' />
</ListItemIcon>
<ListItemText primary='Inbox' />
</MenuItem>
</Menu>
</>
)
}

export default MenuCustomized
Max Height Menu

Use PaperProps prop and use style property to set the height of the menu.


// React Imports
import { useState } from 'react'
import type { MouseEvent } from 'react'

// MUI Imports
import Menu from '@mui/material/Menu'
import MenuItem from '@mui/material/MenuItem'
import IconButton from '@mui/material/IconButton'

const options = [
'None',
'Atria',
'Callisto',
'Dione',
'Ganymede',
'Hangouts Call',
'Luna',
'Oberon',
'Phobos',
'Pyxis',
'Sedna',
'Titania',
'Triton',
'Umbriel'
]

const ITEM_HEIGHT = 48

const MenuMaxHeight = () => {
// States
const [anchorEl, setAnchorEl] = useState<HTMLElement | null>(null)

const handleClick = (event: MouseEvent<HTMLElement>) => {
setAnchorEl(event.currentTarget)
}

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

return (
<>
<IconButton aria-label='more' aria-controls='long-menu' aria-haspopup='true' onClick={handleClick}>
<i className='ri-more-2-line' />
</IconButton>
<Menu
keepMounted
id='long-menu'
anchorEl={anchorEl}
onClose={handleClose}
open={Boolean(anchorEl)}
slotProps={{ paper: { style: { maxHeight: ITEM_HEIGHT * 4.5 } } }}
>
{options.map(option => (
<MenuItem key={option} selected={option === 'Pyxis'} onClick={handleClose}>
{option}
</MenuItem>
))}
</Menu>
</>
)
}

export default MenuMaxHeight
Change Transition

Use TransitionComponent prop to change the transition of the menu.


// React Imports
import { useState } from 'react'
import type { MouseEvent } from 'react'

// MUI Imports
import Menu from '@mui/material/Menu'
import Zoom from '@mui/material/Zoom'
import Button from '@mui/material/Button'
import MenuItem from '@mui/material/MenuItem'

const MenuTransition = () => {
// States
const [anchorEl, setAnchorEl] = useState<HTMLElement | null>(null)

const handleClick = (event: MouseEvent<HTMLElement>) => {
setAnchorEl(event.currentTarget)
}

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

return (
<>
<Button variant='outlined' aria-controls='zoom-menu' aria-haspopup='true' onClick={handleClick}>
Open with zoom transition
</Button>
<Menu
keepMounted
id='zoom-menu'
anchorEl={anchorEl}
onClose={handleClose}
open={Boolean(anchorEl)}
TransitionComponent={Zoom}
>
<MenuItem onClick={handleClose}>Profile</MenuItem>
<MenuItem onClick={handleClose}>My account</MenuItem>
<MenuItem onClick={handleClose}>Logout</MenuItem>
</Menu>
</>
)
}

export default MenuTransition
Context Menu

Use onContextMenu prop in the parent element to manage the context menu.

Apple pie bonbon sweet brownie cake lemon drops carrot cake danish carrot cake. Marzipan jujubes cupcake cake bear claw jujubes. Macaroon candy canes jelly-o sugar plum biscuit. Cupcake cupcake oat cake cookie donut candy canes chupa chups. Jelly beans carrot cake soufflé gummies sweet cake halvah carrot cake. Candy marshmallow apple pie donut toffee pudding jelly croissant jelly. Dragée cake liquorice cake gummi bears. Gummi bears caramels tootsie roll caramels lemon drops caramels chocolate cake jelly oat cake. Oat cake tart biscuit cake.


// React Imports
import { useState } from 'react'
import type { MouseEvent } from 'react'

// MUI Imports
import Menu from '@mui/material/Menu'
import MenuItem from '@mui/material/MenuItem'
import Typography from '@mui/material/Typography'

type State = {
mouseX: number | null
mouseY: number | null
}

const initialState = {
mouseX: null,
mouseY: null
}

const MenuContext = () => {
// States
const [state, setState] = useState<State>(initialState)

const handleClick = (event: MouseEvent<HTMLDivElement>) => {
event.preventDefault()
setState({
mouseX: event.clientX - 2,
mouseY: event.clientY - 4
})
}

const handleClose = () => {
setState(initialState)
}

return (
<div onContextMenu={handleClick} className='cursor-context-menu'>
<Typography>
Apple pie bonbon sweet brownie cake lemon drops carrot cake danish carrot cake. Marzipan jujubes cupcake cake
bear claw jujubes. Macaroon candy canes jelly-o sugar plum biscuit. Cupcake cupcake oat cake cookie donut candy
canes chupa chups. Jelly beans carrot cake soufflé gummies sweet cake halvah carrot cake. Candy marshmallow
apple pie donut toffee pudding jelly croissant jelly. Dragée cake liquorice cake gummi bears. Gummi bears
caramels tootsie roll caramels lemon drops caramels chocolate cake jelly oat cake. Oat cake tart biscuit cake.
</Typography>
<Menu
keepMounted
onClose={handleClose}
open={state.mouseY !== null}
anchorReference='anchorPosition'
anchorPosition={
state.mouseY !== null && state.mouseX !== null ? { top: state.mouseY, left: state.mouseX } : undefined
}
>
<MenuItem onClick={handleClose}>Copy</MenuItem>
<MenuItem onClick={handleClose}>Print</MenuItem>
<MenuItem onClick={handleClose}>Email</MenuItem>
<MenuItem onClick={handleClose}>Highlight</MenuItem>
</Menu>
</div>
)
}

export default MenuContext