Skip to content
Slotted

Context menu

Opens a menu at the pointer on a secondary click, a long press or Shift+F10, over a region the application already draws.

Q3 board deck.pdf

4.2 MB, edited yesterday. Right-click anywhere on this file.

Installation

The packages are a 0.x beta on npm, and their public API changes between beta releases without deprecation. One line installs the binding and the default theme.

bash
pnpm add @slotted/react @slotted/theme @slotted/themes
tsx
import { ContextMenu, ContextMenuTrigger, ContextMenuDefaultsProvider,  } from '@slotted/react/context-menu';import { MenuContent, MenuItem,  MenuSeparator } from '@slotted/react/menu';

Target and menu

The target is the region the menu opens on: a file, a row, a canvas. In React ContextMenuTrigger renders a div around the content, and render takes another element. In Angular slContextMenuTriggerFor is written on the consumer’s own element and receives the menu as a template reference. The target is a component, and Angular puts one component on an element, so a target that is another family’s component, such as slCard, is wrapped in an element of its own. The menu is the menu family unchanged: MenuContent and MenuItem in React, sl-menu and slMenuItem in Angular. A secondary click opens the menu with its corner at the pointer. While the menu shows, another secondary click moves it to the new point. A primary press on the target closes it.

One menu for many rows

One context menu serves a whole list. onOpenChange in React and openChangeRequest in Angular carry the originating event in request.source, and the handler finds the row with event.target.closest(). The menu then names the file it acts on. Mounting one menu per row is the shape this composition avoids.

Supplier contract.docx · 180 KBStock count March.xlsx · 640 KBCarrier rates 2026.pdf · 2.1 MB
Right-click a file.

From the keyboard

Shift+F10 and the Menu key open the menu with focus on the target. The menu hangs from the target, not from the pointer, and focus lands on the first item. Escape closes it and returns focus to the element that held it when the menu opened. The context menu writes no tabindex: a target the page made focusable opens from the keyboard, and one it did not make focusable does not. Set tabIndex in React or tabindex in Angular on a target that has no other way to take focus.

Release notes 4.2

Press Tab to focus this item, then Shift+F10 or the Menu key.

Naming the menu

The menu is named after the target’s text through aria-labelledby, which fits a row or a file. For a canvas or a document, the target’s text does not name the commands: give MenuContent or sl-menu an aria-label. The menu content is the menu family’s, so groups, checkbox items, separators and submenus work as they do in a menu opened from a button.

Warehouse floor plan

Aisles A1 to D4. Aisle labels on. Pick paths off.

Long press on touch

A touch screen has no secondary button. On touch and pen, holding the target opens the menu at the finger. longPressDelay sets the hold in milliseconds and defaults to 500. Lifting the finger or moving it more than 10 pixels before the delay cancels the press, so a scroll that starts on the target stays a scroll. While the press runs the target carries data-pressed, and text selection and the platform callout are off. longPressDelay is a prop of ContextMenu in React and an input of the target in Angular.

Order SO-10482

Out for delivery. On touch, hold for 800 ms.

Turning the long press off

longPress is true by default. Set it to false where touch and pen should keep the platform’s own behavior for a held press. The secondary click and the keyboard keys still open the menu. longPress is a prop of ContextMenu in React and an input of the target in Angular.

Invoice INV-3307

Touch and pen keep their own long-press behavior here.

Defaults for a subtree

ContextMenuDefaultsProvider in React and provideContextMenuDefaults in Angular set longPress and longPressDelay once for everything beneath them. React’s provider also sets disabled. A context menu that states a value of its own keeps it. Here both docks sit under a provider that sets 800 ms, and the second one states 300 ms.

Inbound dock

Holds for 800 ms, the delay the provider sets.

Outbound dock

Holds for 300 ms, the delay this target sets.

Another path to every command

A context menu is found by convention, and no aria-haspopup marks the target, so a reader who never right-clicks must reach every command another way. Here the same commands sit behind an Actions button inside the target, as a menu opened the ordinary way. In React one MenuContent element renders twice. In Angular a menu has one trigger element, so the target and the button each open their own sl-menu, and each repeats the commands.

Shipment SH-20418

Right-click this shipment or press Actions: both open the same commands.

Controlled

Pass open to own the state. onOpenChange in React and openChangeRequest in Angular carry the reason: pointer for a secondary click or a long press, keyboard for Shift+F10 or the Menu key, and escape, outsidePress, select, dismiss or programmatic for the ways out. Moving the menu to a new point while it is open emits nothing, because open did not change. In Angular the state is the [(open)] of sl-menu.

Purchase order PO-5521

Right-click, press Shift+F10, press Escape or click elsewhere.
Closed

Disabled

A disabled context menu never opens and calls nothing on the event. The browser’s own menu opens on the secondary click, so a reader is not left without a menu. The target carries data-disabled. disabled is a prop of ContextMenu in React and an input of sl-menu in Angular.

Draft report

Enabled: right-click opens the commands.

Published report

Disabled: right-click opens the browser menu.

Accessibility

KeyEffectFocus
Shift+F10opens the menu anchored to the targetthe first item
ContextMenuopens the menu anchored to the targetthe first item
Escapecloses the menu holding focusthe element that held focus when the menu opened, or the target
Tabcloses every surface and moves onthe next element in the page after the target
  • The platform supplies the gesture: the contextmenu event is the secondary click, the Menu key and Shift+F10 on every desktop, and the target answers all three with preventDefault while the context menu is enabled and with nothing while it is disabled, so a reader is never left with no menu at all.
  • The target writes no aria-haspopup — it is not permitted on a generic element — so a screen reader is not told the region has a menu. For that reason every command in a context menu also exists by another path, and the other-path example shows one.
  • Focus goes to the first item on open and, on Escape or after a command, back to whatever held it when the menu opened — the checkbox in the row, the tile itself — rather than to a trigger that never had it. A press elsewhere leaves focus where the reader put it.
  • The target writes no tabindex. A target the page made focusable opens from the keyboard; one it did not make focusable does not. A tab stop on every wrapped region would break a grid with roving focus.

API

ContextMenu

Props

openboolean | undefined
Whether the menu shows. Present at all puts the menu in controlled mode: it asks and waits.
defaultOpenboolean
The value an uncontrolled menu starts from, read once. A menu open from the start hangs from the target.
onOpenChange(open, request) => void
Asked before the menu opens or closes, with the reason and the originating event, and refusable.
longPressbooleanDefault true
Whether holding the target on touch and pen opens the menu.
longPressDelaynumberDefault 500
Milliseconds the press is held before the menu opens; movement past ten pixels or lifting first cancels it.
disabledbooleanDefault false
The context menu never opens and the browser's own menu passes through. The target carries data-disabled.
size, side, align, closeOnSelect, loop, typeahead, …MenuProps
Every prop of Menu except anchor and finalFocus, which ContextMenu decides, reaches the menu it renders.
render(props) => ReactNode
On ContextMenuTriggerRenders the consumer's own element as the target — a tr, a li — with the identity, the state and the listeners on it.

Events

NamePayloadWhen
onOpenChange(open: boolean, request: MenuChangeRequest)Before the menu opens or closes. The request carries pointer (a secondary click or a long press), keyboard (Shift+F10, the Menu key), escape, outsidePress (a press anywhere else, the target included), select, dismiss or programmatic, the originating event, and cancel(). Moving to a new point while open emits nothing, because open did not change.

Composition

ContextMenuDefaultsProvidercomponent
On ApplicationSets longPress, longPressDelay and disabled once for a subtree. A menu that states one itself still wins. The surface reads MenuDefaultsProvider.
MenuContent, MenuItem, …components
On Inside a ContextMenuThe surface is the menu family unchanged: submenus, checkbox and radio items, groups, shortcuts, typeahead and tone reach the context menu without a second API. Nothing is re-exported.
refprop
A MenuHandle carrying open(), close() and focus(). open() hangs the menu from the target.
aria-labelattribute
On MenuContentThe surface is named after the target's text unless given an aria-label. The text fits a row and does not fit a canvas.
native propsattributes
On ContextMenuTriggerAnything a div takes reaches the target, and a consumer's own onContextMenu or onPointerDown runs before the family's; preventDefault from it stops the family.

Parts

PartElementRequiredPurpose
rootdiv, or the element render returnsYesThe target: the region the menu opens on. It carries data-ui="context-menu", data-open while the menu shows, data-pressed during a long press and data-disabled while the browser's menu is the one that opens. The menu is the menu family unchanged — data-ui="menu".

Theming

Each token is a CSS custom property: set it in a theme, or on any element to retune every context menu inside it.

Context menu tokens

TokenDefault
--slotted-context-menu-mark-colorvar(--slotted-border-strong, CanvasText)
--slotted-context-menu-mark-widthvar(--slotted-border-width-thin, 1px)

Theme tokens it reads

TokenDefault
--slotted-border-strongSet per scheme by the theme
--slotted-border-width-thin1px
--slotted-duration-short120ms
--slotted-easing-standardcubic-bezier(0.2, 0, 0, 1)
--slotted-focus-ring-colorSet per scheme by the theme
--slotted-focus-ring-offsetvar(--slotted-border-width-thick)
--slotted-focus-ring-widthvar(--slotted-border-width-thick)