Context Menu

A menu opened by right-clicking a target area.

EXAMPLEwasm · webgpu

This demo runs the real GPUI component on WebGPU, which this browser does not expose.

Try Chrome or Edge 113+, or Safari 26+. In Firefox, enable dom.webgpu.enabled.

You can also run it natively: cargo run -p showcase context-menu

The demo failed to start in this browser.

You can run it natively instead: cargo run -p showcase context-menu

Live examples are not supported on most mobile browsers yet — open this page on a desktop or tablet to run the demo.

Recent phones with WebGPU may manage it (downloads ~18 MB):

#Anatomy

Import the component's parts and compose them under its root:

use base_gpui::context_menu::{
    ContextMenuRoot,
    ContextMenuTrigger,
};

ContextMenuRoot::new()
    .child(
        ContextMenuTrigger::new(),
    );
The anatomy is a structural overview. Parts with mutually exclusive modes may need separate instances; each part's section below documents its configuration.

#ContextMenuRoot

Coordinates the component's state and supplies context to its child parts.

Source

use base_gpui::context_menu::ContextMenuRoot;

ContextMenuRoot::new()
    .id("example-id")
    .default_open(true)
    .open(true)
    .disabled(true)
    .loop_focus(true)
    .orientation(/* orientation: MenuOrientation */)
    .close_parent_on_esc(true)
    .highlight_item_on_hover(true)
    .on_open_change(|/* callback arguments */| { /* handle change */ })
    .on_open_change_complete(|/* callback arguments */| { /* handle change */ })
    .style_with_state(|/* callback arguments */| { /* handle change */ });

#Builders

BuilderSignature & description
.newpub fn new() -> Self

Creates a ContextMenuRoot with its default configuration.

.childpub fn child(mut self, child: impl Into<MenuChild<P>>) -> Self
  • child: impl Into<MenuChild<P>>

Adds one typed child to this part.

.childrenpub fn children(mut self, children: impl IntoIterator<Item = impl Into<MenuChild<P>>>) -> Self
  • children: impl IntoIterator<Item = impl Into<MenuChild<P>>>

Adds multiple typed children in iteration order.

.child_anypub fn child_any(mut self, child: impl IntoElement) -> Self
  • child: impl IntoElement

Adds one arbitrary renderable child to this part.

.idpub fn id(mut self, id: impl Into<ElementId>) -> Self
  • id: impl Into<ElementId>

Sets the stable GPUI element identity. Use a unique value when multiple instances can appear in the same view.

.default_openpub fn default_open(mut self, default_open: bool) -> Self
  • default_open: bool

Sets the initial open state for an uncontrolled component.

.openpub fn open(mut self, open: bool) -> Self
  • open: bool

Controls whether the component is open.

.disabledpub fn disabled(mut self, disabled: bool) -> Self
  • disabled: bool

When true, prevents user interaction with this part.

.loop_focuspub fn loop_focus(mut self, loop_focus: bool) -> Self
  • loop_focus: bool

Controls whether keyboard focus wraps from the last enabled item to the first and vice versa.

.orientationpub fn orientation(mut self, orientation: MenuOrientation) -> Self
  • orientation: MenuOrientation

Sets the component's horizontal or vertical orientation and corresponding keyboard behavior.

.close_parent_on_escpub fn close_parent_on_esc(mut self, close_parent_on_esc: bool) -> Self
  • close_parent_on_esc: bool

Controls whether close parent on esc behavior is enabled.

.highlight_item_on_hoverpub fn highlight_item_on_hover(mut self, highlight_item_on_hover: bool) -> Self
  • highlight_item_on_hover: bool

Controls whether highlight item on hover behavior is enabled.

.on_open_changepub fn on_open_change(mut self, on_open_change: impl Fn(bool, &mut MenuOpenChangeDetails<P>, &mut Window, &mut App) + 'static,) -> Self
  • on_open_change: impl Fn(bool, &mut MenuOpenChangeDetails<P>, &mut Window, &mut App) + 'static

Registers a callback invoked when open change occurs.

.on_open_change_completepub fn on_open_change_complete(mut self, on_open_change_complete: impl Fn(bool, &MenuOpenChangeDetails<P>, &mut Window, &mut App) + 'static,) -> Self
  • on_open_change_complete: impl Fn(bool, &MenuOpenChangeDetails<P>, &mut Window, &mut App) + 'static

Registers a callback invoked when open change complete occurs.

.style_with_statepub fn style_with_state(mut self, style: impl Fn(MenuRootStyleState, Div) -> Div + 'static,) -> Self
  • style: impl Fn(MenuRootStyleState, Div) -> Div + 'static,

Styles the part from its current behavioral state while preserving separation between behavior and visual design.

ContextMenuRoot also supports the GPUI traits implemented in its source, such as standard styling or child composition.

#ContextMenuTrigger

Interactive control that opens, closes, or activates the component.

Source

use base_gpui::context_menu::ContextMenuTrigger;

ContextMenuTrigger::new()
    .id("example-id")
    .style_with_state(|/* callback arguments */| { /* handle change */ });

#Builders

BuilderSignature & description
.newpub fn new() -> Self

Creates a ContextMenuTrigger with its default configuration.

.idpub fn id(mut self, id: impl Into<ElementId>) -> Self
  • id: impl Into<ElementId>

Sets the stable GPUI element identity. Use a unique value when multiple instances can appear in the same view.

.child_anypub fn child_any(mut self, child: impl IntoElement) -> Self
  • child: impl IntoElement

Adds one arbitrary renderable child to this part.

.style_with_statepub fn style_with_state(mut self, style: impl Fn(ContextMenuTriggerStyleState, Div) -> Div + 'static,) -> Self
  • style: impl Fn(ContextMenuTriggerStyleState, Div) -> Div + 'static,

Styles the part from its current behavioral state while preserving separation between behavior and visual design.

ContextMenuTrigger also supports the GPUI traits implemented in its source, such as standard styling or child composition.

#Accessibility

Keyboard interaction and accessibility semantics are implemented by the component, independently of visual styling. Known limitations caused by missing GPUI accessibility primitives are documented in the module source and are not silently approximated.

#Stability

Base GPUI is pre-1.0. Builder names and state types may evolve as GPUI and this port mature.