Context menu

Slant / Overlays

A menu at the pointer, or opened by Shift+F10 or the menu key.

Demo

Usage

@tuttn/slant
import { ContextMenu, type MenuItem } from '@tuttn/slant'

// Data: a right-click anywhere in the region opens the menu at the pointer.
// An entry with items is a submenu.
const items: MenuItem[] = [
  { id: 'open', label: 'Open' },
  { id: 'move', label: 'Move to', items: [{ id: 'inbox', label: 'Inbox' }, { id: 'archive', label: 'Archive' }] },
  { separator: true },
  { id: 'delete', label: 'Delete', shortcut: 'Del', keyshortcuts: 'Delete' },
]
<ContextMenu label="File actions" items={items} onSelect={run}>
  <div tabIndex={0} className="file-row">report.pdf</div>
</ContextMenu>

// Parts, and asChild to make the row itself the region, with no wrapper.
<ContextMenu label="File actions" onSelect={run} asChild menu={<>
  <ContextMenu.Item id="open">Open</ContextMenu.Item>
  <ContextMenu.Sub label="Move to">
    <ContextMenu.Item id="inbox">Inbox</ContextMenu.Item>
    <ContextMenu.Item id="archive">Archive</ContextMenu.Item>
  </ContextMenu.Sub>
  <ContextMenu.Separator />
  <ContextMenu.Item id="delete">Delete</ContextMenu.Item>
</>}>
  <li tabIndex={0}>report.pdf</li>
</ContextMenu>

// It keeps its own open state; pass open and onOpenChange to control it.
// Opened that way, it hangs below the region.
<ContextMenu label="File actions" items={items} onSelect={run} open={open} onOpenChange={setOpen}>…</ContextMenu>

Props

PropTypeDefaultDescription
labelstring—The menu's accessible name.
itemsMenuItem[]—The entries as data, as Menu takes them: `{ id, label, kind?, checked?, disabled?, group?, textValue?, shortcut?, keyshortcuts?, items? }` or `{ separator: true }`. An entry with `items` is a submenu.
menuReactNode—The compound form, after any `items`: ContextMenu.Item, ContextMenu.CheckboxItem, ContextMenu.RadioItem, ContextMenu.Group, ContextMenu.Sub and ContextMenu.Separator.
onSelect(id: string) => void—Called with the chosen item's id. Checkbox items keep the menu open; the rest close it.
openboolean—Whether the menu is open. Pass it with onOpenChange to control it; leave it out and the menu keeps its own state.
defaultOpenbooleanfalseWhether the menu starts open, when uncontrolled. Opened without a pointer, it hangs from the region.
onOpenChange(open: boolean) => void—Called with the new state whenever the menu opens or closes.
disabledbooleanfalseLeaves right-clicks and the menu keys to the browser, so its own context menu shows. `open` still opens this one.
asChildbooleanfalseMakes the single child the region, with the handlers, ref, className and other attributes merged onto it, instead of wrapping it in a div.
childrenReactNode—The region: a right-click anywhere in it, or Shift+F10 or the menu key on a focused element inside it, opens the menu.
ContextMenu.Item{ id: string; disabled?; textValue?; shortcut?; keyshortcuts? }—A plain item (menuitem). Choosing it calls onSelect(id) and closes the menu. Menu.Item itself.
ContextMenu.CheckboxItem{ id: string; checked?; disabled?; textValue?; shortcut?; keyshortcuts? }—An on/off item (menuitemcheckbox). Choosing it calls onSelect(id) and leaves the menu open.
ContextMenu.RadioItem{ id: string; checked?; disabled?; textValue?; shortcut?; keyshortcuts? }—One choice of several (menuitemradio), usually inside a ContextMenu.Group.
ContextMenu.Group{ label: string }—Wraps items in a group whose accessible name is label.
ContextMenu.Sub{ label: ReactNode; disabled?; textValue? }—An item that opens a submenu of its children beside it. label is the item’s content and names the submenu.
ContextMenu.Separator{}—A rule between groups (separator).
…restHTMLAttributes<HTMLDivElement>—className, style, other attributes and the ref go on the region’s div, or with asChild on the child. The region gets data-open while its menu is open. Each part forwards its ref and attributes to its own element.

A right-click anywhere in the region opens the menu at the pointer, its start corner on the point (in right-to-left text it hangs the other way, its right edge on the point). Shift+F10 or the menu key on a focused element inside the region opens it below that element. Opened through open or defaultOpen, it hangs below the region, which is why the region keeps a box of its own. Near the bottom or the end of the viewport the list flips above the point or slides back into view, and when a container scrolls, it moves with what was clicked.

Focus lands on the first item. Inside, the keys are Menu’s: the arrows move, Home and End jump, typing jumps to an item, Enter or Space chooses, ArrowRight (ArrowLeft in right-to-left) opens a submenu and the arrow back closes it. Escape closes the innermost list; closing the menu with Escape or by choosing returns focus to the element that had it before the menu opened, normally the row that was right-clicked. Tab closes it and moves on from that element. A press outside closes it; a right-click elsewhere in the region moves it there.

The browser’s own context menu stays away while this one is in charge, including on the open list. disabled hands right-clicks and the keys back to the browser. A context menu is hard to discover, and touch browsers differ (Android opens it on a long press, iOS Safari never does), so offer the same actions somewhere visible too: a Menu button on the row, or keys like Delete, which the shortcut hint can name and keyshortcuts can announce. The hint binds nothing; handle the key yourself.