Menu
Usage
Controlled
To control component opened state provide opened
, onClose
and onOpen
props:
Show menu on hover
To show menu on hover set props:
trigger
to hoverdelay
to number in ms (defaults to 0)
In this case, menu will use onMouseEnter
and onFocus
events instead of onClick
:
Menu.Item component
Menu.Item renders button and accepts following props:
icon
– icon on the leftchildren
– item labelrightSection
– react node, rendered on the right, for example, Badge or keyboard shortcutdisabled
– disables item...others
– Menu.Item produces button element, all other props will be spread to it, for example,onClick
,style
,sx
,className
Disabled item
Custom control
By default, menu button uses ActionIcon with horizontal dots.
You can change it by setting control
and controlRefProp
props.
controlRefProp
is a prop name with which element ref can be accessed.
It defaults to ref
, to change that set controlRefProp="customRefProp"
.
Custom control with component
Change menu position
Menu is rendered inside Portal and its position is controlled with the following props:
position
– left, right, bottom, topplacement
– start, center, endgutter
– spacing between menu body and target element in pxwithArrow
– displays arrow, arrow position is calculated byposition
andplacement
props
Note that in most cases you will need to change Transition to match your position:
Change transition
Menu is built with Transition component. You can customize transition, timing function and duration:
Size and shadow
You can use predefined shadows defined in theme.shadows or your own:
Menu has predefined sizes: xs, sm, md, lg, xl. Size prop controls menu width. You can also use number value to set width in px:
Custom component as Menu.Item
By default menu items render as button, to change that set component
prop on Menu.Item component:
Store items separately
Menu does not support fragments, the following example will not work:
In case you want to store items separately use array instead of fragment:
Add your styles with styles API
You can customize add styles to any part of Menu with Styles API, for example, change hovered item color:
Get control element ref
Accessibility and usability
To make component more accessible for users with screen readers set menuButtonLabel
prop.
Set it in case you use a control which does not include text, for example, default ActionIcon with horizontal dots icon.
Component behavior and properties:
- When menu is opened, focus is trapped inside
- When menu is closed, focus is moved back to menu control
- Focus inside menu is controlled with up and down arrows, tab key has no effect
- By default, when menu item is clicked, menu closes, change it with
closeOnItemClick
prop - Menu is closed when user clicks outside or presses escape
- Menu control has
aria-haspopup
,aria-expanded
,aria-controls
andaria-label
attributes,aria-label
is defined bymenuButtonLabel
prop - Menu body has
menu
role,aria-orientation
is always set tovertical
- Menu item has
menuitem
role