# Context Menu

The menu a right-click opens on a row, a file or a canvas.

The menu a right-click opens on a row, a file or a canvas. The Menu, opened on an area instead of a button: a right-click (or a long press on a touch screen, or the context-menu key) opens it at the pointer, with the same rows, options, groups and sub-menus. The area is yours (a row, a card, a canvas); the popup, the gliding highlight and the keyboard are the Menu's. Behaviour from the Zag menu machine through Ark UI.

Kind: component · Category: Overlays · Status: beta · Targets: React, Vue, Svelte · Behaviour: Zag `menu` machine, through Ark UI

## Add

```bash
npx fndn add context-menu
```

It writes the folder `context-menu/` into the project's ui folder (`index.ts`, `context-menu.tsx`, `ContextMenu.vue`, `ContextMenu.svelte`, `context-menu.recipe.ts`), and what it is built from: [Menu](/components/menu.md). First time: [Installation](/installation.md).

## Usage

```tsx
import { ContextMenu } from "@/components/ui/context-menu";
```

```vue
import { ContextMenu } from "@/components/ui/context-menu";
```

```svelte
import { ContextMenu } from "$lib/components/ui/context-menu";
```

## Parts

In the DOM the root carries `data-slot="context-menu"` and each part `data-slot="context-menu-<part>"`; in the recipe the root is the `base` slot and each part a slot named after it in camelCase.

| Part | Element | data-slot | Flags | Notes |
| --- | --- | --- | --- | --- |
| `root` | `div` | `context-menu` |  | The area (Ark's context trigger, as a div around your content). A right-click inside it opens the Menu at the pointer instead of the browser's menu; a long press does on touch. It takes no look of its own. On a coarse pointer it is not text-selectable and shows no callout, so the long press reaches the menu. The popup is the Menu's (menu-positioner, menu-content, menu-highlight, menu-item …). |

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `items` | `the Menu's entries: actions, 'checkbox', 'group', 'radio-group', 'separator', 'submenu'[]` |  | Entries in order, exactly as the Menu takes them. |
| `disabled` | `boolean` | `false` | The area keeps the browser's own context menu. |
| `label` | `string` |  | The area's accessible name (aria-label). The area is focusable: Shift+F10 or the Menu key opens the menu from it. |
| `open` | `boolean` |  | Bindable (React: `defaultOpen` and `onOpenChange`; Vue: `v-model:open`; Svelte: `bind:open`). Controlled: open with onOpenChange. It opens at the last pointer position, so it needs one: before any right-click (or long press, or Shift+F10) on the area, open=true is refused. The menu stays closed, moves no focus, and onOpenChange(false) puts the host's state right. Once a right-click has given a position, open=true opens the menu there. |

Other attributes and `class` go to the root; `class` is merged through the recipe.

## Events

| Event | Payload |
| --- | --- |
| `select` | (value: string) an action was chosen |
| `checkedChange` | (value: string, checked: boolean) |
| `radioChange` | (name: string, value: string) |
| `openChange` | (open: boolean) |

## Slots

| Slot | Content |
| --- | --- |
| `default` | The area's content |

## States

`open`, `highlighted`, `checked`, `disabled`

## Keyboard and accessibility

Role: menu.

Keyboard:

- Tab reaches the area itself (tabIndex 0, role group, aria-keyshortcuts Shift+F10, the global focus ring); Shift+F10 or the context-menu key with focus on or inside it opens the menu (the browser's contextmenu event)
- Inside, the Menu's keys: ArrowDown / ArrowUp, Home / End, type-ahead, Enter / Space, sub-menus with ArrowRight / ArrowLeft
- Escape closes; focus returns to the area, or to the element inside it that had focus

Notes:

- A context menu is a shortcut: everything in it must also be reachable another way (a visible Menu button, a toolbar), since a right-click is not discoverable.
- It never opens without a pointer position: mounted with open=true, or told to open before any right-click on the area, it stays closed, moves no focus and reports closed through openChange. To open a menu from a button or at mount, use the Menu.
- The area is focusable by itself; give it a name with `label` (its aria-label) when its text does not say what it is.

## Motion

- the Menu's popup: opacity + scale from motion.popup-scale out of the pointer position, duration.popup, easing.out-strong; closes at duration.fast; instant from the keyboard
- the area: none

## Dependencies

- [Menu](/components/menu.md)

## Examples

### File rows in a repository

React · `file-rows.tsx`

```tsx
import { ArrowSquareOut, Copy, File, PencilSimple, Trash } from "@phosphor-icons/react";
import { ContextMenu } from "@/components/ui/context-menu";
import type { MenuEntry } from "@/components/ui/menu";

const files = [
  { name: "deliver.ts", modified: "2 min ago" },
  { name: "retry-policy.ts", modified: "Yesterday" },
  { name: "deliver.test.ts", modified: "Nov 12" },
];

const actions: MenuEntry[] = [
  { value: "open", label: "Open", icon: <ArrowSquareOut data-slot="icon" weight="bold" /> },
  { value: "copy-path", label: "Copy path", icon: <Copy data-slot="icon" weight="bold" /> },
  { value: "rename", label: "Rename", icon: <PencilSimple data-slot="icon" weight="bold" /> },
  { type: "separator" },
  { value: "delete", label: "Delete", tone: "danger", icon: <Trash data-slot="icon" weight="bold" /> },
];

/* each row is its own area: right-click, long press or Shift+F10 opens the menu for that file */
export default function FileRows() {
  return (
    <div className="flex w-80 flex-col">
      {files.map((file) => (
        <ContextMenu key={file.name} items={actions} label={file.name} className="flex items-center gap-2 rounded-control px-2 py-1.5">
          <File className="size-4 shrink-0 text-ink-3" weight="bold" />
          <span className="flex-1 font-mono text-sm text-ink">{file.name}</span>
          <span className="text-xs text-ink-2">{file.modified}</span>
        </ContextMenu>
      ))}
    </div>
  );
}
```

Vue · `file-rows.vue`

```vue
<script setup lang="ts">
import { PhArrowSquareOut, PhCopy, PhFile, PhPencilSimple, PhTrash } from "@phosphor-icons/vue";
import { ContextMenu } from "@/components/ui/context-menu";
import type { MenuEntry } from "@/components/ui/menu";

const files = [
  { name: "deliver.ts", modified: "2 min ago" },
  { name: "retry-policy.ts", modified: "Yesterday" },
  { name: "deliver.test.ts", modified: "Nov 12" },
];

const actions: MenuEntry[] = [
  { value: "open", label: "Open", icon: PhArrowSquareOut },
  { value: "copy-path", label: "Copy path", icon: PhCopy },
  { value: "rename", label: "Rename", icon: PhPencilSimple },
  { type: "separator" },
  { value: "delete", label: "Delete", tone: "danger", icon: PhTrash },
];
</script>

<!-- each row is its own area: right-click, long press or Shift+F10 opens the menu for that file -->
<template>
  <div class="flex w-80 flex-col">
    <ContextMenu v-for="file in files" :key="file.name" :items="actions" :label="file.name" class="flex items-center gap-2 rounded-control px-2 py-1.5">
      <PhFile class="size-4 shrink-0 text-ink-3" weight="bold" />
      <span class="flex-1 font-mono text-sm text-ink">{{ file.name }}</span>
      <span class="text-xs text-ink-2">{{ file.modified }}</span>
    </ContextMenu>
  </div>
</template>
```

Svelte · `file-rows.svelte`

```svelte
<script lang="ts">
  import ArrowSquareOut from "phosphor-svelte/lib/ArrowSquareOutIcon";
  import Copy from "phosphor-svelte/lib/CopyIcon";
  import File from "phosphor-svelte/lib/FileIcon";
  import PencilSimple from "phosphor-svelte/lib/PencilSimpleIcon";
  import Trash from "phosphor-svelte/lib/TrashIcon";
  import { ContextMenu } from "$lib/components/ui/context-menu";
  import type { MenuEntry } from "$lib/components/ui/menu";

  const files = [
    { name: "deliver.ts", modified: "2 min ago" },
    { name: "retry-policy.ts", modified: "Yesterday" },
    { name: "deliver.test.ts", modified: "Nov 12" },
  ];

  const actions: MenuEntry[] = [
    { value: "open", label: "Open", icon: ArrowSquareOut },
    { value: "copy-path", label: "Copy path", icon: Copy },
    { value: "rename", label: "Rename", icon: PencilSimple },
    { type: "separator" },
    { value: "delete", label: "Delete", tone: "danger", icon: Trash },
  ];
</script>

<!-- each row is its own area: right-click, long press or Shift+F10 opens the menu for that file -->
<div class="flex w-80 flex-col">
  {#each files as file (file.name)}
    <ContextMenu items={actions} label={file.name} class="flex items-center gap-2 rounded-control px-2 py-1.5">
      <File class="size-4 shrink-0 text-ink-3" weight="bold" />
      <span class="flex-1 font-mono text-sm text-ink">{file.name}</span>
      <span class="text-xs text-ink-2">{file.modified}</span>
    </ContextMenu>
  {/each}
</div>
```

### Deploy row with a sub-menu

React · `deploy-row.tsx`

```tsx
import { ArrowsClockwise, Copy, Terminal, Trash } from "@phosphor-icons/react";
import { ContextMenu } from "@/components/ui/context-menu";
import { StatusPill } from "@/components/ui/status-pill";
import type { MenuEntry } from "@/components/ui/menu";

const items: MenuEntry[] = [
  { value: "logs", label: "View build logs", icon: <Terminal data-slot="icon" weight="bold" /> },
  { value: "copy-id", label: "Copy deploy ID", icon: <Copy data-slot="icon" weight="bold" /> },
  {
    type: "submenu",
    label: "Redeploy to",
    icon: <ArrowsClockwise data-slot="icon" weight="bold" />,
    items: [
      { value: "redeploy:production", label: "Production" },
      { value: "redeploy:staging", label: "Staging" },
      { value: "redeploy:preview", label: "Preview" },
    ],
  },
  { type: "separator" },
  { value: "delete", label: "Delete deploy", tone: "danger", icon: <Trash data-slot="icon" weight="bold" /> },
];

export default function DeployRow() {
  return (
    <ContextMenu items={items} label="Deploy dpl_7f3a9c" className="flex w-80 items-center gap-3 rounded-control px-2 py-1.5">
      <StatusPill tone="green">Ready</StatusPill>
      <div className="flex min-w-0 flex-1 flex-col">
        <span className="font-mono text-sm text-ink">dpl_7f3a9c</span>
        <span className="text-xs text-ink-2">main · a1b2c3d</span>
      </div>
      <span className="text-xs text-ink-2">2 min ago</span>
    </ContextMenu>
  );
}
```

Vue · `deploy-row.vue`

```vue
<script setup lang="ts">
import { PhArrowsClockwise, PhCopy, PhTerminal, PhTrash } from "@phosphor-icons/vue";
import { ContextMenu } from "@/components/ui/context-menu";
import { StatusPill } from "@/components/ui/status-pill";
import type { MenuEntry } from "@/components/ui/menu";

const items: MenuEntry[] = [
  { value: "logs", label: "View build logs", icon: PhTerminal },
  { value: "copy-id", label: "Copy deploy ID", icon: PhCopy },
  {
    type: "submenu",
    label: "Redeploy to",
    icon: PhArrowsClockwise,
    items: [
      { value: "redeploy:production", label: "Production" },
      { value: "redeploy:staging", label: "Staging" },
      { value: "redeploy:preview", label: "Preview" },
    ],
  },
  { type: "separator" },
  { value: "delete", label: "Delete deploy", tone: "danger", icon: PhTrash },
];
</script>

<template>
  <ContextMenu :items="items" label="Deploy dpl_7f3a9c" class="flex w-80 items-center gap-3 rounded-control px-2 py-1.5">
    <StatusPill tone="green">Ready</StatusPill>
    <div class="flex min-w-0 flex-1 flex-col">
      <span class="font-mono text-sm text-ink">dpl_7f3a9c</span>
      <span class="text-xs text-ink-2">main · a1b2c3d</span>
    </div>
    <span class="text-xs text-ink-2">2 min ago</span>
  </ContextMenu>
</template>
```

Svelte · `deploy-row.svelte`

```svelte
<script lang="ts">
  import ArrowsClockwise from "phosphor-svelte/lib/ArrowsClockwiseIcon";
  import Copy from "phosphor-svelte/lib/CopyIcon";
  import Terminal from "phosphor-svelte/lib/TerminalIcon";
  import Trash from "phosphor-svelte/lib/TrashIcon";
  import { ContextMenu } from "$lib/components/ui/context-menu";
  import { StatusPill } from "$lib/components/ui/status-pill";
  import type { MenuEntry } from "$lib/components/ui/menu";

  const items: MenuEntry[] = [
    { value: "logs", label: "View build logs", icon: Terminal },
    { value: "copy-id", label: "Copy deploy ID", icon: Copy },
    {
      type: "submenu",
      label: "Redeploy to",
      icon: ArrowsClockwise,
      items: [
        { value: "redeploy:production", label: "Production" },
        { value: "redeploy:staging", label: "Staging" },
        { value: "redeploy:preview", label: "Preview" },
      ],
    },
    { type: "separator" },
    { value: "delete", label: "Delete deploy", tone: "danger", icon: Trash },
  ];
</script>

<ContextMenu {items} label="Deploy dpl_7f3a9c" class="flex w-80 items-center gap-3 rounded-control px-2 py-1.5">
  <StatusPill tone="green">Ready</StatusPill>
  <div class="flex min-w-0 flex-1 flex-col">
    <span class="font-mono text-sm text-ink">dpl_7f3a9c</span>
    <span class="text-xs text-ink-2">main · a1b2c3d</span>
  </div>
  <span class="text-xs text-ink-2">2 min ago</span>
</ContextMenu>
```

### View options of a build log

React · `log-options.tsx`

```tsx
import { ContextMenu } from "@/components/ui/context-menu";
import type { MenuEntry } from "@/components/ui/menu";

const lines = [
  "10:42:01 Installing dependencies",
  "10:42:19 Building web-app",
  "10:42:48 Build finished in 47s",
];

/* view options of the area itself: they keep their own state unless you pass the handlers */
const items: MenuEntry[] = [
  { type: "checkbox", value: "wrap", label: "Wrap long lines", checked: true },
  { type: "checkbox", value: "timestamps", label: "Show timestamps", checked: true },
  { type: "separator" },
  {
    type: "radio-group",
    name: "level",
    label: "Show",
    value: "all",
    items: [
      { value: "all", label: "Every line" },
      { value: "warnings", label: "Warnings and errors" },
      { value: "errors", label: "Errors only" },
    ],
  },
];

export default function LogOptions() {
  return (
    <ContextMenu items={items} label="Build log" className="flex w-80 flex-col gap-1 rounded-card bg-inset p-3 font-mono text-xs text-ink-2">
      {lines.map((line) => (
        <span key={line}>{line}</span>
      ))}
    </ContextMenu>
  );
}
```

Vue · `log-options.vue`

```vue
<script setup lang="ts">
import { ContextMenu } from "@/components/ui/context-menu";
import type { MenuEntry } from "@/components/ui/menu";

const lines = [
  "10:42:01 Installing dependencies",
  "10:42:19 Building web-app",
  "10:42:48 Build finished in 47s",
];

/* view options of the area itself: they keep their own state unless you listen to their events */
const items: MenuEntry[] = [
  { type: "checkbox", value: "wrap", label: "Wrap long lines", checked: true },
  { type: "checkbox", value: "timestamps", label: "Show timestamps", checked: true },
  { type: "separator" },
  {
    type: "radio-group",
    name: "level",
    label: "Show",
    value: "all",
    items: [
      { value: "all", label: "Every line" },
      { value: "warnings", label: "Warnings and errors" },
      { value: "errors", label: "Errors only" },
    ],
  },
];
</script>

<template>
  <ContextMenu :items="items" label="Build log" class="flex w-80 flex-col gap-1 rounded-card bg-inset p-3 font-mono text-xs text-ink-2">
    <span v-for="line in lines" :key="line">{{ line }}</span>
  </ContextMenu>
</template>
```

Svelte · `log-options.svelte`

```svelte
<script lang="ts">
  import { ContextMenu } from "$lib/components/ui/context-menu";
  import type { MenuEntry } from "$lib/components/ui/menu";

  const lines = [
    "10:42:01 Installing dependencies",
    "10:42:19 Building web-app",
    "10:42:48 Build finished in 47s",
  ];

  /* view options of the area itself: they keep their own state unless you pass the handlers */
  const items: MenuEntry[] = [
    { type: "checkbox", value: "wrap", label: "Wrap long lines", checked: true },
    { type: "checkbox", value: "timestamps", label: "Show timestamps", checked: true },
    { type: "separator" },
    {
      type: "radio-group",
      name: "level",
      label: "Show",
      value: "all",
      items: [
        { value: "all", label: "Every line" },
        { value: "warnings", label: "Warnings and errors" },
        { value: "errors", label: "Errors only" },
      ],
    },
  ];
</script>

<ContextMenu {items} label="Build log" class="flex w-80 flex-col gap-1 rounded-card bg-inset p-3 font-mono text-xs text-ink-2">
  {#each lines as line (line)}
    <span>{line}</span>
  {/each}
</ContextMenu>
```

## Recipe

`context-menu.recipe.ts`:

```ts
import { tv, type VariantProps } from "../lib/tv";

/* context-menu — the Menu opened on an area. The popup, its rows, the gliding highlight and every
 * motion are the menu recipe's (the adapter renders the Menu's own panel); the area is the
 * caller's and takes no look. On a coarse pointer a long press must reach the menu, not start a
 * text selection or the system callout. */

export const contextMenu = tv({
  base: "block pointer-coarse:select-none pointer-coarse:[-webkit-touch-callout:none]",
});

export type ContextMenuRecipeProps = VariantProps<typeof contextMenu>;
```
