useMenuTrigger

Provides the behavior and accessibility implementation for a menu trigger.

installyarn add @react-aria/menu
version3.4.3
usageimport {useMenuTrigger} from '@react-aria/menu'

API#


useMenuTrigger( props: MenuTriggerAriaProps, state: MenuTriggerState, ref: RefObject<HTMLElement> ): MenuTriggerAria

Features#


There is no native element to implement a menu in HTML that is widely supported. useMenuTrigger combined with useMenu helps achieve accessible menu components that can be styled as needed.

  • Exposed to assistive technology as a button with a menu popup using ARIA (combined with useMenu)
  • Support for mouse, touch, and keyboard interactions
  • Keyboard support for opening the menu using the arrow keys, including automatically focusing the first or last item accordingly

Anatomy#


OpenEditRenameMenuMore ActionsMenu Trigger

A menu trigger consists of a button or other trigger element combined with a popup menu. It should be combined with useButton and useMenu, which handle the implementation of the button and popup menu respectively.

useMenuTrigger returns props that you should spread onto the appropriate element:

NameTypeDescription
menuTriggerPropsAriaButtonPropsProps for the menu trigger element.
menuPropsHTMLAttributes<HTMLElement>Props for the menu.

State is managed by the useMenuTriggerState hook from @react-stately/menu. The state object should be passed as an option to useMenuTrigger.

Example#


This example shows how to build a menu button using useMenuTrigger, useButton, and useMenu.

The menu popup uses useMenu and useMenuItem to render the menu and its items. In addition, a <FocusScope> is used to automatically restore focus to the trigger when the menu closes. A hidden <DismissButton> is added at the start and end of the menu to allow screen reader users to dismiss it easily.

This example does not do any advanced popover positioning or portaling to escape its visual container. See useOverlayTrigger for an example of how to implement this using useOverlayPosition.

In addition, see useMenu for examples of menu item groups, and more complex item content.

Internationalization#


RTL#

In right-to-left languages, the menu button should be mirrored. The arrow should be on the left, and the label should be on the right. In addition, the content of menu items should flip. Ensure that your CSS accounts for this.