# MenuButton

MenuButton is a high-level compound component that combines a labeled button trigger with a dropdown menu in a single, prop-driven API. Pass `buttonText` and an `items` array and the component handles the Dropdown, Menu, and MenuItem composition internally. It is the recommended choice when you need a simple text-triggered menu without custom content inside the menu panel.

MenuButton belongs to the **menu component family** alongside Dropdown, Menu, MenuItem, and IconMenuButton. For icon-only triggers, use IconMenuButton. For menus that need custom content (headers, dividers, mixed elements), use the lower-level Dropdown + Menu composition instead.

```tsx
<MenuButton
  buttonText="Dashboard..."
  items={[{
  text: 'Profile'
}, {
  text: 'My account'
}, {
  text: 'Logout'
}]}
  showIcon
  variant="solid"
  color="primary"
/>
```

| Field                | Description | Default                                                               |
| -------------------- | ----------- | --------------------------------------------------------------------- |
| buttonText           | —           | "Dashboard..."                                                        |
| buttonComponent      | —           | —                                                                     |
| buttonComponentProps | —           | —                                                                     |
| items                | —           | \[\{ text: 'Profile' }, \{ text: 'My account' }, \{ text: 'Logout' }] |
| showIcon             | —           | true                                                                  |
| startDecorator       | —           | —                                                                     |
| endDecorator         | —           | —                                                                     |
| placement            | —           | —                                                                     |
| variant              | —           | "solid"                                                               |
| color                | —           | "primary"                                                             |
| size                 | —           | —                                                                     |

## Usage

```tsx
import { MenuButton } from '@ceed/cds';

function UserMenu() {
  return (
    <MenuButton
      buttonText="Account"
      items={[
        { text: 'Profile', onClick: () => navigate('/profile') },
        { text: 'Settings', onClick: () => navigate('/settings') },
        { text: 'Logout', onClick: handleLogout },
      ]}
    />
  );
}
```

## When to Use Which

| Component           | Trigger     | Content          | Use when                                                     |
| ------------------- | ----------- | ---------------- | ------------------------------------------------------------ |
| **MenuButton**      | Text button | Simple item list | You need a labeled trigger with a flat list of text options  |
| **IconMenuButton**  | Icon button | Simple item list | Space is limited (tables, cards, toolbars)                   |
| **Dropdown + Menu** | Any element | Rich content     | You need dividers, headers, icons in items, or custom layout |

## Features

### Standalone (Without Dropdown Icon)

Use `showIcon={false}` and a custom `buttonComponent` to render MenuButton as a standalone navigation link without the dropdown indicator.

```tsx
<MenuButton
  buttonText="Dashboard"
  items={[{
  text: 'Profile'
}, {
  text: 'My account'
}, {
  text: 'Logout'
}]}
  showIcon={false}
  variant="solid"
  color="primary"
  buttonComponent={Link}
  buttonComponentProps={{
  to: '/'
}}
/>
```

### With End Decorator

Add badges, chips, or icons after the button text using `endDecorator`.

```tsx
<MenuButton
  buttonText="Dashboard"
  items={[{
  text: 'Profile'
}, {
  text: 'My account'
}, {
  text: 'Logout'
}]}
  showIcon
  variant="solid"
  color="primary"
  endDecorator={<Chip color="primary">New</Chip>}
/>
```

### Placement: Bottom Start

Menu aligns to the left edge of the trigger button.

```tsx
<MenuButton
  buttonText="Hi"
  items={[{
  text: 'Profile'
}, {
  text: 'My account'
}, {
  text: 'Logout'
}]}
  showIcon
  variant="solid"
  color="primary"
  placement="bottom-start"
/>
```

### Placement: Bottom

Menu centers below the trigger button.

```tsx
<MenuButton
  buttonText="Hi"
  items={[{
  text: 'Profile'
}, {
  text: 'My account'
}, {
  text: 'Logout'
}]}
  showIcon
  variant="solid"
  color="primary"
  placement="bottom"
/>
```

### Placement: Bottom End

Menu aligns to the right edge of the trigger button.

```tsx
<MenuButton
  buttonText="Hi"
  items={[{
  text: 'Profile'
}, {
  text: 'My account'
}, {
  text: 'Logout'
}]}
  showIcon
  variant="solid"
  color="primary"
  placement="bottom-end"
/>
```

## Common Use Cases

### User Account Menu

```tsx
function AccountMenu({ user, onLogout }) {
  return (
    <MenuButton
      buttonText={user.name}
      startDecorator={<Avatar src={user.avatar} size="sm" />}
      variant="plain"
      color="neutral"
      items={[
        { text: 'Profile', onClick: () => navigate('/profile') },
        { text: 'Account Settings', onClick: () => navigate('/settings') },
        { text: 'Logout', onClick: onLogout },
      ]}
    />
  );
}
```

### Export Options

```tsx
function ExportMenu({ onExport }) {
  return (
    <MenuButton
      buttonText="Export"
      variant="outlined"
      startDecorator={<DownloadIcon />}
      items={[
        { text: 'Export as CSV', onClick: () => onExport('csv') },
        { text: 'Export as Excel', onClick: () => onExport('xlsx') },
        { text: 'Export as PDF', onClick: () => onExport('pdf') },
      ]}
    />
  );
}
```

### Toolbar Actions

```tsx
function ToolbarActions({ selectedItems, onAction }) {
  return (
    <MenuButton
      buttonText="Actions"
      variant="outlined"
      color="neutral"
      disabled={selectedItems.length === 0}
      items={[
        { text: `Edit (${selectedItems.length})`, onClick: () => onAction('edit') },
        { text: 'Duplicate', onClick: () => onAction('duplicate') },
        { text: 'Archive', onClick: () => onAction('archive') },
        { text: 'Delete', onClick: () => onAction('delete') },
      ]}
    />
  );
}
```

## Props and Customization

### Key Props

| Prop              | Type                                                           | Default          | Description                          |
| ----------------- | -------------------------------------------------------------- | ---------------- | ------------------------------------ |
| `buttonText`      | `string`                                                       | (required)       | Text displayed in the trigger button |
| `items`           | `{ text: string; onClick?: () => void }[]`                     | (required)       | Menu item list                       |
| `variant`         | `'solid' \| 'soft' \| 'outlined' \| 'plain'`                   | `'outlined'`     | Button visual style                  |
| `size`            | `'sm' \| 'md' \| 'lg'`                                         | `'md'`           | Button size                          |
| `color`           | `'primary' \| 'neutral' \| 'danger' \| 'success' \| 'warning'` | `'neutral'`      | Button color                         |
| `disabled`        | `boolean`                                                      | `false`          | Disables the button                  |
| `startDecorator`  | `ReactNode`                                                    | -                | Content before button text           |
| `endDecorator`    | `ReactNode`                                                    | -                | Content after button text            |
| `showIcon`        | `boolean`                                                      | `true`           | Show dropdown arrow icon             |
| `placement`       | `Placement`                                                    | `'bottom-start'` | Menu position                        |
| `buttonComponent` | `ElementType`                                                  | -                | Custom trigger element               |

## Best Practices

- **Use descriptive button text.** The button label should clearly indicate what kind of options the menu contains. Avoid vague labels like "More" or "Options" -- prefer specific labels like "Export Options" or "User Actions".

  ```tsx
  {
    /* ✅ Good: Clear purpose */
  }
  <MenuButton buttonText="Export Options" items={exportItems} />;

  {
    /* ❌ Avoid: Vague label */
  }
  <MenuButton buttonText="More" items={items} />;
  ```

- **Keep the items list short and focused.** Aim for 3-7 items that are logically related. If you need more items or mixed content, switch to the Dropdown + Menu composition pattern.

- **Match variant to context.** Use `variant="solid"` for primary action menus, `variant="outlined"` or `variant="plain"` for secondary or navigation menus. Be consistent within the same toolbar or header.

  ```tsx
  {/* ✅ Good: Appropriate variants */}
  <MenuButton buttonText="Create" variant="solid" color="primary" items={createItems} />
  <MenuButton buttonText="Settings" variant="plain" color="neutral" items={settingsItems} />
  ```

- **Do not use MenuButton for form value selection.** MenuButton triggers actions -- it is not a Select replacement. For form inputs where the user picks a value, use the Select component.

  ```tsx
  {
    /* ❌ Bad: Form selection */
  }
  <MenuButton buttonText={selectedValue} items={options} />;

  {
    /* ✅ Good: Use Select for forms */
  }
  <Select value={selectedValue} onChange={handleChange}>
    <Option value="a">Option A</Option>
    <Option value="b">Option B</Option>
  </Select>;
  ```

- **Use `placement` to prevent overflow.** When the trigger is near the right edge of the viewport, use `placement="bottom-end"` so the menu does not extend beyond the screen.

## Accessibility

- **ARIA attributes**: The trigger button automatically receives `aria-haspopup="true"` and `aria-expanded` that reflects the open/closed state. The menu panel has `role="menu"` and each item has `role="menuitem"`.
- **Keyboard navigation**: Enter or Space opens the menu. Arrow Down/Up moves between items. Escape closes the menu and returns focus to the button. Tab closes the menu and moves focus to the next element.
- **Screen reader support**: The button is announced as "\[buttonText], menu button, expanded/collapsed". Menu items are announced with their position (e.g., "Profile, menu item, 1 of 3").
- **Focus management**: Focus moves to the first menu item when the menu opens and returns to the trigger button when the menu closes.
