# Menu Component

Menus present a list of choices in a temporary surface. They appear when users interact with a button, action, or other control.

For dropdown menus, use the [`<mdui-dropdown>`](/en/docs/2/components/dropdown) component.

## Usage {#usage}

Import the component:

```js
import 'mdui/components/menu.js';
import 'mdui/components/menu-item.js';
```

Import the TypeScript type:

```ts
import type { Menu } from 'mdui/components/menu.js';
import type { MenuItem } from 'mdui/components/menu-item.js';
```

Example:

```html
<mdui-menu>
  <mdui-menu-item>Item 1</mdui-menu-item>
  <mdui-menu-item>Item 2</mdui-menu-item>
</mdui-menu>
```

## Examples {#examples}

### Dropdown Menu {#example-dropdown}

To create a dropdown menu, use the [`<mdui-dropdown>`](/en/docs/2/components/dropdown) component.

```html
<mdui-dropdown>
  <mdui-button slot="trigger">Open dropdown</mdui-button>
  <mdui-menu>
    <mdui-menu-item>Item 1</mdui-menu-item>
    <mdui-menu-item>Item 2</mdui-menu-item>
  </mdui-menu>
</mdui-dropdown>
```

### Dense Style {#example-dense}

For a dense menu style, add the `dense` attribute to `<mdui-menu>`.

```html
<mdui-menu dense>
  <mdui-menu-item>Item 1</mdui-menu-item>
  <mdui-menu-item>Item 2</mdui-menu-item>
  <mdui-menu-item>Item 3</mdui-menu-item>
</mdui-menu>
```

### Disabled Items {#example-disabled}

To disable menu items, add the `disabled` attribute to `<mdui-menu-item>`.

```html
<mdui-menu>
  <mdui-menu-item disabled>Item 1</mdui-menu-item>
  <mdui-menu-item>Item 2</mdui-menu-item>
  <mdui-menu-item>Item 3</mdui-menu-item>
</mdui-menu>
```

### Single Selection {#example-selects-single}

For single selection, set the `selects` attribute to `single` on `<mdui-menu>`. The `value` of `<mdui-menu>` is the `value` of the selected `<mdui-menu-item>`.

```html
<mdui-menu selects="single" value="item-2">
  <mdui-menu-item value="item-1">Item 1</mdui-menu-item>
  <mdui-menu-item value="item-2">Item 2</mdui-menu-item>
</mdui-menu>
```

### Multiple Selection {#example-selects-multiple}

For multiple selection, set the `selects` attribute to `multiple` on `<mdui-menu>`. The `value` of `<mdui-menu>` is an array of the selected `<mdui-menu-item>` values.

Note: For multiple selection, the `value` of `<mdui-menu>` is an array and can only be accessed and updated in JavaScript.

```html
<mdui-menu selects="multiple" class="example-multiple">
  <mdui-menu-item value="item-1">Item 1</mdui-menu-item>
  <mdui-menu-item value="item-2">Item 2</mdui-menu-item>
  <mdui-menu-item value="item-3">Item 3</mdui-menu-item>
</mdui-menu>

<script>
  // Set default selection to item-1 and item-2
  const menu = document.querySelector(".example-multiple");
  menu.value = ["item-1", "item-2"];
</script>
```

### Icons {#example-icon}

To add Material Icons on the left and right, add `icon`, `end-icon` attributes to `<mdui-menu-item>`. Use `end-text` attribute to add text on the right side. Alternatively, use `icon`, `end-icon`, `end-text` slots for the same purpose.

Setting the `icon` attribute to an empty string reserves space for a left-side icon so the item aligns with the others.

```html
<mdui-menu>
  <mdui-menu-item icon="visibility" end-icon="add_circle" end-text="Ctrl+X">Item 1</mdui-menu-item>
  <mdui-menu-item>
    Item 2
    <mdui-icon slot="icon" name="visibility"></mdui-icon>
    <mdui-icon slot="end-icon" name="add_circle"></mdui-icon>
    <span slot="end-text">Ctrl+X</span>
  </mdui-menu-item>
  <mdui-menu-item icon="">Item 3</mdui-menu-item>
</mdui-menu>
```

For single or multiple selection, use the `selected-icon` attribute or slot to define the selected-state icon.

```html
<mdui-menu selects="multiple">
  <mdui-menu-item value="item-1" selected-icon="cloud_done">Item 1</mdui-menu-item>
  <mdui-menu-item value="item-2">
    <mdui-icon slot="selected-icon" name="done_outline"></mdui-icon>
    Item 2
  </mdui-menu-item>
</mdui-menu>
```

### Link {#example-link}

To turn the menu item into a link, use the `href` attribute on the `<mdui-menu-item>` component. `download`, `target`, and `rel` attributes are available for link-related functionality.

```html
<mdui-menu>
  <mdui-menu-item href="https://www.mdui.org" target="_blank">Item 1</mdui-menu-item>
  <mdui-menu-item>Item 2</mdui-menu-item>
</mdui-menu>
```

### Submenu {#example-submenu}

Use the `submenu` slot in `<mdui-menu-item>` to define submenu items.

```html
<mdui-menu>
  <mdui-menu-item>
    Line spacing
    <mdui-menu-item slot="submenu">Single</mdui-menu-item>
    <mdui-menu-item slot="submenu">1.5</mdui-menu-item>
    <mdui-menu-item slot="submenu">Double</mdui-menu-item>
    <mdui-menu-item slot="submenu">Custom: 1.2</mdui-menu-item>
  </mdui-menu-item>
  <mdui-menu-item>Paragraph style</mdui-menu-item>
</mdui-menu>
```

Use the `submenu-trigger` attribute on `<mdui-menu>` to define how the submenu opens.

```html
<mdui-menu submenu-trigger="click">
  <mdui-menu-item>
    Line spacing
    <mdui-menu-item slot="submenu">Single</mdui-menu-item>
    <mdui-menu-item slot="submenu">1.5</mdui-menu-item>
    <mdui-menu-item slot="submenu">Double</mdui-menu-item>
    <mdui-menu-item slot="submenu">Custom: 1.2</mdui-menu-item>
  </mdui-menu-item>
  <mdui-menu-item>Paragraph style</mdui-menu-item>
</mdui-menu>
```

When `submenu-trigger="hover"` is set, use `submenu-open-delay` and `submenu-close-delay` attributes on `<mdui-menu>` to set the delay for opening and closing the submenu.

```html
<mdui-menu submenu-trigger="hover" submenu-open-delay="1000" submenu-close-delay="1000">
  <mdui-menu-item>
    Line spacing
    <mdui-menu-item slot="submenu">Single</mdui-menu-item>
    <mdui-menu-item slot="submenu">1.5</mdui-menu-item>
    <mdui-menu-item slot="submenu">Double</mdui-menu-item>
    <mdui-menu-item slot="submenu">Custom: 1.2</mdui-menu-item>
  </mdui-menu-item>
  <mdui-menu-item>Paragraph style</mdui-menu-item>
</mdui-menu>
```

### Custom Content {#example-custom}

To fully customize the menu item content, use the `custom` slot in `<mdui-menu-item>`.

```html
<style>
  .custom-item {
    padding: 4px 12px;
  }

  .custom-item .secondary {
    display: none;
    color: #888;
    font-size: 13px;
  }

  .custom-item:hover .secondary {
    display: block
  }
</style>

<mdui-menu>
  <mdui-menu-item>
    <div slot="custom" class="custom-item">
      <div>ABS</div>
      <div class="secondary">Absolute value of the number</div>
    </div>
  </mdui-menu-item>
  <mdui-menu-item>
    <div slot="custom" class="custom-item">
      <div>ACOS</div>
      <div class="secondary">Arc cosine of the number, in radians</div>
    </div>
  </mdui-menu-item>
  <mdui-menu-item>
    <div slot="custom" class="custom-item">
      <div>ACOSH</div>
      <div class="secondary">Inverse hyperbolic cosine of the number</div>
    </div>
  </mdui-menu-item>
</mdui-menu>
```

## mdui-menu-item API

### Properties

<table>
<thead>
  <tr>
    <th>Attribute</th>
    <th>Property</th>
    <th>Reflect</th>
    <th>Type</th>
    <th>Default</th>
    <th>Description</th>
  </tr>
</thead>
<tbody>
  <tr>
    <td>value</td>
    <td>value</td>
    <td>true</td>
    <td>string</td>
    <td></td>
    <td><p>The value of the menu item.</p>
</td>
  </tr>
  <tr>
    <td>disabled</td>
    <td>disabled</td>
    <td>true</td>
    <td>boolean</td>
    <td>false</td>
    <td><p>Disables the menu item.</p>
</td>
  </tr>
  <tr>
    <td>icon</td>
    <td>icon</td>
    <td>true</td>
    <td>string</td>
    <td></td>
    <td><p>Specifies the Material Icons name for the left icon. Alternatively, use <code>slot=&quot;icon&quot;</code>. An empty string reserves space for an icon.</p>
</td>
  </tr>
  <tr>
    <td>end-icon</td>
    <td>endIcon</td>
    <td>true</td>
    <td>string</td>
    <td></td>
    <td><p>Specifies the Material Icons name for the right icon. Alternatively, use <code>slot=&quot;end-icon&quot;</code>.</p>
</td>
  </tr>
  <tr>
    <td>end-text</td>
    <td>endText</td>
    <td>true</td>
    <td>string</td>
    <td></td>
    <td><p>Specifies the right text. Alternatively, use <code>slot=&quot;end-text&quot;</code>.</p>
</td>
  </tr>
  <tr>
    <td>selected-icon</td>
    <td>selectedIcon</td>
    <td>true</td>
    <td>string</td>
    <td></td>
    <td><p>Specifies the Material Icons name for the selected state. Alternatively, use <code>slot=&quot;selected-icon&quot;</code>.</p>
</td>
  </tr>
  <tr>
    <td>submenu-open</td>
    <td>submenuOpen</td>
    <td>true</td>
    <td>boolean</td>
    <td>false</td>
    <td><p>Opens the submenu.</p>
</td>
  </tr>
  <tr>
    <td>href</td>
    <td>href</td>
    <td>true</td>
    <td>string</td>
    <td></td>
    <td><p>The URL for the link. When set, the component renders as an <code>&lt;a&gt;</code> element and supports link-related attributes.</p>
</td>
  </tr>
  <tr>
    <td>download</td>
    <td>download</td>
    <td>true</td>
    <td>string</td>
    <td></td>
    <td><p>Downloads the linked URL.</p>
<p><strong>Note</strong>: Only available when <code>href</code> is specified.</p>
</td>
  </tr>
  <tr>
    <td>target</td>
    <td>target</td>
    <td>true</td>
    <td>&#39;_blank&#39; | &#39;_parent&#39; | &#39;_self&#39; | &#39;_top&#39;</td>
    <td></td>
    <td><p>Controls where the linked URL opens. Possible values:</p>
<ul>
<li><code>_blank</code>: Opens in a new tab or window.</li>
<li><code>_parent</code>: Opens in the parent browsing context, or <code>_self</code> if there is no parent.</li>
<li><code>_self</code>: Opens in the current browsing context (default).</li>
<li><code>_top</code>: Opens in the topmost browsing context, or <code>_self</code> if there are no ancestors.</li>
</ul>
<p><strong>Note</strong>: Only available when <code>href</code> is specified.</p>
</td>
  </tr>
  <tr>
    <td>rel</td>
    <td>rel</td>
    <td>true</td>
    <td>&#39;alternate&#39; | &#39;author&#39; | &#39;bookmark&#39; | &#39;external&#39; | &#39;help&#39; | &#39;license&#39; | &#39;me&#39; | &#39;next&#39; | &#39;nofollow&#39; | &#39;noreferrer&#39; | &#39;opener&#39; | &#39;prev&#39; | &#39;search&#39; | &#39;tag&#39;</td>
    <td></td>
    <td><p>Specifies the relationship of the linked URL as space-separated link types. Possible values:</p>
<ul>
<li><code>alternate</code>: An alternate version of the current document.</li>
<li><code>author</code>: The author of the current document or article.</li>
<li><code>bookmark</code>: The permalink for the nearest ancestor section.</li>
<li><code>external</code>: The referenced document is not part of the same site as the current document.</li>
<li><code>help</code>: A link to context-sensitive help.</li>
<li><code>license</code>: Content covered by the copyright license described by the referenced document.</li>
<li><code>me</code>: Links to content owned by the current document&#39;s author.</li>
<li><code>next</code>: The next document in the series.</li>
<li><code>nofollow</code>: Not endorsed by the original author or publisher.</li>
<li><code>noreferrer</code>: Prevents the <code>Referer</code> header from being sent. Same effect as <code>noopener</code>.</li>
<li><code>opener</code>: Creates a new browsing context when the hyperlink would otherwise open in a top-level context that is not auxiliary (for example, when <code>target=&quot;_blank&quot;</code> is specified).</li>
<li><code>prev</code>: The previous document in the series.</li>
<li><code>search</code>: Links to a resource that can be used to search through the current document and its related pages.</li>
<li><code>tag</code>: Marks the current document with the given tag.</li>
</ul>
<p><strong>Note</strong>: Only available when <code>href</code> is specified.</p>
</td>
  </tr>
  <tr>
    <td>autofocus</td>
    <td>autofocus</td>
    <td>true</td>
    <td>boolean</td>
    <td>false</td>
    <td><p>Whether the element is focused when the page loads.</p>
</td>
  </tr>
  <tr>
    <td>tabindex</td>
    <td>tabIndex</td>
    <td>false</td>
    <td>number</td>
    <td></td>
    <td><p>The element&#39;s tab order when navigating with the Tab key.</p>
</td>
  </tr>
</tbody>
</table>

### Methods

<table>
<thead>
  <tr>
    <th>Name</th>
    <th>Description</th>
  </tr>
</thead>
<tbody>
  <tr>
    <td>click(): void</td>
    <td><p>Simulates a mouse click on the element.</p>
</td>
  </tr>
  <tr>
    <td>focus(options?: FocusOptions): void</td>
    <td><p>Sets focus on the element. An optional object parameter may include a <code>preventScroll</code> property. If <code>preventScroll</code> is set to <code>true</code>, the page will not scroll to bring the focused element into view.</p>
</td>
  </tr>
  <tr>
    <td>blur(): void</td>
    <td><p>Removes focus from the element.</p>
</td>
  </tr>
</tbody>
</table>

### Events

<table>
<thead>
  <tr>
    <th>Name</th>
    <th>Description</th>
  </tr>
</thead>
<tbody>
  <tr>
    <td>focus</td>
    <td><p>Emitted when the menu item gains focus.</p>
</td>
  </tr>
  <tr>
    <td>blur</td>
    <td><p>Emitted when the menu item loses focus.</p>
</td>
  </tr>
  <tr>
    <td>submenu-open</td>
    <td><p>Emitted when the submenu starts to open. Can be prevented with <code>event.preventDefault()</code>.</p>
</td>
  </tr>
  <tr>
    <td>submenu-opened</td>
    <td><p>Emitted after the submenu has opened and the animations are completed.</p>
</td>
  </tr>
  <tr>
    <td>submenu-close</td>
    <td><p>Emitted when the submenu starts to close. Can be prevented with <code>event.preventDefault()</code>.</p>
</td>
  </tr>
  <tr>
    <td>submenu-closed</td>
    <td><p>Emitted after the submenu has closed and the animations are completed.</p>
</td>
  </tr>
</tbody>
</table>

### Slots

<table>
<thead>
  <tr>
    <th>Name</th>
    <th>Description</th>
  </tr>
</thead>
<tbody>
  <tr>
    <td>(default)</td>
    <td><p>Menu item text.</p>
</td>
  </tr>
  <tr>
    <td>icon</td>
    <td><p>Left icon.</p>
</td>
  </tr>
  <tr>
    <td>end-icon</td>
    <td><p>Right icon.</p>
</td>
  </tr>
  <tr>
    <td>end-text</td>
    <td><p>Right text.</p>
</td>
  </tr>
  <tr>
    <td>selected-icon</td>
    <td><p>Icon for the selected state.</p>
</td>
  </tr>
  <tr>
    <td>submenu</td>
    <td><p>Submenu.</p>
</td>
  </tr>
  <tr>
    <td>custom</td>
    <td><p>Any custom content.</p>
</td>
  </tr>
</tbody>
</table>

### CSS Parts

<table>
<thead>
  <tr>
    <th>Name</th>
    <th>Description</th>
  </tr>
</thead>
<tbody>
  <tr>
    <td>container</td>
    <td><p>Menu item container.</p>
</td>
  </tr>
  <tr>
    <td>icon</td>
    <td><p>Left icon.</p>
</td>
  </tr>
  <tr>
    <td>label</td>
    <td><p>Text content.</p>
</td>
  </tr>
  <tr>
    <td>end-icon</td>
    <td><p>Right icon.</p>
</td>
  </tr>
  <tr>
    <td>end-text</td>
    <td><p>Right text.</p>
</td>
  </tr>
  <tr>
    <td>selected-icon</td>
    <td><p>Icon for the selected state.</p>
</td>
  </tr>
  <tr>
    <td>submenu</td>
    <td><p>Submenu element.</p>
</td>
  </tr>
</tbody>
</table>

## mdui-menu API

### Properties

<table>
<thead>
  <tr>
    <th>Attribute</th>
    <th>Property</th>
    <th>Reflect</th>
    <th>Type</th>
    <th>Default</th>
    <th>Description</th>
  </tr>
</thead>
<tbody>
  <tr>
    <td>selects</td>
    <td>selects</td>
    <td>true</td>
    <td>&#39;single&#39; | &#39;multiple&#39;</td>
    <td></td>
    <td><p>Controls whether menu items can be selected. They are not selectable by default. Possible values:</p>
<ul>
<li><code>single</code>: Only one item can be selected at a time.</li>
<li><code>multiple</code>: Multiple items can be selected.</li>
</ul>
</td>
  </tr>
  <tr>
    <td>value</td>
    <td>value</td>
    <td>false</td>
    <td>string | string[]</td>
    <td></td>
    <td><p>The value of the selected <code>&lt;mdui-menu-item&gt;</code>.</p>
<p><strong>Note</strong>: The HTML attribute always accepts a string and can only be used as an initial value when <code>selects=&quot;single&quot;</code>. The JavaScript property is a string when <code>selects=&quot;single&quot;</code> and an array of strings when <code>selects=&quot;multiple&quot;</code>. When <code>selects=&quot;multiple&quot;</code>, update the JavaScript property to change this value.</p>
</td>
  </tr>
  <tr>
    <td>dense</td>
    <td>dense</td>
    <td>true</td>
    <td>boolean</td>
    <td>false</td>
    <td><p>Indicates whether the menu items use a compact layout.</p>
</td>
  </tr>
  <tr>
    <td>submenu-trigger</td>
    <td>submenuTrigger</td>
    <td>true</td>
    <td>&#39;click&#39; | &#39;hover&#39; | &#39;focus&#39; | &#39;manual&#39; | string</td>
    <td>'click hover'</td>
    <td><p>Defines how submenus open. Multiple space-separated values are supported. Possible values:</p>
<ul>
<li><code>click</code>: Opens the submenu when the menu item is clicked.</li>
<li><code>hover</code>: Opens the submenu when hovering over a menu item.</li>
<li><code>focus</code>: Opens the submenu when the menu item receives focus.</li>
<li><code>manual</code>: Submenus can only be opened and closed programmatically; no other trigger methods can be specified.</li>
</ul>
</td>
  </tr>
  <tr>
    <td>submenu-open-delay</td>
    <td>submenuOpenDelay</td>
    <td>true</td>
    <td>number</td>
    <td>200</td>
    <td><p>The delay (in milliseconds) before a submenu opens on hover.</p>
</td>
  </tr>
  <tr>
    <td>submenu-close-delay</td>
    <td>submenuCloseDelay</td>
    <td>true</td>
    <td>number</td>
    <td>200</td>
    <td><p>The delay (in milliseconds) before a submenu closes on hover.</p>
</td>
  </tr>
</tbody>
</table>

### Methods

<table>
<thead>
  <tr>
    <th>Name</th>
    <th>Description</th>
  </tr>
</thead>
<tbody>
  <tr>
    <td>focus(options?: FocusOptions): void</td>
    <td><p>Sets focus on the element.</p>
</td>
  </tr>
  <tr>
    <td>blur(): void</td>
    <td><p>Removes focus from the element.</p>
</td>
  </tr>
</tbody>
</table>

### Events

<table>
<thead>
  <tr>
    <th>Name</th>
    <th>Description</th>
  </tr>
</thead>
<tbody>
  <tr>
    <td>change</td>
    <td><p>Emitted when the selected state of menu items changes.</p>
</td>
  </tr>
</tbody>
</table>

### Slots

<table>
<thead>
  <tr>
    <th>Name</th>
    <th>Description</th>
  </tr>
</thead>
<tbody>
  <tr>
    <td>(default)</td>
    <td><p>Submenu items (<code>&lt;mdui-menu-item&gt;</code>), dividers (<a href="/en/docs/2/components/divider"><code>&lt;mdui-divider&gt;</code></a>), and other elements.</p>
</td>
  </tr>
</tbody>
</table>

### CSS Custom Properties

<table>
<thead>
  <tr>
    <th>Name</th>
    <th>Description</th>
  </tr>
</thead>
<tbody>
  <tr>
    <td>--shape-corner</td>
    <td><p>The corner radius of the component. You can use a specific pixel value, but it is recommended to reference <a href="/en/docs/2/styles/design-tokens#shape-corner">design tokens</a>.</p>
</td>
  </tr>
</tbody>
</table>

