Disabled menu items must remain in the arrow-key focus sequence so that keyboard users are aware they exist, even though they cannot be activated. Removing a disabled item from the focus sequence hides potentially useful information: users may not realize that a feature exists but is currently unavailable.
Menu
Description
A menu is a widget that offers a list of choices to the user, such as a set of actions or functions. Menu widgets behave like native operating system menus such as the menus that pull down from the menu bars commonly found at the top of many desktop application windows.
Menus are distinct from other selection widgets. Unlike a listbox or a native
HTML <select> element, which allow users to choose and input one or more values from a
set of options, a menu is not used for value selection. Rather, it presents a list of
actions or commands to be executed.
Menus come in two primary forms:
A Popup Menu (
role="menu") is a list of actions or functions that remains hidden until a user requests it by activating a menu button. It is then presented in an overlay—often termed a “popup” or “dropdown.”A Visible Menu (
role="menubar") is a type of menu with a persistent horizontal or vertical bar of menu triggers, similar to the application menubars found in desktop software (for example, File, Edit, View).
Menus are often hierarchical, where a menu items listed in a either a menu or menubar opens a child menu.
In either case, focus is managed such that keyboard users navigate between a menu’s interactive elements using arrow keys. The menu is just one stop in the keyboard tab sequence.
When to use
Use a menu in a web application to allow users to perform an action (for example, “Copy” or “Paste”) or select an option (“Light” or “Dark” mode) for using that application.
When encountering a menu, keyboard and screen reader users expect the widget to
function like menus found in desktop software, expecting to use arrow
keys to navigate between menu items and the Tab key to navigate out of the
menu to the next or previous interactive element in the tab sequence.
Menus should not be used for site navigation. Activating a link in a menu could cause an unexpected change of context. The Site Navigation entry addresses how to create accessible site navigation.
A menu should not be used as a substitute for a select element, listbox or
combobox, which are used to select a value for a subsequent action. Those
patterns carry different semantics and user expectations.
Example
HTML does not provide elements that can provide the semantics and function
needed to create a menu or menubar without the use of ARIA and JavaScript.
Though <menu> is a valid HTML element, it has the default ARIA role of list
and is semantically and functionally equivalent to the HTML <ul> element.
In the following example, ARIA and JavaScript are added to a composition of
generic HTML and <button> elements to create an accessible menu.
A simple popup menu
In this example, the roving tabindex technique is used to manage keyboard
focus by setting tabindex="0" on whichever button has keyboard focus and
setting tabindex="-1" on all others.
The example also uses the following techniques:
- The menu button retains its default role of
buttonwhileroleis explicitly set tomenuitem,menuitemcheckboxormenuitemradio. Using thebuttonelement allows users to interact with each menu item as they would a button to trigger an action, then the HTML structure is modified with custom JavaScript. - The
<hr>elements have their default role ofseparator. - The
<fieldset>element, which has the default role ofgroup, is used to provide therole=menuitemradiobutton group with an accessible name with itsaria-label. - An
aria-checkedattribute on eachmenuitemcheckboxandmenuitemradioitem indicates its selection state. - The Paste menu item is disabled by setting
disabledon the button until something is on the clipboard to paste. In this case, thearia-disabled="true"attribute would be redundant, but would be required to inform assistive technologies of the disabled state if an element other thanbuttonwhere used that does not support thedisabledattribute.
HTML, menu closed
In its closed state, the menu button has aria-expanded="false" and
tabindex="0", while all of the menu items have tabindex="-1".
1<button id="actions-btn"2 aria-controls="actions-menu"3 aria-haspopup="menu"4 aria-expanded="false"5 tabindex="0">Actions6</button>78<div id="actions-menu" role="menu" aria-labelledby="actions-btn" hidden>9 <button role="menuitem" tabindex="-1">10 Copy11 </button>12 <button role="menuitem" tabindex="-1" disabled>13 Paste14 </button>15 <hr />16 <button role="menuitemcheckbox" tabindex="-1" aria-checked="false">17 Show preview18 </button>19 <hr />20 <fieldset aria-label="Text size">21 <button role="menuitemradio" tabindex="-1" aria-checked="true">22 Small text23 </button>24 <button role="menuitemradio" tabindex="-1" aria-checked="false">25 Large text26 </button>27 </fieldset>28</div>
HTML, menu open
In its open state, the menu button has aria-expanded="true" and
tabindex="-1", while one of the menu item buttons has tabindex="0".
1<button id="actions-btn"2 aria-controls="actions-menu"3 aria-haspopup="menu"4 aria-expanded="true"5 tabindex="-1">Actions6</button>78<div id="actions-menu" role="menu" aria-labelledby="actions-btn">9 <button role="menuitem" tabindex="0">10 Copy11 </button>12 <button role="menuitem" tabindex="-1" disabled>13 Paste14 </button>15 <hr />16 <button role="menuitemcheckbox" tabindex="-1" aria-checked="false">17 Show preview18 </button>19 <hr />20 <fieldset aria-label="Text size">21 <button role="menuitemradio" tabindex="-1" aria-checked="true">22 Small text23 </button>24 <button role="menuitemradio" tabindex="-1" aria-checked="false">25 Large text26 </button>27 </fieldset>28</div>
Relevant ARIA Attributes
The previous example illustrates the use of ARIA attributes in a menu.
Identifies a composite widget containing a list of actions that a user can
invoke. Typically hidden until opened by a user operating a menu button and
navigated using arrow keys with the implicit aria-orientation of vertical.
Identifies a type of menu that is persistent with the implicit
aria-orientation of horizontal.
Identifies an interactive element that operates as a button within a menu or menubar to trigger an action.
Identifies a type of menuitem whose action is to set or unset a persistent
option. Must use aria-checked to indicate state.
Identifies a member of a group of menuitemradio elements where selecting one
deselects the other. Must use aria-checked to indicate state.
Identifies a group of menuitem, menuitemcheckbox, or menuitemradio
elements. Should have an accessible name to designate the group.
Identifies an element that visually and semantically separates groups of items within a menu. Does not receive focus and requires no accessible name.
Set to "true" or "false" on a menuitemcheckbox or menuitemradio to indicate its selected state.
Set to "true" on an element with role menuitem, menuitemcheckbox or
menuitemradio that does not natively support the disabled state when that
element is disabled with JavaScript.
Set on an element with role of menu, menubar, menuitem,
menuitemcheckbox, menuitemradio, or group to provide an accessible name
when an accessible name cannot be derived from its text content.
When an element (perhaps a heading) provides a visible label for a menu or
menubar, set aria-labelledby rather than aria-label to provide the same
accessible name as that available to sighted users.
Keyboard users expect to use Up and Down arrow keys when operating a menu with
vertical orientation and Left and Right keys when operating one in a horizontal
orientation. The implicit aria-orientation value for role="menu" is
"vertical" and "horizontal" for role="menubar". Explicitly set
aria-orientation when the visible structure differs from default and ensure
that keyboard operation is consistent with the menu orientation.
When menu focus is managed using the active descendant technique, set the
aria-activedescendant attribute of the menu button to the ID of the focused
menu item, if there is one.
When menu focus is managed using the roving tabindex technique, set
tabindex="0" on the menu item or menu button that should receive keyboard
focus and tabindex="-1" on all others.
The Menu Button entry discusses the ARIA attributes aria-expanded,
aria-haspopup and aria-controls attributes required for a menu button that
is used to open a menu.
Structure and Semantics
All users must be able to identify a menu, understand its structure, and distinguish between its items. Semantic roles and attributes communicate the menu’s purpose, the nature of its items, and any state information (such as whether an item is checked or disabled) to assistive technologies.
Evinced accessibility analysis checks menu structure and semantic in the following tests:
Role
The semantic elements that comprise a menu or menubar are limited to the ARIA
roles menu, menubar, menuitem, menuitemcheckbox, menuitemradio,
group and separator. All elements must be owned by a menu or menubar
container, either directly or through a group element.
Element has incorrect role
| Standard | Criteria |
|---|---|
| WCAG | 4.1.2 Name, Role, Value |
| EN 301 549 | 9.4.1.2 Name, Role, Value |
| Section 508 | 1194.21(d) |
Impact: Critical
Using an incorrect or invalid role in a menu or menubar causes assistive technologies to misrepresent the widget’s purpose and keyboard-interaction model to users.
- Set the appropriate role of
menuormenubaron the menu container. - Set the appropriate role of
menuitem,menuitemcheckboxormenuitemradioon each action-triggering element in the menu. - Group menu items as needed with a
<fieldset>element or other element withrole="group". - Separate groups of menu items as needed with an
<hr>element or other element withrole="separator".
ARIA roles should not be arbitrarily applied to semantic HTML elements. Refer to the W3C recommendation ARIA in HTML for the appropriate use of ARIA with semantic HTML elements.
Menu Has Menuitems
A menu or menubar container must own at least one element with
role="menuitem", role="menuitemcheckbox", or role="menuitemradio". If the
container has no children with one of these required roles, assistive
technologies cannot identify it as a functional menu and users cannot navigate
or activate any items.
No group members with required role
| Standard | Criteria |
|---|---|
| WCAG | 1.3.1 Info and Relationships |
| EN 301 549 | 9.1.3.1 Info and Relationships |
| Section 508 | N/A |
Impact: Serious
Ensure the menu container directly owns, or owns through a group element, at
least one valid menu item role. Elements with other roles, such as button or
option, do not satisfy this requirement.
Accessibility Label
A menu or menubar without a label provides no context to screen reader users about the purpose of the widget. This is especially important when a page contains multiple menus.
Missing label
| Standard | Criteria |
|---|---|
| WCAG | 1.3.1 Info and Relationships |
| EN 301 549 | 9.1.3.1 Info and Relationships |
| Section 508 | N/A |
Impact: Serious
As composites, elements with role=menu or role=menubar do not have a default
label nor can they be provided one with the HTML label element. Provide an
accessible name for every menu and menubar with an ARIA attribute:
- Use
aria-labelledbyto reference the id of a visible label or the triggering button. - Use
aria-labelwhen no visible label is available.
1<!-- Labeled by its trigger button -->2<ul role="menu" aria-labelledby="file-menu-btn">3 ...4</ul>56<!-- Labeled directly -->7<ul role="menubar" aria-label="Application menu">8 ...9</ul>
Menu Item Names
When a name cannot be derived from the text content of a menuitem,
menuitemcheckbox, and menuitemradio element, it must be provided one using
aria-label, aria-labelledby or adding visually hidden text content. Without
a name, screen readers cannot convey the item’s purpose to the user, making the
menu effectively unusable.
Group members missing accessible names
| Standard | Criteria |
|---|---|
| WCAG | 4.1.2 Name, Role, Value |
| EN 301 549 | 9.4.1.2 Name, Role, Value |
| Section 508 | 1194.21(d) |
Impact: Critical
The accessible name for a menu item is most commonly derived from its text
content. When a menu item contains only an icon, provide a name via aria-label
or by including visually hidden text.
In this example, JavaScript was used to add button functionality to an <li>
element.
1<li role="menuitem" aria-label="Delete file">2 <svg aria-hidden="true" focusable="false">...</svg>3</li>
Group contains duplicate names
Best practice.
Impact: Serious
Menu items within the same menu should have unique accessible names. When two or more items share the same name, users cannot distinguish between them, which creates a confusing and potentially error-prone interaction for screen reader users navigating by item name.
If duplicate item labels are intentional (for example, multiple "Edit" items for
different targets), provide additional context using aria-describedby or by
incorporating the target’s name into each item’s accessible name, for example,
“Edit document” and “Edit template.”
Checked Menu Items
Menu items with role="menuitemcheckbox" or role="menuitemradio" must have an
aria-checked value of "true" or "false" that changes when the item is
triggered.
Missing aria-checked attribute
| Standard | Criteria |
|---|---|
| WCAG | 4.1.2 Name, Role, Value |
| EN 301 549 | 9.4.1.2 Name, Role, Value |
| Section 508 | 1194.21(d) |
Impact: Critical
Ensure that each menuitemcheckbox and menuitemradio has an aria-checked
attribute with a "true" or "false" value consistent with its state.
aria-checked value not toggled
| Standard | Criteria |
|---|---|
| WCAG | 4.1.2 Name, Role, Value |
| EN 301 549 | 9.4.1.2 Name, Role, Value |
| Section 508 | 1194.21(d) |
Impact: Critical
The aria-checked value of a menuitemcheckbox or menuitemradio must change
when toggled with the Space key.
Aria Expanded
The action of a menuitem may open a submenu of a menu or a menubar. When
it does, it must meet the accessibility criteria of a menu button. Refer to the
Aria Expanded entry in Menu Button for details.
Aria Haspopup
The action of a menuitem may open a submenu of a menu or a menubar. When
it does, it must meet the accessibility criteria of a menu button. Refer to the
Aria Haspopup entry in Menu Button for details.
Site Navigation Detected
The menu and menubar roles are not recommended for site navigation. Refer to
the Site Navigation entry for recommendations for site navigation.
Misused role for site navigation
| Standard | Criteria |
|---|---|
| WCAG | 4.1.2 Name, Role, Value |
| EN 301 549 | N/A |
| Section 508 | N/A |
Impact: Serious
Disabled
Element type does not support disabled state
| Standard | Criteria |
|---|---|
| WCAG | 4.1.2 Name, Role, Value |
| EN 301 549 | 9.4.1.2 Name, Role, Value |
| Section 508 | 1194.21(d) |
Impact: Serious
The disabled HTML attribute is a feature of native form controls and
interactive elements such as buttons. Applying disabled to a generic element
such as a <div> or <li> used as a menu item has no semantic or functional
effect. JavaScript must be used to disable custom menu items and use
aria-disabled="true" to programmatically communicate the disabled state to
assistive technologies.
Conflicting disabled values
| Standard | Criteria |
|---|---|
| WCAG | 4.1.2 Name, Role, Value |
| EN 301 549 | 9.4.1.2 Name, Role, Value |
| Section 508 | 1194.21(d) |
Impact: Serious
Applying conflicting disabled indicators—for example, inconsistently setting the
disabled HTML attribute and aria-disabled="true" or styling an item as
visually enabled while aria-disabled="true" is set. Doing so creates an
inconsistent experience between what sighted users see and what assistive
technologies report. Ensure the visual presentation of a menu item’s disabled
state matches its programmatic state exactly.
Relationships
A menu exists in relationship with the control that opens it and, optionally, with groups that organize its items. The triggering element must expose its association with the menu, and any structural groupings within the menu must be programmatically conveyed.
Evinced accessibility analysis checks menu relationships in the following tests:
Aria Owns
The aria-owns attribute is set on a menu or menuitem to convey a
parent-child relationship between a (parent) menu and a (child) menu item when a
menu item cannot be contained within the menu’s DOM hierarchy. If it is, a
parent-child relationship is implied.
Avoid using aria-owns if possible. Assistive technology support is poor and it
can produce an inconsistent reading order. If you must use it:
- Preserve DOM order: DOM children of the container should come before
explicitly owned elements in the DOM order unless you explicitly list the DOM
children in
aria-ownsin the desired reading order. - Match expected roles: All elements specified in
aria-ownsmust have roles that are allowed to be children of the container. - Avoid duplicate or inconsistent references: An element’s
idmust not appear in more than one element’saria-ownsattribute at any time.
Invalid aria-owns values
Best practice.
Elements referenced by the aria-owns attribute on a menu or menubar
element should have an allowed child role of menuitem, menuitemcheckbox,
menuitemradio, group or separator.
If the owned elements roles are incorrect, screen readers can misinterpret the relationships within the menu, which can confuse users.
Avoid aria-owns
Best practice.
While aria-owns can define a parent-child relationship between elements that
are not nested in the DOM, it is often poorly supported by assistive
technologies and can produce an unpredictable reading order. For menus, it is
best practice to use standard DOM nesting: place menuitem elements as direct
descendants of the menu or menubar container, or nest them within a group
element that is itself a child of the container.
Operation
Pointer operation
A user with a mouse, touch screen, or other pointing device should be able to:
Click or tap a menu button to display its hidden menu.
Move the pointer over menu items to highlight them.
Click or tap a menu item to execute its action. The menu typically closes after activation.
Click or tap outside the menu to dismiss it without making a selection.
Keyboard operation
Keyboard interaction is central to a menu’s accessibility. Users navigating with a keyboard must be able to open, navigate, and close a menu without a pointer device. The most critical requirements are:
- Users must be able to reach the menu trigger with
Taband activate it withEnterorSpace. - Arrow keys must move focus between menu items without leaving the menu.
- Users must be able to select an option or action with
EnterorSpace. Escapemust close the menu and return focus to the trigger without triggering a menu item.Tabmust close the menu and move focus forward in the page tab sequence.Shift+Tabmust close the menu and move focus backward in the page tab sequence.
Keyboard users expect the following behavior when a key is pressed:
A menu or menubar should be a single stop in the focus sequence, focusing on the menu trigger button when the menu is hidden or the first menu item in a visible menu when tabbing forward or the last when tabbing in reverse order.
When focused on a menu button or menu item, the action of the Tab key should be to set focus on the next interactive item after the menu or, using Shift+Tab, the next previous interactive item without triggering a menu action.
- With focus on a menu button, opens its associated menu and moves focus to the first item.
- With focus on a menu item, trigger its action. In the case of a “popup” menu
triggered with a menu button, the menu should be closed and focus moved to the
triggering menu button unless keeping the menu active is beneficial for user
experience, which could be the case when a menu contains
menuitemcheckboxormenuitemradioelements.
For a menu in a vertical orientation (default for menu):
With focus on a menu button (or menu item in a
menubar) that triggers a hidden menu, open its associated menu withDown Arrowmoving focus to the first item andUp Arrowmoving focus to the first.With focus on a menu item,
Down Arrowmoves focus to the next item, optionally wrapping to the first item, andUp Arrowmoves focus to the previous item, optionally wrapping to the last.
For a menu in a horizontal orientation (default for menubar):
In a menubar, moves focus to the next top-level item. On any menuitem that
has a submenu, opens that submenu and moves focus to its first item.
In a menubar, moves focus to the previous top-level item. When focus is inside a submenu, closes the submenu and returns focus to the parent item.
Moves focus to the first menu item.
Moves focus to the last menu item.
Moves focus to the next item whose accessible name begins with that character (typeahead).
Closes the menu and returns focus to the element that opened it.
Closes the menu. Focus moves to the next focusable element in the page’s tab sequence; it does not move through menu items.
Closes the menu. Focus moves to the previous focusable element in the page’s tab sequence.
Activates the focused menuitem and closes the menu. If the focused menuitem
has a submenu, Enter opens the submenu and moves focus to its first item.
Toggles the checked state of a menuitemcheckbox, or selects a menuitemradio
item and deselects others in the group. The menu remains open after the state
change, allowing users to interact with multiple items in a single session.
Evinced accessibility analysis checks menu operability in the following tests:
Focus Sequence
Composite unpredictable focus
| Standard | Criteria |
|---|---|
| WCAG | 2.1.1 Keyboard |
| EN 301 549 | 9.2.1.1 Keyboard |
| Section 508 | N/A |
Impact: Serious
Focus behavior within the menu must be predictable and follow the expected arrow-key navigation pattern. If focus moves to unexpected elements, skips items, or behaves inconsistently when a user navigates with arrow keys, the menu is not reliably operable by keyboard. Ensure that focus follows a consistent, sequential path through all enabled menu items and that no item is bypassed unexpectedly.
Redundant focusable children
| Standard | Criteria |
|---|---|
| WCAG | 1.3.1 Info and Relationships |
| EN 301 549 | 9.1.3.1 Info and Relationships |
| Section 508 | N/A |
Impact: Moderate
Menu items should not contain nested focusable elements (such as buttons or
links) that are independently reachable via Tab or arrow keys. Focusable
children inside a menuitem are redundant and disruptive: they interfere with
the menu’s managed focus pattern, may trap keyboard users, and are likely to be
ignored or misread by screen readers.
If a menu item requires a secondary action, consider placing it outside the menu
or implementing it as a separate menuitem.
Composite not in focus sequence
| Standard | Criteria |
|---|---|
| WCAG | 2.1.1 Keyboard |
| EN 301 549 | 9.2.1.1 Keyboard |
| Section 508 | N/A |
Impact: Critical
The menu component itself, or one or more of its items, is not reachable through
keyboard interaction. A menu that keyboard users cannot reach or navigate is
entirely inaccessible to those users. Ensure that the menu trigger is in the
page’s Tab sequence and that all enabled menu items are reachable via arrow
keys once the menu is open.
Skipped shift-tab evaluation
The tester was unable to evaluate reverse (Shift+Tab) keyboard navigation for
this menu. This may occur when another focus sequence or keyboard operation test
have failed. Manually verify that:
- Pressing
Escapecloses the menu and returns focus specifically to the element that opened it. - Pressing
Tabcloses the menu and moves focus to the next focusable element in the page’s tab sequence (not necessarily the trigger). - Pressing
Shift+Tabcloses the menu and moves focus to the previous focusable element in the page’s tab sequence. - In no case is focus lost or sent to the document body.
Keyboard Navigation
Arrow key navigation failed
The tester attempted to navigate the menu using arrow keys but was unable to do
so. This may indicate that arrow key event handlers are missing or not
functioning correctly, or that the menu items are not receiving focus. Verify
that keydown event listeners are registered on the menu container and that
they correctly move focus between menuitem elements in response to ArrowUp
and ArrowDown (or ArrowLeft and ArrowRight for a horizontal menubar).
Disabled menu item not discoverable
| Standard | Criteria |
|---|---|
| WCAG | 1.3.1 Info and Relationships |
| EN 301 549 | 9.1.3.1 Info and Relationships |
| Section 508 | N/A |
Impact: Minor
Ensure that items with aria-disabled="true" are still reachable via arrow key
navigation and are announced by screen readers as disabled.
Menu unexpected focus shift
| Standard | Criteria |
|---|---|
| WCAG | 2.1.1 Keyboard |
| EN 301 549 | 9.2.1.1 Keyboard |
| Section 508 | N/A |
Impact: Critical
After navigating within a menu or activating a menu item, focus must move to a predictable location. Unexpected focus shifts—such as focus jumping to an unrelated part of the page, returning to the top of the document, or disappearing entirely—disorient keyboard and screen reader users. Ensure that:
- When a menu item is activated, focus moves to the expected location (typically the trigger, or the newly revealed content).
- When a menu is closed without making a selection, focus returns to the control that opened it.
- Focus is never lost (sent to
<body>or dropped entirely).
Skipped menu arrow key navigation
The tester was unable to complete an evaluation of arrow key navigation for this
menu. This typically occurs when the menu is not open during testing, the menu
items are not receiving keyboard events, or focus management prevents the test
runner from simulating arrow key interaction. Manually verify that all menu
items are reachable via ArrowUp and ArrowDown keys (or ArrowLeft and
ArrowRight for a menubar).
Keyboard Navigation Orientation
Invalid aria-orientation value
The aria-orientation attribute accepts only "horizontal" or "vertical" as
valid values. An invalid value (for example, a misspelling or a custom string)
is ignored by assistive technologies, which may then fall back to an incorrect
default and communicate wrong key behavior to users. If aria-orientation is
used, verify that its value is exactly "horizontal" or "vertical".
Conflicting aria-orientation
| Standard | Criteria |
|---|---|
| WCAG | 2.1.1 Keyboard |
| EN 301 549 | 9.2.1.1 Keyboard |
| Section 508 | N/A |
Impact: Serious
The aria-orientation value does not match the menu’s visual or functional
orientation. For example, a menu that lays out its items horizontally but has
aria-orientation="vertical" will cause screen readers to instruct users to use
the wrong arrow keys to navigate. Ensure the aria-orientation attribute
accurately reflects the rendered layout:
role="menu"has an implicitaria-orientationof"vertical". Only setaria-orientation="horizontal"when the menu is rendered horizontally.role="menubar"has an implicitaria-orientationof"horizontal". Only setaria-orientation="vertical"when the menubar is rendered vertically.- Omit the attribute entirely when it matches the implicit default for the role.
Skipped Menus
Skipped submenu analysis
A menuitem that should have opened a submenu failed to do so. Such a
menuitem should function as a menu button and satisfy the tests for Menu
Control Sanity in the Menu Button entry.
In addition to the possible issues listed there, this test could fail if the
submenu fails to open on a Arrow Right or, if the parent menu’s orientation is
horizontal, Arrow Down key action on the parent menu’s triggering menuitem.
Unexpected Interactives
Unexpected interactive elements in menu
| Standard | Criteria |
|---|---|
| WCAG | 4.1.2 Name, Role, Value |
| EN 301 549 | 9.4.1.2 Name, Role, Value |
| Section 508 | 1194.21(d) |
Impact: Minor
Interactive elements such as buttons, links, or form controls with in a menu
or menubar container that do not have the role of menuitem,
menuitemcheckbox or menuitemradio are unexpected and can break the menu’s
focus management and reading order.
Screen readers may expose these elements in ways that are inconsistent with the
menu’s interaction model, confusing users about the available actions. Ensure
that all interactive content within a menu is wrapped in an element with an
appropriate menu item role (menuitem, menuitemcheckbox, or menuitemradio).
Event Listener Type
Deprecated keypress listener
The keypress event is deprecated and its behavior is inconsistent across
browsers, particularly with non-Latin input methods (IME). For menu keyboard
handling, use keydown events instead. The keydown event fires for all keys,
including arrow keys, Escape, Enter, and Space, and is consistently
supported across browsers and assistive technology combinations.
keydown event handler example
1menuContainer.addEventListener("keydown", (event) => {2 switch (event.key) {3 case "ArrowDown":4 focusNextItem();5 event.preventDefault();6 break;7 case "ArrowUp":8 focusPreviousItem();9 event.preventDefault();10 break;11 case "Escape":12 closeMenu();13 triggerButton.focus();14 break;15 }16});