Skip to
Keyboard shortcuts

Use the up and down arrow keys to move between items. Press Alt+Shift+K to open this list again.

Alpha version out now. The API may still change.

Menu

A button that opens a short list of actions. Someone using only a keyboard opens it, moves with the arrow keys and lands back on the button.

Package
@kvirn-ui/react
Pattern
APG Menu Button
Testing
Automated. Core 0 of 2, Release 0 of 3

When to use it#

  • Use a menu for several related actions on one thing, such as print, download and withdraw on a case, or the view options of a table. It suits staff tools where space is tight.
  • Menus are for actions, not navigation. A list of links to pages is a Navigation or a Disclosure, never a menu.
  • Not for one or two actions, and not for the main action of a page: show visible buttons. In a service for residents, hidden actions are easy to miss, so prefer buttons that are always on screen.
  • Not for a short panel with text or a form: use a Popover. Not for choosing a value in a form: use a Listbox.
  • There are no submenus: a menu never opens another menu.

Example#

Use cases#

Actions for one row of a list

A list of cases often has the same few actions on each row. Give every trigger the row’s name, so “Actions for case 2024-118” and “Actions for case 2024-131” differ. The menu is named by its trigger.

Your cases

  • Case 2024-118
  • Case 2024-131

Warning: Don't forget

Start the trigger’s name with its visible text, then add what it is for, hidden from sight. A name that doesn’t start with the visible text fails 2.5.3 Label in Name, and identical “Actions” buttons leave a screen reader user guessing which row each one belongs to.

An action that asks first

An action that can’t be undone, such as withdrawing an application, opens an AlertDialog from its item. Keep the open state yourself and set it in onSelect. When the dialog closes, focus goes back to the menu’s trigger.

Withdraw the application?

You can’t undo this. We will not process the application.

Props in this example:

An action that is not available now

A disabled item stays in the menu, and the arrow keys still reach it, so people know the action exists. A screen reader says it is unavailable, not why. Say why in text on the page and link it to the item.

Sharing opens when the case is closed.

Props in this example:

Options that stay on or off

A checkbox item turns a setting on or off, and radio items choose one value from a few. Here the checkbox item keeps the menu open, so the user can change more than one setting. A radio item closes the menu, which is the default. Name the radio group, here with aria-label. Without a name, a screen reader reads the choices without saying what they choose between (1.3.1 Info and Relationships).

Grid view is off. Sorted by Name.

Props in this example:

Warning: Don't forget

A change is not announced once the menu has closed, and the closed menu shows nothing. Show the current setting on the page, as the line above the button does.

Know why it closed

Keep the open state yourself when the page needs to react to the menu opening or closing. onOpenChange says why it changed: a press, a key, Escape and so on. This example shows the last reason. With open set, closing is a request: you decide whether to close.

Last change: No change yet.

Props in this example:

Information: Did you know?

Put a confirmation in an element that is always on the page, such as an <output>, never inside the menu: the menu is closed when the action has run.

Accessibility#

What it does for you#

  • APG pattern: Menu Button (the Actions Menu Button example) and the menu roles of Menubar (menuitemcheckbox, menuitemradio, groups, separators).
  • Deviations: none from the APG keyboard practice. Defaults to know: no submenus and no menubar, so ArrowLeft and ArrowRight are not handled; a disabled item stays focusable with aria-disabled (the APG's own rule for composites); items are <button>s; pointer movement over an item focuses it (one highlight).
  • Native elements used: <button> (Trigger and every item), <div popover="auto" role="menu"> (Popup), <div role="group"> (Group, RadioGroup), <div role="separator">. Nothing for Root: it renders no element.

A Menu is a short list of actions that a button opens: print, share, delete. It is not navigation: links to pages belong in a Navigation or a Disclosure, never in role="menu", because a screen reader then switches to a mode that does not read links as links. The browser puts the menu in the top layer, KvirnUI places it next to the button, and focus moves onto its items as the user arrows through them. Enter or Space runs an item, closes the menu and returns focus to the button. Escape and a press outside close it.

PartElement / roleARIA / stateNotes
Menu.Rootnone: it renders no elementnoneOwns the open state: open / defaultOpen / onOpenChange(open, { reason, event }), and placement, offset (4), padding (8). Reasons: trigger-press, key, item-press, escape, outside-press, light-dismiss, tab, focus-out (focus went to an element outside the menu, not by Tab)
Menu.Trigger<button type="button">aria-haspopup="menu", aria-expanded="true" | "false", aria-controls (the popup's id). data-open while openClass kv-menu-trigger. The anchor the popup is placed against. Its name is the consumer's content. Compose with as={Button}
Menu.Popup<div popover="auto" role="menu">, always renderednamed by the trigger (aria-labelledby) unless the consumer sets aria-label or aria-labelledby. data-open, data-placement, data-detachedClass kv-menu-popup. Its items stay mounted while it is closed, so an uncontrolled checkbox or radio item keeps its state. Hidden by the browser while closed (out of the accessibility tree). Inline placement as Popover's. Scrolls inside when too tall
Menu.Item<button type="button" role="menuitem">, tabindex 0 on the focused item, -1 on the restaria-disabled="true" while disabled. data-disabled, data-highlighted while it has focusClass kv-menu-item. onSelect(event), closeOnSelect (default true), disabled, textValue (the typeahead label, default the trimmed text). Rendered as a link it warns: actions only
Menu.CheckboxItem<button type="button" role="menuitemcheckbox">aria-checked="true" | "false", as Item. data-checkedClass kv-menu-checkbox-item. checked / defaultChecked / onCheckedChange(checked). Closes the menu by default
Menu.RadioGroup<div role="group">named by aria-label or aria-labelledby (the consumer's)Class kv-menu-radio-group. value / defaultValue / onValueChange(value). A dev warning fires without a name
Menu.RadioItem<button type="button" role="menuitemradio">aria-checked, as Item. data-checkedClass kv-menu-radio-item. value is required. Selecting it sets the group's value
Menu.Group<div role="group">aria-labelledby the GroupLabel inside it, unless the consumer names itClass kv-menu-group. A dev warning fires with neither a GroupLabel nor a name
Menu.GroupLabel<div> (not focusable, not an item)its id is the group's aria-labelledbyClass kv-menu-group-label
Menu.Separator<div role="separator">noneClass kv-menu-separator. Not an item: arrows and typeahead skip it
useMenuthe same attributes, for your own elementstriggerProps, popupProps, getItemProps(kind, options), isOpen, placementOptions as the Root. The refs are in the props: mergeProps merges yours

Each rule below has a test:

  • Wiring. aria-controls is the popup's id on every render, aria-expanded follows the state, aria-haspopup="menu" says what opens. The trigger is a real <button type="button">. The popup is named by the trigger unless the consumer names it.
  • Items. Roving tabindex: the focused item has tabindex="0", every other item -1, and focus moves onto items with .focus(). While no item has focus nothing in the menu is tabbable, so the menu is never a Tab stop of its own. Items are found in the DOM at key time ([role^="menuitem"]), so a conditional item, a fragment or a custom wrapper just works. A separator and a group label are not items.
  • Disabled items are focusable, reachable by arrows and typeahead, and say aria-disabled="true". Activation is blocked and the menu stays open. A screen reader reads them as "dimmed" or "unavailable".
  • Checkbox and radio items toggle or select on activation, are controlled or uncontrolled, and close the menu by default (closeOnSelect={false} keeps it open). A radio item in a group with value is checked when its value equals the group's.
  • Dismissal. Escape closes the innermost layer only. A press outside closes it (the trigger is not outside: its press toggles). Focus moving to an element outside the menu another way (a screen reader command, a script) closes it too (focus-out). The platform hiding the popup (another auto popover opening) closes it in the state too (light-dismiss).
  • Composition. Inside a vertical Toolbar, ArrowDown on the trigger opens the menu and the toolbar does not also move focus. Under Tooltip.Trigger, the tooltip closes while the menu is open. Inside a Dialog, the menu stacks above it and Escape closes the menu only. The popup leaves the toolbar's context.
  • Server rendering. The server writes the trigger and the closed popup with its items (popover, role="menu", data-placement), and no inline position.
  • as. Menu.Trigger takes a component (as={Button}, or Toolbar.Item as={Menu.Trigger} the other way round) that must render a button and spread its props on a DOM node; its props are plain props, merged with the part's (handlers chain, className joins, refs merge). A trigger that is not a <button> warns in development. Allowed elements: Popup, Group and RadioGroup div (default) or section; GroupLabel div (default), span or p; another tag warns once in development and falls back. The items (Item, CheckboxItem, RadioItem) and Separator have no as: an action is a <button> with a menu role, and a link in a menu would be announced as a menu item.
  • Dev warnings: a part outside a Root; a Trigger that is not a <button>; a RadioGroup or Group with no name.

It announces nothing.

What you need to do#

  • Render Menu.Popup right after Menu.Trigger, in the same parent. Tab and Shift+Tab leave the menu as documented only if the popup follows the trigger in the DOM (2.4.3 Focus Order (A)).
  • Use it for actions, not navigation. Links to pages are a Navigation or a Disclosure. A Menu.Item has no as, so it can't be made a link.
  • Name the trigger with visible text, or aria-label for an icon-only button (4.1.2 Name, Role, Value (A); 2.5.3 Label in Name (A)). The popup is named by it unless you pass aria-label.
  • Name a RadioGroup and a Group: a GroupLabel or aria-label (1.3.1 Info and Relationships (A)).
  • Give an item with non-text content a textValue so typeahead has a label.
  • A single action is a button, not a menu with one item.
  • Don't hide the only way to do something in a menu without a visible hint that it exists: the trigger names it ("Actions").
  • Move focus yourself when an item opens a Dialog, or let the Dialog do it: a Dialog with no Trigger of its own that an item opened returns focus to the menu's trigger once the menu has closed.
  • Everything in button.a11y.md applies to the trigger.

Warning: Don't forget

Put Menu.Popup right after Menu.Trigger, in the same parent. Otherwise Tab and Shift+Tab don’t leave the menu as described under Keyboard (2.4.3 Focus Order).

Visual and modes#

  • Focus indicator: the focused item is the highlight (a solid fill in the default theme, the system's highlight colour in forced colours). A checked state is never shown by colour alone: a tick and a dot are drawn from borders. The trigger is a Button and shows the standard focus ring, at least 2px thick with at least 3:1 contrast against its background (2.4.7 Focus Visible (AA); the default theme also meets 2.4.13 Focus Appearance (AAA, not counted toward the AA bar)).
  • Target size: items are never smaller than 24 by 24 CSS pixels, and larger in the default density (2.5.8 Target Size (Minimum) (AA)).
  • Colour: highlighted, disabled and checked items keep at least 4.5:1 contrast for text (1.4.3 Contrast (Minimum) (AA)) and the popup's edge at least 3:1 (1.4.11 Non-text Contrast (AA)).
  • forced-colors behaviour: the popup keeps a visible edge in the system's text colour, the highlighted item uses the system's highlight colours, and checked state is drawn from borders.
  • reduced-motion behaviour: no positional animation, ever.
  • Reflow: at 320px the popup is never wider than the screen minus its padding, and scrolls inside when it is too tall. No horizontal scrolling at 320 CSS px (1.4.10 Reflow, AA): the reflow sweep runs every story at 320 CSS px and fails on horizontal overflow.
  • RTL: bottom-start lines the popup's right edge with the trigger's. ArrowUp and ArrowDown never flip.

Focus management#

  • Initial focus: moves onto an item when the menu is opened after mount, in an effect after the popup is shown: the first item for Enter, Space, a press and ArrowDown, the last for ArrowUp. A menu with no items keeps focus on the trigger. A menu that starts open (defaultOpen, or open true on the first render) is open but moves no focus on load (3.2.1 On Focus (A)): focus stays where it was, and Escape and the arrows work once focus is in it. Only a later opening (a press, a key, open going from false to true) takes focus.
  • Trap: no. Nothing is inert and Tab leaves the menu, closing it. Page content behind the menu stays reachable once it is closed.
  • Restore to: the trigger, then the element that had focus before the menu opened, after an item runs, Escape and an outside press that lost focus to body once focus had been in the menu (or the menu had an opener). A defaultOpen menu the platform closes before anyone interacted leaves focus alone. Never body. After an outside press the return does not scroll the page (preventScroll); after Escape or an item it does, so the trigger comes into view (2.4.11 Focus Not Obscured (Minimum) (AA)). Tab, Shift+Tab and a focus-out close leave focus where it went. Focus the user moved to another control is never taken.
  • Never obscured by: the popup never covers its anchor (2.4.11 Focus Not Obscured (Minimum) (AA)), it sits in the top layer and the highlighted item is scrolled into view by focusing it. A consumer with a sticky header keeps scroll-padding.

WCAG success criteria#

  • 1.3.1 Info and Relationships (A), 4.1.2 Name, Role, Value (A): the menu roles, aria-checked, groups, separators, the trigger's aria-haspopup, aria-expanded and aria-controls.
  • 2.1.1 Keyboard (A), 2.1.2 No Keyboard Trap (A): every item reachable by keys, Tab leaves.
  • 2.4.3 Focus Order (A): focus enters the menu on open and returns to the trigger.
  • 2.4.7 Focus Visible (AA), 2.4.11 Focus Not Obscured (Minimum) (AA): the highlight, and the popup never covers its anchor.
  • 1.4.3 Contrast (Minimum) (AA): the text of highlighted, disabled and checked items.
  • 2.5.3 Label in Name (A): the trigger is named by its visible text (Consumer responsibilities).
  • 2.5.8 Target Size (Minimum) (AA): items are never smaller than 24 by 24 CSS pixels.
  • 3.2.1 On Focus (A): opening needs a press or a key, and nothing moves focus on its own.

Keyboard#

Focus
roving tabindex
Selection follows focus
n/a
Arrows wrap
yes
Shortcuts
none
KeyWhereWhat happens
Tabbefore the triggerMoves focus to the trigger. The trigger is one stop and the closed menu has none
Test: menu.test.tsx › Tab focuses the trigger, and is one stop
Shift+Tabon the triggerMoves focus to the previous focusable element
Test: menu.test.tsx › Shift+Tab leaves the trigger backwards
Enter / Spaceon the triggerOpens the menu and moves focus to the first item
Test: menu.test.tsx › Enter and Space on the trigger open the menu with focus on the first item
ArrowDown / ArrowUpon the triggerOpens the menu and moves focus to the first / last item
Test: menu.test.tsx › ArrowDown and ArrowUp on the trigger open the menu on the first and last item
Pointer presson the triggerOpens the menu, or closes it when it is open. Opening focuses the first item. A closing press does not reopen it
Test: menu.test.tsx › a pointer press on the trigger toggles the menu, and a closing press does not reopen it
ArrowDown / ArrowUpin the menuMoves focus to the next / previous item, wrapping from the last to the first and back. Disabled items are included. Up and Down never flip in right-to-left text
Test: menu.test.tsx › ArrowDown and ArrowUp move between items, wrap and include disabled items
Home / Endin the menuMoves focus to the first / last item
Test: menu.test.tsx › Home and End move to the first and last item
Printable characterin the menuMoves focus to the next item whose text starts with the characters typed, by the page's locale. The word ends after 500 ms. A repeated character cycles. Ctrl, Alt, Meta and IME input are ignored
Test: menu.test.tsx › a character moves focus by typeahead, a word narrows it and a pause resets it
Spacein the menu, typeahead word in progressAdds a space to the word and does not activate the item
Test: menu.test.tsx › Space during a typeahead word adds a space and does not activate
Enter / Spaceon an itemRuns the item, toggles a checkbox item or selects a radio item, closes the menu and returns focus to the trigger. With closeOnSelect={false} the menu stays open
Test: menu.test.tsx › Enter and Space on an item run it, close the menu and return focus to the trigger
Enter / Spaceon a disabled itemDoes nothing. The menu stays open
Test: menu.test.tsx › Enter and Space on a disabled item do nothing and keep the menu open
Escapein the menuCloses the innermost layer only and returns focus to the trigger
Test: menu.test.tsx › Escape closes the menu and returns focus to the trigger, and only the menu
Tabin the menuMoves focus on to the next focusable element after the trigger and closes the menu. Tab is never prevented: the menu is no trap
Test: menu.test.tsx › Tab in the menu closes it and moves focus to the next focusable after the trigger
Shift+Tabin the menuMoves focus to the previous focusable element, which is the trigger, and closes the menu
Test: menu.test.tsx › Shift+Tab in the menu closes it and moves focus to the trigger
ArrowLeft / ArrowRightin the menuNot handled and not prevented: there are no submenus
Test: menu.test.tsx › ArrowLeft and ArrowRight are not handled or prevented
Pointer move / presson an itemMoving over an item focuses it. Pressing runs it. A press outside the menu closes it
Test: menu.test.tsx › moving the pointer over an item focuses it, a press runs it and a press outside closes the menu

The menu has no shortcuts and handles no other key: Page Up and Page Down are the browser's own, and Escape is read on the document, after the page's handlers (a handler that calls preventDefault() on it keeps the menu open). A Space after a typeahead word has been pause-ended for more than 500 ms activates the item: type, then pause, then Space.

API reference#

import { Menu, useMenu } from '@kvirn-ui/react'

Menu.Root

Renders no element. It owns the open state and passes it to the parts inside it.

PropTypeDefaultDescription
openboolean–Controlled: whether the menu is open. Pair it with onOpenChange.
defaultOpenbooleanfalseUncontrolled: whether the menu starts open.
onOpenChange(open: boolean, details: MenuChangeDetails) => void–Called when the user opens or closes it. It only reports: with open set, you change open yourself. details.reason is trigger-press, key, item-press, escape, outside-press, light-dismiss, tab or focus-out, and details.event is the native event.
placementPlacement'bottom-start'Where the popup goes when there is room: top, bottom, start or end, with -start, -center or -end. It flips when it does not fit, and start and end follow the reading direction.
offsetnumber4The gap between the trigger and the popup, in pixels.
paddingnumber8The space kept to the edge of the viewport, in pixels.
childrenReactNode–The Trigger and the Popup. Root renders no element.

Menu.Trigger

Renders <button type="button">. It takes every attribute of a <button>, and the popup is placed against it.

PropTypeDefaultDescription
asElementType'button'A component to render instead of the button, with its props set on the Trigger: as={Button} className="kv-button--primary". It must forward its ref and spread its props on a DOM node.
Class or attributeValuesMeaning
kv-menu-triggeralwaysThe part class.
aria-haspopup"menu"The trigger opens a menu.
aria-expanded"true" or "false"Whether the popup is open.
aria-controlsthe popup’s idPoints at the popup, which stays in the page while closed.
data-openpresent or absentThe popup is open.

Menu.Popup

Renders <div popover="auto"> with the role menu. The popup and its items stay in the page while it is closed, and the browser hides them. Render it right after the trigger.

PropTypeDefaultDescription
as'div' | 'section''div'The role is menu either way. Another tag warns once in development and renders a div.
Class or attributeValuesMeaning
kv-menu-popupalwaysThe part class.
popover"auto"Puts the popup in the top layer. The browser hides it while closed.
role"menu"Named by the trigger unless you give it aria-label or aria-labelledby.
data-openpresent or absentThe popup is open.
data-placementa Placement, such as bottom-startThe side the popup is on now, after it flipped.
data-detachedpresent or absentThe trigger is scrolled out of view. The popup is hidden until it comes back.

Menu.Item

Renders <button type="button"> with the role menuitem. It is never a link: for a destination, use Navigation.

PropTypeDefaultDescription
onSelect(event: MouseEvent<HTMLButtonElement>) => void–Runs when the item is chosen by Enter, Space or a press. It does not run on a disabled item. Call event.preventDefault() to keep the menu open and skip the toggle of a checkbox or radio item.
closeOnSelectbooleantrueCloses the menu after the item is chosen. Set false to keep it open.
disabledbooleanfalseSets aria-disabled and data-disabled. The item stays focusable, so the arrows and typeahead reach it, but it cannot be chosen.
textValuestringthe item’s trimmed textThe text typeahead matches against, when the visible text is not the right label.
Class or attributeValuesMeaning
kv-menu-itemalwaysThe part class.
role"menuitem"A checkbox item is "menuitemcheckbox" and a radio item "menuitemradio".
aria-disabled"true" or absentThe item cannot be chosen.
data-disabledpresent or absentThe item cannot be chosen.
data-highlightedpresent or absentThe item has focus.

Menu.CheckboxItem

Renders <button> with the role menuitemcheckbox. It toggles when chosen.

PropTypeDefaultDescription
checkedboolean–Controlled: whether the item is checked. Pair it with onCheckedChange.
defaultCheckedbooleanfalseUncontrolled: whether the item starts checked.
onCheckedChange(checked: boolean) => void–Called with the new value when the item is chosen.
onSelect(event: MouseEvent<HTMLButtonElement>) => void–Runs when the item is chosen by Enter, Space or a press. It does not run on a disabled item. Call event.preventDefault() to keep the menu open and skip the toggle of a checkbox or radio item.
closeOnSelectbooleantrueCloses the menu after the item is chosen. Set false to keep it open.
disabledbooleanfalseSets aria-disabled and data-disabled. The item stays focusable, so the arrows and typeahead reach it, but it cannot be chosen.
textValuestringthe item’s trimmed textThe text typeahead matches against, when the visible text is not the right label.
Class or attributeValuesMeaning
kv-menu-checkbox-item, kv-menu-radio-itemalwaysThe part class.
aria-checked"true" or "false"Whether it is checked or chosen.
data-checkedpresent or absentWhether it is checked or chosen.
data-disabled, data-highlightedpresent or absentAs on Item.

Menu.RadioGroup

Renders <div role="group"> around the radio items that share a value. It is not the RadioGroup component.

PropTypeDefaultDescription
valuestring–Controlled: the value of the chosen item. Pair it with onValueChange.
defaultValuestring–Uncontrolled: the value of the item that starts chosen.
onValueChange(value: string) => void–Called with the value of the item that was chosen.
as'div' | 'section''div'The role is group either way. Another tag warns once in development and renders a div.
Class or attributeValuesMeaning
kv-menu-radio-groupalwaysThe part class.
role"group"Name it with aria-label or aria-labelledby.

Menu.RadioItem

Renders <button> with the role menuitemradio. Put it inside a Menu.RadioGroup.

PropTypeDefaultDescription
valuestringRequiredThe value this item stands for inside its RadioGroup.
onSelect(event: MouseEvent<HTMLButtonElement>) => void–Runs when the item is chosen by Enter, Space or a press. It does not run on a disabled item. Call event.preventDefault() to keep the menu open and skip the toggle of a checkbox or radio item.
closeOnSelectbooleantrueCloses the menu after the item is chosen. Set false to keep it open.
disabledbooleanfalseSets aria-disabled and data-disabled. The item stays focusable, so the arrows and typeahead reach it, but it cannot be chosen.
textValuestringthe item’s trimmed textThe text typeahead matches against, when the visible text is not the right label.
Class or attributeValuesMeaning
kv-menu-checkbox-item, kv-menu-radio-itemalwaysThe part class.
aria-checked"true" or "false"Whether it is checked or chosen.
data-checkedpresent or absentWhether it is checked or chosen.
data-disabled, data-highlightedpresent or absentAs on Item.

Menu.Group

Renders <div>. It takes every attribute of a <div> and passes ref to it. It has the role group. Name it with a GroupLabel.

PropTypeDefaultDescription
as'div' | 'section''div'The role is group either way. Another tag warns once in development and renders a div.
Class or attributeValuesMeaning
kv-menu-groupalwaysThe part class.
role"group"Named by the GroupLabel inside it.

Menu.GroupLabel

Renders <div>. It takes every attribute of a <div> and passes ref to it. It names the Group around it.

PropTypeDefaultDescription
as'div' | 'span' | 'p''div'Another tag warns once in development and renders a div.
Class or attributeValuesMeaning
kv-menu-group-labelalwaysThe part class.

Menu.Separator

Renders <div>. It takes every attribute of a <div> and passes ref to it. It has the role separator: a divider between items.

Class or attributeValuesMeaning
kv-menu-separatoralwaysThe part class.
role"separator"A divider between items.

useMenu

The hook gives props for your own elements: a trigger, a popup and the items in it. Put the popup right after the trigger in the DOM, and mark the item that has focus with isCurrent.

Options

PropTypeDefaultDescription
openboolean–Controlled: whether the menu is open. Pair it with onOpenChange.
defaultOpenbooleanfalseUncontrolled: whether the menu starts open.
onOpenChange(open: boolean, details: MenuChangeDetails) => void–Called when the user opens or closes it. It only reports: with open set, you change open yourself. details.reason is trigger-press, key, item-press, escape, outside-press, light-dismiss, tab or focus-out, and details.event is the native event.
placementPlacement'bottom-start'Where the popup goes when there is room: top, bottom, start or end, with -start, -center or -end. It flips when it does not fit, and start and end follow the reading direction.
offsetnumber4The gap between the trigger and the popup, in pixels.
paddingnumber8The space kept to the edge of the viewport, in pixels.

Result

PropTypeDefaultDescription
isOpenboolean–Whether the menu is open.
placementPlacement–The placement in use. It differs from the one asked for when the popup flipped.
triggerPropsMenuTriggerPartProps–Spread on a <button>: type, aria-haspopup, aria-expanded, aria-controls, data-open, ref and the pointer, click and key handlers.
popupPropsMenuPopupPartProps–Spread on a <div>: popover, role menu, aria-labelledby the trigger, data-open, data-placement, ref and the handlers.
getItemProps(kind: 'item' | 'checkbox' | 'radio', options?: MenuItemOptions) => MenuItemPartProps–The props of one item: its role, tabindex, aria-disabled, aria-checked and handlers. The options are disabled, closeOnSelect, onSelect, textValue, checked and isCurrent, true on the item that has focus.

Known issues#

  • A menu that starts open does not take focus, so a keyboard user has to Tab to the trigger and press ArrowDown (no item is tabbable until one has focus). Start a menu open only for demonstrations and tests.
  • A Tab pressed during IME composition is ignored by the menu, so a Tab that then leaves the page for browser chrome (focus to nowhere) does not close it.
  • The focused item has tabindex="0", the others -1 (roving tabindex). A scrolling region needs a tabbable element for axe (scrollable-region-focusable), and items at -1 do not count. Tab and Shift+Tab still leave the menu from the item, because no other element in the popup is tabbable; the popup itself is not focusable.
  • A checkbox or radio change is not announced after the menu closes. Focus returns to the trigger, which does not carry the new state. Show the state in the trigger's text where it matters.
  • Disabled items stay reachable, so a screen reader user hears "dimmed". That is deliberate (discoverability), and a reason to explain why an action is unavailable: explain it in visible text on the page, and point the item's aria-describedby at it, so a screen reader reads "Share, dimmed, <reason>" (menu.test.tsx › a disabled item points at the reason on the page with aria-describedby).
  • Hover moves focus (one highlight, as native menus do). A magnifier user may find the focus moving under the pointer surprising.
  • No submenus in this version, and no context menu or menubar.
  • Tab is never intercepted. The browser moves focus from the item and the menu closes on that focus move (deferred one task, reason tab), with no focus return. Chromium does as documented; check Safari and Firefox yourself.

Testing#

The automated tests prove the keyboard behaviour, the names and states, that axe finds no violations in every story and theme, and, for the components they cover, that the stories hold up in forced colours, reduced motion, text spacing and a 320 pixel width. An Automated row passes on these tests alone. A Core or Release row passes only when an automated agent check and a person on the real assistive technology have both checked it. "agent-verified" means automated tests in the repository (with a virtual screen reader for the screen reader rows), not the real assistive technology.

AT + browser + OSAgent checkHuman checkResultNotes
Automated (re-runnable tests)
axe, WCAG 2.2 A and AA, every story, 4 themespassn/apassmenu.stories.tsx, every story, 4 themes
Markup and ARIA (roles, names, ids, states)passn/apassmenu.test.tsx › a disabled item has aria-disabled and data-disabled, and is still a button that takes focus; expectNoA11yViolations
Keyboard contract rows (Vitest browser)passn/apass16 rows, each names a test (menu.test.tsx › Tab focuses the trigger, and is one stop)
Focus order and managementpassn/apassmenu.test.tsx asserts focus (toHaveFocus); first rule: Tab focuses the trigger, and is one stop
Display modes (forced colours, 320px reflow, text spacing, reduced motion)passn/apassthe four sweep projects (storybook-forced-colors, -reduced-motion, -reflow, -text-spacing), run in CI
Core (required for beta)
NVDA + Chrome + Windowspendingpendingpending
VoiceOver + Safari + iOSpendingpendingpending
Release (before 1.0 and each minor)
JAWS + Chrome + Windowspendingpendingpending
TalkBack + Chrome + Androidpendingpendingpending
VoiceOver + Safari + macOSpendingpendingpending

Also pending, by hand: Tab out of the menu on Safari and Firefox (the focus start point after the popup closes), and how screen readers read disabled items.

What to check#

With a screen reader and the keyboard, try:

  • Tab to the button, then press Enter or ArrowDown: the first item has focus, and its name and role are read.
  • ArrowDown past the last item wraps to the first. Typing a first letter jumps to a matching item.
  • A disabled item is read as unavailable, with its linked reason.
  • Choose a checkbox or radio item: note what is read, and check that the page shows the new setting.
  • Escape closes the menu and focus returns to the button.
  • Tab while the menu is open closes it and moves to the next control.