Components
#Menu
#Menu
is a top-level component that contains menu items and other lower level submenus.
Working with a MenuButton or a compatible button component, Menu
manages states which controls the display of its contents and maintains focus among its descendants.
It should be able to serve the majority of use cases. If you need more controls on how and when a menu is open or closed, you might use a ControlledMenu.
Props
Name | Type | Default | Description |
---|---|---|---|
align | string | 'start' | Can be 'start', 'center', or 'end'. It sets alignment of menu with anchor element. |
aria-label | string | Sets If not provided, one will be generated from the string content of menu button, or the default 'Menu'. | |
arrow | boolean | Set | |
arrowProps | object | Properties of this object are spread to the menu arrow DOM element. | |
arrowProps.className | string | function | A string that will be appended to the When a function is provided, it will be called by passing an object with the following properties and should return a CSS class name.
| |
boundingBoxPadding | string | Specify bounding box padding in pixels. Use a syntax similar to the CSS | |
boundingBoxRef | object | A ref object attached to a DOM element within which menu will be positioned. If not provided, the nearest ancestor which has CSS Supports | |
children | node | function | Menu items underneath the menu, or a function that returns them. The function will be called by passing an object with the following properties:
| |
containerProps | object | Properties of this object are spread to the root DOM element containing the menu. | |
direction | string | 'bottom' or 'right' for SubMenu | Can be 'left', 'right', 'top', or 'bottom'. It sets direction in which menu expands against anchor element. |
focusProps | object | Properties of this object are spread to a DOM element which captures focus for the menu. | |
gap | number | 0 | Add a gap (gutter) between menu and its anchor element. The value (in pixels) can be negative. |
initialMounted | boolean | By default menu isn't mounted into DOM until it's opened for the first time. Setting the prop to | |
instanceRef | React.Ref <MenuInstance> | A ref which attaches to menu component and provides the follow methods:
| |
menuButton | element | function | Can be a MenuButton, a It also accepts a function that returns one of the above. The function will be called by passing an object with the following properties:
If a React component is provided, it needs to implement the following contracts:
Please note MenuButton has one additional benefit that it has managed | |
menuClassName | string | function | A string that will be appended to the When a function is provided, it will be called by passing an object with the following properties and should return a CSS class name.
| |
menuStyle | CSSProperties | This value is forwarded to the | |
onItemClick | function | Event fired when descendent menu items are clicked. Event object properties:
| |
onMenuChange | function | Event fired when menu states have changed. Event object properties:
| |
overflow | string | 'visible' | Can be 'visible', 'auto', or 'hidden'. It makes the menu list scrollable or hidden when there is not enough viewport space to display all menu items. The value is similar to the CSS |
portal | boolean | { target?: Element; stablePosition?: boolean; } | If
Portal allows menu to visually “break out” of its container. Typical use cases may include:
Note: portal breaks tab sequence and may impact the accessibility of your website. | |
position | string | 'auto' | It sets the position of menu related to its anchor element.
|
reposition | string | 'auto' | It specifies when menu is repositioned.
|
repositionFlag | number | string | Use this prop to explicitly reposition menu. Whenever the prop has a new value, menu position will be recalculated and updated. You might use a counter and increase it every time. Warning: don't update this prop in rapid succession, which is inefficient and might cause infinite rendering of component. E.g., don't change the value of this prop in | |
setDownOverflow | boolean | Set computed overflow amount down to a child | |
shift | number | 0 | Shift menu's position away from its anchor element. The value (in pixels) can be negative. |
submenuCloseDelay | number | 150 | Set a delay in ms before closing a submenu when it's open and mouse is moving over other items in the parent menu list. |
submenuOpenDelay | number | 300 | Set a delay in ms before opening a submenu when mouse moves over it. |
theming | string | It sets a CSS | |
transition | boolean | { open?: boolean; close?: boolean; item?: boolean; } | Enable or disable transition effects in the You can set 'open', 'close', 'item' at the same time with one boolean value or separately with an object. If you enable transition on menu, make sure to add | |
transitionTimeout | number | 500 | A fallback timeout to stop transition if |
unmountOnClose | boolean | By default menu remains in DOM when it's closed. Setting the prop to | |
viewScroll | string | 'initial' | It sets the behaviour of menu and any of its descendent submenus when window is scrolling.
|
MenuItem
#MenuItem
represents an item under a menu which can be activated.
It can be a regular menu item, a checkbox item (type="checkbox"
), a radio item (type="radio"
).
Props
Name | Type | Default | Description |
---|---|---|---|
checked | boolean | Set | |
children | node | function | Contents of the menu item, or a function that returns it. The function will be called by passing an object with the following properties:
| |
className | string | function | A string that will be appended to the When a function is provided, it will be called by passing an object with the following properties and should return a CSS class name.
| |
disabled | boolean | Set | |
href | string | If provided, menu item renders an HTML <a> element with this href attribute. | |
onClick | function | Event fired when the menu item is clicked. Event object properties:
| |
type | string | Set this prop to 'checkbox' or 'radio' to make it a checkbox or radio menu item. | |
value | any | Any value provided to this prop will be available in the event object of click events. It's useful for helping identify which menu item is clicked when you listen the |
MenuButton
#MenuButton
works with a Menu and controls its open and close.
Props
Name | Type | Default | Description |
---|---|---|---|
children | node | Contents of the menu button. | |
className | string | function | A string that will be appended to the When a function is provided, it will be called by passing an object with the following properties and should return a CSS class name.
| |
disabled | boolean | Set |
FocusableItem
#FocusableItem
can be used to wrap focusable element (input, button) in a menu item. It manages focus automatically among other menu items during mouse and keyboard interactions.
Props
Name | Type | Default | Description |
---|---|---|---|
children | function | A function which returns what to be rendered. It will be called by passing an object with the following properties:
| |
className | string | function | A string that will be appended to the When a function is provided, it will be called by passing an object with the following properties and should return a CSS class name.
| |
disabled | boolean | Set Please note this prop only removes the current item from mouse and keyboard interaction sequences. You still need to disable any focusable element which you have supplied in its children. This prop is passed to the |
SubMenu
#SubMenu
is a menu container under other menu or submenu components.
It consists of a menu item and a sub-level menu containing submenu items. Use label
prop to set its own contents, and place the submenu items it contains in the children
prop.
Props
Name | Type | Default | Description |
---|---|---|---|
align | string | 'start' | Can be 'start', 'center', or 'end'. It sets alignment of menu with anchor element. |
aria-label | string | Sets If not provided, one will be generated from the string content of | |
arrow | boolean | Set | |
arrowProps | object | Properties of this object are spread to the menu arrow DOM element. | |
arrowProps.className | string | function | A string that will be appended to the When a function is provided, it will be called by passing an object with the following properties and should return a CSS class name.
| |
children | node | function | Menu items underneath the menu, or a function that returns them. The function will be called by passing an object with the following properties:
| |
direction | string | 'bottom' or 'right' for SubMenu | Can be 'left', 'right', 'top', or 'bottom'. It sets direction in which menu expands against anchor element. |
disabled | boolean | Set | |
focusProps | object | Properties of this object are spread to a DOM element which captures focus for the menu. | |
gap | number | 0 | Add a gap (gutter) between menu and its anchor element. The value (in pixels) can be negative. |
instanceRef | React.Ref <MenuInstance> | A ref which attaches to menu component and provides the follow methods:
| |
itemProps | object | Properties of this object are spread to the submenu item DOM element. | |
itemProps.className | string | function | A string that will be appended to the When a function is provided, it will be called by passing an object with the following properties and should return a CSS class name.
| |
label | node | function | Contents of the submenu item, or a function that returns it. The function will be called by passing an object with the following properties:
| |
menuClassName | string | function | A string that will be appended to the When a function is provided, it will be called by passing an object with the following properties and should return a CSS class name.
| |
menuStyle | CSSProperties | This value is forwarded to the | |
onMenuChange | function | Event fired when menu states have changed. Event object properties:
| |
openTrigger | undefined | 'none' | 'clickOnly' |
| |
overflow | string | 'visible' | Can be 'visible', 'auto', or 'hidden'. It makes the menu list scrollable or hidden when there is not enough viewport space to display all menu items. The value is similar to the CSS |
position | string | 'auto' | It sets the position of menu related to its anchor element.
|
setDownOverflow | boolean | Set computed overflow amount down to a child | |
shift | number | 0 | Shift menu's position away from its anchor element. The value (in pixels) can be negative. |
MenuRadioGroup
#MenuRadioGroup
is a container of menu items which are similar to radio buttons.
Props
Name | Type | Default | Description |
---|---|---|---|
aria-label | string | Sets If not provided, it will be set as the value of | |
children | node | The only permitted children is | |
name | string | Sets the radio group name (optional). The name will be passed to the | |
onRadioChange | function | Event fired when a child menu item is clicked (selected). Event object properties:
| |
value | any | Sets value of the radio group. The child menu item which has the same value (strict equality ===) as the radio group is marked as checked. |
MenuGroup
#MenuGroup
is used to wrap a subset of related menu items and make them scrollable.
Props
Name | Type | Default | Description |
---|---|---|---|
takeOverflow | boolean | Set |
MenuHeader
#MenuHeader
can be used to provide presentational information for a group of related menu items.
MenuDivider
#MenuDivider
can be used to make a group of related menu items visually separated from other items. It has aria
roles that can be recognised by assistive technologies.
ControlledMenu
#ControlledMenu
is a top-level component that contains menu items and other lower level submenus.
It's different from a Menu that ControlledMenu
allows you to control how and when a menu is open or closed, rather than controlled by a menu button.
For example, you might need to open a menu when something on the page is hovered by a mouse, or you need to position the menu to something other than the menu button. ControlledMenu
can be also use to implement a context menu.
When using ControlledMenu
, it's your job to set focus to the desirable item after menu opens and move focus back to your menu button after it closes, which can be done by setting menuItemFocus
, and in the onClose
event, respectively. However, depending on your requirements, both of them might be optional.
Props
Name | Type | Default | Description |
---|---|---|---|
align | string | 'start' | Can be 'start', 'center', or 'end'. It sets alignment of menu with anchor element. |
anchorPoint | object | Use this prop only for context menu. See an example. An object describes viewport coordinates to which context menu will be positioned. It's an object with the shape of | |
anchorRef | object | Not needed for context menu. A ref object attached to a DOM element to which menu will be positioned. Supports | |
aria-label | string | Sets If not provided, it will be set as 'Menu'. | |
arrow | boolean | Set | |
arrowProps | object | Properties of this object are spread to the menu arrow DOM element. | |
arrowProps.className | string | function | A string that will be appended to the When a function is provided, it will be called by passing an object with the following properties and should return a CSS class name.
| |
boundingBoxPadding | string | Specify bounding box padding in pixels. Use a syntax similar to the CSS | |
boundingBoxRef | object | A ref object attached to a DOM element within which menu will be positioned. If not provided, the nearest ancestor which has CSS Supports | |
captureFocus | boolean | true | If |
children | node | function | Menu items underneath the menu, or a function that returns them. The function will be called by passing an object with the following properties:
| |
containerProps | object | Properties of this object are spread to the root DOM element containing the menu. | |
direction | string | 'bottom' or 'right' for SubMenu | Can be 'left', 'right', 'top', or 'bottom'. It sets direction in which menu expands against anchor element. |
focusProps | object | Properties of this object are spread to a DOM element which captures focus for the menu. | |
gap | number | 0 | Add a gap (gutter) between menu and its anchor element. The value (in pixels) can be negative. |
initialMounted | boolean | By default menu isn't mounted into DOM until it's opened for the first time. Setting the prop to | |
menuClassName | string | function | A string that will be appended to the When a function is provided, it will be called by passing an object with the following properties and should return a CSS class name.
| |
menuItemFocus | object | Sets which menu item receives focus (hover) when menu opens. You will usually set this prop when the menu is opened by keyboard events. It's an object with the shape of:
The
If you don't intend to update focus (hover) position, it's important to keep this prop's identity stable when your component re-renders. | |
menuStyle | CSSProperties | This value is forwarded to the | |
onClose | function | Event fired when menu is about to close. Event object properties:
| |
onItemClick | function | Event fired when descendent menu items are clicked. Event object properties:
| |
overflow | string | 'visible' | Can be 'visible', 'auto', or 'hidden'. It makes the menu list scrollable or hidden when there is not enough viewport space to display all menu items. The value is similar to the CSS |
portal | boolean | { target?: Element; stablePosition?: boolean; } | If
Portal allows menu to visually “break out” of its container. Typical use cases may include:
Note: portal breaks tab sequence and may impact the accessibility of your website. | |
position | string | 'auto' | It sets the position of menu related to its anchor element.
|
reposition | string | 'auto' | It specifies when menu is repositioned.
|
repositionFlag | number | string | Use this prop to explicitly reposition menu. Whenever the prop has a new value, menu position will be recalculated and updated. You might use a counter and increase it every time. Warning: don't update this prop in rapid succession, which is inefficient and might cause infinite rendering of component. E.g., don't change the value of this prop in | |
setDownOverflow | boolean | Set computed overflow amount down to a child | |
shift | number | 0 | Shift menu's position away from its anchor element. The value (in pixels) can be negative. |
state | string | Controls the state of menu: | |
submenuCloseDelay | number | 150 | Set a delay in ms before closing a submenu when it's open and mouse is moving over other items in the parent menu list. |
submenuOpenDelay | number | 300 | Set a delay in ms before opening a submenu when mouse moves over it. |
theming | string | It sets a CSS | |
transition | boolean | { open?: boolean; close?: boolean; item?: boolean; } | Enable or disable transition effects in the You can set 'open', 'close', 'item' at the same time with one boolean value or separately with an object. If you enable transition on menu, make sure to add | |
transitionTimeout | number | 500 | A fallback timeout to stop transition if |
unmountOnClose | boolean | By default menu remains in DOM when it's closed. Setting the prop to | |
viewScroll | string | 'initial' | It sets the behaviour of menu and any of its descendent submenus when window is scrolling.
|
Hooks
#useMenuState
#useMenuState
is a custom Hook that helps manage the states of ControlledMenu.
The Hook returns several states which are used by ControlledMenu
and can be spread to its props. See an example.
function useMenuState(options?: {
initialOpen?: boolean;
initialMounted?: boolean;
unmountOnClose?: boolean;
transition?: boolean | {
open?: boolean;
close?: boolean;
item?: boolean;
};
transitionTimeout?: number;
}): [
// Menu props object which can be spread to <ControlledMenu/>
{
state?: 'opening' | 'open' | 'closing' | 'closed';
endTransition: () => void;
},
// toggleMenu function
(open?: boolean) => void
];
The hook function options are the same as props on Menu
component.
toggleMenu
:
- If no parameter is supplied, this function will toggle state between open and close phases.
- You can set a boolean parameter to explicitly switch into one of the two phases.
The Menu component uses this hook internally to manage its states.
useClick
#useClick
hook can be used with ControlledMenu to create a toggle menu which has a similar experience to the Menu
component.
The Hook returns an object with props which can be spread to the anchor element. See an example.
useHover
#useHover
hook can be used with ControlledMenu to create a hover menu.
The Hook returns an object with anchorProps
and hoverProps
, which can be spread to the anchor element and menu, respectively. See an example.
Accessibility
#React-Menu implements WAI-ARIA roles, states, and properties which adhere to the WAI-ARIA Authoring Practices. For more details, please refer to the website.
Keyboard
#React-Menu supports the following keyboard interactions:
Menu
- Return activates a menu item and closes the menu.
- Space activates a menu item and closes the menu; for radio and checkbox item, activates the menu item without closing the menu.
- Down Arrow moves focus to the next item, wrapping from the last to the first.
- Up Arrow moves focus to the previous item, wrapping from the first to the last.
- Home moves focus to the first item.
- End moves focus to the last item.
- Esc Closes a menu and move focus to its associated menu button.
- Left Arrow Closes a submenu if it is open.
- Return | Space | Right Arrow When focus is in a submenu item, opens the submenu, and moves focus to the first menu item.
MenuButton
- Return | Space | Down Arrow opens the associated menu and moves focus to the first menu item.
- Up Arrow opens the associated menu and moves focus to the last menu item.