# Menu usage guidelines

> Discover Cimpress UI's menu component.

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

Menus provide compact lists of options that allow users to take an action or navigate to another part of the UI.

### When to use

- Use to allow users to take actions from a list of options.
- Use when space prevents multiple buttons or actions from being displayed together or when you have a list of 4 or more options.
- Use to allow users to take action on or transform tables, [cards](/components/card), or other data.

### When not to use

- When there are 3 or fewer options, consider using [radio buttons](/components/radio-group), [checkboxes](/components/checkbox), [buttons](/components/button), or [switches](/components/switch) where relevant.
- For primary or critical actions where visibility is necessary. For this, either use more visible components like [buttons](/components/button) and [radio buttons](/components/radio-group), or use [buttons](/components/button) for the primary action and a [menu](/components/menu) for all secondary or tertiary actions.

## Usage

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

export default function Demo() {
  return (
    <MenuRoot>
      <Button iconEnd={<IconChevronDown />}>Menu</Button>
      <Menu>
        <MenuItem>Option 1</MenuItem>
        <MenuItem>Option 2</MenuItem>
        <MenuItem>Option 3</MenuItem>
      </Menu>
    </MenuRoot>
  );
}
```

A common way to display a menu is with a button that includes a text label and a down chevron icon. The button serves as a trigger that opens the menu when pressed.

### Icon-only trigger

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

export default function Demo() {
  return (
    <MenuRoot>
      <IconButton icon={<IconMenuMoreVertical />} aria-label="Menu" />
      <Menu>
        <MenuItem>Option 1</MenuItem>
        <MenuItem>Option 2</MenuItem>
        <MenuItem>Option 3</MenuItem>
      </Menu>
    </MenuRoot>
  );
}
```

Icon buttons can be used in cases where space is limited or where the context makes the menu's purpose clear (after another, related primary action for example). When using an icon button trigger, make sure the icon clearly communicates the menu's purpose.

### With sections

Menu sections are headings that can be used to categorize the items in a menu if there's a large number of options or if the added context helps users understand or differentiate menu items. menu section text should be short, distinct, and clearly label the category of menu items.

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

export default function Demo() {
  return (
    <MenuRoot>
      <Button iconEnd={<IconChevronDown />}>Menu</Button>
      <Menu>
        <MenuSection title="Section 1">
          <MenuItem>Option 1</MenuItem>
          <MenuItem>Option 2</MenuItem>
        </MenuSection>
        <MenuSection title="Section 2">
          <MenuItem>Option 3</MenuItem>
          <MenuItem>Option 4</MenuItem>
        </MenuSection>
      </Menu>
    </MenuRoot>
  );
}
```

### With dividers

Menu dividers add visual distinctions between menu items. They should be used extremely sparingly and only when it is important to visually separate one or two items from the rest of the list. They are commonly used before the last item in the list when that item is more important, destructive, or significantly different from the other options (delete, for example).

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

export default function Demo() {
  return (
    <MenuRoot>
      <Button iconEnd={<IconChevronDown />}>Menu</Button>
      <Menu>
        <MenuItem>Option 1</MenuItem>
        <MenuItem>Option 2</MenuItem>
        <MenuItem>Option 3</MenuItem>
        <Divider />
        <MenuItem>Option 4</MenuItem>
        <MenuItem>Option 5</MenuItem>
      </Menu>
    </MenuRoot>
  );
}
```

### With descriptions

Item descriptions can be added to provide more context for items in a menu. Descriptions should be used consistently in a menu and should only be used in menus with a small number of options. Be careful not to overwhelm the user with too much text in a small list.

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

export default function Demo() {
  return (
    <MenuRoot>
      <Button iconEnd={<IconChevronDown />}>Menu</Button>
      <Menu>
        <MenuItem description="Description 1">Option 1</MenuItem>
        <MenuItem description="Description 2">Option 2</MenuItem>
        <MenuItem description="Description 3">Option 3</MenuItem>
      </Menu>
    </MenuRoot>
  );
}
```

### With icons

Icons can be used with menu items to differentiate them and provide additional context to users. Icons should be used consistently across menus and should clearly represent the menu item. Icons should be used infrequently in menus and only where they add value.

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

export default function Demo() {
  return (
    <MenuRoot>
      <Button iconEnd={<IconChevronDown />}>Menu</Button>
      <Menu>
        <MenuItem icon={<IconAdd />}>Option 1</MenuItem>
        <MenuItem icon={<IconAdd />}>Option 2</MenuItem>
        <MenuItem icon={<IconAdd />}>Option 3</MenuItem>
      </Menu>
    </MenuRoot>
  );
}
```

### With selectable items

Menu items can be made selectable, allowing both single and multiple selection. A single menu can contain multiple sections, each with independently selectable items.

```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>
  );
}
```

### With submenus

Menu items can open additional submenus. The presence of a submenu is indicated by a chevron icon at the end of a menu item.

```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>
  );
}
```

## Formatting and layout

### Placement

Menus can be either right-aligned or left-aligned with any related components or containers. Unlike the form components, menus should not ever span the full width of the container. Their width should instead always be determined by the length of the text label.

Alignment of the menu relative to its trigger can be customized using the `align` property.

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

export default function Demo() {
  return (
    <Stack gap={16} align="center">
      <MenuRoot>
        <Button iconEnd={<IconChevronDown />}>Start-aligned</Button>
        <Menu align="start">
          <MenuItem>Create new resource type</MenuItem>
          <MenuItem>Create resource type hierarchy</MenuItem>
        </Menu>
      </MenuRoot>

      <MenuRoot>
        <Button iconEnd={<IconChevronDown />}>End-aligned</Button>
        <Menu align="end">
          <MenuItem>Create new resource type</MenuItem>
          <MenuItem>Create resource type hierarchy</MenuItem>
        </Menu>
      </MenuRoot>
    </Stack>
  );
}
```

### Spacing

- Menus have 16px of spacing between them and any related component (related buttons, for example).
- Menus have at least 24px of spacing between them and any unrelated components or sections of the UI.

### Button alignment

Menus can be grouped with buttons when there are 4 or more actions being grouped together. Button should be used for the primary or most-used action and any secondary actions should be put in the menu.

Buttons and menus should be grouped horizontally in a row with 16px spacing between them. The menu should always be the right-most component in the group.

**Do:**

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

export default function Demo() {
  return (
    <Stack direction="horizontal" gap={16}>
      <Button>Edit</Button>
      <MenuRoot>
        <Button iconEnd={<IconChevronDown />}>More</Button>
        <Menu>
          <MenuItem>Copy</MenuItem>
          <MenuItem>Move</MenuItem>
          <MenuItem>Archive</MenuItem>
        </Menu>
      </MenuRoot>
    </Stack>
  );
}
```

**Don't:**

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

export default function Demo() {
  return (
    <Stack direction="horizontal" gap={16}>
      <Button>Edit</Button>
      <Button>Copy</Button>
      <Button>Move</Button>
      <Button>Archive</Button>
    </Stack>
  );
}
```

Up to two buttons can be grouped with a menu. Button groups should never have more than one menu.

**Do:**

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

export default function Demo() {
  return (
    <Stack direction="horizontal" gap={16}>
      <Button>Edit</Button>
      <Button>Delete</Button>
      <MenuRoot>
        <Button iconEnd={<IconChevronDown />}>More</Button>
        <Menu>
          <MenuItem>Copy</MenuItem>
          <MenuItem>Move</MenuItem>
          <MenuItem>Archive</MenuItem>
        </Menu>
      </MenuRoot>
    </Stack>
  );
}
```

**Don't:**

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

export default function Demo() {
  return (
    <Stack direction="horizontal" gap={16}>
      <Button>Edit</Button>
      <Button>Delete</Button>
      <Button>Copy</Button>
      <Button>Move</Button>
      <MenuRoot>
        <Button iconEnd={<IconChevronDown />}>More</Button>
        <Menu>
          <MenuItem>Archive</MenuItem>
        </Menu>
      </MenuRoot>
    </Stack>
  );
}
```

**Don't:**

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

export default function Demo() {
  return (
    <Stack direction="horizontal" gap={16}>
      <Button>Edit</Button>
      <MenuRoot>
        <Button iconEnd={<IconChevronDown />}>Copy and move</Button>
        <Menu>
          <MenuItem>Copy</MenuItem>
          <MenuItem>Move</MenuItem>
        </Menu>
      </MenuRoot>
      <MenuRoot>
        <Button iconEnd={<IconChevronDown />}>Publish</Button>
        <Menu>
          <MenuItem>Save as draft</MenuItem>
          <MenuItem>Publish</MenuItem>
        </Menu>
      </MenuRoot>
    </Stack>
  );
}
```

## Content writing

### Label

- Do use a short description of just one or two words for the label such as "Attribute".
- Do use descriptive language that makes it clear what actions the user will find in the menu.
- Do not format labels in the form of a question such as "What action would you like to take?"
- Do not include verbs in your label such as "Select an action".

### List items

- List items should be ordered intentionally to make finding an option easier for users. They can be organized from most selected to least selected or alphabetically.
- Section headings should be short and clearly state how the section has been categorized.
