Menu

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

installyarn add @adobe/react-spectrum
added3.0.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 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(null);

  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(null);

  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(
    null
  );

  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.

import type {Selection} from '@adobe/react-spectrum';

function Example() {
  let [selected, setSelected] = React.useState<Selection>(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>
    </>
  );
}
import type {Selection} from '@adobe/react-spectrum';

function Example() {
  let [selected, setSelected] = React.useState<Selection>(
    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>
    </>
  );
}
import type {Selection} from '@adobe/react-spectrum';

function Example() {
  let [
    selected,
    setSelected
  ] = React.useState<
    Selection
  >(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<Selection>(
    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 === 'all' ? 'all' : [...selected].join(', ')}
      </p>
    </>
  );
}
function Example() {
  let [selected, setSelected] = React.useState<Selection>(
    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 === 'all'
          ? 'all'
          : [...selected].join(', ')}
      </p>
    </>
  );
}
function Example() {
  let [
    selected,
    setSelected
  ] = React.useState<
    Selection
  >(
    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 ===
            'all'
          ? 'all'
          : [...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#

import type {Selection} from '@adobe/react-spectrum';

function Example() {
  let [selected, setSelected] = React.useState<Selection>(
    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>
  );
}
import type {Selection} from '@adobe/react-spectrum';

function Example() {
  let [selected, setSelected] = React.useState<Selection>(
    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>
  );
}
import type {Selection} from '@adobe/react-spectrum';

function Example() {
  let [
    selected,
    setSelected
  ] = React.useState<
    Selection
  >(
    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.

import type {Selection} from '@adobe/react-spectrum';

function Example() {
  let [selected, setSelected] = React.useState<Selection>(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>
  );
}
import type {Selection} from '@adobe/react-spectrum';

function Example() {
  let [selected, setSelected] = React.useState<Selection>(
    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>
  );
}
import type {Selection} from '@adobe/react-spectrum';

function Example() {
  let [
    selected,
    setSelected
  ] = React.useState<
    Selection
  >(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 '@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<object>The contents of the collection.
autoFocusbooleanFocusStrategyWhere the focus should be set.
shouldFocusWrapbooleanWhether keyboard navigation is circular.
itemsIterable<object>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.
onClose() => voidHandler that is called when the menu should close after selecting an item.
onSelectionChange( (keys: Selection )) => anyHandler that is called when the selection changes.
Layout
NameTypeDefaultDescription
flexResponsive<stringnumberboolean>When used in a flex layout, specifies how the element will grow or shrink to fit the space available. See MDN.
flexGrowResponsive<number>When used in a flex layout, specifies how the element will grow to fit the space available. See MDN.
flexShrinkResponsive<number>When used in a flex layout, specifies how the element will shrink to fit the space available. See MDN.
flexBasisResponsive<numberstring>When used in a flex layout, specifies the initial main size of the element. See MDN.
alignSelfResponsive<'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.
justifySelfResponsive<'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.
orderResponsive<number>The layout order for the element within a flex or grid container. See MDN.
gridAreaResponsive<string>When used in a grid layout, specifies the named grid area that the element should be placed in within the grid. See MDN.
gridColumnResponsive<string>When used in a grid layout, specifies the column the element should be placed in within the grid. See MDN.
gridRowResponsive<string>When used in a grid layout, specifies the row the element should be placed in within the grid. See MDN.
gridColumnStartResponsive<string>When used in a grid layout, specifies the starting column to span within the grid. See MDN.
gridColumnEndResponsive<string>When used in a grid layout, specifies the ending column to span within the grid. See MDN.
gridRowStartResponsive<string>When used in a grid layout, specifies the starting row to span within the grid. See MDN.
gridRowEndResponsive<string>When used in a grid layout, specifies the ending row to span within the grid. See MDN.
Spacing
NameTypeDefaultDescription
marginResponsive<DimensionValue>The margin for all four sides of the element. See MDN.
marginTopResponsive<DimensionValue>The margin for the top side of the element. See MDN.
marginBottomResponsive<DimensionValue>The margin for the bottom side of the element. See MDN.
marginStartResponsive<DimensionValue>The margin for the logical start side of the element, depending on layout direction. See MDN.
marginEndResponsive<DimensionValue>The margin for the logical end side of an element, depending on layout direction. See MDN.
marginXResponsive<DimensionValue>The margin for both the left and right sides of the element. See MDN.
marginYResponsive<DimensionValue>The margin for both the top and bottom sides of the element. See MDN.
Sizing
NameTypeDefaultDescription
widthResponsive<DimensionValue>The width of the element. See MDN.
minWidthResponsive<DimensionValue>The minimum width of the element. See MDN.
maxWidthResponsive<DimensionValue>The maximum width of the element. See MDN.
heightResponsive<DimensionValue>The height of the element. See MDN.
minHeightResponsive<DimensionValue>The minimum height of the element. See MDN.
maxHeightResponsive<DimensionValue>The maximum height of the element. See MDN.
Positioning
NameTypeDefaultDescription
positionResponsive<'static''relative''absolute''fixed''sticky'>Specifies how the element is positioned. See MDN.
topResponsive<DimensionValue>The top position for the element. See MDN.
bottomResponsive<DimensionValue>The bottom position for the element. See MDN.
leftResponsive<DimensionValue>The left position for the element. See MDN. Consider using start instead for RTL support.
rightResponsive<DimensionValue>The right position for the element. See MDN. Consider using start instead for RTL support.
startResponsive<DimensionValue>The logical start position for the element, depending on layout direction. See MDN.
endResponsive<DimensionValue>The logical end position for the element, depending on layout direction. See MDN.
zIndexResponsive<number>The stacking order for the element. See MDN.
isHiddenResponsive<boolean>Hides 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>