MenuTrigger
The MenuTrigger serves as a wrapper around a Menu and its associated trigger, linking the Menu's open state with the trigger's press state.
| install | yarn add @react-spectrum/menu |
|---|---|
| version | 3.6.3 |
| usage | import {MenuTrigger, Menu} from '@react-spectrum/menu' |
Example#
Content#
The MenuTrigger accepts exactly two children: the element which triggers the opening of the Menu and the Menu itself. The trigger element must be the first child passed into the MenuTrigger and should support press events.
Events#
MenuTrigger accepts an onOpenChange handler which is triggered whenever the Menu is opened or closed.
Long press#
By default, a MenuTrigger's Menu is opened by pressing the trigger element or activating it via the Space or Enter keys. However, there may be cases in which your trigger element
should perform a separate default action on press such as selection, and should only display the Menu when long pressed. This behavior can be changed by providing "longPress" to the trigger prop. With this prop, the Menu will only be opened upon
pressing and holding the trigger element or by using the Option (Alt on Windows) + Down Arrow/Up Arrow keys while focusing the trigger element.
The example below illustrates how one would setup a MenuTrigger to have long press behavior.
Props#
| Name | Type | Default | Description |
children | ReactElement[] | — | The contents of the MenuTrigger - a trigger and a Menu. |
trigger | MenuTriggerType | 'press' | How the menu is triggered. |
align | Alignment | 'start' | Alignment of the menu relative to the trigger. |
direction | 'bottom'
| 'top'
| 'left'
| 'right'
| 'start'
| 'end' | 'bottom' | Where the Menu opens relative to its trigger. |
closeOnSelect | boolean | true | Whether the Menu closes when a selection is made. |
shouldFlip | boolean | true | Whether the menu should automatically flip direction when space is limited. |
isOpen | boolean | — | Whether the overlay is open by default (controlled). |
defaultOpen | boolean | — | Whether the overlay is open by default (uncontrolled). |
Events
| Name | Type | Default | Description |
onOpenChange | (
(isOpen: boolean
)) => void | — | Handler that is called when the overlay's open state changes. |
Visual options#
Align and direction#
The align prop aligns the Menu relative to the trigger and the direction prop controls the direction the Menu will render.
Close on selection#
By default, the Menu closes when an item is selected. To change this, set the closeOnSelect prop to false. This might be useful when multiple selection is used. See menu selection for more information.
Flipping#
By default, the Menu flips direction automatically upon opening when space is limited. To change this, set the shouldFlip prop to false. Try scrolling the viewport close to the edge of the trigger in the example to see this in action.
Open#
The isOpen and defaultOpen props on the MenuTrigger control whether the Menu is open by default.
They apply controlled and uncontrolled behavior on the Menu respectively.