# IconMenuButton

IconMenuButton is a compact menu trigger that displays only an icon (typically a "more" or kebab icon) to reveal a dropdown list of actions. It shares the same prop-driven API as MenuButton -- pass an `items` array and the component handles the rest -- but it takes up minimal screen space by omitting visible text. This makes it ideal for table rows, card actions, list items, and toolbars where space is limited.

IconMenuButton is part of the **menu component family** alongside Dropdown, Menu, MenuItem, and MenuButton. When space allows, prefer MenuButton for its clearer labeling. When you need custom content inside the menu (dividers, headers, icons on items), use the lower-level Dropdown + Menu composition instead.

```tsx
<IconMenuButton
  icon={<MoreVert />}
  items={[{
  text: 'Profile'
}, {
  text: 'My account'
}, {
  text: 'Logout'
}]}
  variant="solid"
  color="primary"
/>
```

| Field                | Description | Default                                                               |
| -------------------- | ----------- | --------------------------------------------------------------------- |
| icon                 | —           | \<MoreVert />                                                         |
| buttonComponent      | —           | —                                                                     |
| buttonComponentProps | —           | —                                                                     |
| items                | —           | \[\{ text: 'Profile' }, \{ text: 'My account' }, \{ text: 'Logout' }] |
| placement            | —           | —                                                                     |
| variant              | —           | "solid"                                                               |
| color                | —           | "primary"                                                             |
| size                 | —           | —                                                                     |

## Usage

```tsx
import { IconMenuButton } from '@ceed/cds';
import MoreVertIcon from '@mui/icons-material/MoreVert';

function RowActions({ onEdit, onDelete }) {
  return (
    <IconMenuButton
      icon={<MoreVertIcon />}
      buttonComponentProps={{ 'aria-label': 'Row actions' }}
      items={[
        { text: 'Edit', onClick: onEdit },
        { text: 'Delete', onClick: onDelete },
      ]}
    />
  );
}
```

## Features

### Standalone

Icon button rendered without dropdown menu functionality, useful as a custom link trigger.

```tsx
<IconMenuButton
  icon={<MoreVert />}
  items={[{
  text: 'Profile'
}, {
  text: 'My account'
}, {
  text: 'Logout'
}]}
  variant="solid"
  color="primary"
  buttonComponent={Link}
  buttonComponentProps={{
  to: '/'
}}
/>
```

### Placement: Bottom Start

Menu aligns to the left edge of the icon button.

```tsx
<IconMenuButton
  icon={<MoreVert />}
  items={[{
  text: 'Profile'
}, {
  text: 'My account'
}, {
  text: 'Logout'
}]}
  variant="solid"
  color="primary"
  placement="bottom-start"
  buttonComponentProps={{
  'aria-label': 'Hi'
}}
/>
```

### Placement: Bottom

Menu centers below the icon button.

```tsx
<IconMenuButton
  icon={<MoreVert />}
  items={[{
  text: 'Profile'
}, {
  text: 'My account'
}, {
  text: 'Logout'
}]}
  variant="solid"
  color="primary"
  placement="bottom"
  buttonComponentProps={{
  'aria-label': 'Hi'
}}
/>
```

### Placement: Bottom End

Menu aligns to the right edge of the icon button. Recommended when the button is positioned on the right side of a container to prevent the menu from overflowing the viewport.

```tsx
<IconMenuButton
  icon={<MoreVert />}
  items={[{
  text: 'Profile'
}, {
  text: 'My account'
}, {
  text: 'Logout'
}]}
  variant="solid"
  color="primary"
  placement="bottom-end"
  buttonComponentProps={{
  'aria-label': 'Hi'
}}
/>
```

## Common Use Cases

### Table Row Actions

```tsx
function DataTableRow({ row, onEdit, onDelete, onDuplicate }) {
  return (
    <tr>
      <td>{row.name}</td>
      <td>{row.status}</td>
      <td>
        <IconMenuButton
          icon={<MoreVertIcon />}
          variant="plain"
          color="neutral"
          size="sm"
          buttonComponentProps={{ 'aria-label': `Actions for ${row.name}` }}
          items={[
            { text: 'Edit', onClick: () => onEdit(row.id) },
            { text: 'Duplicate', onClick: () => onDuplicate(row.id) },
            { text: 'Delete', onClick: () => onDelete(row.id) },
          ]}
        />
      </td>
    </tr>
  );
}
```

### Card Actions

```tsx
function ProductCard({ product, onAction }) {
  return (
    <Card>
      <CardContent>
        <Box sx={{ display: 'flex', justifyContent: 'space-between' }}>
          <Typography level="title-md">{product.name}</Typography>
          <IconMenuButton
            icon={<MoreVertIcon />}
            variant="plain"
            size="sm"
            buttonComponentProps={{ 'aria-label': `Actions for ${product.name}` }}
            items={[
              { text: 'View Details', onClick: () => onAction('view', product.id) },
              { text: 'Edit', onClick: () => onAction('edit', product.id) },
              { text: 'Delete', onClick: () => onAction('delete', product.id) },
            ]}
          />
        </Box>
      </CardContent>
    </Card>
  );
}
```

### Toolbar Overflow

```tsx
function EditorToolbar({ onAction }) {
  return (
    <Stack direction="row" gap={1} alignItems="center">
      <IconButton onClick={() => onAction('bold')}>
        <FormatBoldIcon />
      </IconButton>
      <IconButton onClick={() => onAction('italic')}>
        <FormatItalicIcon />
      </IconButton>
      <Divider orientation="vertical" />
      <IconMenuButton
        icon={<MoreHorizIcon />}
        variant="plain"
        color="neutral"
        buttonComponentProps={{ 'aria-label': 'More formatting options' }}
        items={[
          { text: 'Strikethrough', onClick: () => onAction('strikethrough') },
          { text: 'Superscript', onClick: () => onAction('superscript') },
          { text: 'Code', onClick: () => onAction('code') },
        ]}
      />
    </Stack>
  );
}
```

## Props and Customization

### Key Props

| Prop                   | Type                                                                   | Default     | Description                                                              |
| ---------------------- | ---------------------------------------------------------------------- | ----------- | ------------------------------------------------------------------------ |
| `icon`                 | `ReactNode`                                                            | (required)  | Icon element to render in the trigger button                             |
| `items`                | `{ text: string; component?: ElementType; componentProps?: object }[]` | -           | Menu items. `componentProps` reaches Joy `MenuItem`, so `disabled` works |
| `size`                 | `'sm' \| 'md' \| 'lg'`                                                 | `'md'`      | Button size                                                              |
| `color`                | `'primary' \| 'neutral' \| 'danger' \| 'success' \| 'warning'`         | `'neutral'` | Button color                                                             |
| `variant`              | `'solid' \| 'soft' \| 'outlined' \| 'plain'`                           | `'plain'`   | Button visual style                                                      |
| `disabled`             | `boolean`                                                              | `false`     | Disables the button                                                      |
| `loading`              | `boolean`                                                              | `false`     | Shows loading indicator                                                  |
| `placement`            | `Placement`                                                            | `'bottom'`  | Menu position                                                            |
| `buttonComponent`      | `ElementType`                                                          | -           | Custom trigger element                                                   |
| `buttonComponentProps` | `object`                                                               | -           | Props passed to the trigger. Reaches Joy `IconButton`, so `sx` works     |

> **Note**: `buttonComponentProps` is typed as `ComponentProps<buttonComponent> & AriaAttributes` plus an explicit
> allow-list of Joy `IconButton` props — currently only `sx`. `items[].componentProps` works the same way against Joy
> `MenuItem` — currently only `disabled`. Every other Joy prop still reaches the underlying Joy component at runtime,
> but it does not type-check: extend the `Pick<...>` in `IconMenuButton.tsx` when you need one.

## Best Practices

- **Always provide `aria-label`.** Since IconMenuButton has no visible text, screen readers rely entirely on the `aria-label` to announce the button's purpose. Include context when possible.

  ```tsx
  {
    /* ✅ Good: Descriptive, contextual label */
  }
  <IconMenuButton
    icon={<MoreVertIcon />}
    buttonComponentProps={{ 'aria-label': `Actions for ${item.name}` }}
    items={items}
  />;

  {
    /* ❌ Bad: No accessible name */
  }
  <IconMenuButton icon={<MoreVertIcon />} items={items} />;
  ```

- **Use familiar icons.** Stick to universally recognized menu icons like MoreVert or MoreHoriz. Unfamiliar icons confuse users about what will happen when they click.

  ```tsx
  {/* ✅ Good: Standard kebab/meatball icons */}
  <IconMenuButton icon={<MoreVertIcon />} />
  <IconMenuButton icon={<MoreHorizIcon />} />

  {/* ❌ Avoid: Non-standard icon for a menu trigger */}
  <IconMenuButton icon={<StarIcon />} />
  ```

- **Prefer `variant="plain"` in data-dense contexts.** In tables, lists, and cards, a plain icon button reduces visual noise while remaining discoverable on hover.

- **Position menus to avoid overflow.** Use `placement="bottom-end"` for buttons near the right edge of a container, and `placement="bottom-start"` for buttons near the left edge.

- **Do not hide primary actions.** Important actions like "Save" or "Submit" should always be visible as standalone buttons. Reserve IconMenuButton for secondary or contextual actions.

## Accessibility

- **ARIA requirements**: Always pass `'aria-label'` through `buttonComponentProps`. The button automatically receives `aria-haspopup="true"` and `aria-expanded` reflecting the menu state.
- **Keyboard navigation**: Enter or Space opens the menu. Arrow Down/Up moves between items. Escape closes the menu and returns focus to the icon button. Tab closes the menu and advances focus.
- **Focus visibility**: The icon button displays a visible focus ring when focused via keyboard, and menu items have clear focus indicators for keyboard navigation.
- **Touch targets**: Ensure the button size is at least 44x44 CSS pixels for comfortable touch interaction. Avoid using `size="sm"` in mobile-primary interfaces unless the surrounding layout provides adequate spacing.
