close

useMenu

Provides the behavior and accessibility implementation for a menu component. A menu displays a list of actions or options that a user can choose.

Example
Menu.tsx
Menu.css
Button.css
Popover.css
import {MenuButton, MenuItem} from './Menu';

<MenuButton aria-label="Actions" onAction={key => alert(key)}>
  <MenuItem id="copy">Copy</MenuItem>
  <MenuItem id="cut">Cut</MenuItem>
  <MenuItem id="paste">Paste</MenuItem>
</MenuButton>

API

Shows a menu component with labels pointing to its parts, including the trigger, menu, group, section heading, menu item, menu item label, menu item description, and menu item keyboard shortcut elements.Option 1Option 2Menu itemMenu item keyboard shortcutMenu item labelDescriptionDescriptionOption 3DescriptionMenu item descriptionMenuSECTION TITLESection headerSectionKMenu item descriptionPopoverMore ActionsMenu Trigger
<MenuTrigger>
  <Button />
  <Popover>
    <Menu>
      <MenuItem>
        <Text slot="label" />
        <Text slot="description" />
        <Keyboard />
        <SelectionIndicator />
      </MenuItem>
      <Separator />
      <MenuSection>
        <Header />
        <MenuItem />
      </MenuSection>
      <SubmenuTrigger>
        <MenuItem />
        <Popover>
          <Menu />
        </Popover>
      </SubmenuTrigger>
    </Menu>
  </Popover>
</MenuTrigger>
useMenuTriggerState(props: ): useTreeState<T>(props: <T>): <T>useMenuTrigger<T>( props: , state: , ref: <Elementnull> ): <T>useMenu<T>( props: <T>, state: <T>, ref: <HTMLElementnull> ): useMenuItem<T>( props: , state: <T>, ref: <FocusableElementnull> ): useMenuSection(props: ):

Properties

NameType
focusStrategynull

Controls which item will be auto focused when the menu opens.

pointnull

The cursor position when the overlay was triggered, relative to the window viewport.

Methods

open(focusStrategy?: null): void
Opens the menu.
toggle(focusStrategy?: null): void
Toggles the menu.
setOpen(isOpen: boolean): void
Sets whether the overlay is open.
close(): void
Closes the overlay.
setPoint(point: ): void
Sets the cursor position relative to the window viewport.

TreeState

Properties

NameType
collection<<T>>

A collection of items in the tree.

expandedKeysSet<Key>

A set of keys for items that are expanded.

selectionManager

A selection manager to read and update multiple selection state.

disabledKeysSet<Key>

A set of keys for items that are disabled.

Methods

toggleKey(key: Key): void
Toggles the expanded state for an item by its key.
setExpandedKeys(keys: Set<Key>): void
Replaces the set of expanded keys.

AriaMenuTriggerProps

NameType
type'menu''listbox'

The type of menu that the menu trigger opens.

isDisabledboolean

Whether menu trigger is disabled.

trigger

How menu is triggered.

NameType
menuTriggerProps

Props for the menu trigger element.

menuProps<T>

Props for the menu.

AriaMenuOptions

NameType
isVirtualizedboolean

Whether the menu uses virtual scrolling.

keyboardDelegate

An optional keyboard delegate implementation for type to select, to override the default.

shouldUseVirtualFocusboolean

Whether the menu items should use virtual focus instead of being focused directly.

itemsIterable<T>

Item objects in the collection.

selectionMode

The type of selection that is allowed in the collection.

selectedKeys'all'Iterable<Key>

The currently selected keys in the collection (controlled).

defaultSelectedKeys'all'Iterable<Key>

The initial selected keys in the collection (uncontrolled).

onSelectionChange(keys: ) => void

Handler that is called when the selection changes.

disabledKeysIterable<Key>

The item keys that are disabled. These items cannot be selected, focused, or otherwise interacted with.

disallowEmptySelectionboolean

Whether the collection allows empty selection.

shouldFocusWrapboolean

Whether keyboard navigation is circular.

escapeKeyBehavior'clearSelection''none'

Whether pressing the escape key should clear selection in the menu or not.

Most experiences should not modify this option as it eliminates a keyboard user's ability to easily clear selection. Only use if the escape key is being handled externally or should not trigger selection clearing contextually.

NameType
menuPropsDOMAttributes

Props for the menu element.

AriaMenuItemProps

NameType
keyKey

The unique key for the menu item.

isVirtualizedboolean

Whether the menu item is contained in a virtual scrolling menu.

selectionManager

Override of the selection manager. By default, state.selectionManager is used.

NameType
menuItemPropsDOMAttributes

Props for the menu item element.

labelPropsDOMAttributes

Props for the main text element inside the menu item.

descriptionPropsDOMAttributes

Props for the description text element inside the menu item, if any.

keyboardShortcutPropsDOMAttributes

Props for the keyboard shortcut text element inside the item, if any.

isFocusedboolean

Whether the item is currently focused.

isFocusVisibleboolean

Whether the item is keyboard focused.

isSelectedboolean

Whether the item is currently selected.

isPressedboolean

Whether the item is currently in a pressed state.

isDisabledboolean

Whether the item is disabled.

AriaMenuSectionProps

NameType
headingReactNode

The heading for the section.

NameType
itemPropsDOMAttributes

Props for the wrapper list item.

headingPropsDOMAttributes

Props for the heading element, if any.

groupPropsDOMAttributes

Props for the group element.