ContextMenuProvider
HkContextMenuProvider is the mount-once host of the application's context menu. Wrap the app in it and every right-click (or long-press) that nobody claimed becomes a menu at the pointer; the provider renders the app subtree plus exactly one menu surface, and never anything else.
- Reach for it once, near the app root. Per-row menus are not its job: an element that should have its own menu binds it locally (Vue's useContextMenuTrigger()), and the provider stays the single surface those requests all land on.
- It is a host, not a menu: the rows, the levels, the cascading submenus and the mobile bottom sheet belong to the menu it hosts. On the Vue side that menu is HkMenu (popup variant); on the Rust side the menu grammar is the host's content slot, because HkMenu has no Rust mirror. The provider's own contribution is three things — the state, the pointer geometry, and the two close paths.
- Compare with Popover (a panel that hangs off a trigger) and SelectPanel (a select field's dropdown): this surface has no trigger of its own — it opens where the pointer was, so everything outside it is "outside".
Import
;
use *; // ContextMenuProvider, ContextMenuViewport, ContextMenuPlacement,
// context_menu_placement, context_menu_position_style,
// context_menu_surface_shown, CONTEXT_MENU_HOST_CLASS,
// CONTEXT_MENU_PANEL_CLASS, CONTEXT_MENU_OFFSET_PX
use *; // ContextMenuProviderProps, rsx!, VNode, Element, EventHandler
The Rust form is ContextMenuProvider(ContextMenuProviderProps { … }); the app subtree is children: Element (the default slot) and the menu is menu: Option<Element>. Inside rsx! the app subtree may be written directly as the component's children. Closed — or open with no menu — the provider renders the app subtree and nothing else.
The quadrant decides where it grows
The menu opens away from the pointer's nearest edges: bottom-start in the upper-left half, bottom-end upper-right, top-start lower-left, top-end lower-right. The halves split the popup viewport — the window minus the app-chrome band the host reserved (popupViewportRect() in Vue) — so a menu opened over a desktop shell's reserved strip grows into the usable space instead of under it. With no viewport at all (no DOM in Vue; no viewport prop in Rust) the answer is bottom-start.
The panel geometry is the shared select-panel geometry applied to a zero-size anchor at the pointer (pointAnchor in Vue, a width: 0, height: 0 rect in Rust), with Vue's HkMenu default anchor offset of 6px. That is why a context menu sits exactly where every other family popup would, instead of at a bespoke coordinate.
The two cards are not the same picture, and that is the honest comparison. The TSX card is the real thing: a provider on the Vue side renders no surface until a request arrives, so its static state is the bare app subtree — right-click the card and the menu appears at the pointer. The Rust card has no request channel (there is no provide/inject, no useContextMenu), so its state is the props: open + x/y + viewport + menu paint the open surface statically, here a bottom-start menu 6px below the pointer at (120, 96) — left:120px;top:102px — with rows the host wrote itself (a glyph through the crate's Icon, then a label).
Snapshot note. The Rust surface is div.hk-select-popout-host[data-side][data-align] (HkSelectPanel's canvas, compiled by the Select mirror — this component owns no stylesheet) wrapping div.hk-select-popout[aria-label], then the host's menu. Vue renders the same grammar through HkMenu + HkSelectPanel. Nothing here is teleported on the Rust side, so the surface paints where it is written.
The controlled request, and the two close paths
Vue keeps open, request and a menuEpoch in refs and exposes open(request) / close() through provide(CONTEXT_MENU_KEY). Every open bumps the epoch, which remounts the HkMenu instance — a replacement menu starts from clean cascade levels, a clean back-guard and no hovered submenu, without racing a close-then-reopen toggle through the scheduler.
The Rust mirror has neither per-instance component state nor a provide/inject channel, so it is fully controlled: the host owns open, the trigger supplies x/y (plus the optional viewport), the title and the menu content, and hears a close request through on_close. Nothing is stored. The two close channels are Escape on the panel and a click outside the surface; both call the same writer, exactly once per open.
The bottom half of the same 720×420 viewport is the other two quadrants. The Rust card's pointer at (400, 320) lands below the vertical midpoint, so the menu grows upwards — data-side="top" data-align="start" — which is the one thing the first example cannot show. The TSX card takes the other path: defaults installs the app-wide regime (a window-level contextmenu listener that yields to element-level bindings, consults registered contributors — the built-in clipboard menu claims editable targets — and suppresses the platform menu everywhere else).
Rust gap — no default regime. There is no defaults prop on the Rust side and no registerContextMenuDefault(...) analogue: the mirror renders what the host hands it and never listens on the window. A Rust app that wants "right-click is always ours" wires its own contextmenu listener and passes the request in.
[D] No epoch, no instance state. Vue's menuEpoch remount exists so a fresh menu starts clean; the Rust mirror renders no menu instance state at all (the level/rows are the host's content), so there is nothing to reset — and nothing that would reset a host's cascading menu between two opens. A Rust host that implements cascades must clear them itself when it reopens.
[D] An open request with no content paints nothing in Rust (context_menu_surface_shown(open, has_menu) is the gate, exported for hosts that want to ask). Vue's closed HkMenu mounts no surface either, but Vue's open request with an empty items array still paints an empty level's box. If the two sides must look identical for a degenerate request, the host filters the request before passing it down.
Source defect (Rust). The title becomes the panel's aria-label (aria-label={title}, dropped when empty) but the panel carries no role="menu" — that role, and the rows inside it, are the host's menu element. A Rust host that forgets the role ships an unlabelled popout with focusable buttons inside; the Vue side gets the role from HkMenu. The role="menu" line in the example above is not optional decoration.
Source defect (Vue). close(announce) is documented as firing onClose "exactly once per open", and the justSelected one-shot flag exists to stop a selection's trailing update:open(false) from firing it a second time. That flag is a single boolean shared by the whole provider, and onSelect sets it while it also nulls request; a menu the select handler opens in the meantime inherits the stale flag, and its own close is then swallowed with no hook at all. The Rust mirror has exactly one close writer (Escape and the outside click both call on_close), so it cannot reproduce the double or the missing call — which is why a Vue app that opens a menu from onSelect or onClose should not treat the hook as a reliable "closed without a selection" signal.
API
Vue
Props
| Name | Type | Default | Required |
|---|---|---|---|
| defaults | Boolean | false | — |
Rust
Props — ContextMenuProvider
| Prop | Type | Default | Description |
|---|---|---|---|
| open | bool | false | Controlled menu state (Vue: the provider's own open ref). |
| x | f64 | 0.0 | Pointer client X of the request (Vue: request.x). |
| y | f64 | 0.0 | Pointer client Y of the request (Vue: request.y). |
| viewport | Option<ContextMenuViewport> | None | The popup viewport the quadrant halves split (Vue: popupViewportRect()); None resolves to bottom-start like Vue does without a DOM. |
| title | String | "" | Accessible name of the menu surface (Vue: request.title; empty drops the attribute). |
| on_close | Option<EventHandler<()>> | None | Close request (Escape, a click outside the menu, or the host's own dismissal paths). |
| menu | Option<Element> | None | The menu content — the level/rows (div.hk-menu-level[role=menu] …) the host supplies, since HkMenu is not mirrored in this wave. |
| children | Element | (empty) | The app subtree the provider wraps (Vue: the default slot). |
Accessibility
- The surface is named by title (aria-label), and the menu role is the host's: put role="menu" on the level and role="menuitem" on the rows. Nothing passes that role down from the provider on the Rust side; on the Vue side it comes from HkMenu.
- Escape closes the menu from the panel, so the panel must hold focus for the key to reach it. Vue's HkSelectPanel focuses on open; the Rust mirror does not — the host must move focus into the menu when it opens and return it to the element that opened it.
- Both sides dismiss on an outside click (Vue's HkMenu installs a capture-phase document click listener; the Rust shield listens on the page's click). A press that started inside the menu and released outside therefore does not dismiss it on either side.
- Right-click is not available to every user. Anything a context menu offers must also be reachable from a visible affordance (a row's trailing menu button, the toolbar). The context menu is an accelerator, never the only path.
- Keyboard users reach a context menu through Shift+F10 / the context-menu key, which fires contextmenu at the focused element — the Vue default regime (defaults) already routes that; a Rust host must handle the key itself.
- [D] Vue's HkMenu opens a full-width bottom sheet with a back guard below 768px. The Rust provider has no sheet: the popout renders at every width. A host that wants the sheet grammar mounts one.
- [D] The Rust mirror has no auto-flip, no cascade and no keyboard row navigation — they are HkSelectPanel / HkMenu runtime contracts (measured boxes, per-level panels, document listeners). The quadrant is a preference on both sides: in Vue the panel still flips afterwards if the chosen side cannot host it; in Rust nothing does.
Related components
- Popover — the anchored surface for content that hangs off a trigger.
- SelectPanel — the dropdown canvas this surface borrows (hk-select-popout*) and the panel geometry it pins at the pointer.
- PopupSelect — a field-shaped selector, when the "menu" picks a value rather than running a verb.
- FloatingLayer — chrome that must outlive windows; nothing on this page floats above a modal.