# Menus **This documentation is fully rendered on the [Material Web catalog](https://material-web.dev/components/menu/).** [Menus](https://m3.material.io/components/menus) display a list of choices on a temporary surface. A phone showing a vertical threedot, pressed, icon button and a menu floating below it with the following visible items: Revert, Settings, and Send Feedback * [Design article](https://m3.material.io/components/menus) * [Source code](https://github.com/material-components/material-web/tree/main/menu) ## Usage When opened, menus position themselves to an anchor. Thus, either `anchor` or `anchorElement` must be supplied to `md-menu` before opening. Additionally, a shared parent of `position:relative` should be present around the menu and it's anchor. Menus also render menu items such as `md-menu-item` and handle keyboard navigation between `md-menu-item`s as well as typeahead functionality. Additionally, `md-menu` interacts with `md-menu-item`s to help you determine how a menu was closed. Listen for and inspect the `close-menu` custom event's `details` to determine what action and items closed the menu. !["Two filled buttons next to each other. The first one says set with idref and the other says set with element ref. There is an opened menu anchored to the bottom of the right button with the items: Apple, Banana, Cucumber."](images/menu/usage.webp) ```html Set with idref Set with element ref ``` ### Submenus You can compose submenus inside of an ``'s `submenu` slot, but first the `has-overflow` attribute must be set on the root `` to disable overflow scrolling and display the nested submenus. !["A filled button that says menu with submenus. There is a menu anchored to the bottom of it with the first item selected that says fruits with A followed by a right arrow. To the right is anchored a submenu with 3 items, Apricot, Avocado, Apples. The Apples item is selected and has a left arrow before the text and another submenu anchored to it on the left. That menu has three items, Fuji, Granny Smith, and Red Delicious."](images/menu/usage-submenu.webp) ```html Menu with Submenus arrow_left arrow_right ``` ### Fixed menus Internally menu uses `position: absolute` by default. Though there are cases when the anchor and the node cannot share a common ancestor that is `position: relative`, or sometimes, menu will render below another item due to limitations with `position: absolute`. In most of these cases, you would want to use the `fixed` attribute to position the menu relative to the window instead of relative to the parent. > Note: Fixed menu positions are positioned relative to the window and not the > document. This means that the menu will not scroll with the anchor as the page > is scrolled. !["A filled button that says open fixed menu. There is an open menu anchored to the bottom of the button with three items, Apple, Banana, and Cucumber."](images/menu/usage-fixed.webp) ```html
Open fixed menu
``` ## Accessibility By default Menu is set up to function as a `role="menu"` with children as `role="menuitem"`. A common use case for this is the menu button example, where you would need to add keyboard interactions to the button to open the menu ([see W3C example](https://www.w3.org/WAI/ARIA/apg/patterns/menu-button/examples/menu-button-actions/)). Menu can also be adapted for other use cases. The role of the `md-list` inside the menu can be set with the `type` attribute. The role of each individual `md-menu-item` can also be set with the `type` attribute. Anything else slotted into the menu that is not a list item in most cases should be set to `role="none"`, and `md-divider` should have `role="separator"`. ```html ``` ## Theming Menus support [Material theming](../theming/README.md) and can be customized in terms of color. Additionally, `md-menu` composes `md-list`, and each menu item extends `md-list-item` ([see theming documentation](./list#theming)), so most the tokens for those components can also be used for Menu. ### Menu Tokens Token | Default value ----------------------------------------- | ------------------------------------ `--md-menu-container-color` | `--md-sys-color-surface-container` `--md-menu-container-shape` | `4px` `--md-menu-item-container-color` | `--md-sys-color-surface-container` `--md-menu-item-selected-container-color` | `--md-sys-color-secondary-container` * [Menu tokens](https://github.com/material-components/material-web/blob/main/tokens/_md-comp-menu.scss) * [Menu item tokens](https://github.com/material-components/material-web/blob/main/tokens/_md-comp-menu-item.scss) * [List tokens](https://github.com/material-components/material-web/blob/main/tokens/_md-comp-list.scss) * [List item tokens](https://github.com/material-components/material-web/blob/main/tokens/_md-comp-list-item.scss) ### Example ![A filled button with the text Themed menu. Attached is a 3 item menu with the items Apple, Banana, and Cucumber. They are both in a green hue and the menu has a sharp 0px border radius.](images/menu/theming.webp) ```html Themed menu ```