<!-- index: /slotted/llms.txt -->
# context-menu

`data-ui="context-menu"` · Menu opened by right click on a target.

## Import

```tsx
import {
  ContextMenu,
  ContextMenuTrigger,
  ContextMenuDefaultsProvider,
} from '@slotted/react/context-menu';
import { MenuContent, MenuItem, MenuSeparator } from '@slotted/react/menu';
```

## Minimal example

Example

```tsx
import { ContextMenu, ContextMenuTrigger } from '@slotted/react/context-menu';
import { MenuContent, MenuItem, MenuSeparator } from '@slotted/react/menu';
import { Stack } from '@slotted/react/stack';
import { Heading, Text } from '@slotted/react/typography';

export default function ContextMenuBasic() {
  return (
    <ContextMenu>
      <ContextMenuTrigger>
        <Stack gap={1}>
          <Heading level={2} size="md">
            Q3 board deck.pdf
          </Heading>
          <Text size="sm" tone="muted">
            4.2 MB, edited yesterday. Right-click anywhere on this file.
          </Text>
        </Stack>
      </ContextMenuTrigger>
      <MenuContent>
        <MenuItem>Open</MenuItem>
        <MenuItem>Rename</MenuItem>
        <MenuItem>Duplicate</MenuItem>
        <MenuSeparator />
        <MenuItem tone="danger">Move to trash</MenuItem>
      </MenuContent>
    </ContextMenu>
  );
}
```

## Anatomy

| Part | Element | Required |
| --- | --- | --- |
| `root` | div, or the element render returns | yes |

## Props

| Name | Type | Default | Applies to | Description |
| --- | --- | --- | --- | --- |
| `open` | `boolean \| undefined` | — | ContextMenu | Whether the menu shows. Present at all puts the menu in controlled mode: it asks and waits. |
| `defaultOpen` | `boolean` | — | ContextMenu | The value an uncontrolled menu starts from, read once. A menu open from the start hangs from the target. |
| `onOpenChange` | `(open, request) => void` | — | ContextMenu | Asked before the menu opens or closes, with the reason and the originating event, and refusable. |
| `longPress` | `boolean` | true | ContextMenu | Whether holding the target on touch and pen opens the menu. |
| `longPressDelay` | `number` | 500 | ContextMenu | Milliseconds the press is held before the menu opens; movement past ten pixels or lifting first cancels it. |
| `disabled` | `boolean` | false | ContextMenu | The context menu never opens and the browser's own menu passes through. The target carries data-disabled. |
| `size, side, align, closeOnSelect, loop, typeahead, …` | `MenuProps` | — | ContextMenu | Every prop of Menu except anchor and finalFocus, which ContextMenu decides, reaches the menu it renders. |
| `render` | `(props) => ReactNode` | — | ContextMenuTrigger | Renders the consumer's own element as the target — a tr, a li — with the identity, the state and the listeners on it. |
| `ContextMenuDefaultsProvider` | `component` | — | Application | Sets longPress, longPressDelay and disabled once for a subtree. A menu that states one itself still wins. The surface reads MenuDefaultsProvider. |
| `MenuContent, MenuItem, …` | `components` | — | Inside a ContextMenu | The surface is the menu family unchanged: submenus, checkbox and radio items, groups, shortcuts, typeahead and tone reach the context menu without a second API. Nothing is re-exported. |
| `ref` | `prop` | — | ContextMenu | A MenuHandle carrying open(), close() and focus(). open() hangs the menu from the target. |
| `aria-label` | `attribute` | — | MenuContent | The surface is named after the target's text unless given an aria-label. The text fits a row and does not fit a canvas. |
| `native props` | `attributes` | — | ContextMenuTrigger | Anything a div takes reaches the target, and a consumer's own onContextMenu or onPointerDown runs before the family's; preventDefault from it stops the family. |

## Events

| Name | Payload | When |
| --- | --- | --- |
| `onOpenChange` | `(open: boolean, request: MenuChangeRequest)` | Before the menu opens or closes. The request carries pointer (a secondary click or a long press), keyboard (Shift+F10, the Menu key), escape, outsidePress (a press anywhere else, the target included), select, dismiss or programmatic, the originating event, and cancel(). Moving to a new point while open emits nothing, because open did not change. |

## Axes and defaults

| Axis | Values | Default |
| --- | --- | --- |
| `longPress` | `true`, `false` | `true` |
| `disabled` | `true`, `false` | `false` |

## Rules

- The platform supplies the gesture: the contextmenu event is the secondary click, the Menu key and Shift+F10 on every desktop, and the target answers all three with preventDefault while the context menu is enabled and with nothing while it is disabled, so a reader is never left with no menu at all.
- The target writes no aria-haspopup — it is not permitted on a generic element — so a screen reader is not told the region has a menu. For that reason every command in a context menu also exists by another path, and the other-path example shows one.
- Focus goes to the first item on open and, on Escape or after a command, back to whatever held it when the menu opened — the checkbox in the row, the tile itself — rather than to a trigger that never had it. A press elsewhere leaves focus where the reader put it.
- The target writes no tabindex. A target the page made focusable opens from the keyboard; one it did not make focusable does not. A tab stop on every wrapped region would break a grid with roving focus.

## Tokens

`--slotted-border-strong`, `--slotted-border-width-thin`, `--slotted-context-menu-mark-color`, `--slotted-context-menu-mark-width`, `--slotted-duration-short`, `--slotted-easing-standard`, `--slotted-focus-ring-color`, `--slotted-focus-ring-offset`, `--slotted-focus-ring-width`

## Examples

### Playground

The menu hangs from the tile because it opened from script; right-click anywhere on the tile and it moves to the point. The controls under the canvas change the long press and disable the family, which hands the click to the browser.

Excerpt — not a complete module

```tsx
<ContextMenu>
  <ContextMenuTrigger className="tile">…</ContextMenuTrigger>
  <MenuContent aria-label="Document actions">
    <MenuItem>Open</MenuItem>
    <MenuItem tone="danger">Move to trash</MenuItem>
  </MenuContent>
</ContextMenu>;
```

### The target

The tile is the target and paints nothing at rest; while the menu shows, an inward one-pixel mark laid over the tile says which one the commands act on, following the tile’s own corner. The tile on the right was opened from script, so the menu hangs from it the way a keyboard opening places it. A target wrapped around a card would frame the card’s round corners with a square: make the card the target.

Excerpt — not a complete module

```tsx
<ContextMenu defaultOpen>
  <ContextMenuTrigger className="tile" tabIndex={0}>
    …
  </ContextMenuTrigger>
  <MenuContent>…</MenuContent>
</ContextMenu>;
```

### One menu for many rows

A list of files with one ContextMenu around all of them. Right-click any row: the request carries the event, the handler reads the row from it, and the menu names the file it acts on. Mounting a menu per row is the shape this composition exists to avoid.

Example

```tsx
import { useState } from 'react';
import { ContextMenu, ContextMenuTrigger } from '@slotted/react/context-menu';
import { MenuContent, MenuItem, MenuSeparator } from '@slotted/react/menu';
import type { MenuChangeRequest } from '@slotted/react/menu';
import { Stack } from '@slotted/react/stack';
import { Text } from '@slotted/react/typography';

const FILES = [
  { name: 'Supplier contract.docx', size: '180 KB' },
  { name: 'Stock count March.xlsx', size: '640 KB' },
  { name: 'Carrier rates 2026.pdf', size: '2.1 MB' },
];

export default function ContextMenuRows() {
  const [file, setFile] = useState<string | null>(null);

  function handleOpenChange(open: boolean, request: MenuChangeRequest) {
    if (open) setFile(fileOf(request.source));
  }

  return (
    <Stack gap={3}>
      <ContextMenu onOpenChange={handleOpenChange}>
        <ContextMenuTrigger>
          <Stack gap={3}>
            {FILES.map((entry) => (
              <Text data-file={entry.name} key={entry.name}>
                {entry.name} · {entry.size}
              </Text>
            ))}
          </Stack>
        </ContextMenuTrigger>
        <MenuContent aria-label={file ?? 'File actions'}>
          <MenuItem>Open</MenuItem>
          <MenuItem>Download</MenuItem>
          <MenuSeparator />
          <MenuItem tone="danger">Move to trash</MenuItem>
        </MenuContent>
      </ContextMenu>
      <Text size="sm" tone="muted">
        {file === null ? 'Right-click a file.' : `The menu acts on ${file}.`}
      </Text>
    </Stack>
  );
}

function fileOf(source: unknown): string | null {
  const target = (source as Event | undefined)?.target;
  const row = target instanceof Element ? target.closest('[data-file]') : null;

  return row?.getAttribute('data-file') ?? null;
}
```

### From the keyboard

Tab to the tile — it is focusable because the page said so, with tabIndex — and press Shift+F10 or the Menu key. The menu hangs from the tile rather than from wherever the pointer last was, focus lands on the first item, and Escape brings it back to the tile.

Example

```tsx
import { ContextMenu, ContextMenuTrigger } from '@slotted/react/context-menu';
import { MenuContent, MenuItem, MenuSeparator } from '@slotted/react/menu';
import { Stack } from '@slotted/react/stack';
import { Heading, Text } from '@slotted/react/typography';

export default function ContextMenuKeyboard() {
  return (
    <ContextMenu>
      <ContextMenuTrigger tabIndex={0}>
        <Stack gap={1}>
          <Heading level={2} size="md">
            Release notes 4.2
          </Heading>
          <Text size="sm" tone="muted">
            Press Tab to focus this item, then Shift+F10 or the Menu key.
          </Text>
        </Stack>
      </ContextMenuTrigger>
      <MenuContent>
        <MenuItem>Open</MenuItem>
        <MenuItem>Copy link</MenuItem>
        <MenuSeparator />
        <MenuItem>Archive</MenuItem>
      </MenuContent>
    </ContextMenu>
  );
}
```

### On touch

There is no secondary button on a touch screen: holding the tile for half a second opens the menu at the finger, while text selection and the platform's callout are held off for the duration of the press. Lifting or moving past ten pixels before then cancels it, so a scroll that starts on the tile is still a scroll.

Example

```tsx
import { ContextMenu, ContextMenuTrigger } from '@slotted/react/context-menu';
import { MenuContent, MenuItem } from '@slotted/react/menu';
import { Stack } from '@slotted/react/stack';
import { Heading, Text } from '@slotted/react/typography';

export default function ContextMenuTouch() {
  return (
    <ContextMenu longPressDelay={800}>
      <ContextMenuTrigger>
        <Stack gap={1}>
          <Heading level={2} size="md">
            Order SO-10482
          </Heading>
          <Text size="sm" tone="muted">
            Out for delivery. On touch, hold for 800 ms.
          </Text>
        </Stack>
      </ContextMenuTrigger>
      <MenuContent>
        <MenuItem>View order</MenuItem>
        <MenuItem>Track delivery</MenuItem>
        <MenuItem>Print packing slip</MenuItem>
      </MenuContent>
    </ContextMenu>
  );
}
```

### The other path

A context menu is found by convention, and a reader who never right-clicks must still reach every command in it: the same actions sit behind the Actions button on the tile, as a menu opened the ordinary way. The two surfaces are one MenuContent rendered twice.

Example

```tsx
import { Button } from '@slotted/react/button';
import { ContextMenu, ContextMenuTrigger } from '@slotted/react/context-menu';
import { Inline } from '@slotted/react/inline';
import { Menu, MenuContent, MenuItem, MenuTrigger } from '@slotted/react/menu';
import { Stack } from '@slotted/react/stack';
import { Heading, Text } from '@slotted/react/typography';

export default function ContextMenuOtherPath() {
  const actions = (
    <MenuContent aria-label="Shipment actions">
      <MenuItem>Track shipment</MenuItem>
      <MenuItem>Print label</MenuItem>
      <MenuItem>Contact carrier</MenuItem>
    </MenuContent>
  );

  return (
    <ContextMenu>
      <ContextMenuTrigger>
        <Stack gap={2}>
          <Heading level={2} size="md">
            Shipment SH-20418
          </Heading>
          <Text size="sm" tone="muted">
            Right-click this shipment or press Actions: both open the same commands.
          </Text>
          <Inline>
            <Menu>
              <MenuTrigger
                render={(props) => (
                  <Button {...props} variant="outline">
                    Actions
                  </Button>
                )}
              />
              {actions}
            </Menu>
          </Inline>
        </Stack>
      </ContextMenuTrigger>
      {actions}
    </ContextMenu>
  );
}
```

### Disabled

A disabled context menu suppresses nothing: the browser's own menu opens on the secondary click, because taking both away would leave the reader with none. The target says so with data-disabled and paints nothing, since the content it wraps still works.

Example

```tsx
import { ContextMenu, ContextMenuTrigger } from '@slotted/react/context-menu';
import { MenuContent, MenuItem } from '@slotted/react/menu';
import { Stack } from '@slotted/react/stack';
import { Heading, Text } from '@slotted/react/typography';

export default function ContextMenuDisabled() {
  return (
    <Stack gap={4}>
      <ContextMenu>
        <ContextMenuTrigger>
          <Stack gap={1}>
            <Heading level={2} size="md">
              Draft report
            </Heading>
            <Text size="sm" tone="muted">
              Enabled: right-click opens the commands.
            </Text>
          </Stack>
        </ContextMenuTrigger>
        <MenuContent>
          <MenuItem>Edit</MenuItem>
          <MenuItem>Publish</MenuItem>
        </MenuContent>
      </ContextMenu>
      <ContextMenu disabled>
        <ContextMenuTrigger>
          <Stack gap={1}>
            <Heading level={2} size="md">
              Published report
            </Heading>
            <Text size="sm" tone="muted">
              Disabled: right-click opens the browser menu.
            </Text>
          </Stack>
        </ContextMenuTrigger>
        <MenuContent>
          <MenuItem>Edit</MenuItem>
          <MenuItem>Unpublish</MenuItem>
        </MenuContent>
      </ContextMenu>
    </Stack>
  );
}
```

### States

Three tiles: at rest nothing is painted; open draws the inward mark in the color that separates two surfaces; disabled paints nothing either; pressed exists only for the half second a finger is held, and turns off selection rather than painting.

Excerpt — not a complete module

```css
[data-ui='context-menu'][data-open] {
  --slotted-context-menu-mark-color: var(--slotted-tone-accent-border);
}
[data-ui='context-menu'][data-pressed] {
  opacity: 0.9;
}
```

### canvas.tsx

Example

```tsx
import { useState } from 'react';
import { ContextMenu, ContextMenuTrigger } from '@slotted/react/context-menu';
import {
  MenuCheckboxItem,
  MenuContent,
  MenuGroup,
  MenuItem,
  MenuSeparator,
} from '@slotted/react/menu';
import { Stack } from '@slotted/react/stack';
import { Heading, Text } from '@slotted/react/typography';

export default function ContextMenuCanvas() {
  const [labels, setLabels] = useState(true);
  const [paths, setPaths] = useState(false);

  return (
    <ContextMenu>
      <ContextMenuTrigger>
        <Stack gap={1}>
          <Heading level={2} size="md">
            Warehouse floor plan
          </Heading>
          <Text size="sm" tone="muted">
            Aisles A1 to D4. {labels ? 'Aisle labels on.' : 'Aisle labels off.'}{' '}
            {paths ? 'Pick paths on.' : 'Pick paths off.'}
          </Text>
        </Stack>
      </ContextMenuTrigger>
      <MenuContent aria-label="Floor plan actions">
        <MenuGroup label="View">
          <MenuCheckboxItem checked={labels} onCheckedChange={setLabels}>
            Aisle labels
          </MenuCheckboxItem>
          <MenuCheckboxItem checked={paths} onCheckedChange={setPaths}>
            Pick paths
          </MenuCheckboxItem>
        </MenuGroup>
        <MenuSeparator />
        <MenuItem>Export as PDF</MenuItem>
      </MenuContent>
    </ContextMenu>
  );
}
```

### controlled.tsx

Example

```tsx
import { useState } from 'react';
import { ContextMenu, ContextMenuTrigger } from '@slotted/react/context-menu';
import { MenuContent, MenuItem } from '@slotted/react/menu';
import type { MenuChangeRequest } from '@slotted/react/menu';
import { Stack } from '@slotted/react/stack';
import { Heading, Text } from '@slotted/react/typography';

export default function ContextMenuControlled() {
  const [open, setOpen] = useState(false);
  const [last, setLast] = useState('Closed');

  function handleOpenChange(next: boolean, request: MenuChangeRequest) {
    setOpen(next);
    setLast(`${next ? 'Opened' : 'Closed'} by ${request.reason}`);
  }

  return (
    <Stack gap={3}>
      <ContextMenu onOpenChange={handleOpenChange} open={open}>
        <ContextMenuTrigger tabIndex={0}>
          <Stack gap={1}>
            <Heading level={2} size="md">
              Purchase order PO-5521
            </Heading>
            <Text size="sm" tone="muted">
              Right-click, press Shift+F10, press Escape or click elsewhere.
            </Text>
          </Stack>
        </ContextMenuTrigger>
        <MenuContent>
          <MenuItem>Approve</MenuItem>
          <MenuItem>Request changes</MenuItem>
          <MenuItem>Send to supplier</MenuItem>
        </MenuContent>
      </ContextMenu>
      <Text size="sm" tone="muted">
        {last}
      </Text>
    </Stack>
  );
}
```

### defaults.tsx

Example

```tsx
import {
  ContextMenu,
  ContextMenuDefaultsProvider,
  ContextMenuTrigger,
} from '@slotted/react/context-menu';
import { MenuContent, MenuItem } from '@slotted/react/menu';
import { Stack } from '@slotted/react/stack';
import { Heading, Text } from '@slotted/react/typography';

export default function ContextMenuDefaults() {
  return (
    <ContextMenuDefaultsProvider longPressDelay={800}>
      <Stack gap={4}>
        <ContextMenu>
          <ContextMenuTrigger>
            <Stack gap={1}>
              <Heading level={2} size="md">
                Inbound dock
              </Heading>
              <Text size="sm" tone="muted">
                Holds for 800 ms, the delay the provider sets.
              </Text>
            </Stack>
          </ContextMenuTrigger>
          <MenuContent>
            <MenuItem>Schedule unloading</MenuItem>
            <MenuItem>View arrivals</MenuItem>
          </MenuContent>
        </ContextMenu>
        <ContextMenu longPressDelay={300}>
          <ContextMenuTrigger>
            <Stack gap={1}>
              <Heading level={2} size="md">
                Outbound dock
              </Heading>
              <Text size="sm" tone="muted">
                Holds for 300 ms, the delay this target sets.
              </Text>
            </Stack>
          </ContextMenuTrigger>
          <MenuContent>
            <MenuItem>Schedule loading</MenuItem>
            <MenuItem>View departures</MenuItem>
          </MenuContent>
        </ContextMenu>
      </Stack>
    </ContextMenuDefaultsProvider>
  );
}
```

### long-press-off.tsx

Example

```tsx
import { ContextMenu, ContextMenuTrigger } from '@slotted/react/context-menu';
import { MenuContent, MenuItem } from '@slotted/react/menu';
import { Stack } from '@slotted/react/stack';
import { Heading, Text } from '@slotted/react/typography';

export default function ContextMenuLongPressOff() {
  return (
    <ContextMenu longPress={false}>
      <ContextMenuTrigger tabIndex={0}>
        <Stack gap={1}>
          <Heading level={2} size="md">
            Invoice INV-3307
          </Heading>
          <Text size="sm" tone="muted">
            Touch and pen keep their own long-press behavior here.
          </Text>
        </Stack>
      </ContextMenuTrigger>
      <MenuContent>
        <MenuItem>Send reminder</MenuItem>
        <MenuItem>Record payment</MenuItem>
        <MenuItem>Download PDF</MenuItem>
      </MenuContent>
    </ContextMenu>
  );
}
```
