A Menu
is a widget that offers a list of choices to the user, such as a set of actions or functions.
The various elements inside a menu are given as <MenuItem/>
, or <MenuHeading/>
in case of Menu Heading. Menus can also have nested menus using a SubMenu
.
A <MenuDivider/>
can be used to separate between groups of similar menu items.
A menu is usually opened, or made visible, by activating a Dropdown
, or DropdownButton
, choosing an item in a menu that opens a sub menu, or by invoking a command, such as Shift + F10 in Windows, that opens a context specific menu. When a user activates a choice in a menu, the menu usually closes unless the choice opened a submenu.
A menu that is visually persistent is a menubar. A menubar is typically horizontal and is often used to create a menu bar similar to those found near the top of the window in many desktop applications, offering the user quick access to a consistent set of commands.
A common convention for indicating that a menu item launches a dialog box is to append "…" (ellipsis) to the menu item label, e.g., "Save as …".
The Menu
component implements the Menu or Menu bar design pattern, per WAI-ARIA 1.1.
The following description of keyboard behaviors assumes:
- A horizontal
menubar
containing severalmenuitem
elements. - All items in the
menubar
have child submenus that contain multiple vertically arranged items. - Some of the
menuitem
elements in the submenus have child submenus with items that are also vertically arranged.
When reading the following descriptions, also keep in mind that:
- Focusable elements, which may have role
menuitem
,menuitemradio
, ormenuitemcheckbox
, are referred to as items. - If a behavior applies to only certain types of items, e.g.,
menuitem
elements, the specific role name is used. - Submenus, also known as pop-up menus, are elements with role
menu
. - Except where noted, menus opened from a menu button behave the same as menus opened from a menubar.
-
When a
menu
opens, or when amenubar
receives focus, keyboard focus is placed on the first item. All items are focusable and keyboard focus is managed within the menu using a roving tabindex as described in § 6.6 Keyboard Navigation Inside Components. -
Enter:
- When focus is on a
menuitem
that has a submenu, opens the submenu and places focus on its first item. - Otherwise, activates the item and closes the menu.
- When focus is on a
-
Space:
- (Optional): When focus is on a
menuitemcheckbox
, changes the state without closing the menu. - (Optional): When focus is on a
menuitemradio
that is not checked, without closing the menu, checks the focusedmenuitemradio
and unchecks any other checkedmenuitemradio
element in the same group. - (Optional): When focus is on a
menuitem
that has a submenu, opens the submenu and places focus on its first item. - (Optional): When focus is on a
menuitem
that does not have a submenu, activates themenuitem
and closes the menu.
- (Optional): When focus is on a
-
Down Arrow:
- When focus is on a
menuitem
in amenubar
, opens its submenu and places focus on the first item in the submenu. - When focus is in a
menu
, moves focus to the next item, optionally wrapping from the last to the first.
- When focus is on a
-
Up Arrow:
- When focus is in a
menu
, moves focus to the previous item, optionally wrapping from the first to the last. - (Optional): When focus is on a
menuitem
in amenubar
, opens its submenu and places focus on the last item in the submenu.
- When focus is in a
-
Right Arrow:
-
When focus is in a
menubar
, moves focus to the next item, optionally wrapping from the last to the first. -
When focus is in a
menu
and on amenuitem
that has a submenu, opens the submenu and places focus on its first item. -
When focus is in a
menu
and on an item that does not have a submenu, performs the following 3 actions:- Closes the submenu and any parent menus.
- Moves focus to the next
menuitem
in themenubar
. - Either: (Recommended) opens the submenu of that
menuitem
without moving focus into the submenu, or opens the submenu of thatmenuitem
and places focus on the first item in the submenu.
Note that if the
menubar
were not present, e.g., the menus were opened from a menu button, Right Arrow would not do anything when focus is on an item that does not have a submenu.
-
-
Left Arrow:
-
When focus is in a
menubar
, moves focus to the previous item, optionally wrapping from the first to the last. -
When focus is in a submenu of an item in a
menu
, closes the submenu and returns focus to the parentmenuitem
. -
When focus is in a submenu of an item in a
menubar
, performs the following 3 actions:- Closes the submenu.
- Moves focus to the previous
menuitem
in themenubar
. - Either: (Recommended) opens the submenu of that
menuitem
without moving focus into the submenu, or opens the submenu of thatmenuitem
and places focus on the first item in the submenu.
-
-
Home: If arrow key wrapping is not supported, moves focus to the first item in the current
menu
ormenubar
. -
End: If arrow key wrapping is not supported, moves focus to the last item in the current
menu
ormenubar
. -
Any key that corresponds to a printable character (Optional): Move focus to the next menu item in the current menu whose label begins with that printable character.
-
Escape: Close the
menu
that contains focus and return focus to the element or context, e.g., menu button or parentmenuitem
, from which the menu was opened. -
Tab: Moves focus to the next element in the tab sequence, and if the item that had focus is not in a
menubar
, closes itsmenu
and all open parentmenu
containers. -
Shift + Tab: Moves focus to the previous element in the tab sequence, and if the item that had focus is not in a
menubar
, closes itsmenu
and all open parentmenu
containers.
-
Disabled menu items are focusable but cannot be activated.
-
A separator, (e.g.
MenuDivider
) in a menu is not focusable or interactive. -
If a
menu
is opened or amenubar
receives focus as a result of a context action, Escape or Enter may return focus to the invoking context. For example, a rich text editor may have amenubar
that receives focus when a shortcut key, e.g., alt + F10, is pressed while editing. In this case, pressing Escape or activating a command from the menu may return focus to the editor. -
Although it is recommended that authors avoid doing so, some implementations of navigation menubars may have
menuitem
elements that both perform a function and open a submenu. In such implementations, Enter and Space perform a navigation function, e.g., load new content, while Down Arrow, in a horizontal menubar, opens the submenu associated with that samemenuitem
. -
When items in a
menubar
are arranged vertically and items inmenu
containers are arranged horizontally:- Down Arrow performs as Right Arrow is described above, and vice versa.
- Up Arrow performs as Left Arrow is described above, and vice versa.
-
A menu is a container of items that represent choices. The element serving as the menu has a role of either
menu
ormenubar
. -
The items contained in a menu are child elements of the containing menu or menubar and have any of the following roles:
-
If activating a
menuitem
opens a submenu, themenuitem
is known as a parentmenuitem
. A submenu'smenu
element is:- Contained inside the same
menu
element as its parentmenuitem
. (Note: React Spectrum usesaria-owns
to define the relationship between the parentmenuitem
and its expanded submenu) - Is the sibling element immediately following its parent
menuitem
.
- Contained inside the same
-
A parent
menuitem
hasaria-haspopup
set to eithermenu
ortrue
. -
A parent
menuitem
hasaria-expanded
set tofalse
when its childmenu
is not visible and set totrue
when the childmenu
is visible. -
One of the following approaches is used to enable scripts to move focus among items in a
menu
as described in § 6.6 Keyboard Navigation Inside Components:- The menu container has
tabindex
set to-1
or0
andaria-activedescendant
set to the ID of the focused item. - Each item in the menu has
tabindex
set to-1
, except in a menubar, where the first item hastabindex
set to 0.
- The menu container has
-
When a
menuitemcheckbox
ormenuitemradio
is checked,aria-checked
is set totrue
. -
When a menu item is disabled,
aria-disabled
is set to true. -
Items in a menu may be divided into groups:
- The simplest to define a group is by placing an element with a role of separator between groups.
For example, this technique should be used when a menu contains a set of
menuitemradio
items. - To define a labelled group, items should be contained within an element with the role of
group
. The group element should be labelled usingaria-label
or witharia-labelledby
referencing a visible text element heading, included as a child of the group, byid
. Since amenu
does not normally expect a text node or heading as a descendant, this visible text element heading should be removed from the accessibility tree as a standalone accessibility object usingaria-hidden
.
- The simplest to define a group is by placing an element with a role of separator between groups.
For example, this technique should be used when a menu contains a set of
-
All separators should have
aria-orientation
consistent with the separator's orientation. -
If a
menubar
has a visible label, the element with rolemenubar
hasaria-labelledby
set to a value that refers to the labelling element. Otherwise, themenubar
element has a label provided byaria-label
. -
If a
menubar
is vertically oriented, it hasaria-orientation
set tovertical
. The default value ofaria-orientation
for amenubar
ishorizontal
. -
An element with role
menu
either has:aria-labelledby
set to a value that refers to themenuitem
orbutton
that controls its display.- A label provided by
aria-label
.
-
If a
menu
is horizontally oriented, it hasaria-orientation
set tohorizontal
. The default value ofaria-orientation
for amenu
isvertical
.
If aria-owns
is set on the menu
container to include elements that are not DOM children of the container, those elements will appear in the reading order in the sequence they are referenced and after any items that are DOM children. Scripts that manage focus need to ensure the visual focus order matches this assistive technology reading order.
In React Spectrum, Menu
has been implemented as a List
contained within a Popover
. MenuItem
, MenuHeading
and MenuDivider
In the Menu
component:
- The
Menu
component uses rolemenu
by default, but may also use rolelistbox
depending on context. For example, inAutocomplete
, theMenu
uses rolelistbox
). MenuItem
component accepts rolemenuitem
,menuitemcheckbox
, andmenuitemradio
, but may also use roleoption
depending on context. For example, inAutocomplete
,MenuItem
uses roleoption
)
When a MenuItem
has role menuitemcheckbox
or menuitemradio
, aria-checked
is used to indicate the selected state, rather than aria-selected
.
Menu
inherits keyboard interaction from List
, which uses the FocusManager
utility component with the following parameters:
itemSelector
equal to.spectrum-Menu-item:not(.is-disabled)
.selectedItemSelector
equal to.spectrum-Menu-item:not(.is-disabled).is-selected
.typeToSelect
defaults totrue
- In
Menu
,autoFocus
defaults totrue
, so that theMenu
will receive focus when it mounts within anOverlay
. - When rendered in a
Portal
using anOverlay
,Menu
andSubMenu
both trap focus, pressingTab
orShift + Tab
does not advance to the next or previous tabbable element, because the next focusable element before or after the element that opened theMenu
is difficult to determine without crawling the DOM.
Popover
List
ListItem
OverlayTrigger