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 floatingrole="menu")GhostContextMenuItem(role="menuitem")GhostContextMenuGroup(logical grouping container)GhostContextMenuLabel(non-focusable section label)GhostContextMenuSeparator(horizontal rule between sections)GhostContextMenuCheckboxItem(role="menuitemcheckbox")GhostContextMenuRadioGroup/GhostContextMenuRadioItem(menuitemradioset)useGhostContextMenu(composable for open state, IDs, and pointer anchor)- Shared menu utilities remain
useGhostMenuCollection,getMenuItems,focusFirstMenuItem,focusLastMenuItemfrom the existing module.
Themed exports from @ghostvue/themes: all parts above plus ghost-context-menu.css via default.css.
Props and configuration
GhostContextMenu (root)
| Prop | Type | Default | Contract |
|---|---|---|---|
open | boolean | undefined | undefined | Controlled open state. Omit for uncontrolled mode. |
defaultOpen | boolean | false | Initial open state in uncontrolled mode. |
GhostContextMenuTrigger
| Prop | Type | Default | Contract |
|---|---|---|---|
as | GhostContextMenuTriggerElement | 'span' | Host element wrapping the context target. |
disabled | boolean | false | Suppresses open gestures and keyboard shortcuts. |
longPressDelay | number | 500 | Milliseconds 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), ormodelValue+update:modelValue(v-model).checkedwins 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
autoUpdateon scroll/resize only). data-stateon trigger and content matches dropdown conventions.- First-paint guard:
visibility: hiddenuntiluseFloatingresolvesisPositioned.
Accessibility contract
WAI-ARIA APG Menu pattern (context menu variant):
Semantics
- Trigger:
aria-haspopup="menu",aria-expanded,aria-controlswhile applicable,data-part="trigger". - Content:
role="menu",tabindex="-1",data-part="content". - Items: same roles and
data-parthooks asGhostDropdownMenu.
Keyboard interaction
| Key | Where | Behavior |
|---|---|---|
ContextMenu | Focused trigger | Open at trigger focus rect center or last pointer. |
Shift+F10 | Focused trigger | Open at trigger focus rect center or last pointer. |
Escape | Open menu | Close; restore focus to trigger. |
| Arrow/Home/End/typeable | Open menu | Same as useGhostMenuCollection (shared with dropdown). |
Tab | Open menu | Close without restoring focus to trigger. |
Focus behavior
- On open after positioned:
focusFirstMenuItemon 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". Consumerclassandstylemerge 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
useGhostContextMenufor open state, IDs, and virtual reference coordinates.useFloatingwith a virtualReferenceElement.useDismissableLayer,useFocusScope,GhostPortal(same overlay stack as dropdown).useGhostMenuCollectionunchanged 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;contextmenudefault 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/basicandcontext-menu/themedvia Docker baselines.
Documentation plan
docs/components/context-menu.mdwith headless and themed examples (including DataTable-style row target).- Executable examples under
examples/context-menu/.
Compatibility and release impact
- Additive minor Changeset for
@ghostvue/vueand@ghostvue/themes. - Additive changes to
use-ghost-menu-collection.tsonly 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
| Prop | Type | Default | Contract |
|---|---|---|---|
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 consumertabindexstill passes through) and noaria-haspopup,aria-controls, oraria-expanded. It keepsdata-part="trigger"anddata-state. - Before-open handshake. A composite inside the wrapper (such as
GhostListbox) registers a resolver with the nearestdescendant-mode trigger through an internal injection. On every open path, after the existingdisabledanddefaultPreventedchecks and immediately before opening, the trigger calls the resolver with the source (pointer,long-press, orkeyboard) and the originating element: thecontextmenutarget, the element under the recordedpointerdownfor long-press, ordocument.activeElementforShift+F10/ContextMenu. The resolver may change focus and state (the listbox applies its selection rule here) and returns the restore target element, ornullto 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.activeElementis an element inside the wrapper other than the wrapper itself and it is the event's target or contains it (for long-press, the recordedpointerdowntarget). 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
clickthat 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, andTabwith 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 withtabindex="0"after that update, else leaves focus where the browser puts it.Tabfrom the open menu still closes without restoring focus, as today. disabledworks 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.