# Context Menu (/docs/components/context-menu)

> This page documents oakoss/ui, built on React Aria Components, not shadcn/ui or Radix. Treat it as authoritative over what you know of those libraries.

<Tabs defaultValue="Preview">
  <TabsList>
    <TabsTrigger value="Preview">
      Preview
    </TabsTrigger>

    <TabsTrigger value="Code">
      Code
    </TabsTrigger>
  </TabsList>

  <TabsContent value="Preview">
    <ContextMenuDemo />
  </TabsContent>

  <TabsContent value="Code">
    ```tsx
    import {
      ContextMenu,
      ContextMenuItem,
      ContextMenuSeparator,
      ContextMenuTrigger,
    } from '#/components/ui/overlays/context-menu';
    import { Pressable } from 'react-aria-components';

    export function ContextMenuDemo() {
      return (
        <div className="not-prose w-full max-w-sm">
          <ContextMenuTrigger>
            <Pressable>
              <div
                aria-label="report.pdf"
                className="grid h-32 place-items-center rounded-panel border border-dashed border-input text-sm text-muted-foreground outline-offset-2 focus-visible:outline-2 focus-visible:outline-ring"
                role="button"
                tabIndex={0}
              >
                Right-click or long press here
              </div>
            </Pressable>
            <ContextMenu>
              <ContextMenuItem id="open">Open</ContextMenuItem>
              <ContextMenuItem id="rename">Rename</ContextMenuItem>
              <ContextMenuSeparator />
              <ContextMenuItem id="delete" intent="destructive">
                Delete
              </ContextMenuItem>
            </ContextMenu>
          </ContextMenuTrigger>
        </div>
      );
    }

    ```
  </TabsContent>
</Tabs>


## Installation [#installation]

<Tabs defaultValue="CLI">
  <TabsList>
    <TabsTrigger value="CLI">
      CLI
    </TabsTrigger>

    <TabsTrigger value="Manual">
      Manual
    </TabsTrigger>
  </TabsList>

  <TabsContent value="CLI">
    <CodeBlockTabs defaultValue="npm" groupId="package-manager" persist>
      <CodeBlockTabsList>
        <CodeBlockTabsTrigger value="npm">
          npm
        </CodeBlockTabsTrigger>

        <CodeBlockTabsTrigger value="pnpm">
          pnpm
        </CodeBlockTabsTrigger>

        <CodeBlockTabsTrigger value="yarn">
          yarn
        </CodeBlockTabsTrigger>

        <CodeBlockTabsTrigger value="bun">
          bun
        </CodeBlockTabsTrigger>
      </CodeBlockTabsList>

      <CodeBlockTab value="npm">
        ```bash
        npx shadcn@latest add @oakoss/context-menu
        ```
      </CodeBlockTab>

      <CodeBlockTab value="pnpm">
        ```bash
        pnpm dlx shadcn@latest add @oakoss/context-menu
        ```
      </CodeBlockTab>

      <CodeBlockTab value="yarn">
        ```bash
        yarn dlx shadcn@latest add @oakoss/context-menu
        ```
      </CodeBlockTab>

      <CodeBlockTab value="bun">
        ```bash
        bunx --bun shadcn@latest add @oakoss/context-menu
        ```
      </CodeBlockTab>
    </CodeBlockTabs>
  </TabsContent>

  <TabsContent value="Manual">
    <Steps>
      <Step>
        Set up the theme and base styles from [Installation](/docs/installation).
      </Step>

      <Step>
        Install the packages.

        <CodeBlockTabs defaultValue="npm" groupId="package-manager" persist>
          <CodeBlockTabsList>
            <CodeBlockTabsTrigger value="npm">
              npm
            </CodeBlockTabsTrigger>

            <CodeBlockTabsTrigger value="pnpm">
              pnpm
            </CodeBlockTabsTrigger>

            <CodeBlockTabsTrigger value="yarn">
              yarn
            </CodeBlockTabsTrigger>

            <CodeBlockTabsTrigger value="bun">
              bun
            </CodeBlockTabsTrigger>
          </CodeBlockTabsList>

          <CodeBlockTab value="npm">
            ```bash
            npm install cn react-aria-components tailwind-variants
            ```
          </CodeBlockTab>

          <CodeBlockTab value="pnpm">
            ```bash
            pnpm add cn react-aria-components tailwind-variants
            ```
          </CodeBlockTab>

          <CodeBlockTab value="yarn">
            ```bash
            yarn add cn react-aria-components tailwind-variants
            ```
          </CodeBlockTab>

          <CodeBlockTab value="bun">
            ```bash
            bun add cn react-aria-components tailwind-variants
            ```
          </CodeBlockTab>
        </CodeBlockTabs>
      </Step>

      <Step>
        Copy each file into your project, and change its `#/` imports to your aliases.

        * [`components/ui/feedback/spinner.tsx`](https://github.com/oakoss/ui/blob/main/packages/ui/src/components/ui/feedback/spinner.tsx)

        * [`components/icons.tsx`](https://github.com/oakoss/ui/blob/main/packages/ui/src/components/icons.tsx)

        * [`components/icon-placeholder.tsx`](https://github.com/oakoss/ui/blob/main/packages/ui/src/components/icon-placeholder.tsx)

        * [`lib/cx.ts`](https://github.com/oakoss/ui/blob/main/packages/ui/src/lib/cx.ts)

        * [`lib/cn-config.ts`](https://github.com/oakoss/ui/blob/main/packages/ui/src/lib/cn-config.ts)

        * [`components/ui/inputs/button.tsx`](https://github.com/oakoss/ui/blob/main/packages/ui/src/components/ui/inputs/button.tsx)

        * [`lib/recipes.ts`](https://github.com/oakoss/ui/blob/main/packages/ui/src/lib/recipes.ts)

        * [`components/ui/overlays/dialog.tsx`](https://github.com/oakoss/ui/blob/main/packages/ui/src/components/ui/overlays/dialog.tsx)

        * [`hooks/use-scroll-region.ts`](https://github.com/oakoss/ui/blob/main/packages/ui/src/hooks/use-scroll-region.ts)

        * [`components/ui/overlays/popover.tsx`](https://github.com/oakoss/ui/blob/main/packages/ui/src/components/ui/overlays/popover.tsx)

        * [`components/ui/overlays/context-menu.tsx`](https://github.com/oakoss/ui/blob/main/packages/ui/src/components/ui/overlays/context-menu.tsx)

        * [`components/ui/overlays/menu.tsx`](https://github.com/oakoss/ui/blob/main/packages/ui/src/components/ui/overlays/menu.tsx)

        * [`components/ui/overlays/menu-item.tsx`](https://github.com/oakoss/ui/blob/main/packages/ui/src/components/ui/overlays/menu-item.tsx)

        * [`lib/empty-state.ts`](https://github.com/oakoss/ui/blob/main/packages/ui/src/lib/empty-state.ts)

        * [`lib/text-value.ts`](https://github.com/oakoss/ui/blob/main/packages/ui/src/lib/text-value.ts)
      </Step>

      <Step>
        Replace each `<IconPlaceholder>` in `icons.tsx` with your icon library's icon, named in its props. The CLI does this for you; without it, icons render as empty squares.
      </Step>
    </Steps>
  </TabsContent>
</Tabs>


This also installs [Popover](/docs/components/popover), whose panel the menu
opens in.

## Usage [#usage]

```tsx
import {
  ContextMenu,
  ContextMenuItem,
  ContextMenuTrigger,
} from '#/components/ui/overlays/context-menu';
import { Pressable } from 'react-aria-components';

<ContextMenuTrigger>
  <Pressable>
    <div aria-label="report.pdf" role="button" tabIndex={0}>
      report.pdf
    </div>
  </Pressable>
  <ContextMenu onAction={(key) => run(key)}>
    <ContextMenuItem id="open">Open</ContextMenuItem>
    <ContextMenuItem id="delete" intent="destructive">
      Delete
    </ContextMenuItem>
  </ContextMenu>
</ContextMenuTrigger>;
```

The target is a React Aria component, or a focusable element inside React
Aria's `Pressable`, so the keyboard can reach it. Its items, groups, labels,
separators and submenus are [Dropdown Menu](/docs/components/dropdown-menu)'s
with `context-menu-*` slots, so they take the same props.

## Examples [#examples]

### With a visible menu [#with-a-visible-menu]

A context menu is a shortcut, not the only way to an action, so the same
actions also sit in a Dropdown Menu on the item.

<Tabs defaultValue="Preview">
  <TabsList>
    <TabsTrigger value="Preview">
      Preview
    </TabsTrigger>

    <TabsTrigger value="Code">
      Code
    </TabsTrigger>
  </TabsList>

  <TabsContent value="Preview">
    <ContextMenuVisible />
  </TabsContent>

  <TabsContent value="Code">
    ```tsx
    import type { ReactNode } from 'react';

    import * as Icon from '#/components/icons';
    import { Button } from '#/components/ui/inputs/button';
    import {
      ContextMenu,
      ContextMenuItem,
      ContextMenuTrigger,
    } from '#/components/ui/overlays/context-menu';
    import {
      DropdownMenu,
      DropdownMenuItem,
      DropdownMenuTrigger,
    } from '#/components/ui/overlays/dropdown-menu';
    import { Pressable } from 'react-aria-components';

    export function ContextMenuVisible() {
      return (
        <div className="not-prose flex w-full max-w-sm items-center gap-2 rounded-panel border border-border p-2">
          <ContextMenuTrigger>
            <Pressable>
              <div
                className="flex-1 rounded-control px-2 py-1.5 text-sm outline-offset-2 focus-visible:outline-2 focus-visible:outline-ring"
                role="button"
                tabIndex={0}
              >
                report.pdf
              </div>
            </Pressable>
            <ContextMenu>{actions(ContextMenuItem)}</ContextMenu>
          </ContextMenuTrigger>
          <DropdownMenuTrigger>
            <Button aria-label="More actions" size="icon-sm" variant="ghost">
              <Icon.Ellipsis />
            </Button>
            <DropdownMenu placement="bottom end">
              {actions(DropdownMenuItem)}
            </DropdownMenu>
          </DropdownMenuTrigger>
        </div>
      );
    }

    function actions(Item: typeof ContextMenuItem | typeof DropdownMenuItem) {
      return [
        <Item id="open" key="open">
          Open
        </Item>,
        <Item id="delete" intent="destructive" key="delete">
          Delete
        </Item>,
      ] satisfies ReactNode[];
    }

    ```
  </TabsContent>
</Tabs>


## Accessibility [#accessibility]

React Aria opens the menu on the browser's `contextmenu` event, which a
right-click, `Ctrl` + click on macOS and a long press on Android fire. On iOS,
which doesn't fire it, a long press opens the menu. The menu opens with its
corner on the pointer, and from the keyboard where the browser places the
event; on macOS, in browsers that fire no event for `Ctrl` + `Enter`, it opens
at the middle of the target. It's named by its target.

The target announces no popup, since opening a menu isn't its main action. The
menu keeps focus while open and returns it to the target on close; inside it,
the keys are [Dropdown Menu](/docs/components/dropdown-menu#accessibility)'s,
and the menu itself has focus first, so ↓ moves to the first item.

| Key                                   | Action                              |
| ------------------------------------- | ----------------------------------- |
| `Shift` + `F10`, the context menu key | Opens the menu on Windows and Linux |
| `Ctrl` + `Enter`                      | Opens the menu on macOS             |
| `Escape`                              | Closes the menu                     |

## API [#api]

### ContextMenuTrigger [#contextmenutrigger]

Renders React Aria's [`MenuTrigger`](https://react-aria.adobe.com/Menu#api)
with `trigger="contextMenu"`. Accepts its props except `trigger`.

### ContextMenu [#contextmenu]

The popover and its menu, as [`DropdownMenu`](/docs/components/dropdown-menu#dropdownmenu),
with an `offset` of 0 so it sits on the pointer.

#### Props [#props]

| Prop          | Type     | Description |
| ------------- | -------- | ----------- |
| `className?`  | `string` |             |
| `emptyLabel?` | `string` |             |

Also accepts all [React Aria `Menu` props](https://react-aria.adobe.com/Menu#api).

#### Data attributes [#data-attributes]

| Attribute       | Present when                                                    |
| --------------- | --------------------------------------------------------------- |
| `data-slot`     | Always: `context-menu-content` (the panel), `context-menu-list` |
| `data-entering` | The panel is fading in                                          |
| `data-exiting`  | The panel is fading out                                         |

### Other parts [#other-parts]

`ContextMenuItem`, `ContextMenuGroup`, `ContextMenuLabel`,
`ContextMenuSeparator`, `ContextMenuSub`, `ContextMenuSubTrigger` and
`ContextMenuSubContent` take [Dropdown Menu's
props](/docs/components/dropdown-menu#api), with `context-menu-*` in place of
`dropdown-menu-*` in their `data-slot`s.
