Menu
Menus display a list of actions or options that a user can choose.
install | yarn add @react-spectrum/menu |
---|---|
version | 3.6.4 |
usage | import {Menu, MenuTrigger} from '@react-spectrum/menu' |
Example#
<MenuTrigger>
<ActionButton>
Edit
</ActionButton>
<Menu onAction={(key) => alert(key)}>
<Item key="cut">Cut</Item>
<Item key="copy">Copy</Item>
<Item key="paste">Paste</Item>
<Item key="replace">Replace</Item>
</Menu>
</MenuTrigger>
<MenuTrigger>
<ActionButton>
Edit
</ActionButton>
<Menu onAction={(key) => alert(key)}>
<Item key="cut">Cut</Item>
<Item key="copy">Copy</Item>
<Item key="paste">Paste</Item>
<Item key="replace">Replace</Item>
</Menu>
</MenuTrigger>
<MenuTrigger>
<ActionButton>
Edit
</ActionButton>
<Menu
onAction={(key) =>
alert(key)}
>
<Item key="cut">
Cut
</Item>
<Item key="copy">
Copy
</Item>
<Item key="paste">
Paste
</Item>
<Item key="replace">
Replace
</Item>
</Menu>
</MenuTrigger>
Content#
Menu follows the Collection Components API, accepting both static and dynamic collections. Similar to Picker, Menu accepts <Item>
elements as children, each with a key
prop. Basic usage of Menu, seen in the example above, shows multiple items populated with a string. Static collections, as in this example, can be used when the full list of items is known ahead of time.
Dynamic collections, as shown below, can be used when the items come from an external data source such as an API call, or update over time. Providing the data in this way allows for Menu to automatically cache the rendering of each item, which dramatically improves performance.
As seen below, an iterable list of items is passed to the Menu using the items
prop. Each item accepts a key
prop, which is passed to the event handler to identify the selected item. Alternatively, if the item objects contain an id property, this is then used automatically and a key prop is not required. See the Events section for more detail on selection and actions.
function Example() {
let menuItems = [
{name: 'Cut'},
{name: 'Copy'},
{name: 'Paste'},
{name: 'Replace'}
];
return (
<MenuTrigger>
<ActionButton>
Edit
</ActionButton>
<Menu items={menuItems}>
{item => <Item key={item.name}>{item.name}</Item>}
</Menu>
</MenuTrigger>
);
}
function Example() {
let menuItems = [
{name: 'Cut'},
{name: 'Copy'},
{name: 'Paste'},
{name: 'Replace'}
];
return (
<MenuTrigger>
<ActionButton>
Edit
</ActionButton>
<Menu items={menuItems}>
{item => <Item key={item.name}>{item.name}</Item>}
</Menu>
</MenuTrigger>
);
}
function Example() {
let menuItems = [
{ name: 'Cut' },
{ name: 'Copy' },
{ name: 'Paste' },
{ name: 'Replace' }
];
return (
<MenuTrigger>
<ActionButton>
Edit
</ActionButton>
<Menu
items={menuItems}
>
{(item) => (
<Item
key={item
.name}
>
{item.name}
</Item>
)}
</Menu>
</MenuTrigger>
);
}
Trays#
On mobile, Menus automatically display in a tray instead of a popover to improve usability.
Internationalization#
In order to internationalize a Menu, the content provided to all child items should be localized. For languages that are read right-to-left (e.g. Hebrew and Arabic), the layout of the Menu is flipped.
Events#
Use the onSelectionChange
prop as a callback to handle press events on items when selectionMode is either single or multiple. See Selection for more information.
Menu also supports the onAction
callback when selectionMode
is none
(default).
function Example() {
let [action, setAction] = React.useState('');
return (
<>
<MenuTrigger>
<ActionButton>
Edit
</ActionButton>
<Menu onAction={setAction}>
<Item key="cut">Cut</Item>
<Item key="copy">Copy</Item>
<Item key="paste">Paste</Item>
</Menu>
</MenuTrigger>
<p>Action: {action}</p>
</>
);
}
function Example() {
let [action, setAction] = React.useState('');
return (
<>
<MenuTrigger>
<ActionButton>
Edit
</ActionButton>
<Menu onAction={setAction}>
<Item key="cut">Cut</Item>
<Item key="copy">Copy</Item>
<Item key="paste">Paste</Item>
</Menu>
</MenuTrigger>
<p>Action: {action}</p>
</>
);
}
function Example() {
let [
action,
setAction
] = React.useState('');
return (
<>
<MenuTrigger>
<ActionButton>
Edit
</ActionButton>
<Menu
onAction={setAction}
>
<Item key="cut">
Cut
</Item>
<Item key="copy">
Copy
</Item>
<Item key="paste">
Paste
</Item>
</Menu>
</MenuTrigger>
<p>
Action: {action}
</p>
</>
);
}
Selection#
Menu supports multiple selection modes. By default, selection is disabled, however this can be changed using the selectionMode
prop.
Use defaultSelectedKeys
to provide a default set of selected items (uncontrolled) and selectedKeys
to set the selected items (controlled). The value of the selected keys must match the key
prop of the items.
See the react-stately
Selection docs for caveats regarding selection prop typing.
function Example() {
let [selected, setSelected] = React.useState(new Set(['middle']));
return (
<>
<MenuTrigger>
<ActionButton>
Align
</ActionButton>
<Menu
selectionMode="single"
selectedKeys={selected}
onSelectionChange={setSelected}
>
<Item key="left">Left</Item>
<Item key="middle">Middle</Item>
<Item key="right">Right</Item>
</Menu>
</MenuTrigger>
<p>Current selection (controlled): {[...selected]}</p>
</>
);
}
function Example() {
let [selected, setSelected] = React.useState(
new Set(['middle'])
);
return (
<>
<MenuTrigger>
<ActionButton>
Align
</ActionButton>
<Menu
selectionMode="single"
selectedKeys={selected}
onSelectionChange={setSelected}
>
<Item key="left">Left</Item>
<Item key="middle">Middle</Item>
<Item key="right">Right</Item>
</Menu>
</MenuTrigger>
<p>Current selection (controlled): {[...selected]}</p>
</>
);
}
function Example() {
let [
selected,
setSelected
] = React.useState(
new Set(['middle'])
);
return (
<>
<MenuTrigger>
<ActionButton>
Align
</ActionButton>
<Menu
selectionMode="single"
selectedKeys={selected}
onSelectionChange={setSelected}
>
<Item key="left">
Left
</Item>
<Item key="middle">
Middle
</Item>
<Item key="right">
Right
</Item>
</Menu>
</MenuTrigger>
<p>
Current selection
(controlled):
{' '}
{[...selected]}
</p>
</>
);
}
Set selectionMode
prop to multiple
to allow more than one selection.
function Example() {
let [selected, setSelected] = React.useState(new Set(['Sidebar', 'Console']));
return (
<>
<MenuTrigger closeOnSelect={false}>
<ActionButton>
Show
</ActionButton>
<Menu
selectionMode="multiple"
selectedKeys={selected}
onSelectionChange={setSelected}
>
<Item key="Sidebar">Sidebar</Item>
<Item key="Searchbar">Searchbar</Item>
<Item key="Tools">Tools</Item>
<Item key="Console">Console</Item>
</Menu>
</MenuTrigger>
<p>Current selection (controlled): {[...selected].join(', ')}</p>
</>
);
}
function Example() {
let [selected, setSelected] = React.useState(
new Set(['Sidebar', 'Console'])
);
return (
<>
<MenuTrigger closeOnSelect={false}>
<ActionButton>
Show
</ActionButton>
<Menu
selectionMode="multiple"
selectedKeys={selected}
onSelectionChange={setSelected}
>
<Item key="Sidebar">Sidebar</Item>
<Item key="Searchbar">Searchbar</Item>
<Item key="Tools">Tools</Item>
<Item key="Console">Console</Item>
</Menu>
</MenuTrigger>
<p>
Current selection (controlled):{' '}
{[...selected].join(', ')}
</p>
</>
);
}
function Example() {
let [
selected,
setSelected
] = React.useState(
new Set([
'Sidebar',
'Console'
])
);
return (
<>
<MenuTrigger
closeOnSelect={false}
>
<ActionButton>
Show
</ActionButton>
<Menu
selectionMode="multiple"
selectedKeys={selected}
onSelectionChange={setSelected}
>
<Item key="Sidebar">
Sidebar
</Item>
<Item key="Searchbar">
Searchbar
</Item>
<Item key="Tools">
Tools
</Item>
<Item key="Console">
Console
</Item>
</Menu>
</MenuTrigger>
<p>
Current selection
(controlled):
{' '}
{[...selected]
.join(', ')}
</p>
</>
);
}
Sections#
Menu supports sections in order to group options. Sections can be used by wrapping groups of Items in a Section
component. Each Section
takes a title
and key
prop.
Static Items#
function Example() {
let [selected, setSelected] = React.useState(new Set(['bold', 'left']));
return (
<MenuTrigger>
<ActionButton>
Edit
</ActionButton>
<Menu
selectionMode="multiple"
selectedKeys={selected}
onSelectionChange={setSelected}
>
<Section title="Styles">
<Item key="bold">Bold</Item>
<Item key="underline">Underline</Item>
</Section>
<Section title="Align">
<Item key="left">Left</Item>
<Item key="middle">Middle</Item>
<Item key="right">Right</Item>
</Section>
</Menu>
</MenuTrigger>
);
}
function Example() {
let [selected, setSelected] = React.useState(
new Set(['bold', 'left'])
);
return (
<MenuTrigger>
<ActionButton>
Edit
</ActionButton>
<Menu
selectionMode="multiple"
selectedKeys={selected}
onSelectionChange={setSelected}
>
<Section title="Styles">
<Item key="bold">Bold</Item>
<Item key="underline">Underline</Item>
</Section>
<Section title="Align">
<Item key="left">Left</Item>
<Item key="middle">Middle</Item>
<Item key="right">Right</Item>
</Section>
</Menu>
</MenuTrigger>
);
}
function Example() {
let [
selected,
setSelected
] = React.useState(
new Set([
'bold',
'left'
])
);
return (
<MenuTrigger>
<ActionButton>
Edit
</ActionButton>
<Menu
selectionMode="multiple"
selectedKeys={selected}
onSelectionChange={setSelected}
>
<Section title="Styles">
<Item key="bold">
Bold
</Item>
<Item key="underline">
Underline
</Item>
</Section>
<Section title="Align">
<Item key="left">
Left
</Item>
<Item key="middle">
Middle
</Item>
<Item key="right">
Right
</Item>
</Section>
</Menu>
</MenuTrigger>
);
}
Dynamic Items#
Sections used with dynamic items are populated from a hierarchical data structure. Similarly to the props on Menu, <Section>
takes an array of data using the items
prop.
function Example() {
let [selected, setSelected] = React.useState(new Set([1,3]));
let openWindows = [
{
name: 'Left Panel',
id: 'left',
children: [
{id: 1, name: 'Final Copy (1)'}
]
},
{
name: 'Right Panel',
id: 'right',
children: [
{id: 2, name: 'index.ts'},
{id: 3, name: 'package.json'},
{id: 4, name: 'license.txt'}
]
}
];
return (
<MenuTrigger>
<ActionButton>
Window
</ActionButton>
<Menu
items={openWindows}
selectionMode="multiple"
selectedKeys={selected}
onSelectionChange={setSelected}>
{item => (
<Section items={item.children} title={item.name}>
{item => <Item>{item.name}</Item>}
</Section>
)}
</Menu>
</MenuTrigger>
);
}
function Example() {
let [selected, setSelected] = React.useState(
new Set([1, 3])
);
let openWindows = [
{
name: 'Left Panel',
id: 'left',
children: [
{ id: 1, name: 'Final Copy (1)' }
]
},
{
name: 'Right Panel',
id: 'right',
children: [
{ id: 2, name: 'index.ts' },
{ id: 3, name: 'package.json' },
{ id: 4, name: 'license.txt' }
]
}
];
return (
<MenuTrigger>
<ActionButton>
Window
</ActionButton>
<Menu
items={openWindows}
selectionMode="multiple"
selectedKeys={selected}
onSelectionChange={setSelected}
>
{(item) => (
<Section items={item.children} title={item.name}>
{(item) => (
<Item>
{item.name}
</Item>
)}
</Section>
)}
</Menu>
</MenuTrigger>
);
}
function Example() {
let [
selected,
setSelected
] = React.useState(
new Set([1, 3])
);
let openWindows = [
{
name: 'Left Panel',
id: 'left',
children: [
{
id: 1,
name:
'Final Copy (1)'
}
]
},
{
name:
'Right Panel',
id: 'right',
children: [
{
id: 2,
name:
'index.ts'
},
{
id: 3,
name:
'package.json'
},
{
id: 4,
name:
'license.txt'
}
]
}
];
return (
<MenuTrigger>
<ActionButton>
Window
</ActionButton>
<Menu
items={openWindows}
selectionMode="multiple"
selectedKeys={selected}
onSelectionChange={setSelected}
>
{(item) => (
<Section
items={item
.children}
title={item
.name}
>
{(item) => (
<Item>
{item
.name}
</Item>
)}
</Section>
)}
</Menu>
</MenuTrigger>
);
}
Accessibility#
Sections without a title
must provide an aria-label
for accessibility.
Complex Menu Items#
Items within Menu also allow for additional content used to better communicate options. Icons and descriptions can be added to the children
of Item
as shown in the example below. If a description is added, the prop slot="description"
must be used to distinguish the different <Text>
elements.
import {Keyboard, Text} from '@react-spectrum/text';
<MenuTrigger>
<ActionButton>
Edit
</ActionButton>
<Menu>
<Item key="cut" textValue="cut">
<Cut size="S"/>
<Text>Cut</Text>
<Keyboard>⌘X</Keyboard>
</Item>
<Item key="copy" textValue="copy">
<Copy size="S"/>
<Text>Copy</Text>
<Keyboard>⌘C</Keyboard>
</Item>
<Item key="paste" textValue="paste">
<Paste size="S"/>
<Text>Paste</Text>
<Keyboard>⌘V</Keyboard>
</Item>
</Menu>
</MenuTrigger>
import {Keyboard, Text} from '@react-spectrum/text';
<MenuTrigger>
<ActionButton>
Edit
</ActionButton>
<Menu>
<Item key="cut" textValue="cut">
<Cut size="S"/>
<Text>Cut</Text>
<Keyboard>⌘X</Keyboard>
</Item>
<Item key="copy" textValue="copy">
<Copy size="S"/>
<Text>Copy</Text>
<Keyboard>⌘C</Keyboard>
</Item>
<Item key="paste" textValue="paste">
<Paste size="S"/>
<Text>Paste</Text>
<Keyboard>⌘V</Keyboard>
</Item>
</Menu>
</MenuTrigger>
import {
Keyboard,
Text
} from '@react-spectrum/text';
<MenuTrigger>
<ActionButton>
Edit
</ActionButton>
<Menu>
<Item
key="cut"
textValue="cut"
>
<Cut size="S" />
<Text>Cut</Text>
<Keyboard>
⌘X
</Keyboard>
</Item>
<Item
key="copy"
textValue="copy"
>
<Copy size="S" />
<Text>Copy</Text>
<Keyboard>
⌘C
</Keyboard>
</Item>
<Item
key="paste"
textValue="paste"
>
<Paste size="S" />
<Text>
Paste
</Text>
<Keyboard>
⌘V
</Keyboard>
</Item>
</Menu>
</MenuTrigger>
Props#
Name | Type | Default | Description |
children | CollectionChildren<object> | — | The contents of the collection. |
autoFocus | boolean | FocusStrategy | — | Where the focus should be set. |
shouldFocusWrap | boolean | — | Whether keyboard navigation is circular. |
items | Iterable<object> | — | Item objects in the collection. |
disabledKeys | Iterable<Key> | — | The item keys that are disabled. These items cannot be selected, focused, or otherwise interacted with. |
selectionMode | SelectionMode | — | The type of selection that is allowed in the collection. |
disallowEmptySelection | boolean | — | Whether the collection allows empty selection. |
selectedKeys | 'all' | Iterable<Key> | — | The currently selected keys in the collection (controlled). |
defaultSelectedKeys | 'all' | Iterable<Key> | — | The initial selected keys in the collection (uncontrolled). |
Events
Name | Type | Default | Description |
onAction | (
(key: Key
)) => void | — | Handler that is called when an item is selected. |
onSelectionChange | (
(keys: Selection
)) => any | — | Handler that is called when the selection changes. |
Layout
Name | Type | Default | Description |
flex | Responsive<string
| number
| boolean> | — | When used in a flex layout, specifies how the element will grow or shrink to fit the space available. See MDN. |
flexGrow | Responsive<number> | — | When used in a flex layout, specifies how the element will grow to fit the space available. See MDN. |
flexShrink | Responsive<number> | — | When used in a flex layout, specifies how the element will shrink to fit the space available. See MDN. |
flexBasis | Responsive<number | string> | — | When used in a flex layout, specifies the initial main size of the element. See MDN. |
alignSelf | Responsive<'auto'
| 'normal'
| 'start'
| 'end'
| 'center'
| 'flex-start'
| 'flex-end'
| 'self-start'
| 'self-end'
| 'stretch'> | — | Overrides the alignItems property of a flex or grid container. See MDN. |
justifySelf | Responsive<'auto'
| 'normal'
| 'start'
| 'end'
| 'flex-start'
| 'flex-end'
| 'self-start'
| 'self-end'
| 'center'
| 'left'
| 'right'
| 'stretch'> | — | Specifies how the element is justified inside a flex or grid container. See MDN. |
order | Responsive<number> | — | The layout order for the element within a flex or grid container. See MDN. |
gridArea | Responsive<string> | — | When used in a grid layout, specifies the named grid area that the element should be placed in within the grid. See MDN. |
gridColumn | Responsive<string> | — | When used in a grid layout, specifies the column the element should be placed in within the grid. See MDN. |
gridRow | Responsive<string> | — | When used in a grid layout, specifies the row the element should be placed in within the grid. See MDN. |
gridColumnStart | Responsive<string> | — | When used in a grid layout, specifies the starting column to span within the grid. See MDN. |
gridColumnEnd | Responsive<string> | — | When used in a grid layout, specifies the ending column to span within the grid. See MDN. |
gridRowStart | Responsive<string> | — | When used in a grid layout, specifies the starting row to span within the grid. See MDN. |
gridRowEnd | Responsive<string> | — | When used in a grid layout, specifies the ending row to span within the grid. See MDN. |
Spacing
Name | Type | Default | Description |
margin | Responsive<DimensionValue> | — | The margin for all four sides of the element. See MDN. |
marginTop | Responsive<DimensionValue> | — | The margin for the top side of the element. See MDN. |
marginBottom | Responsive<DimensionValue> | — | The margin for the bottom side of the element. See MDN. |
marginStart | Responsive<DimensionValue> | — | The margin for the logical start side of the element, depending on layout direction. See MDN. |
marginEnd | Responsive<DimensionValue> | — | The margin for the logical end side of an element, depending on layout direction. See MDN. |
marginX | Responsive<DimensionValue> | — | The margin for both the left and right sides of the element. See MDN. |
marginY | Responsive<DimensionValue> | — | The margin for both the top and bottom sides of the element. See MDN. |
Sizing
Name | Type | Default | Description |
width | Responsive<DimensionValue> | — | The width of the element. See MDN. |
minWidth | Responsive<DimensionValue> | — | The minimum width of the element. See MDN. |
maxWidth | Responsive<DimensionValue> | — | The maximum width of the element. See MDN. |
height | Responsive<DimensionValue> | — | The height of the element. See MDN. |
minHeight | Responsive<DimensionValue> | — | The minimum height of the element. See MDN. |
maxHeight | Responsive<DimensionValue> | — | The maximum height of the element. See MDN. |
Positioning
Name | Type | Default | Description |
position | Responsive<'static'
| 'relative'
| 'absolute'
| 'fixed'
| 'sticky'> | — | Specifies how the element is positioned. See MDN. |
top | Responsive<DimensionValue> | — | The top position for the element. See MDN. |
bottom | Responsive<DimensionValue> | — | The bottom position for the element. See MDN. |
left | Responsive<DimensionValue> | — | The left position for the element. See MDN. Consider using start instead for RTL support. |
right | Responsive<DimensionValue> | — | The right position for the element. See MDN. Consider using start instead for RTL support. |
start | Responsive<DimensionValue> | — | The logical start position for the element, depending on layout direction. See MDN. |
end | Responsive<DimensionValue> | — | The logical end position for the element, depending on layout direction. See MDN. |
zIndex | Responsive<number> | — | The stacking order for the element. See MDN. |
isHidden | Responsive<boolean> | — | Hides the element. |
Accessibility
Name | Type | Default | Description |
id | string | — | The element's unique identifier. See MDN. |
aria-label | string | — | Defines a string value that labels the current element. |
aria-labelledby | string | — | Identifies the element (or elements) that labels the current element. |
aria-describedby | string | — | Identifies the element (or elements) that describes the object. |
aria-details | string | — | Identifies the element (or elements) that provide a detailed, extended description for the object. |
Advanced
Name | Type | Default | Description |
UNSAFE_className | string | — | Sets the CSS className for the element. Only use as a last resort. Use style props instead. |
UNSAFE_style | CSSProperties | — | Sets inline style for the element. Only use as a last resort. Use style props instead. |
Visual Options#
Autofocus#
Applying autoFocus
to the Menu of the MenuTrigger sets focus to a Menu Item
within the Menu upon opening.
Disabled#
<MenuTrigger>
<ActionButton>
Filter
</ActionButton>
<Menu
items={[
{name: 'tiff', id: 'a1b2c3'},
{name: 'png', id: 'g5h1j9'},
{name: 'jpg', id: 'p8k3i4'},
{name: 'PDF', id: 'j7i3a0'}
]}
disabledKeys={['a1b2c3', 'p8k3i4']}>
{item => <Item>{item.name}</Item>}
</Menu>
</MenuTrigger>
<MenuTrigger>
<ActionButton>
Filter
</ActionButton>
<Menu
items={[
{name: 'tiff', id: 'a1b2c3'},
{name: 'png', id: 'g5h1j9'},
{name: 'jpg', id: 'p8k3i4'},
{name: 'PDF', id: 'j7i3a0'}
]}
disabledKeys={['a1b2c3', 'p8k3i4']}>
{item => <Item>{item.name}</Item>}
</Menu>
</MenuTrigger>
<MenuTrigger>
<ActionButton>
Filter
</ActionButton>
<Menu
items={[
{
name: 'tiff',
id: 'a1b2c3'
},
{
name: 'png',
id: 'g5h1j9'
},
{
name: 'jpg',
id: 'p8k3i4'
},
{
name: 'PDF',
id: 'j7i3a0'
}
]}
disabledKeys={[
'a1b2c3',
'p8k3i4'
]}
>
{(item) => (
<Item>
{item.name}
</Item>
)}
</Menu>
</MenuTrigger>