# Context menu

The actions for what is under the pointer, opened by a right-click, a long press or Shift+F10.

- Family: overlay
- Import: `import { ContextMenu, ContextMenuTrigger } from "@senvenli/react/overlay";`
- Page: https://design.senvenli.com/docs/components/context-menu
- JSON: https://design.senvenli.com/docs/components/context-menu.json

## Example: On a row

Right-click, long-press, or Shift+F10 on the focused button.

```tsx
import { Button } from "@senvenli/react/action";
import { Text } from "@senvenli/react/display";
import { Box } from "@senvenli/react/layout";
import { ContextMenu } from "@senvenli/react/overlay";
import { useState } from "react";

export default function Example() {
  const [last, setLast] = useState("nothing yet");
  return (
    <ContextMenu
      aria-label="Actions for ORD-482"
      items={[
        { id: "open", label: "Open", icon: "external-link" },
        { id: "duplicate", label: "Duplicate", icon: "copy" },
        { id: "delete", label: "Delete", icon: "trash-2", tone: "danger" },
      ]}
      onAction={(id) => setLast(String(id))}
    >
      <Box padding="space-200" background="surface-sunken" radius="large">
        <Button variant="subtle">ORD-482 · Jordan Lee</Button>
        <Text size="body-small" tone="subtle">Last action: {last}</Text>
      </Box>
    </ContextMenu>
  );
}
```

## Examples

### Composed from parts

A `ContextMenuTrigger` holds the region; the parts after it are the menu.

```tsx
import { Text } from "@senvenli/react/display";
import { Box } from "@senvenli/react/layout";
import { ContextMenu, ContextMenuTrigger, MenuItem, MenuSeparator } from "@senvenli/react/overlay";

export default function Example() {
  return (
    <ContextMenu aria-label="Actions for report.pdf">
      <ContextMenuTrigger>
        <Box padding="space-200" background="surface-sunken" radius="large">
          <Text>report.pdf · right-click or long-press</Text>
        </Box>
      </ContextMenuTrigger>
      <MenuItem id="open" icon="external-link">
        Open
      </MenuItem>
      <MenuItem id="rename" icon="pencil" shortcut="F2">
        Rename
      </MenuItem>
      <MenuSeparator />
      <MenuItem id="delete" icon="trash-2" tone="danger">
        Delete
      </MenuItem>
    </ContextMenu>
  );
}
```

## Guidelines

### When to use it

- Rows, cards or a canvas where people expect right-click actions.

### Use something else when

- [menu](https://design.senvenli.com/docs/components/menu.md): The actions belong to a visible button.

### Do and avoid

- Never the only way to an action: put it in a visible Menu too.
- Same order as the row's menu.

## Accessibility

Built in:

- Opens at the pointer, or under the focused element with Shift+F10 or the Menu key; focus goes back on close.

You provide:

- An aria-label for the menu.
- With children: a textValue on a MenuItem whose label is not plain text, so typing can jump to it; the destructive item last, after a MenuSeparator.

## API reference

### ContextMenu

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| children | `React.ReactNode` |  | yes | The region a right-click, a long press or Shift+F10 opens the menu on: a row, a card, a canvas. Or, to compose the menu from parts: a `ContextMenuTrigger` holding that region, followed by `MenuItem`, `MenuSection`, `MenuSubmenu` and `MenuSeparator`. |
| items | `readonly MenuEntry[]` |  | no | The menu as data; leave it out when composing with a `ContextMenuTrigger`. |
| onAction | `(id: Key) => void` |  | no |  |
| disabledKeys | `Iterable<Key>` |  | no |  |
| aria-label | `string` |  | yes | Names the menu ("Actions for ORD-482"). |

### ContextMenuTrigger

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| children | `React.ReactNode` |  | yes | The region that opens the menu. |

### Tokens it reads

`--ds-background-neutral-bold`, `--ds-background-neutral-pressed`, `--ds-background-neutral-subtle-hovered`, `--ds-blanket`, `--ds-border`, `--ds-border-focused`, `--ds-easing-enter`, `--ds-easing-exit`, `--ds-icon-danger`, `--ds-icon-subtle`, `--ds-radius-large`, `--ds-radius-small`, `--ds-radius-xlarge`, `--ds-shadow-overlay`, `--ds-space-050`, `--ds-space-075`, `--ds-space-100`, `--ds-space-150`, `--ds-space-200`, `--ds-space-300`, `--ds-surface-overlay`, `--ds-text`, `--ds-text-danger`, `--ds-text-disabled`, `--ds-text-inverse`, `--ds-text-subtle`, `--ds-text-subtlest`, `--font-ds-mono`

## Related

- [menu](https://design.senvenli.com/docs/components/menu.md)
