Skip to content

GhostContextMenu specification ​

Status: Approved pending review.

The approved nested-menu contract covers child menus opened from this host.

Purpose and user problems ​

GhostContextMenu exposes a context menu (right-click, long-press, and keyboard context-menu shortcuts) anchored at the pointer. It reuses the same item model, useGhostMenuCollection keyboard behavior, overlay dismissal, and focus restoration patterns as GhostDropdownMenu, but positions the surface with useFloating against a virtual reference at the event coordinates instead of a menu-button anchor.

Primary use cases include DataTable row actions and any surface where a right-click or long-press should reveal actions without a visible menu button.

Non-goals ​

  • Menu button toggle semantics (GhostDropdownMenu).
  • Modal dialog, popover, tooltip, or native <select> behavior.
  • Replacing the browser default context menu when the component is closed or when event.preventDefault() is called by the consumer before open.
  • Decorative icons, swipe navigation, or custom animation contracts beyond data-state.

Public API ​

Parts and exports ​

Headless exports from @ghostvue/vue:

  • GhostContextMenu (root state and context provider; renderless)
  • GhostContextMenuTrigger (context target; not a menu button)
  • GhostContextMenuContent (positioned, dismissable floating role="menu")
  • GhostContextMenuItem (role="menuitem")
  • GhostContextMenuGroup (logical grouping container)
  • GhostContextMenuLabel (non-focusable section label)
  • GhostContextMenuSeparator (horizontal rule between sections)
  • GhostContextMenuCheckboxItem (role="menuitemcheckbox")
  • GhostContextMenuRadioGroup / GhostContextMenuRadioItem (menuitemradio set)
  • useGhostContextMenu (composable for open state, IDs, and pointer anchor)
  • Shared menu utilities remain useGhostMenuCollection, getMenuItems, focusFirstMenuItem, focusLastMenuItem from the existing module.

Themed exports from @ghostvue/themes: all parts above plus ghost-context-menu.css via default.css.

Props and configuration ​

GhostContextMenu (root) ​

PropTypeDefaultContract
openboolean | undefinedundefinedControlled open state. Omit for uncontrolled mode.
defaultOpenbooleanfalseInitial open state in uncontrolled mode.

GhostContextMenuTrigger ​

PropTypeDefaultContract
asGhostContextMenuTriggerElement'span'Host element wrapping the context target.
disabledbooleanfalseSuppresses open gestures and keyboard shortcuts.
longPressDelaynumber500Milliseconds before a long-press opens the menu (touch/pen only).

Opens on contextmenu (right-click), long-press (pointer hold without scroll), Shift+F10, and the ContextMenu key when focused. Does not open on primary click or tap. Does not call preventDefault() on contextmenu unless the menu actually opens (after disabled checks and consumer onContextmenu without defaultPrevented).

GhostContextMenuContent ​

Same floating and portal props as GhostDropdownMenuContent (placement, offset, flip, shift, strategy, portal, portalTo). Default placement is bottom-start relative to the virtual pointer anchor.

Items, checkbox, radio, label, separator, group ​

Behavior matches GhostDropdownMenu counterparts (roles, v-model contracts, select emits, disabled skip, checkbox stays open, radio closes unless preventDefault()).

v-model contracts ​

  • GhostContextMenu: open + update:open (v-model:open).
  • GhostContextMenuCheckboxItem: checked + update:checked (v-model:checked), or modelValue + update:modelValue (v-model). checked wins when both are set.
  • GhostContextMenuRadioGroup: modelValue + update:modelValue.

Events, slots, and exposed methods ​

  • Root default slot: { open: boolean, close: () => void }.
  • Parts expose { element, focus, blur } where focusable.

State model ​

  • Binary open/closed on the root; content mounts with v-if="open".
  • Pointer coordinates captured when open is requested; frozen for that open cycle (reposition via autoUpdate on scroll/resize only).
  • data-state on trigger and content matches dropdown conventions.
  • First-paint guard: visibility: hidden until useFloating resolves isPositioned.

Accessibility contract ​

WAI-ARIA APG Menu pattern (context menu variant):

Semantics ​

  • Trigger: aria-haspopup="menu", aria-expanded, aria-controls while applicable, data-part="trigger".
  • Content: role="menu", tabindex="-1", data-part="content".
  • Items: same roles and data-part hooks as GhostDropdownMenu.

Keyboard interaction ​

KeyWhereBehavior
ContextMenuFocused triggerOpen at trigger focus rect center or last pointer.
Shift+F10Focused triggerOpen at trigger focus rect center or last pointer.
EscapeOpen menuClose; restore focus to trigger.
Arrow/Home/End/typeableOpen menuSame as useGhostMenuCollection (shared with dropdown).
TabOpen menuClose without restoring focus to trigger.

Focus behavior ​

  • On open after positioned: focusFirstMenuItem on content (no “focus last” on context open).
  • On close (Escape, outside pointer without focusable target, item/radio select): restore focus to the trigger element via useFocusScope.

Rendering and customization ​

  • Root: renderless provider.
  • Content: class ghost-context-menu-content, data-part="content". Consumer class and style merge with the library class and floating position styles.
  • Themed CSS mirrors dropdown menu tokens on .ghost-context-menu-content.

Environment behavior ​

SSR and hydration ​

Closed menus render trigger only (v-if content). useGhostId supplies SSR-safe IDs. Integration: tests/integration/ssr-hydration.test.ts closed fixture.

Mobile and touch ​

Long-press opens only when the pointer stays within a small movement threshold and the delay elapses; pointercancel, pointerup, and scroll-like movement cancel the timer. Native contextmenu may still fire on long-press on some platforms; opening suppresses default only when this menu opens.

Composition ​

  • useGhostContextMenu for open state, IDs, and virtual reference coordinates.
  • useFloating with a virtual ReferenceElement.
  • useDismissableLayer, useFocusScope, GhostPortal (same overlay stack as dropdown).
  • useGhostMenuCollection unchanged for item keyboard behavior.

Test plan ​

  • Unit (packages/vue/src/GhostContextMenu.test.ts): controlled/uncontrolled open; right-click opens at coordinates; long-press timing and cancel on move/up; no open on quick tap; contextmenu default not prevented when disabled; keyboard open; menu collection behavior; focus restore; native context menu not prevented when menu does not open.
  • SSR: closed menu hydration fixture.
  • Theme CSS: token-only stylesheet integration test.
  • E2E: axe, open via contextmenu, item activation, Escape, disabled trigger.
  • Visual: context-menu/basic and context-menu/themed via Docker baselines.

Documentation plan ​

  • docs/components/context-menu.md with headless and themed examples (including DataTable-style row target).
  • Executable examples under examples/context-menu/.

Compatibility and release impact ​

  • Additive minor Changeset for @ghostvue/vue and @ghostvue/themes.
  • Additive changes to use-ghost-menu-collection.ts only if required (must not alter dropdown behavior).

Open architectural questions ​

The shared submenu specification resolves layering, focus, and dismissal.

Composite trigger amendment ​

Important

Approved on 2026-12-01 by zprue743 with the GhostListbox specification. This amendment adds one prop to GhostContextMenuTrigger so a context menu can serve a composite widget whose items hold focus, such as a listbox, tree, or grid. The default keeps the behavior above unchanged.

Problem ​

Today the trigger is itself the focus target. It renders tabindex="0" and aria-haspopup="menu", moves focus to itself when a pointer opens the menu, anchors keyboard opening to its own rect, and restores focus to itself on close. Wrapped around a listbox, that adds an extra Tab stop, puts aria-haspopup on an element that does not support it, takes focus away from the option the user right-clicked, and returns focus to the wrapper instead of the row.

Proposed public API ​

PropTypeDefaultContract
focusTarget'self' | 'descendant''self''self' is the behavior specified above. 'descendant' makes the trigger a passive wrapper.

With focusTarget="descendant":

  • Markup. The wrapper renders no default tabindex (a consumer tabindex still passes through) and no aria-haspopup, aria-controls, or aria-expanded. It keeps data-part="trigger" and data-state.
  • Before-open handshake. A composite inside the wrapper (such as GhostListbox) registers a resolver with the nearest descendant-mode trigger through an internal injection. On every open path, after the existing disabled and defaultPrevented checks and immediately before opening, the trigger calls the resolver with the source (pointer, long-press, or keyboard) and the originating element: the contextmenu target, the element under the recorded pointerdown for long-press, or document.activeElement for Shift+F10 / ContextMenu. The resolver may change focus and state (the listbox applies its selection rule here) and returns the restore target element, or null to veto. On a veto the menu does not open and the event is not default-prevented. The resolver and injection are not public API.
  • Without a registered composite, a gesture opens only when, after the event has reached the wrapper, document.activeElement is an element inside the wrapper other than the wrapper itself and it is the event's target or contains it (for long-press, the recorded pointerdown target). That element is the restore target. Otherwise the menu does not open and the browser default is not suppressed.
  • Long-press report. When a long-press opens the menu, the trigger tells the registered composite, so it can ignore the click that may follow when the finger lifts.
  • Pointer opening (contextmenu, long-press) anchors at the pointer, as today, and does not move focus to the wrapper.
  • Keyboard opening (Shift+F10, ContextMenu) anchors at the center of the restore target's rect instead of the wrapper's.
  • While open. The wrapper is not exempt from outside-pointer dismissal in this mode. A pointer down anywhere inside the wrapper closes the menu under the base outside-pointer rule: no restore when the pointer target is or is inside a focusable element, otherwise restore to the restore target. The gesture then proceeds normally (a primary click selects the row it lands on). A secondary click reopens the menu through the handshake with a new restore target.
  • Close. Every place this spec says "restore focus to the trigger" reads "restore focus to the restore target" in this mode, including Escape, item selection, and Tab with submenus. If the restore target is still connected and focusable, focus returns to it. If it was removed (for example by a Delete item), the trigger waits for the DOM update (nextTick) and then asks the registered composite to focus its current tab stop. Without a registered composite it focuses the first element inside the wrapper with tabindex="0" after that update, else leaves focus where the browser puts it. Tab from the open menu still closes without restoring focus, as today.
  • disabled works as today, and the handshake is not called.

Compatibility ​

Additive. Every existing usage keeps focusTarget="self", and the existing unit and e2e tests must pass unchanged. The listbox build adds tests for the descendant mode: no tab stop and no aria-haspopup on the wrapper; the handshake runs on right-click, long-press, and keyboard opening and not when the trigger is disabled or the event is default-prevented; a veto opens nothing and leaves the browser default; the long-press report; opening on a focused descendant and not otherwise without a composite; keyboard anchoring; a pointer down inside the wrapper closes the open menu; focus restore; and the deferred restore through the composite when the restore target was removed. Changeset: covered by the GhostListbox minor Changeset for @ghostvue/vue.