Menu

Menus display a list of actions or options that a user can choose.

installyarn add @adobe/react-spectrum
version3.2.0
usageimport {Menu, MenuTrigger} from '@adobe/react-spectrum'

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 an 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.

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.

View guidelines

import {Keyboard, Text} from '@adobe/react-spectrum';
<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 '@adobe/react-spectrum';
<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 '@adobe/react-spectrum';
<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#


NameTypeDefaultDescription
childrenCollectionChildren<T>The contents of the collection.
autoFocusbooleanFocusStrategyWhere the focus should be set.
shouldFocusWrapbooleanWhether keyboard navigation is circular.
itemsIterable<T>Item objects in the collection.
disabledKeysIterable<Key>The item keys that are disabled. These items cannot be selected, focused, or otherwise interacted with.
selectionModeSelectionModeThe type of selection that is allowed in the collection.
disallowEmptySelectionbooleanWhether 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
NameTypeDefaultDescription
onAction( (key: Key )) => voidHandler that is called when an item is selected.
onSelectionChange( (keys: Selection )) => anyHandler that is called when the selection changes.
Layout
NameTypeDefaultDescription
flexstringnumberbooleanWhen used in a flex layout, specifies how the element will grow or shrink to fit the space available. See MDN.
flexGrownumberWhen used in a flex layout, specifies how the element will grow to fit the space available. See MDN.
flexShrinknumberWhen used in a flex layout, specifies how the element will shrink to fit the space available. See MDN.
flexBasisnumberstringWhen used in a flex layout, specifies the initial main size of the element. See MDN.
alignSelf'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'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.
ordernumberThe layout order for the element within a flex or grid container. See MDN.
gridAreastringWhen used in a grid layout, specifies the named grid area that the element should be placed in within the grid. See MDN.
gridColumnstringWhen used in a grid layout, specifies the column the element should be placed in within the grid. See MDN.
gridRowstringWhen used in a grid layout, specifies the row the element should be placed in within the grid. See MDN.
gridColumnStartstringWhen used in a grid layout, specifies the starting column to span within the grid. See MDN.
gridColumnEndstringWhen used in a grid layout, specifies the ending column to span within the grid. See MDN.
gridRowStartstringWhen used in a grid layout, specifies the starting row to span within the grid. See MDN.
gridRowEndstringWhen used in a grid layout, specifies the ending row to span within the grid. See MDN.
Spacing
NameTypeDefaultDescription
marginDimensionValueThe margin for all four sides of the element. See MDN.
marginTopDimensionValueThe margin for the top side of the element. See MDN.
marginBottomDimensionValueThe margin for the bottom side of the element. See MDN.
marginStartDimensionValueThe margin for the logical start side of the element, depending on layout direction. See MDN.
marginEndDimensionValueThe margin for the logical end side of an element, depending on layout direction. See MDN.
marginXDimensionValueThe margin for both the left and right sides of the element. See MDN.
marginYDimensionValueThe margin for both the top and bottom sides of the element. See MDN.
Sizing
NameTypeDefaultDescription
widthDimensionValueThe width of the element. See MDN.
minWidthDimensionValueThe minimum width of the element. See MDN.
maxWidthDimensionValueThe maximum width of the element. See MDN.
heightDimensionValueThe height of the element. See MDN.
minHeightDimensionValueThe minimum height of the element. See MDN.
maxHeightDimensionValueThe maximum height of the element. See MDN.
Positioning
NameTypeDefaultDescription
position'static''relative''absolute''fixed''sticky'Specifies how the element is positioned. See MDN.
topDimensionValueThe top position for the element. See MDN.
bottomDimensionValueThe bottom position for the element. See MDN.
leftDimensionValueThe left position for the element. See MDN. Consider using start instead for RTL support.
rightDimensionValueThe right position for the element. See MDN. Consider using start instead for RTL support.
startDimensionValueThe logical start position for the element, depending on layout direction. See MDN.
endDimensionValueThe logical end position for the element, depending on layout direction. See MDN.
zIndexnumberThe stacking order for the element. See MDN.
isHiddenbooleanHides the element.
Accessibility
NameTypeDefaultDescription
idstringThe element's unique identifier. See MDN.
aria-labelstringDefines a string value that labels the current element.
aria-labelledbystringIdentifies the element (or elements) that labels the current element.
aria-describedbystringIdentifies the element (or elements) that describes the object.
aria-detailsstringIdentifies the element (or elements) that provide a detailed, extended description for the object.
Advanced
NameTypeDefaultDescription
UNSAFE_classNamestringSets the CSS className for the element. Only use as a last resort. Use style props instead.
UNSAFE_styleCSSPropertiesSets 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>