Skip to content

Material 3

Components

Menu

Menus display a list of choices on a temporary surface.

9:30

New contact

Menus display a list of choices on a temporary surface. Use a menu to show a temporary set of actions, opened from a button or another control. To show actions on screen at all times, use a toolbar instead.

A menu is composed of these parts:

PartUse
MenuTriggerHolds the element that opens the menu, and the menu's popover
MenuPopoverThe panel of the menu, placed by its trigger
MenuThe list of items in the panel
MenuItemA row of the menu: an action, a choice or a link
MenuSection, HeaderA group of items, and its name
SeparatorA divider between items or sections
SubmenuTriggerHolds an item and the popover of the submenu it opens
Text, KeyboardThe label, the supporting text and the keyboard command of a row

Import MenuPopover, Menu and MenuItem from @material-web/react, and MenuTrigger, SubmenuTrigger, MenuSection, Header, Separator, Text and Keyboard from react-aria-components, as for React Aria's Menu.

import { Menu, MenuItem, MenuPopover } from '@material-web/react'
import {
  Header,
  Keyboard,
  MenuSection,
  MenuTrigger,
  Separator,
  SubmenuTrigger,
  Text,
} from 'react-aria-components'

Basic menu

Wrap the element that opens the menu and a MenuPopover in a MenuTrigger. Put a Menu in the popover, and write each row as a MenuItem. Selecting an item closes the menu.

import { Icon, IconButton, Menu, MenuItem, MenuPopover } from '@material-web/react'
import { MenuTrigger, Separator } from 'react-aria-components'

export default function Basic() {
  return (
    <MenuTrigger>
      <IconButton aria-label="More options">
        <Icon name="more_vert" />
      </IconButton>
      <MenuPopover>
        <Menu>
          <MenuItem textValue="Preview">
            <Icon slot="leading" name="visibility" />
            Preview
          </MenuItem>
          <MenuItem textValue="Share">
            <Icon slot="leading" name="person_add" />
            Share
          </MenuItem>
          <MenuItem textValue="Get link">
            <Icon slot="leading" name="link" />
            Get link
          </MenuItem>
          <Separator />
          <MenuItem textValue="Remove">
            <Icon slot="leading" name="delete" />
            Remove
          </MenuItem>
        </Menu>
      </MenuPopover>
    </MenuTrigger>
  )
}

Write the parts of a menu in this order:

  • In the MenuTrigger, put the element that opens the menu first: an IconButton, a Button or any other React Aria button.
  • After it, put the MenuPopover.
  • In the MenuPopover, put the Menu.
  • In the Menu, put a MenuItem for each row.

To open the menu from an element of your own, wrap the element in React Aria's Pressable, and give it an interactive role, such as role="button". See Custom trigger.

To render the items from data, pass items to the Menu and a function as its children. See Collections.

Typing a letter moves the focus to an item by its textValue. An item whose children are a plain string, as in <MenuItem>Preview</MenuItem>, uses that string. Set a textValue on an item that holds anything else, such as an icon or a Keyboard: its types require one.

A menu is 112dp to 280dp wide. A label that is wider wraps onto more lines.

Slots

  1. leading
  2. label
  3. description
  4. trailing
SlotChildUse
leadingAn Icon, a Text or an element of your own, such as an image or a color swatchBefore the label
labelA TextThe label, which names the item
descriptionA TextThe supporting text, under the label. Together with a keyboard command, it describes the item.
trailingAn Icon, a Text badge, a Keyboard or an element of your ownThe item's end, in the order written. Several trailing parts follow each other.

Write an item's parts as its children, and place each one with a slot. An Icon or a Keyboard may also have no slot. A Keyboard with no slot, the keyboard command, is placed at the item's end, in the order written.

import { Icon, IconButton, Menu, MenuItem, MenuPopover } from '@material-web/react'
import { Keyboard, MenuTrigger, Text } from 'react-aria-components'

export default function Items() {
  return (
    <MenuTrigger>
      <IconButton aria-label="Edit">
        <Icon name="edit" />
      </IconButton>
      <MenuPopover>
        <Menu>
          <MenuItem textValue="Cut">
            <Icon slot="leading" name="content_cut" />
            <Text slot="label">Cut</Text>
            <Keyboard>⌘X</Keyboard>
          </MenuItem>
          <MenuItem textValue="Copy">
            <Icon slot="leading" name="content_copy" />
            <Text slot="label">Copy</Text>
            <Text slot="description">Copies the selection</Text>
            <Keyboard>⌘C</Keyboard>
          </MenuItem>
          <MenuItem textValue="Paste">
            <Icon slot="leading" name="content_paste" />
            <Text slot="label">Paste</Text>
            <Text slot="trailing">New</Text>
          </MenuItem>
          <MenuItem textValue="Save to cloud">
            <Icon slot="leading" name="cloud" />
            <Text slot="label">Save to cloud</Text>
            <Icon slot="trailing" name="open_in_new" />
          </MenuItem>
        </Menu>
      </MenuPopover>
    </MenuTrigger>
  )
}

Write an item's label as its text, as in <MenuItem>Preview</MenuItem>. The text names the item, also when an Icon is beside it.

For an item with a description, a keyboard command or other text, put the label in a Text with slot="label". The label then names the item, even when the item has an aria-label. Without a label slot, all of the item's text names it. Set an aria-label on an item with no label.

<MenuItem textValue="Copy" onAction={copy}>
  <Icon slot="leading" name="content_copy" />
  <Text slot="label">Copy</Text>
  <Text slot="description">Copies the selection</Text>
  <Keyboard>⌘C</Keyboard>
</MenuItem>

For a text badge, write <Text slot="trailing">New</Text>. A Text, an Icon or a Keyboard with a slot it does not take throws an Invalid slot error. For an element of your own in a slot, see Account menu.

Account menu

The leading and trailing slots of an item take elements of your own too, such as an avatar, an image or a color swatch. Your own element is placed by its slot attribute, which is not checked. It takes the item's text color unless you set another.

import { Icon, IconButton, Menu, MenuItem, MenuPopover } from '@material-web/react'
import { MenuTrigger, Separator, Text } from 'react-aria-components'

const accounts = [
  ['Britta Holt', 'britta@example.com'],
  ['Jonas Meyer', 'jonas@example.com'],
] as const

export default function Account() {
  return (
    <MenuTrigger>
      <IconButton variant="tonal" aria-label="Account">
        <Icon name="account_circle" />
      </IconButton>
      <MenuPopover placement="bottom end">
        <Menu>
          {accounts.map(([name, email]) => (
            <MenuItem key={email} textValue={name}>
              <span
                slot="leading"
                className="grid size-8 place-items-center rounded-full bg-primary text-label-lg text-on-primary"
              >
                {name[0]}
              </span>
              <Text slot="label">{name}</Text>
              <Text slot="description">{email}</Text>
            </MenuItem>
          ))}
          <Separator />
          <MenuItem textValue="Add another account">
            <Icon slot="leading" name="person_add" />
            Add another account
          </MenuItem>
          <MenuItem textValue="Sign out">
            <Icon slot="leading" name="logout" />
            Sign out
          </MenuItem>
        </Menu>
      </MenuPopover>
    </MenuTrigger>
  )
}

Sections

Group items in a MenuSection, and name the group with a Header inside it. Separate items or sections with a Separator. A header or a divider does not take the focus.

import { Button, Icon, Menu, MenuItem, MenuPopover } from '@material-web/react'
import { Header, MenuSection, MenuTrigger, Separator } from 'react-aria-components'

export default function Sections() {
  return (
    <MenuTrigger>
      <Button variant="tonal">Format</Button>
      <MenuPopover>
        <Menu>
          <MenuSection>
            <Header>Style</Header>
            <MenuItem textValue="Bold">
              <Icon slot="leading" name="format_bold" />
              Bold
            </MenuItem>
            <MenuItem textValue="Italic">
              <Icon slot="leading" name="format_italic" />
              Italic
            </MenuItem>
          </MenuSection>
          <Separator />
          <MenuSection>
            <Header>Alignment</Header>
            <MenuItem textValue="Left">
              <Icon slot="leading" name="format_align_left" />
              Left
            </MenuItem>
            <MenuItem textValue="Center">
              <Icon slot="leading" name="format_align_center" />
              Center
            </MenuItem>
          </MenuSection>
        </Menu>
      </MenuPopover>
    </MenuTrigger>
  )
}

To open a submenu from an item, wrap the item and a second MenuPopover in a SubmenuTrigger: the item first, then the popover with a Menu in it. Submenus nest to any depth. The item shows an arrow at its end, and its submenu opens beside it, at the item's end and aligned to its top (end top).

  • A submenu opens after the pointer has rested on its item for 200ms, on a press, and with Space, Enter or the arrow key toward it. Set delay on the SubmenuTrigger to change the 200ms.
  • The arrow key back or Escape closes the submenu. Selecting one of its items closes the whole menu.
  • A submenu opens on the side of its item that has room.
import { Icon, IconButton, Menu, MenuItem, MenuPopover } from '@material-web/react'
import { Keyboard, MenuTrigger, Separator, SubmenuTrigger, Text } from 'react-aria-components'

export default function Submenu() {
  return (
    <MenuTrigger>
      <IconButton aria-label="More options">
        <Icon name="more_vert" />
      </IconButton>
      <MenuPopover>
        <Menu>
          <MenuItem textValue="Share">
            <Icon slot="leading" name="person_add" />
            <Text slot="label">Share</Text>
            <Keyboard>⌘S</Keyboard>
          </MenuItem>
          <SubmenuTrigger>
            <MenuItem textValue="Move to">
              <Icon slot="leading" name="drive_file_move" />
              Move to
            </MenuItem>
            <MenuPopover>
              <Menu>
                <MenuItem>Archive</MenuItem>
                <MenuItem>Spam</MenuItem>
                <SubmenuTrigger>
                  <MenuItem>Labels</MenuItem>
                  <MenuPopover>
                    <Menu>
                      <MenuItem>Personal</MenuItem>
                      <MenuItem>Work</MenuItem>
                    </Menu>
                  </MenuPopover>
                </SubmenuTrigger>
              </Menu>
            </MenuPopover>
          </SubmenuTrigger>
          <Separator />
          <MenuItem textValue="Remove">
            <Icon slot="leading" name="delete" />
            Remove
          </MenuItem>
        </Menu>
      </MenuPopover>
    </MenuTrigger>
  )
}

Handling actions

Handle an item's press with its own onAction, or with the Menu's onAction, which is called with the item's id.

No action yet
import { Button, Menu, MenuItem, MenuPopover } from '@material-web/react'
import { useState } from 'react'
import { MenuTrigger, type Key } from 'react-aria-components'

export default function Actions() {
  const [action, setAction] = useState<Key | null>(null)
  return (
    <>
      <MenuTrigger>
        <Button variant="tonal">Edit</Button>
        <MenuPopover>
          <Menu onAction={setAction}>
            <MenuItem id="cut">Cut</MenuItem>
            <MenuItem id="copy">Copy</MenuItem>
            <MenuItem id="paste">Paste</MenuItem>
          </Menu>
        </MenuPopover>
      </MenuTrigger>
      <span className="text-body-md text-on-surface-variant">
        {action ? `Last action: ${action}` : 'No action yet'}
      </span>
    </>
  )
}

Selection

Set selectionMode on the Menu to single or multiple. Control the selection with selectedKeys and onSelectionChange, or set the initial selection with defaultSelectedKeys. The keys are the items' ids. See React Aria's Selection guide.

A selected item changes its shape and color and shows a checkmark before its text. The checkmark takes the place of the item's leading part, if it has one.

A menu with a selection opens with the focus on its selected item. With several selected, that is the first key of selectedKeys as you pass it, not the first in the items' order.

import { Button, Icon, Menu, MenuItem, MenuPopover } from '@material-web/react'
import { useState } from 'react'
import { MenuTrigger, type Key } from 'react-aria-components'

const orders = ['Name', 'Date modified', 'Size']

export default function SingleSelect() {
  const [order, setOrder] = useState<Key>('Name')
  return (
    <MenuTrigger>
      <Button variant="outlined">
        <Icon name="sort" />
        {`Sort by ${String(order).toLowerCase()}`}
      </Button>
      <MenuPopover>
        <Menu
          selectionMode="single"
          disallowEmptySelection
          selectedKeys={[order]}
          onSelectionChange={(keys) => {
            const [key] = keys === 'all' ? [] : [...keys]
            if (key !== undefined) setOrder(key)
          }}
        >
          {orders.map((name) => (
            <MenuItem key={name} id={name}>
              {name}
            </MenuItem>
          ))}
        </Menu>
      </MenuPopover>
    </MenuTrigger>
  )
}

A click selects an item and closes a single-select menu, but leaves a multi-select menu open. Space selects and leaves either menu open, and Enter selects and closes it. Set shouldCloseOnSelect on the Menu, a MenuSection or a MenuItem to choose whether a selection closes the menu. Set disallowEmptySelection to keep a click on the selected item from clearing the selection.

Checkbox and radio menu items

The items of a menu with selectionMode="single" are radio items: choosing one clears the others. The items of a menu with selectionMode="multiple" are checkbox items: each one is turned on and off by itself.

A multi-select menu stays open while its items are clicked.

import { Button, Menu, MenuItem, MenuPopover } from '@material-web/react'
import { useState } from 'react'
import { MenuTrigger, type Selection } from 'react-aria-components'

const diets = ['Gluten-free', 'Kosher', 'Nut-free', 'Vegan', 'Vegetarian']

export default function MultiSelect() {
  const [chosen, setChosen] = useState<Selection>(new Set(['Nut-free']))
  const names = chosen === 'all' ? diets : diets.filter((diet) => chosen.has(diet))
  return (
    <MenuTrigger>
      <Button variant="outlined">{names.length ? names.join(', ') : 'Diet'}</Button>
      <MenuPopover>
        <Menu selectionMode="multiple" selectedKeys={chosen} onSelectionChange={setChosen}>
          {diets.map((diet) => (
            <MenuItem key={diet} id={diet}>
              {diet}
            </MenuItem>
          ))}
        </Menu>
      </MenuPopover>
    </MenuTrigger>
  )
}

Set a selectionMode on a MenuSection to select its items separately from the menu's, or not at all. A section takes selectedKeys, onSelectionChange, defaultSelectedKeys and disallowEmptySelection as the Menu does, and a click in it closes the menu as in a menu of its selectionMode. See Section-level selection.

import { Button, Menu, MenuItem, MenuPopover } from '@material-web/react'
import { useState } from 'react'
import { Header, MenuSection, MenuTrigger, Separator, type Selection } from 'react-aria-components'

export default function SectionSelection() {
  const [style, setStyle] = useState<Selection>(new Set(['bold']))
  const [align, setAlign] = useState<Selection>(new Set(['left']))
  return (
    <MenuTrigger>
      <Button variant="outlined">Format</Button>
      <MenuPopover>
        <Menu>
          <MenuSection selectionMode="multiple" selectedKeys={style} onSelectionChange={setStyle}>
            <Header>Style</Header>
            <MenuItem id="bold">Bold</MenuItem>
            <MenuItem id="italic">Italic</MenuItem>
            <MenuItem id="underline">Underline</MenuItem>
          </MenuSection>
          <Separator />
          <MenuSection
            selectionMode="single"
            disallowEmptySelection
            selectedKeys={align}
            onSelectionChange={setAlign}
          >
            <Header>Alignment</Header>
            <MenuItem id="left">Left</MenuItem>
            <MenuItem id="center">Center</MenuItem>
            <MenuItem id="right">Right</MenuItem>
          </MenuSection>
          <Separator />
          <MenuItem id="clear">Clear formatting</MenuItem>
        </Menu>
      </MenuPopover>
    </MenuTrigger>
  )
}

Opening and closing

A press on the trigger opens the menu. Control the open state with isOpen and onOpenChange on the MenuTrigger, or set defaultOpen to start with the menu open.

Selecting an item, Escape or a click outside closes the menu and returns the focus to the trigger. While the menu is open, the rest of the page does not scroll, and a click outside closes the menu without reaching what was clicked.

Set trigger="longPress" to open the menu with a long press, for a trigger that has an action of its own: a short press runs the trigger's onPress. Alt with Enter, Space or an arrow key opens the menu from the keyboard. See Long press. To open the menu with a right click, see Context menu.

Context menu

Set trigger="contextMenu" on the MenuTrigger to open the menu with a right click, a long press on a touch screen, or the system's keyboard shortcut for a context menu, such as Control+Enter on macOS. The menu opens at the pointer. See React Aria's Context menu.

import { Menu, MenuItem, MenuPopover } from '@material-web/react'
import { Button, MenuTrigger, Separator } from 'react-aria-components'

export default function Context() {
  return (
    <MenuTrigger trigger="contextMenu">
      <Button className="focus-ring box-border grid h-32 w-full max-w-80 place-items-center rounded-shape-md border border-dashed border-outline bg-transparent p-space-200 font-plain text-body-md text-on-surface-variant">
        Right-click here, or press and hold
      </Button>
      <MenuPopover>
        <Menu>
          <MenuItem>Open</MenuItem>
          <MenuItem>Rename</MenuItem>
          <MenuItem>Duplicate</MenuItem>
          <Separator />
          <MenuItem>Move to trash</MenuItem>
        </Menu>
      </MenuPopover>
    </MenuTrigger>
  )
}

Disabled

Set isDisabled on an item to disable it. The keyboard skips it, and it ignores clicks. To disable items by their ids, set disabledKeys on the Menu.

import { Icon, IconButton, Menu, MenuItem, MenuPopover } from '@material-web/react'
import { MenuTrigger } from 'react-aria-components'

export default function Disabled() {
  return (
    <MenuTrigger>
      <IconButton aria-label="Edit">
        <Icon name="edit" />
      </IconButton>
      <MenuPopover>
        <Menu>
          <MenuItem textValue="Undo">
            <Icon slot="leading" name="undo" className="rtl:icon-mirrored" />
            Undo
          </MenuItem>
          <MenuItem textValue="Redo" isDisabled>
            <Icon slot="leading" name="redo" className="rtl:icon-mirrored" />
            Redo
          </MenuItem>
          <MenuItem textValue="Cut">
            <Icon slot="leading" name="content_cut" />
            Cut
          </MenuItem>
        </Menu>
      </MenuPopover>
    </MenuTrigger>
  )
}

Color

Set color on the Menu to standard (the default) or vibrant. Standard is the surface-colored style with lower emphasis, and vibrant is the tertiary-colored style with higher emphasis. A vibrant menu is more prominent: use it sparingly.

A submenu's Menu uses the color of the Menu it is in unless you set its own.

import { Button, Icon, Menu, MenuItem, MenuPopover } from '@material-web/react'
import { MenuTrigger } from 'react-aria-components'

export default function Colors() {
  return (
    <>
      {(['standard', 'vibrant'] as const).map((color) => (
        <MenuTrigger key={color}>
          <Button variant="tonal" className="capitalize">
            {color}
          </Button>
          <MenuPopover>
            <Menu color={color}>
              <MenuItem textValue="Preview">
                <Icon slot="leading" name="visibility" />
                Preview
              </MenuItem>
              <MenuItem textValue="Share">
                <Icon slot="leading" name="person_add" />
                Share
              </MenuItem>
              <MenuItem textValue="Get link">
                <Icon slot="leading" name="link" />
                Get link
              </MenuItem>
            </Menu>
          </MenuPopover>
        </MenuTrigger>
      ))}
    </>
  )
}

Placement

Place the menu with these props of the MenuPopover:

  • placement is the side of the trigger the menu opens on: bottom start (the default), below the trigger and aligned to its start, or any other of React Aria's placements.
  • offset is the distance between the menu and the trigger: 0 (the default) keeps them flush.
  • containerPadding is the distance the menu keeps from the window's edges: 8dp (the default).
  • shouldFlip controls whether the menu turns to the side with more room, which it does unless you turn it off.
import { Button, Menu, MenuItem, MenuPopover } from '@material-web/react'
import { MenuTrigger } from 'react-aria-components'

const placements = ['bottom start', 'bottom end', 'top start', 'end top', 'start top'] as const

export default function Placement() {
  return (
    <>
      {placements.map((placement) => (
        <MenuTrigger key={placement}>
          <Button variant="tonal">{placement}</Button>
          <MenuPopover placement={placement}>
            <Menu>
              <MenuItem>Preview</MenuItem>
              <MenuItem>Share</MenuItem>
              <MenuItem>Get link</MenuItem>
            </Menu>
          </MenuPopover>
        </MenuTrigger>
      ))}
    </>
  )
}

To make the menu as wide as its trigger, set className="w-(--trigger-width)" on the MenuPopover.

Size and scrolling

When its items do not fit, the menu takes the room there is in the window and scrolls. To make it shorter, set maxHeight on the MenuPopover, in pixels.

import { Button, Menu, MenuItem, MenuPopover } from '@material-web/react'
import { MenuTrigger } from 'react-aria-components'

const states = [
  'Alabama',
  'Alaska',
  'Arizona',
  'Arkansas',
  'California',
  'Colorado',
  'Connecticut',
  'Delaware',
  'Florida',
  'Georgia',
  'Hawaii',
  'Idaho',
  'Illinois',
  'Indiana',
  'Iowa',
  'Kansas',
  'Kentucky',
  'Louisiana',
  'Maine',
  'Maryland',
]

const heights = [
  ['State', undefined],
  ['State, max 240dp', 240],
] as const

export default function Scrolling() {
  return (
    <>
      {heights.map(([label, maxHeight]) => (
        <MenuTrigger key={label}>
          <Button variant="outlined">{label}</Button>
          <MenuPopover maxHeight={maxHeight}>
            <Menu>
              {states.map((state) => (
                <MenuItem key={state}>{state}</MenuItem>
              ))}
            </Menu>
          </MenuPopover>
        </MenuTrigger>
      ))}
    </>
  )
}

Give an item an href to make it a link. It also takes target and rel. To follow the link with a client-side router, wrap the app in React Aria's RouterProvider. See Routing.

import { Button, Icon, Menu, MenuItem, MenuPopover } from '@material-web/react'
import { MenuTrigger } from 'react-aria-components'

export default function Links() {
  return (
    <MenuTrigger>
      <Button variant="tonal">Read more</Button>
      <MenuPopover>
        <Menu>
          <MenuItem href="/components/fab-menu">FAB menu</MenuItem>
          <MenuItem
            textValue="Menus in Material 3"
            href="https://m3.material.io/components/menus"
            target="_blank"
            rel="noreferrer"
          >
            Menus in Material 3
            <Icon slot="trailing" name="open_in_new" />
          </MenuItem>
        </Menu>
      </MenuPopover>
    </MenuTrigger>
  )
}

Right to left

A menu's direction follows the locale, not the page's dir. For a right-to-left menu, set the locale with I18nProvider. The arrow keys follow the locale too. See Internationalization.

Customization

Pass classes in className to restyle a part of a menu: the MenuPopover is the panel, with its corners, shadow and width, the Menu is the list, with its background color, and a MenuItem is a row. A class of yours replaces the part's own class for the same property. See Styling for the classes, the states and the theme.

import { Button, Icon, Menu, MenuItem, MenuPopover } from '@material-web/react'
import { MenuTrigger, Separator } from 'react-aria-components'

export default function Custom() {
  return (
    <MenuTrigger>
      <Button variant="tonal">Message</Button>
      <MenuPopover>
        <Menu className="bg-surface-container-high">
          <MenuItem textValue="Star">
            {({ isFocused }) => (
              <>
                <Icon
                  slot="leading"
                  name="star"
                  className={isFocused ? 'icon-filled' : undefined}
                />
                Star
              </>
            )}
          </MenuItem>
          <MenuItem textValue="Archive">
            <Icon slot="leading" name="archive" />
            Archive
          </MenuItem>
          <Separator />
          <MenuItem textValue="Delete" className="text-error">
            <Icon slot="leading" name="delete" className="text-error" />
            Delete
          </MenuItem>
        </Menu>
      </MenuPopover>
    </MenuTrigger>
  )
}

In a standard menu, an item's icons, supporting text and keyboard command keep a color of their own: to recolor them with the item, give them the class too.

  • States: Style a state of an item with its variant, such as focus:, selected: or disabled:. See States.
  • Functions: Pass a function as an item's children to render them from the item's state: isHovered, isPressed, isFocused, isFocusVisible, isSelected, isDisabled, hasSubmenu and isOpen. On an item, isFocused is true for the keyboard or the pointer, and isFocusVisible for the keyboard only. className and style take a function too, on each of the three parts. See Children as a function.
  • Ancestors: The menu is rendered at the end of the page, not next to its trigger. A density class, a color role or a data-theme on an element around the trigger does not reach it. Set it on the MenuPopover itself, or on the root element.
  • Every menu: To restyle all the menus at once, change the theme.

Density

Add density-1, density-2 or density-3 to the MenuPopover to make each item 4dp shorter for each step. See Density.

import { Button, Icon, Menu, MenuItem, MenuPopover } from '@material-web/react'
import { MenuTrigger } from 'react-aria-components'

const densities = [
  ['Default', undefined],
  ['Dense', 'density-3'],
] as const

export default function Dense() {
  return (
    <>
      {densities.map(([label, density]) => (
        <MenuTrigger key={label}>
          <Button variant="tonal">{label}</Button>
          <MenuPopover className={density}>
            <Menu>
              <MenuItem textValue="Single">
                <Icon slot="leading" name="density_large" />
                Single
              </MenuItem>
              <MenuItem textValue="1.15">
                <Icon slot="leading" name="density_medium" />
                1.15
              </MenuItem>
              <MenuItem textValue="Double">
                <Icon slot="leading" name="density_small" />
                Double
              </MenuItem>
            </Menu>
          </MenuPopover>
        </MenuTrigger>
      ))}
    </>
  )
}

Note: The menu is rendered at the end of the page, not next to its trigger. A density class on an element around the trigger does not reach it. Add the class to the MenuPopover, or to an element that contains it, such as the document's root element.

Transitions

Set shouldSkipAnimation on the MenuPopover to open and close the menu instantly, as in a dense product on desktop.

import { Icon, IconButton, Menu, MenuItem, MenuPopover } from '@material-web/react'
import { MenuTrigger } from 'react-aria-components'

export default function Instant() {
  return (
    <MenuTrigger>
      <IconButton aria-label="More options">
        <Icon name="more_vert" />
      </IconButton>
      <MenuPopover shouldSkipAnimation>
        <Menu>
          <MenuItem>Preview</MenuItem>
          <MenuItem>Share</MenuItem>
          <MenuItem>Get link</MenuItem>
        </Menu>
      </MenuPopover>
    </MenuTrigger>
  )
}

Limitations

Controls in items

An item takes every press on its row, and Tab does not move the focus into it, so a button, a switch or another control in an item cannot be used. An item performs one action. For items that turn on and off, use checkbox items.

Uncontrolled selection

A selection set with defaultSelectedKeys starts over each time the menu opens. Control it with selectedKeys to keep it.