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.
import { ContextMenu, ContextMenuTrigger } from '@slotted/react/context-menu';import { MenuContent, MenuItem, MenuSeparator } from '@slotted/react/menu';import { Stack } from '@slotted/react/stack';import { Heading, Text } from '@slotted/react/typography';export default function ContextMenuBasic() { return ( <ContextMenu> <ContextMenuTrigger> <Stack gap={1}> <Heading level={2} size="md"> Q3 board deck.pdf </Heading> <Text size="sm" tone="muted"> 4.2 MB, edited yesterday. Right-click anywhere on this file. </Text> </Stack> </ContextMenuTrigger> <MenuContent> <MenuItem>Open</MenuItem> <MenuItem>Rename</MenuItem> <MenuItem>Duplicate</MenuItem> <MenuSeparator /> <MenuItem tone="danger">Move to trash</MenuItem> </MenuContent> </ContextMenu> );}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.
pnpm add @slotted/react @slotted/theme @slotted/themesimport { 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.
import { useState } from 'react';import { ContextMenu, ContextMenuTrigger } from '@slotted/react/context-menu';import { MenuContent, MenuItem, MenuSeparator } from '@slotted/react/menu';import type { MenuChangeRequest } from '@slotted/react/menu';import { Stack } from '@slotted/react/stack';import { Text } from '@slotted/react/typography';const FILES = [ { name: 'Supplier contract.docx', size: '180 KB' }, { name: 'Stock count March.xlsx', size: '640 KB' }, { name: 'Carrier rates 2026.pdf', size: '2.1 MB' },];export default function ContextMenuRows() { const [file, setFile] = useState<string | null>(null); function handleOpenChange(open: boolean, request: MenuChangeRequest) { if (open) setFile(fileOf(request.source)); } return ( <Stack gap={3}> <ContextMenu onOpenChange={handleOpenChange}> <ContextMenuTrigger> <Stack gap={3}> {FILES.map((entry) => ( <Text data-file={entry.name} key={entry.name}> {entry.name} · {entry.size} </Text> ))} </Stack> </ContextMenuTrigger> <MenuContent aria-label={file ?? 'File actions'}> <MenuItem>Open</MenuItem> <MenuItem>Download</MenuItem> <MenuSeparator /> <MenuItem tone="danger">Move to trash</MenuItem> </MenuContent> </ContextMenu> <Text size="sm" tone="muted"> {file === null ? 'Right-click a file.' : `The menu acts on ${file}.`} </Text> </Stack> );}function fileOf(source: unknown): string | null { const target = (source as Event | undefined)?.target; const row = target instanceof Element ? target.closest('[data-file]') : null; return row?.getAttribute('data-file') ?? null;}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.
import { ContextMenu, ContextMenuTrigger } from '@slotted/react/context-menu';import { MenuContent, MenuItem, MenuSeparator } from '@slotted/react/menu';import { Stack } from '@slotted/react/stack';import { Heading, Text } from '@slotted/react/typography';export default function ContextMenuKeyboard() { return ( <ContextMenu> <ContextMenuTrigger tabIndex={0}> <Stack gap={1}> <Heading level={2} size="md"> Release notes 4.2 </Heading> <Text size="sm" tone="muted"> Press Tab to focus this item, then Shift+F10 or the Menu key. </Text> </Stack> </ContextMenuTrigger> <MenuContent> <MenuItem>Open</MenuItem> <MenuItem>Copy link</MenuItem> <MenuSeparator /> <MenuItem>Archive</MenuItem> </MenuContent> </ContextMenu> );}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.
import { useState } from 'react';import { ContextMenu, ContextMenuTrigger } from '@slotted/react/context-menu';import { MenuCheckboxItem, MenuContent, MenuGroup, MenuItem, MenuSeparator,} from '@slotted/react/menu';import { Stack } from '@slotted/react/stack';import { Heading, Text } from '@slotted/react/typography';export default function ContextMenuCanvas() { const [labels, setLabels] = useState(true); const [paths, setPaths] = useState(false); return ( <ContextMenu> <ContextMenuTrigger> <Stack gap={1}> <Heading level={2} size="md"> Warehouse floor plan </Heading> <Text size="sm" tone="muted"> Aisles A1 to D4. {labels ? 'Aisle labels on.' : 'Aisle labels off.'}{' '} {paths ? 'Pick paths on.' : 'Pick paths off.'} </Text> </Stack> </ContextMenuTrigger> <MenuContent aria-label="Floor plan actions"> <MenuGroup label="View"> <MenuCheckboxItem checked={labels} onCheckedChange={setLabels}> Aisle labels </MenuCheckboxItem> <MenuCheckboxItem checked={paths} onCheckedChange={setPaths}> Pick paths </MenuCheckboxItem> </MenuGroup> <MenuSeparator /> <MenuItem>Export as PDF</MenuItem> </MenuContent> </ContextMenu> );}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.
import { ContextMenu, ContextMenuTrigger } from '@slotted/react/context-menu';import { MenuContent, MenuItem } from '@slotted/react/menu';import { Stack } from '@slotted/react/stack';import { Heading, Text } from '@slotted/react/typography';export default function ContextMenuTouch() { return ( <ContextMenu longPressDelay={800}> <ContextMenuTrigger> <Stack gap={1}> <Heading level={2} size="md"> Order SO-10482 </Heading> <Text size="sm" tone="muted"> Out for delivery. On touch, hold for 800 ms. </Text> </Stack> </ContextMenuTrigger> <MenuContent> <MenuItem>View order</MenuItem> <MenuItem>Track delivery</MenuItem> <MenuItem>Print packing slip</MenuItem> </MenuContent> </ContextMenu> );}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.
import { ContextMenu, ContextMenuTrigger } from '@slotted/react/context-menu';import { MenuContent, MenuItem } from '@slotted/react/menu';import { Stack } from '@slotted/react/stack';import { Heading, Text } from '@slotted/react/typography';export default function ContextMenuLongPressOff() { return ( <ContextMenu longPress={false}> <ContextMenuTrigger tabIndex={0}> <Stack gap={1}> <Heading level={2} size="md"> Invoice INV-3307 </Heading> <Text size="sm" tone="muted"> Touch and pen keep their own long-press behavior here. </Text> </Stack> </ContextMenuTrigger> <MenuContent> <MenuItem>Send reminder</MenuItem> <MenuItem>Record payment</MenuItem> <MenuItem>Download PDF</MenuItem> </MenuContent> </ContextMenu> );}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.
import { ContextMenu, ContextMenuDefaultsProvider, ContextMenuTrigger,} from '@slotted/react/context-menu';import { MenuContent, MenuItem } from '@slotted/react/menu';import { Stack } from '@slotted/react/stack';import { Heading, Text } from '@slotted/react/typography';export default function ContextMenuDefaults() { return ( <ContextMenuDefaultsProvider longPressDelay={800}> <Stack gap={4}> <ContextMenu> <ContextMenuTrigger> <Stack gap={1}> <Heading level={2} size="md"> Inbound dock </Heading> <Text size="sm" tone="muted"> Holds for 800 ms, the delay the provider sets. </Text> </Stack> </ContextMenuTrigger> <MenuContent> <MenuItem>Schedule unloading</MenuItem> <MenuItem>View arrivals</MenuItem> </MenuContent> </ContextMenu> <ContextMenu longPressDelay={300}> <ContextMenuTrigger> <Stack gap={1}> <Heading level={2} size="md"> Outbound dock </Heading> <Text size="sm" tone="muted"> Holds for 300 ms, the delay this target sets. </Text> </Stack> </ContextMenuTrigger> <MenuContent> <MenuItem>Schedule loading</MenuItem> <MenuItem>View departures</MenuItem> </MenuContent> </ContextMenu> </Stack> </ContextMenuDefaultsProvider> );}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.
import { Button } from '@slotted/react/button';import { ContextMenu, ContextMenuTrigger } from '@slotted/react/context-menu';import { Inline } from '@slotted/react/inline';import { Menu, MenuContent, MenuItem, MenuTrigger } from '@slotted/react/menu';import { Stack } from '@slotted/react/stack';import { Heading, Text } from '@slotted/react/typography';export default function ContextMenuOtherPath() { const actions = ( <MenuContent aria-label="Shipment actions"> <MenuItem>Track shipment</MenuItem> <MenuItem>Print label</MenuItem> <MenuItem>Contact carrier</MenuItem> </MenuContent> ); return ( <ContextMenu> <ContextMenuTrigger> <Stack gap={2}> <Heading level={2} size="md"> Shipment SH-20418 </Heading> <Text size="sm" tone="muted"> Right-click this shipment or press Actions: both open the same commands. </Text> <Inline> <Menu> <MenuTrigger render={(props) => ( <Button {...props} variant="outline"> Actions </Button> )} /> {actions} </Menu> </Inline> </Stack> </ContextMenuTrigger> {actions} </ContextMenu> );}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.
import { useState } from 'react';import { ContextMenu, ContextMenuTrigger } from '@slotted/react/context-menu';import { MenuContent, MenuItem } from '@slotted/react/menu';import type { MenuChangeRequest } from '@slotted/react/menu';import { Stack } from '@slotted/react/stack';import { Heading, Text } from '@slotted/react/typography';export default function ContextMenuControlled() { const [open, setOpen] = useState(false); const [last, setLast] = useState('Closed'); function handleOpenChange(next: boolean, request: MenuChangeRequest) { setOpen(next); setLast(`${next ? 'Opened' : 'Closed'} by ${request.reason}`); } return ( <Stack gap={3}> <ContextMenu onOpenChange={handleOpenChange} open={open}> <ContextMenuTrigger tabIndex={0}> <Stack gap={1}> <Heading level={2} size="md"> Purchase order PO-5521 </Heading> <Text size="sm" tone="muted"> Right-click, press Shift+F10, press Escape or click elsewhere. </Text> </Stack> </ContextMenuTrigger> <MenuContent> <MenuItem>Approve</MenuItem> <MenuItem>Request changes</MenuItem> <MenuItem>Send to supplier</MenuItem> </MenuContent> </ContextMenu> <Text size="sm" tone="muted"> {last} </Text> </Stack> );}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.
import { ContextMenu, ContextMenuTrigger } from '@slotted/react/context-menu';import { MenuContent, MenuItem } from '@slotted/react/menu';import { Stack } from '@slotted/react/stack';import { Heading, Text } from '@slotted/react/typography';export default function ContextMenuDisabled() { return ( <Stack gap={4}> <ContextMenu> <ContextMenuTrigger> <Stack gap={1}> <Heading level={2} size="md"> Draft report </Heading> <Text size="sm" tone="muted"> Enabled: right-click opens the commands. </Text> </Stack> </ContextMenuTrigger> <MenuContent> <MenuItem>Edit</MenuItem> <MenuItem>Publish</MenuItem> </MenuContent> </ContextMenu> <ContextMenu disabled> <ContextMenuTrigger> <Stack gap={1}> <Heading level={2} size="md"> Published report </Heading> <Text size="sm" tone="muted"> Disabled: right-click opens the browser menu. </Text> </Stack> </ContextMenuTrigger> <MenuContent> <MenuItem>Edit</MenuItem> <MenuItem>Unpublish</MenuItem> </MenuContent> </ContextMenu> </Stack> );}Accessibility
| Key | Effect | Focus |
|---|---|---|
| Shift+F10 | opens the menu anchored to the target | the first item |
| ContextMenu | opens the menu anchored to the target | the first item |
| Escape | closes the menu holding focus | the element that held focus when the menu opened, or the target |
| Tab | closes every surface and moves on | the 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.
longPressbooleanDefaulttrue- Whether holding the target on touch and pen opens the menu.
longPressDelaynumberDefault500- Milliseconds the press is held before the menu opens; movement past ten pixels or lifting first cancels it.
disabledbooleanDefaultfalse- 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
| Name | Payload | When |
|---|---|---|
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
| Part | Element | Required | Purpose |
|---|---|---|---|
root | div, or the element render returns | Yes | The 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
| Token | Default |
|---|---|
--slotted-context-menu-mark-color | var(--slotted-border-strong, CanvasText) |
--slotted-context-menu-mark-width | var(--slotted-border-width-thin, 1px) |
Theme tokens it reads
| Token | Default |
|---|---|
--slotted-border-strong | Set per scheme by the theme |
--slotted-border-width-thin | 1px |
--slotted-duration-short | 120ms |
--slotted-easing-standard | cubic-bezier(0.2, 0, 0, 1) |
--slotted-focus-ring-color | Set per scheme by the theme |
--slotted-focus-ring-offset | var(--slotted-border-width-thick) |
--slotted-focus-ring-width | var(--slotted-border-width-thick) |