# RightClickMenu (/components/right-click-menu)

<ReferenceLinks
  links={[
    { label: "Source", href: meta.links.source, iconName: "code" },
    { label: "Stories", href: meta.links.stories },
  ]}
/>

<Stage code={"<RightClickMenu items={heroItems}>\n  <div style={area}>Right-click this entity</div>\n</RightClickMenu>"}>
  <RightClickMenu items={heroItems}>
    <div style={area}>Right-click this entity</div>
  </RightClickMenu>
</Stage>

## Installation [#installation]

1. Install the package.

```sh
npm install @hashintel/ds-components
```

2. Import the component.

```tsx
import { RightClickMenu } from "@hashintel/ds-components";

export const EntityCard = ({ entityId }: { entityId: string }) => (
  <RightClickMenu
    items={[
      { text: "Open entity", onClick: () => openEntity(entityId) },
      { text: "Duplicate", onClick: () => duplicateEntity(entityId) },
    ]}
  >
    <article>{/* the right-clickable area */}</article>
  </RightClickMenu>
);
```

### When to use [#when-to-use]

<DoList
  items={[
    "Use RightClickMenu as a shortcut to actions on a specific object — a table row, graph node, file, or canvas region.",
    "Use it on large surfaces (canvases, editors) where a persistent toolbar for every object would add noise.",
  ]}
/>

### When not to use [#when-not-to-use]

<DontList
  items={[
    "Avoid making it the only path to an action — right-click is invisible until tried, and many users never try.",
    "Don't wrap text inputs or selectable prose — it suppresses the browser's native context menu (copy, paste, spell-check) and disables text selection in the area.",
  ]}
/>

## Anatomy [#anatomy]

The wrapped children are the right-clickable area; `items` describes the menu
rows using the same items model as Menu — actions, links, submenus, and
labelled groups:

```tsx
<RightClickMenu
  items={[
    // an action — onClick receives the item's id (defaults to its text)
    { text: "Open entity", onClick: openEntity },
    // a link
    { text: "Open in new tab", href: entityUrl, target: "_blank" },
    // a group draws dividers; an empty label makes it a plain divider
    {
      id: "danger",
      label: "",
      items: [{ text: "Delete entity", tone: "error", onClick: confirmDelete }],
    },
  ]}
>
  <EntityCard /> {/* the right-clickable area */}
</RightClickMenu>
```

## Examples [#examples]

### Basic [#basic]

Pass `items` with an `onClick` per action and wrap the area the menu belongs
to. Right-click (or long-press on touch) anywhere inside it opens the menu at
the pointer.

<Stage code={"<RightClickMenu items={basicItems}>\n  <div style={area}>Right-click for entity actions</div>\n</RightClickMenu>"}>
  <RightClickMenu items={basicItems}>
    <div style={area}>Right-click for entity actions</div>
  </RightClickMenu>
</Stage>

### Grouped actions [#grouped-actions]

Wrap related items in `{ id, label, items }` groups — dividers draw between
groups automatically. Pass an empty `label` for a divider without a heading.

<Stage code={"<RightClickMenu items={groupedItems}>\n  <div style={area}>Right-click for grouped actions</div>\n</RightClickMenu>"}>
  <RightClickMenu items={groupedItems}>
    <div style={area}>Right-click for grouped actions</div>
  </RightClickMenu>
</Stage>

### Icons and shortcut hints [#icons-and-shortcut-hints]

An item's `icon` reinforces its label; `suffix` right-aligns extra content,
conventionally the keyboard shortcut. The suffix only displays the hint — the
shortcut itself must be registered elsewhere.

<Stage code={"<RightClickMenu items={flowItems}>\n  <div style={area}>Right-click this flow node</div>\n</RightClickMenu>"}>
  <RightClickMenu items={flowItems}>
    <div style={area}>Right-click this flow node</div>
  </RightClickMenu>
</Stage>

### Disabled items [#disabled-items]

Disable actions that don't currently apply instead of removing them — a stable
list keeps the menu predictable between opens. Disabled items are skipped by
keyboard navigation.

<Stage code={"<RightClickMenu items={propertyItems}>\n  <div style={area}>Right-click with nothing copied</div>\n</RightClickMenu>"}>
  <RightClickMenu items={propertyItems}>
    <div style={area}>Right-click with nothing copied</div>
  </RightClickMenu>
</Stage>

### Destructive actions [#destructive-actions]

Set `tone: "error"` on destructive items and keep them last, in a group of
their own, so a slipped pointer doesn't land on them. Confirm irreversible
actions before running them.

<Stage code={"<RightClickMenu items={archiveItems}>\n  <div style={area}>Right-click this entity</div>\n</RightClickMenu>"}>
  <RightClickMenu items={archiveItems}>
    <div style={area}>Right-click this entity</div>
  </RightClickMenu>
</Stage>

### Submenus [#submenus]

Give an item `subItems` to open a nested menu on hover or ArrowRight. Keep
nesting to one level — deeper trees are hard to traverse with a pointer.

<Stage code={"<RightClickMenu items={moveItems}>\n  <div style={area}>Right-click to move this entity</div>\n</RightClickMenu>"}>
  <RightClickMenu items={moveItems}>
    <div style={area}>Right-click to move this entity</div>
  </RightClickMenu>
</Stage>

### Checkable view options [#checkable-view-options]

Mark stateful rows with `selected` and `selectedStyle: "checkbox"`;
`keepOpenOnSelect` keeps the menu up while the user toggles several. Rebuild
`items` from state — the menu renders them fresh on every open.

```tsx
import { useState } from "react";

import { RightClickMenu } from "@hashintel/ds-components";

export const GraphCanvas = () => {
  const [showGrid, setShowGrid] = useState(true);
  const [snapToGrid, setSnapToGrid] = useState(false);
  return (
    <RightClickMenu
      items={[
        {
          text: "Show grid",
          selected: showGrid,
          selectedStyle: "checkbox",
          keepOpenOnSelect: true,
          onClick: () => setShowGrid((previous) => !previous),
        },
        {
          text: "Snap to grid",
          selected: snapToGrid,
          selectedStyle: "checkbox",
          keepOpenOnSelect: true,
          onClick: () => setSnapToGrid((previous) => !previous),
        },
      ]}
    >
      <section>{/* canvas */}</section>
    </RightClickMenu>
  );
};
```

### Link items [#link-items]

Items with `href` render real anchors — right for "Open in new tab" rows.
`target: "_blank"` is the only target supported.

<Stage code={"<RightClickMenu items={linkItems}>\n  <div style={area}>Right-click this entity type</div>\n</RightClickMenu>"}>
  <RightClickMenu items={linkItems}>
    <div style={area}>Right-click this entity type</div>
  </RightClickMenu>
</Stage>

### Placement [#placement]

The menu opens at the pointer; `position` sets the preferred side and
alignment relative to it, and another placement is chosen when the preferred
one doesn't fit.

<Stage code={"<RightClickMenu items={basicItems} position=\"right-start\">\n  <div style={area}>position=\"right-start\"</div>\n</RightClickMenu>"}>
  <RightClickMenu items={basicItems} position="right-start">
    <div style={area}>position="right-start"</div>
  </RightClickMenu>
</Stage>

## Behavior [#behavior]

- The menu opens on right-click, or a long press from touch and
  pen pointers. Opening suppresses the browser's native context menu inside
  the area.
- Menu contents mount on open and unmount on close, so `items` built from
  current state are always fresh.

## Content [#content]

- Write item labels verb-first, 1–3 words, sentence case: "Rename",
  "Copy entity ID", "Delete entity". No punctuation.
- Show only actions relevant to the object, most-used first — a context menu
  is a shortcut, not a command reference. Keep it under ~10 items per level,
  grouping once you pass 5.
- Keep group labels to one or two category words: "View", "Organize".

## Accessibility [#accessibility]

RightClickMenu implements the
[WAI-ARIA menu pattern](https://www.w3.org/WAI/ARIA/apg/patterns/menu/).

### What RightClickMenu provides [#what-rightclickmenu-provides]

- Menu semantics, highlight-based arrow-key navigation, typeahead, and Escape
  dismissal.
- Long-press opening on touch and pen pointers, so the menu isn't mouse-only.

### What you must do [#what-you-must-do]

- Make the wrapped area focusable (or ensure it contains a focusable element)
  so keyboard users can reach it and open the menu.

## API reference [#api-reference]

### RightClickMenu [#rightclickmenu]

<PropsTable of={RightClickMenu} defaults={{ position: '"bottom-start"' }} />

### MenuItem [#menuitem]

The shape of `items` and `subItems` entries — the same items model as Menu.
Each takes `text` plus exactly one of `onClick`, `href`, or `subItems` — or a
lone `custom` node for a presentational row.

<PropsTable of={MenuItemDoc} defaults={{ selectedStyle: '"highlight"' }} />

### MenuItemGroup [#menuitemgroup]

A labelled group of items, usable wherever items are: `{ id, label, items }`.

<PropsTable of={MenuItemGroupDoc} />
