# Context Menu

## Usage

`ContextMenu` gives one region of the interface its own actions: a right-click inside the trigger area — or the keyboard's context-menu key, or a long press on touch — opens a menu at the pointer. When `label` is omitted, the menu's name follows the active locale (`Context menu` in English). It fits list rows, canvases, file nodes, and table cells: places where the actions belong to a specific object and a permanent row of buttons would only add clutter. Each row behaves like a menu row, with an icon, a description, a shortcut hint, or a disabled state.

The pointer position is converted into the trigger's own coordinates and materialized as a zero-size anchor, so the panel is positioned relative to the trigger rather than the viewport. It follows the content when the page scrolls, flips above the pointer near the bottom edge, and never leaves the screen.

There are two ways to describe the content. The data-driven one hands the rows to `items`:

```vue
<script setup lang="ts">
import type { ContextMenuOption } from '@astryx-vue/core/ContextMenu'
import { ContextMenu } from '@astryx-vue/core/ContextMenu'

const items: ContextMenuOption[] = [
  { label: 'Rename', icon: 'i-carbon-tools' },
  { label: 'Copy link', icon: 'i-carbon-copy' },
  { type: 'divider' },
  { label: 'Delete', variant: 'destructive' },
]
</script>

<template>
  <ContextMenu :items="items" label="File actions">
    <div>Right-click here</div>
  </ContextMenu>
</template>
```

An entry can be an action, a divider, a section, a checkbox row, or a radio group. An action that also carries nested entries becomes a submenu. A checkbox row carries whether it is on and a change callback, and toggling it leaves the menu open. A radio group carries the current value and its options, and choosing one closes the menu. When the menu is composed by hand, those rows go in the content slot under the context menu's own names, and they behave like the dropdown menu's rows. Put the built-in icon in the icon slot and pass its name.

When the menu needs to be composed by hand, use the `content` slot with the exported menu components instead. Both styles share one keyboard model:

```vue
<template>
  <ContextMenu label="File actions">
    <div>Right-click here</div>
    <template #content>
      <ContextMenuItem icon="i-carbon-tools" label="Rename" />
      <ContextMenuDivider />
      <ContextMenuGroup title="Danger zone">
        <ContextMenuItem label="Delete" variant="destructive" />
      </ContextMenuGroup>
    </template>
  </ContextMenu>
</template>
```

Open state is managed for you: a right-click opens the menu, and a click outside, Escape, or Tab closes it. Every change is reported through the `openChange` event. Bind `v-model:is-open` when something outside the component should decide, and the menu renders exactly the bound value.

Focus lands on the first available row when the menu opens. The arrow keys move between rows and wrap at both ends, Home and End jump to the first and last row, PageDown and PageUp move by a screenful, and typing letters jumps to the next row whose label starts with them — pressing the same letter again cycles through the matches. Enter and Space activate the focused row, Escape closes the menu and returns focus to whatever was focused before it opened, and Tab closes the menu while letting the browser move focus on from there. A disabled row is marked `aria-disabled`, leaves the tab order, and is skipped by both the arrow keys and typeahead. The surface is `role="menu"`, rows are `role="menuitem"`, sections are `role="group"`, and dividers are `role="separator"`.

A submenu flies out beside its row on a wide screen. On a compact touch surface the next level replaces the current rows and adds a Back row. It can also be forced to always fly out or always replace the rows. While the next level is loading, the caret can become a spinner.

`isDisabled` stops the component intercepting the right-click, so the browser's own menu still appears and people keep their spell-check, copy, and inspect tools. `triggerAs` decides whether the trigger renders as a block `div` or an inline `span`; the inline form lets a word inside running prose own a context menu without breaking the layout.

The panel is at least 160px wide and grows with its rows unless `menuWidth` pins a width; either way it is capped to the viewport, and content taller than 300px scrolls inside it. `size` changes only the rows' block padding, from `sm` to `lg`.

### Anatomy

- **Trigger area**: Caller-provided region that accepts right-click, keyboard context-menu, and long-press input.
- **Pointer menu surface**: Cursor-positioned menu panel used by the pointer presentation.
- **Pointer action row**: DropdownMenu-owned action, selectable option, or submenu row in the pointer presentation.
- **Touch sheet frame**: BottomSheet panel, content area, handle, and optional scrim that host touch actions.
- **Touch menu surface**: ContextMenu-owned content panel rendered inside the touch sheet frame.
- **Touch action list**: Spacious List that groups data-driven touch actions.
- **Touch action row**: ListItem button used for a data-driven action or drill-in entry in the touch presentation.

## Best practices

| Guidance | Practices |
| --- | --- |
| Do | Name what happens in each row. "Rename" and "Copy link" are understood; "Action" and "More" are not. |
| Do | Group related actions with sections and dividers once a menu holds more than a handful of rows. |
| Do | Offer a visible route to the same actions. Not everyone knows to right-click, and screen reader users need to be told the menu exists. |
| Do | Keep destructive actions at the bottom of the menu and mark them `variant="destructive"`. |
| Do | Use `triggerAs="span"` when a word inside running prose needs the menu, so no block element lands inside a paragraph. |
| Don't | Make the context menu the only way to reach important actions. Both the right-click and the long press are hidden gestures. |
| Don't | Stack more than ten or so rows without grouping them. Long menus are slow to scan and hard to reach the end of. |
| Don't | Put a multi-step form inside a menu. A dialog or its own page is the right home for that. |

## Examples

### Basic

An adaptive context area: long-press opens a BottomSheet on compact touch screens, while right-click opens a cursor-positioned menu elsewhere.

```vue
<script setup lang="ts">
import type { ContextMenuOption } from '@astryx-vue/core/ContextMenu'
import { ContextMenu } from '@astryx-vue/core/ContextMenu'

const items: ContextMenuOption[] = [
  { label: 'Cut', onClick: () => {} },
  { label: 'Copy', onClick: () => {} },
  { label: 'Paste', onClick: () => {} },
  { type: 'divider' },
  { label: 'Delete', onClick: () => {} },
]
</script>

<template>
  <ContextMenu presentation="adaptive" :items="items">
    <div
      style="padding: 48px; border-width: 2px; border-style: dashed; border-color: #d1d5db; border-radius: 8px; text-align: center; color: #6b7280; user-select: none;"
    >
      Long-press or right-click this area
    </div>
  </ContextMenu>
</template>
```

### Bottom sheet

A ContextMenu using the explicit BottomSheet presentation. Long-press the target on touch devices or right-click it with a pointer. Keep a visible menu trigger for important mobile actions.

```vue
<script setup lang="ts">
import type { ContextMenuOption } from '@astryx-vue/core/ContextMenu'
import { ContextMenu } from '@astryx-vue/core/ContextMenu'

const items: ContextMenuOption[] = [
  { label: 'Rename document', description: 'Change the title shown to collaborators.', icon: 'i-heroicons-pencil', onClick: () => {} },
  { label: 'Duplicate document', description: 'Create a copy in the same workspace.', icon: 'i-heroicons-document-duplicate', onClick: () => {} },
  { label: 'Share document', description: 'Invite people or copy a share link.', icon: 'i-heroicons-share', onClick: () => {} },
  { label: 'Delete document', description: 'Move this document to the trash.', icon: 'i-heroicons-trash', variant: 'destructive', onClick: () => {} },
]
</script>

<template>
  <ContextMenu presentation="bottom-sheet" label="Document actions" :items="items">
    <div style="display: flex; flex-direction: column; gap: 8px; padding: 32px; border: 1px solid #d1d5db; border-radius: 12px;">
      <strong>Quarterly plan</strong>
      <span>Long-press on touch or right-click for document actions.</span>
    </div>
  </ContextMenu>
</template>
```

### Checkbox and radio

Pin and mute from the custom panel without closing it. Choosing a density closes the menu and updates the card.

```vue
<script setup lang="ts">
import { Card } from '@astryx-vue/core/Card'
import { ContextMenu, ContextMenuCheckboxItem, ContextMenuDivider, ContextMenuRadioGroup, ContextMenuRadioItem } from '@astryx-vue/core/ContextMenu'
import { Icon } from '@astryx-vue/core/Icon'
import { Text } from '@astryx-vue/core/Text'
import { ref } from 'vue'

const pinned = ref(true)
const muted = ref(false)
const density = ref('comfortable')
</script>

<template>
  <ContextMenu label="Canvas actions">
    <Card :padding="6">
      <Text as="div" color="secondary">
        Right-click to pin, mute, or choose a density. Checkboxes stay open; the density choice closes the menu.
      </Text>
      <Text as="div" color="secondary" type="supporting">
        {{ pinned ? 'Pinned' : 'Unpinned' }} · {{ muted ? 'Muted' : 'Notifications on' }} · {{ density }}
      </Text>
    </Card>
    <template #content>
      <ContextMenuCheckboxItem v-model="pinned" label="Pin to top">
        <template #icon>
          <Icon name="carbon:pin" />
        </template>
      </ContextMenuCheckboxItem>
      <ContextMenuCheckboxItem v-model="muted" label="Mute">
        <template #icon>
          <Icon name="carbon:volume-mute" />
        </template>
      </ContextMenuCheckboxItem>
      <ContextMenuDivider />
      <ContextMenuRadioGroup v-model="density" label="Density">
        <ContextMenuRadioItem value="compact" label="Compact" />
        <ContextMenuRadioItem value="comfortable" label="Comfortable" />
      </ContextMenuRadioGroup>
    </template>
  </ContextMenu>
</template>
```

## Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `items` | `ContextMenuOption[]` | - | The data-driven menu content. An entry is an action, a divider, a section, a checkbox row (`type: 'checkbox'`), or a radio group (`type: 'radio'`). An action that carries nested entries becomes a submenu. Omit it and use the content slot. |
| `menuWidth` | `number \| string` | `undefined` | Width of the menu panel. Numbers are interpreted as pixels. Without it the panel is at least 160px wide and sizes to its rows; either way it never overflows the viewport. |
| `size` | `'sm' \| 'md' \| 'lg'` | `'md'` | Row density. It changes the rows' block padding only; type, radius, and panel styling stay the same in every size. |
| `label` | `string` | the active locale (`Context menu` in English) | Accessible name of the menu, announced through the panel's `aria-label` when it opens. |
| `isDisabled` | `boolean` | `false` | Leaves the right-click to the browser and never opens the menu; an open menu is closed. The native context menu still appears. |
| `triggerAs` | `'div' \| 'span'` | `'div'` | Whether the trigger renders as a block `div` or an inline `span` for a word inside running prose. |
| `v-model:isOpen` | `boolean` | - | Controlled open state. While bound the menu renders exactly this value; leave it unbound to let the menu own its state, opened by a right-click, the context-menu key, or a long press. |

## Slots

| Name | Description |
| --- | --- |
| `default` | The trigger area: right-clicking this content opens the menu. |
| `content` | Menu content composed by hand: action rows, dividers, groups, submenus, checkbox rows, and radio groups. This is the custom panel. Ignored when the data array is given. |

## Events and template ref

| Name | Signature | Description |
| --- | --- | --- |
| `openChange` | `(isOpen: boolean) => void` | Fired when the menu opens or closes, before focus moves. |
| `select` | `(item: ContextMenuItemData, event: MouseEvent) => void` | Fired when a row of the `items` data API is activated, with the row and the click that activated it. Rows inside a submenu bubble up to the same event. |
| `update:isOpen` | `(isOpen: boolean) => void` | Listened to by `v-model:isOpen`; it rarely needs handling by hand. |
| `element` | `HTMLElement \| null` | The trigger element, exposed through a template ref. |
| `open()` | `() => void` | Opens the menu at the trigger's top-left corner, for a caller with no pointer event to hand. |
| `close()` | `() => void` | Closes the menu and returns focus to wherever it was before the menu opened. |
| `focus()` / `blur()` | `() => void` | Focuses or blurs the trigger element. |

## Sub-components

Compound mode and data mode render the same row components, so their styling, keyboard behavior, and theme variables cannot drift apart.

| Component | Props | Description |
| --- | --- | --- |
| `ContextMenuItem` | `icon`, `label`, `description`, `endContent`, `href`, `target`, `rel`, `isDisabled`, `hasCloseOnSelect`, `variant` | One action row, rendered as `role="menuitem"`. With `href` the root becomes a real `<a>`, so a modified click and a middle click keep the browser's meaning. Slots: `label`, `icon`, `description`, `endContent`; emits `click` when activated. |
| `ContextMenuDivider` | `variant` | A rule between groups of rows, rendered as `role="separator"` so it is never a stop in the arrow order. |
| `ContextMenuGroup` | `title` | A titled group, rendered as `role="group"` named by its heading. Slot: `default`. |
| `ContextMenuCheckboxItem` | `v-model`, `label`, `description`, `icon`, `isDisabled`, `hasCloseOnSelect` | A checkbox row with role `menuitemcheckbox`. Toggling leaves the menu open. It is the same component as the dropdown menu's checkbox row. |
| `ContextMenuRadioGroup` | `v-model`, `label`, `hasCloseOnSelect` | A radio group. Choosing a value closes the menu. `label` is the group's accessible name. |
| `ContextMenuRadioItem` | `value`, `label`, `description`, `icon`, `isDisabled` | One radio option. It must sit inside a radio group, and its role is `menuitemradio`. |
| `ContextMenuSubMenu` | `icon`, `label`, `description`, `isDisabled`, `items`, `hasSpinner`, `menuWidth`, `presentation` | A row that reveals the next level. By default a wide screen flies out and a compact touch surface replaces the current rows with a Back row. Either form can be forced. `hasSpinner` replaces the caret, and `menuWidth` sizes the flyout. Emits `openChange`. |

## Theming

```css
.my-context-menu {
  --astryx-context-menu-background: #ffffff;
  --astryx-context-menu-item-focus-background: #0536590c;
}
```

- `--astryx-context-menu-background`: panel background.
- `--astryx-context-menu-foreground`: panel text color.
- `--astryx-context-menu-shadow`: panel shadow.
- `--astryx-context-menu-item-foreground`: row text color.
- `--astryx-context-menu-item-focus-background`: background of the highlighted row, for both hover and keyboard focus.
- `--astryx-context-menu-item-pressed-background`: background of a row while a mouse holds it down, painted only where hover exists.
- `--astryx-context-menu-item-description-foreground`: row description color.
- `--astryx-context-menu-item-error-foreground`: label and icon color of a `variant="destructive"` row.
- `--astryx-context-menu-section-foreground`: section heading color.
- `--astryx-context-menu-block-cap`: maximum panel height, resolved against the viewport; the default takes the smaller of 300px and the viewport height.
- `--astryx-spacing-1`, `--astryx-radius-container`, `--astryx-text-label-size`: padding, radius, and type reuse the global tokens.

The neutral theme adapts to light and dark mode on its own.

## Native attributes

The root element is the trigger: a `<div>`, or a `<span>` with `triggerAs="span"`.

The following go to the trigger element:

- HTML attributes such as `id` or `data-*`.
- `aria-*` attributes.

`class` and `style`, on the other hand, go to the menu panel: the panel is the painted, themeable surface (the `astryx-context-menu` class), which mirrors how upstream hands `className` and `style` to the menu.

The trigger itself carries no `role`, `aria-haspopup`, or `aria-expanded`: it is not a button, the right-click is a system-level gesture, and the menu is named by its own `label` only while it is open. The menu panel is teleported to `document.body`, so it inherits nothing from the trigger; the panel carries its own `data-size`, `data-placement`, `data-overflow`, `data-width`, and `data-astryx-menu-press` for styling and tests.
