forty-cdk
llms.txt

Primitives

Context Menu

A menu opened by right-click or long-press, anchored to the pointer position.

forty-cdk/context-menu WAI-ARIA APG

Right-click the region (or focus it and press Shift+F10) and the menu opens at the pointer with data-state="open"; the arrow keys then walk the items.

Right-click anywhere in this area (or focus it and press Shift+F10)

Opened via the contextmenu event (right-click, long-press on touch) and via the keyboard activators Shift+F10 and the dedicated ContextMenu key. The native browser context menu is suppressed. Pointer activations anchor the menu at the cursor; keyboard activations anchor it at the bounding rect of the focused element so screen-reader / keyboard-only users get the menu next to whatever they're working on. Floating-ui's virtual element handles either case: placement, flip, and shift middleware still apply, so the menu is repositioned to stay on-screen automatically.

New to overlays in forty-cdk? Your first overlay walks a Popover from empty markup to styled-and-animated and explains the @if / open-state model and the portal → global CSS rule.

When to choose

  • Context Menu: actions for a region, opened by right-click, long-press, Shift+F10 or the ContextMenu key and anchored at the pointer. It is a single-opener preset: one root, one region.
  • Dropdown Menu: the same menu surface opened from a visible <button>. Choose it when the actions need a control the user can see and reach with Tab.
  • Menu: the opener-agnostic root, for when one menu definition must be driven by several openers at once (a right-click region and a kebab button).
  • Menubar: a persistent bar of menus with arrow navigation between them.

Anatomy

<div forContextMenu #menu="forContextMenu">
  <div forContextMenuTrigger tabindex="0">Right-click anywhere here</div>
  <!-- @if (menu.open()) { -->
  <div forMenuContent>
    <button forMenuItem>Rename</button>
    <button forMenuItem>Duplicate</button>
    <button forMenuItem>Delete</button>
  </div>
  <!-- } -->
</div>

The menu items themselves come from the menu/ folder.

Examples

Rich content

The same menu vocabulary the Dropdown Menu exposes, anchored to the pointer on right-click: plain forMenuItem actions, a forMenuCheckboxItem toggle, a forMenuRadioGroup, a forMenuSub submenu, and grouped labels with separators. Checkbox and radio items call preventDefault() on (activate) to stay open; plain items close the menu and bubble up through any open submenu.

Right-click anywhere in this area (or focus it and press Shift+F10)

API

ForContextMenu

PropertyTypeDescription
open
model
Two-way bindable. Whether the menu is shown.
Default: false
side
input
Anchor side relative to the pointer. The default is read from provideForContextMenuDefaults for the surrounding scope.
Default: 'bottom'
align
input
Alignment along side ('start' / 'center' / 'end'). The default is read from provideForContextMenuDefaults for the surrounding scope.
Default: 'start'
sideOffset
input
Gap (px) between the pointer and the menu along the main axis.
Default: 0
alignOffset
input
Gap (px) along the cross axis (parallel to side).
Default: 0
fallbackAxisSideDirection
input
When both sides of the preferred axis overflow, lets flip drop the menu to a perpendicular side instead of clipping. 'none' keeps only the opposite same-axis placement. The default is read from provideForContextMenuDefaults for the surrounding scope. Set it once for the whole app rather than per call site.
Default: 'none'
avoidCollisions
input
When true, flip and shift keep the content inside the viewport. Set false for strict positioning where overflow is acceptable.
Default: true
collisionPadding
input
Padding (px) for the flip / shift / size collision middlewares. The default is read from provideForContextMenuDefaults for the surrounding scope.
Default: 8
sticky
input
'always' keeps the requested placement even when the content leaves the viewport. 'partial' and false let shift slide it back into view.
Default: 'partial'
hideWhenDetached
input
When true, the content carries data-detached while the anchor has scrolled out of every clipping ancestor, so CSS can hide it.
Default: false
clipUntilPositioned
input
Clips the content until its first position resolves, so it never flashes at the viewport corner. Set false so an animate.enter plays from its first frame.
Default: true
loop
input
Whether arrow navigation wraps.
Default: true
dir
input
Writing direction. null inherits the nearest [dir] / <html dir>, else 'ltr'. In RTL the swap is automatic: ArrowLeft opens submenus and ArrowRight closes them. Inherited by every nested [forMenuSub] underneath unless overridden.
Default: null
disabled
input
When true, the contextmenu event falls through to the native browser menu.
Default: false
dismissible
input
When false, Escape and outside interactions don't close.
Default: true
returnFocus
input
When true, focus returns on close to the trigger element, or to the focused element inside it that a keyboard activation started from, except after an outside or Tab close.
Default: true
ariaLabel
input
Accessible name reflected as aria-label on [forMenuContent]. The root's only name hook for a context menu: the right-click region is never used as an aria-labelledby target.
Default: null
escapeKeyDown
output
Output. Escape pressed while the menu is the topmost dismissible layer.
Default: —
pointerDownOutside
output
Output. Pointer-down on a target outside content + trigger.
Default: —
focusOutside
output
Output. Focus moves outside content + trigger.
Default: —
interactOutside
output
Output. A composite that fires alongside the two above (and shares their veto state).
Default: —
autoFocusOnOpen
output
Output. Just before the imperative focus move on mount.
Default: —
autoFocusOnClose
output
Output. Just before the imperative focus move on unmount.
Default: —

Same vetoable dismiss API as DropdownMenu. Call preventDefault() on the emitted veto to suppress the directive's default action; the original DOM event, when present, is on .event.

(autoFocusOnOpen) / (autoFocusOnClose) are output-shape because ContextMenu always routes close transitions through [(open)] (via the implicit openChange emitter). Dialog and Drawer take callback-shape inputs for the same pair instead: either can be closed by a direct open.set(false) that bypasses the (dismiss) output entirely, so their close hook has to be a stored function reference that still runs during teardown.

Data attributes

PieceAttributeValues
[forContextMenu]data-stateopen | closed
[forContextMenu]data-disabledpresent | absent
[forContextMenuTrigger]data-stateopen | closed
[forContextMenuTrigger]data-disabledpresent | absent

Scoped defaults

provideForContextMenuDefaults configures context-menu defaults for an injector subtree, either at the application root or in any component's providers array. Partial overrides inherit unspecified keys from the parent scope (or the library fallbacks at the root).

KeyLibrary fallbackMeaning
side'bottom'Anchor side, relative to the pointer, for context menus that don't set side themselves.
align'start'Alignment along side for context menus that don't set align themselves.
sideOffset0Main-axis gap (px) between the pointer and the menu for context menus that don't set sideOffset themselves.
collisionPadding8Collision-middleware padding (px) for context menus that don't set it themselves.
fallbackAxisSideDirection'none'Perpendicular side flip may drop to when both sides of the preferred axis overflow. 'none' keeps only the opposite same-axis placement. Only consulted while avoidCollisions is on.
looptrueWhether arrow navigation wraps past the first / last enabled item, for context menus that don't set loop themselves.

Per-instance inputs always win over the scope defaults. Submenus inside a context menu take their timings and placement from provideForMenuDefaults; see the Menu README.

import { type ApplicationConfig } from '@angular/core';
import { provideForContextMenuDefaults } from 'forty-cdk/defaults';

export const appConfig: ApplicationConfig = {
  providers: [provideForContextMenuDefaults({ sideOffset: 2, fallbackAxisSideDirection: 'end' })],
};

Accessibility

[forContextMenu] implements the WAI-ARIA Menu pattern. The trigger captures contextmenu, Shift+F10, and the ContextMenu key; the menu surface and item roles come from the shared menu/ primitives. Pointer activations anchor at the cursor; keyboard activations anchor at the bounding rect of the focused element so keyboard-only users get the menu next to whatever they're working on.

  • Name the menu with [ariaLabel]. Unlike [forDropdownMenu] / [forMenubar] / [forMenuSub], the content surface emits no aria-labelledby fallback here: the trigger is the whole right-click region, so pointing the menu's name at it would make screen readers announce the entire row / card text as the menu name. With no [ariaLabel] the role="menu" surface simply has no accessible name. A consumer-set static aria-labelledby on [forMenuContent] is still preserved, so pointing at your own visible heading also works. Submenus nested inside a context menu are unaffected: a [forMenuSubContent] is still labelled by its [forMenuSubTrigger].

Styling

forty-cdk ships no styles: put your own class on each piece and key your CSS off the data-* attributes listed under Data attributes, not off the for* selectors (Styling forty-cdk explains why).

The menu content ([forMenuContent], from the menu/ folder) portals to document.body, so it sits outside the trigger's DOM subtree and descendant selectors won't reach it. Style it with global CSS or a class on the content element. The content host also exposes the shared positioner custom properties (--for-floating-anchor-width / --for-floating-anchor-height, --for-floating-available-width / --for-floating-available-height, --for-floating-content-transform-origin); see Styling floating content for the full list and the animation rules.

.context-menu-trigger[data-state='open'] {
  outline: 2px solid hotpink;
}

Behavior notes

  • Trigger is NOT exempt from outside-pointer / outside-focus checks. Unlike DropdownMenu (where the trigger button toggles via its own click handler), the context-menu region is treated like any other "outside" element: a left-click on the region while the menu is open closes it. Right-clicking again reopens the menu at the new position. When the close and the reopen land in the same task, as they do where the browser fires contextmenu on mouse down (macOS, Linux), the mounted surface is reused rather than rebuilt: it moves to the new point without replaying animate.enter / animate.leave, and focus moves back to its first item, honouring (autoFocusOnOpen). Where contextmenu follows mouse up (Windows), the surface renders closed in between and is mounted again, so its animations replay. Keep enter/leave animations cheap, or gate expensive ones, since a user can fire this rapidly.
  • Nested regions. A [forContextMenuTrigger] inside another one takes precedence: a right-click, Shift+F10, the ContextMenu key or a long-press on the inner region opens the inner menu only, and the outer menu opens from the parts of its region the inner one does not cover. A disabled inner region lets the outer menu open. The trigger also ignores a contextmenu or key press that a handler beneath it already cancelled with preventDefault(), so calling it on a descendant is how you keep a context menu from opening there.
  • Virtual anchor. Right-click captures a 0×0 rect at the pointer location. Shift+F10 and ContextMenu snapshot the bounding rect of the focused element (or the trigger if focus is on it directly), so the menu floats off the element under attention. Closing a keyboard-opened menu returns focus to that same element, so a row in a roving-tabindex list keeps its position; when a menu action removed it, focus falls back to the region. Both forms feed floating-ui's flip and shift middleware, so corners and screen edges work without special-casing.
  • Keyboard activators only fire while focus is inside the trigger. Keyboard events dispatch to the focused element, so Shift+F10 / ContextMenu anywhere outside the trigger goes to the browser default. The trigger is focusable by default (host-bound tabindex="-1"), so this works out of the box and focus can return to it programmatically when the menu closes. Your own tabindex wins over that default: use tabindex="0" if you want the region itself reachable via Tab.
  • Native menu suppressed. The trigger calls event.preventDefault() on contextmenu and on the keyboard activators. After a keyboard open it also cancels the contextmenu the platform raises when the key is released (Windows), wherever focus has moved by then, so the browser's menu is not painted over the one that just opened. Set disabled to let the browser's native menu surface for that region.
  • An open that renders nothing closes again. When a right-click, Shift+F10, the ContextMenu key or a long-press opens a menu whose template renders no [forMenuContent], the trigger closes it after the next render with reason 'programmatic', and dev mode logs FORCDK-CONTEXT-MENU-002 once. The native menu cannot come back for that gesture, because it was already suppressed, so bind [disabled] to the same condition that renders the content: [disabled]="!hasActions(col)" beside @if (hasActions(col)).
  • Touch long-press. The trigger runs its own long-press timer (a touch pointerdown held ~500 ms, without lifting or moving past a small tolerance, opens the menu at the touch point). This is required because iOS Safari never fires the contextmenu event a long-press synthesizes elsewhere; where the browser does synthesize it (Android, desktop touch emulation) the two paths stay mutually exclusive, so the menu opens exactly once. For the press to survive on iOS, suppress the native callout / text-selection on the trigger with CSS, since otherwise the OS gesture cancels the press:
.context-menu-trigger {
  -webkit-touch-callout: none;
  user-select: none;
}
  • Mount equals open. As in the rest of the library, wrap [forMenuContent] in @if (open()) and use animate.enter / animate.leave for transitions.

The minimal "right-click → show menu" case needs neither a separate open signal nor a two-way binding. [forContextMenu] is exportAs: 'forContextMenu', so expose the directive instance with a template reference variable (#menu="forContextMenu") and drive the @if straight off its own open() signal, as above. The contextmenu gesture, item activation, Escape, and outside dismissal all flip it.

Reach for the explicit [(open)]="mySignal" model binding only when the component class needs to read or drive open state (to open it programmatically, persist it, or react to it elsewhere):

<div forContextMenu [(open)]="open">
  <div forContextMenuTrigger class="context-menu-trigger">Right-click anywhere here.</div>
  @if (open()) {
  <div forMenuContent>…</div>
  }
</div>

Triggers stamped from outside-declared templates

Angular resolves ng-template DI at the template's declaration site, not where it is stamped. A [forContextMenuTrigger] declared in a template outside the root throws the orphan error even when the template is rendered inside the root via ngTemplateOutlet. For that case the selector attribute accepts the root reference as a value, routerLink-style. Grab it with #root="forContextMenu" and pass it through the outlet context. The bare valueless attribute keeps resolving via DI.

<div forContextMenu #root="forContextMenu">
  <ng-container *ngTemplateOutlet="chip; context: { root }" />
  @if (root.open()) {
  <div forMenuContent>…</div>
  }
</div>

<ng-template #chip let-root="root">
  <span [forContextMenuTrigger]="root">Right-click here</span>
</ng-template>

Sharing one menu with a second opener

[forContextMenu] is a single-opener preset: one root, one right-click region. When the same actions must also be reachable another way (the canonical case being a table row with a right-click region and a kebab button), bind the trigger to a [forMenu] root instead, which drives one [forMenuContent] block from any number of openers. See Shared openers.

The same explicit-reference input carries it, and here the binding is required rather than optional: the trigger resolves FOR_CONTEXT_MENU_CONTEXT, which [forMenu] deliberately does not provide (forty-cdk/menu must not depend on forty-cdk/context-menu).

<tr forMenu #row="forMenu" [(open)]="open" ariaLabel="Row actions">
  <td [forContextMenuTrigger]="row">…cells…</td>
  <td>
    <button [forDropdownMenuTrigger]="row" [menuPositioning]="{ sideOffset: 4 }">⋮</button>
  </td>
  <!-- one content block, no duplication -->
</tr>

Both triggers carry [menuPositioning], a partial { side, align, sideOffset, alignOffset } override applied only to the opens that trigger drives, with each omitted key falling back to the root's input. It exists because a shared root cannot pick offsets that suit heterogeneous openers: the region above keeps the root's sideOffset of 0 (flush at the cursor, which is what a pointer-anchored menu wants), while the sibling button opener asks for the 4px of clearance a menu button wants. Under a [forContextMenu] root it resolves the same way, where it is simply a per-trigger spelling of the root's inputs. See Per-opener positioning.

Wrapping in a design system

Subclass the root and re-provide both of its tokens, FOR_MENU_CONTEXT and FOR_CONTEXT_MENU_CONTEXT, with useExisting pointing at the subclass, since Angular does not inherit a directive's providers. Leaving out the second one throws FORCDK-CONTEXT-MENU-001 from the trigger. Wrapping non-form roots walks the pattern.