# Menu API

> Explore the API of Cimpress UI's menu component.

Source: https://ui.cimpress.io/components/menu/api/

## Import

Cimpress UI exports six menu-related components:

- `MenuRoot`: component that connects a trigger button with its associated menu.
- `SubmenuRoot`: component that connects a trigger menu item with its associated submenu.
- `Menu`: component that displays a collapsible list of options and actions that users can choose from.
- `MenuItem`: component that renders a single interactive item within a menu.
- `MenuSection`: component that groups related menu items together under a section title.
- `Divider`: component that adds a horizontal line between menu items to visually separate them.

```tsx
import { Divider, Menu, MenuItem, MenuRoot, MenuSection, SubmenuRoot } from '@cimpress-ui/react';
```

## Accessibility notes

Menu trigger button follows the [ARIA menu button pattern](https://www.w3.org/WAI/ARIA/apg/patterns/menu-button/). See the linked page for a list of available keyboard interactions.

Collapsible menu list follows the [ARIA menu pattern](https://www.w3.org/WAI/ARIA/apg/patterns/menubar/). See the linked page for a list of available keyboard interactions.

Menu trigger button requires a textual label to remain accessible to assistive technologies. See our [accessibility guide](/dev-guides/accessibility#labelling) for more details.

## Triggering a menu

To open a menu when a trigger button is pressed, wrap both the trigger button and the menu component with a `MenuRoot` component. This will automatically associate the button with the menu, without any manual setup.

```tsx
import { Button, Menu, MenuItem, MenuRoot } from '@cimpress-ui/react';

export default function Demo() {
  return (
    <MenuRoot>
      <Button>Open menu</Button>
      <Menu aria-label="Menu">
        <MenuItem>A menu can be opened by an automatically attached trigger button.</MenuItem>
      </Menu>
    </MenuRoot>
  );
}
```

## Submenus

To create submenus, wrap the submenu and the menu item that should open it with a `SubmenuRoot` component. This will automatically associate the menu item with its submenu.

```tsx
import { Button, Menu, MenuItem, MenuRoot, SubmenuRoot } from '@cimpress-ui/react';

export default function Demo() {
  return (
    <MenuRoot>
      <Button>Open menu</Button>
      <Menu aria-label="Menu">
        <MenuItem>This menu item doesn't have a submenu.</MenuItem>

        <SubmenuRoot>
          <MenuItem>This menu item has a submenu.</MenuItem>
          <Menu aria-label="Submenu">
            <MenuItem>A submenu can be opened by hovering or focusing the triggering menu item.</MenuItem>
          </Menu>
        </SubmenuRoot>
      </Menu>
    </MenuRoot>
  );
}
```

## Selectable items

Menu items can be made selectable. Both single and multiple selection is supported. Each menu section has its own selection state.

```tsx
import { Button, Menu, MenuItem, MenuRoot, MenuSection, type Selection } from '@cimpress-ui/react';
import {
  IconChevronDown,
  IconTextAlignmentCenter,
  IconTextAlignmentLeft,
  IconTextAlignmentRight,
  IconTextBold,
  IconTextItalic,
  IconTextStrikethrough,
  IconTextUnderline,
} from '@cimpress-ui/react/icons';
import { useState } from 'react';

const TEXT_STYLE_ITEMS = [
  { id: 'bold', label: 'Bold', icon: <IconTextBold /> },
  { id: 'italic', label: 'Italic', icon: <IconTextItalic /> },
  { id: 'underline', label: 'Underline', icon: <IconTextUnderline /> },
  { id: 'strikethrough', label: 'Strikethrough', icon: <IconTextStrikethrough /> },
];

const TEXT_ALIGNMENT_ITEMS = [
  { id: 'left', label: 'Left', icon: <IconTextAlignmentLeft /> },
  { id: 'center', label: 'Center', icon: <IconTextAlignmentCenter /> },
  { id: 'right', label: 'Right', icon: <IconTextAlignmentRight /> },
];

export default function Demo() {
  const [selectedTextStyles, setSelectedTextStyles] = useState<Selection>(() => new Set(['bold', 'italic']));
  const [selectedTextAlignment, setSelectedTextAlignment] = useState<Selection>(() => new Set(['left']));

  return (
    <MenuRoot>
      <Button iconEnd={<IconChevronDown />}>Text formatting</Button>
      <Menu>
        <MenuSection
          title="Style"
          items={TEXT_STYLE_ITEMS}
          selectionMode="multiple"
          selectedKeys={selectedTextStyles}
          onSelectionChange={setSelectedTextStyles}
        >
          {(item) => <MenuItem icon={item.icon}>{item.label}</MenuItem>}
        </MenuSection>
        <MenuSection
          title="Alignment"
          items={TEXT_ALIGNMENT_ITEMS}
          selectionMode="single"
          selectedKeys={selectedTextAlignment}
          onSelectionChange={setSelectedTextAlignment}
        >
          {(item) => <MenuItem icon={item.icon}>{item.label}</MenuItem>}
        </MenuSection>
      </Menu>
    </MenuRoot>
  );
}
```

## Collections

Menu is a collection component. See our [collection components guide](/dev-guides/collection-components) to learn how to work with collections.

## API reference

### MenuRoot

Encapsulates a menu trigger and its associated menu. The trigger can be any Cimpress UI button, and the menu will be displayed when the trigger is activated.

See [menu usage guidelines](https://ui.cimpress.io/components/menu/).

**Props: `MenuRootProps`**

- `children` (required): `ReactNode`

  The menu trigger with its associated menu. Provide the trigger as the first child, and the menu as the second child.

- `isOpen`: `boolean`

  Whether the overlay is open by default (controlled).

- `defaultOpen`: `boolean`

  Whether the overlay is open by default (uncontrolled).

- `onOpenChange`: `(isOpen: boolean) => void`

  Handler that is called when the overlay's open state changes.

### SubmenuRoot

Encapsulates a submenu trigger and its associated menu. The trigger must be a `MenuItem`, and the menu will be displayed when the trigger is hovered or focused.

See [menu usage guidelines](https://ui.cimpress.io/components/menu/).

**Props: `SubmenuRootProps`**

- `children` (required): `(ReactElement<any, string | JSXElementConstructor<any>>)[]`

  The submenu trigger with its associated menu. Provide the item trigger as the first child, and the menu as the second child.

### Menu

Displays a collapsible list of options and actions that users can choose from.

**Props: `MenuProps<T extends CollectionItem = CollectionItem>`**

- `align`: `'end' | 'start'`

  The alignment of the menu overlay relative to its trigger.

  Defaults to `'start'`.

- `onAction`: `(key: Key) => void`

  Handler that is called when an item is selected. When using `onAction`, all items must have an `id` prop defined.

- `onScroll`: `UIEventHandler<HTMLDivElement>`

  Handler that is called when the list of items is scrolled.

- `children`: `ReactNode | ((item: T) => ReactNode)`

  The contents of the collection.

- `items`: `Iterable<T, any, any>`

  The items to display in the collection.

- `selectionMode`: `SelectionMode`

  The type of selection that is allowed in the collection.

- `selectedKeys`: `'all' | (Iterable<Key, any, any>)`

  The currently selected keys in the collection (controlled).

- `onSelectionChange`: `(keys: Selection) => void`

  Handler that is called when the selection changes.

### MenuItem

Renders a single list item within `Menu`.

**Props: `MenuItemProps`**

- `children` (required): `StringLikeChildren`

  The content to display as the label.

- `id`: `Key`

  The ID of the item. Has to be unique across all sections and items.

- `description`: `string`

  Additional description for the menu item.

- `icon`: `ReactNode`

  An icon representing the menu item.

- `href`: `string`

  A URL to link to.

- `routerOptions`: `undefined`

  Options for the configured client side router.

- `hrefLang`: `string`

  Hints at the human language of the linked URL.
  See[MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#hreflang).

- `target`: `HTMLAttributeAnchorTarget`

  The target window for the link. See
  [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#target).

- `rel`: `string`

  The relationship between the linked resource and the current page. See
  [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel).

- `download`: `string | boolean`

  Causes the browser to download the linked URL. A string may be provided to suggest a file name.
  See [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#download).

- `ping`: `string`

  A space-separated list of URLs to ping when the link is followed. See
  [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#ping).

- `referrerPolicy`: `HTMLAttributeReferrerPolicy`

  How much of the referrer to send when following the link. See
  [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#referrerpolicy).

- `onHoverStart`: `(e: HoverEvent) => void`

  Handler that is called when a hover interaction starts.

- `onHoverEnd`: `(e: HoverEvent) => void`

  Handler that is called when a hover interaction ends.

- `isDisabled`: `boolean`

  Whether the item is disabled.

- `onAction`: `() => void`

  Handler that is called when the item is selected.

### MenuSection

Groups list items within `Menu` into a section.

**Props: `MenuSectionProps<T extends CollectionItem = CollectionItem>`**

- `title` (required): `string`

  The content to display as the section title.

- `id`: `Key`

  The ID of the section. Has to be unique across all sections and items.

- `children`: `ReactNode | ((item: T) => ReactNode)`

  The contents of the collection.

- `items`: `Iterable<T, any, any>`

  The items to display in the collection.

- `selectionMode`: `SelectionMode`

  The type of selection that is allowed in the collection.

- `selectedKeys`: `'all' | (Iterable<Key, any, any>)`

  The currently selected keys in the collection (controlled).

- `onSelectionChange`: `(keys: Selection) => void`

  Handler that is called when the selection changes.

### Divider

Renders a divider between adjacent content.

- `ref`: `Ref<HTMLDivElement>`

  The `ref` type for this component.

**Props: `DividerProps`**

- `orientation`: `'vertical' | 'horizontal'`

  The orientation of the divider (horizontal or vertical).

  Defaults to `'horizontal'`.

- `id`: `string`

  The element's unique identifier. See [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id).

- `data-cim-style-root`: `boolean`

  Use this attribute to "claim" the component tree for exclusive Cimpress UI usage.

- `UNSAFE_className`: `string`

  Sets the CSS className for the element. Only use as a **last resort**. Use style props instead.

  See [styling guide](https://ui.cimpress.io/dev-guides/styling/).

- `UNSAFE_style`: `CSSProperties`

  Sets the CSS style for the element. Only use as a **last resort**. Use style props instead.

  See [styling guide](https://ui.cimpress.io/dev-guides/styling/).

**Props: `StyleProps`**

- `margin`: `Responsive<Spacing | "auto">`

  The amount of margin applied to all edges of this component.

- `marginX`: `Responsive<Spacing | "auto">`

  The amount of margin applied to the left and right edges of this component. Takes priority over `margin`.

- `marginY`: `Responsive<Spacing | "auto">`

  The amount of margin applied to the top and bottom edges of this component. Takes priority over `margin`.

- `marginTop`: `Responsive<Spacing | "auto">`

  The amount of margin applied to the top edge of this component. Takes priority over `marginY` and `margin`.

- `marginRight`: `Responsive<Spacing | "auto">`

  The amount of margin applied to the right edge of this component. Takes priority over `marginX` and `margin`.

- `marginBottom`: `Responsive<Spacing | "auto">`

  The amount of margin applied to the bottom edge of this component. Takes priority over `marginY` and `margin`.

- `marginLeft`: `Responsive<Spacing | "auto">`

  The amount of margin applied to the left edge of this component. Takes priority over `marginX` and `margin`.
