Skip to content

Contextual Menu

A menu summoned by right-click (or the keyboard menu key) on a region, opening a role="menu" of action items at the pointer. Reuses the same headless menu primitive as DropdownMenu.

<script>
import ContextMenu from "@invisible-ui/svelte/ContextMenu.svelte";
</script>

In Elements, the children are the region. Assign the actions through the items property and listen for select, which fires after the menu closes:

<ds-context-menu label="Page actions">
<div>Right-click here</div>
</ds-context-menu>
<script type="module">
import "@invisible-ui/elements/define";
const menu = document.querySelector("ds-context-menu");
menu.items = [
{ value: "back", label: "Back" },
{ value: "reload", label: "Reload" },
];
menu.addEventListener("select", (event) => run(event.detail.value));
</script>

In React, the children are the region. items takes the same entries as Dropdown Menu, submenus included:

<ContextMenu
label="Page actions"
items={[
{ value: "reload", label: "Reload" },
{ type: "submenu", value: "share", label: "Share", items: [{ value: "email", label: "Email" }] },
]}
onSelect={(value) => run(value)}
>
<div>Right-click here</div>
</ContextMenu>
PropTypeDefaultDescription
items *MenuItem[]—
disabledbooleanfalse
onSelect((value: string) => void) | undefinedundefinedCalled with the chosen item's value.
labelstring | undefinedundefinedAccessible name for the popup (there is no labelling trigger).

Slot: the default slot is the region the menu is summoned from.

  • The popup is role="menu" (named via aria-label) with role="menuitem" actions; disabled items carry aria-disabled.
  • Opens at the pointer on right-click, or at the focused region via the keyboard context-menu key. Arrow / Home / End move, typeahead jumps, Enter/click activates, Escape / Tab / outside-pointer close. DOM focus moves into the menu and is restored to the previously focused element on close.
  • Positioning (flip/shift against a virtual anchor at the pointer) via @floating-ui/dom. The menu opens toward the inline end of the pointer, and from the keyboard at the region’s inline-start top corner: in right-to-left text it opens to the left, from the top right corner.
  • Under a coarse pointer every item keeps a hit area of at least 44 by 44 (--ds-metrics-density-touch-min-target-size).
<ContextMenu
items={[
{ value: "reload", label: "Reload" },
{ value: "inspect", label: "Inspect" },
]}
onSelect={(v) => run(v)}
>
<div>Right-click this area</div>
</ContextMenu>

Themeable via the shared --ds-menu-* custom properties.