Context Menu
Displays a menu located at the pointer, triggered by a right click or a long press.
<NaviusContextMenu>
<NaviusContextMenuTrigger class="... border border-dashed border-border">
Right click here
</NaviusContextMenuTrigger>
<NaviusContextMenuPositioner>
<NaviusContextMenuPopup class="... min-w-[14rem] bg-popover p-1 shadow-md">
<NaviusContextMenuItem>Back</NaviusContextMenuItem>
<NaviusContextMenuItem Disabled="true">Forward</NaviusContextMenuItem>
<NaviusContextMenuItem>Reload</NaviusContextMenuItem>
<NaviusContextMenuSub>
<NaviusContextMenuSubTrigger>More Tools</NaviusContextMenuSubTrigger>
<NaviusContextMenuSubContent class="... bg-popover p-1 shadow-md">
<NaviusContextMenuItem>Save Page As…</NaviusContextMenuItem>
<NaviusContextMenuItem>Developer Tools</NaviusContextMenuItem>
</NaviusContextMenuSubContent>
</NaviusContextMenuSub>
<NaviusContextMenuSeparator class="-mx-1 my-1 h-px bg-border" />
<NaviusContextMenuCheckboxItem @bind-Checked="_showBookmarks">
<NaviusContextMenuItemIndicator>✓</NaviusContextMenuItemIndicator>
Show Bookmarks
</NaviusContextMenuCheckboxItem>
<NaviusContextMenuSeparator class="-mx-1 my-1 h-px bg-border" />
<NaviusContextMenuLabel>People</NaviusContextMenuLabel>
<NaviusContextMenuRadioGroup @bind-Value="_person">
<NaviusContextMenuRadioItem Value="pedro">
<NaviusContextMenuItemIndicator>●</NaviusContextMenuItemIndicator>
Pedro Duarte
</NaviusContextMenuRadioItem>
<NaviusContextMenuRadioItem Value="colm">
<NaviusContextMenuItemIndicator>●</NaviusContextMenuItemIndicator>
Colm Tuite
</NaviusContextMenuRadioItem>
</NaviusContextMenuRadioGroup>
</NaviusContextMenuPopup>
</NaviusContextMenuPositioner>
</NaviusContextMenu>
@code {
private bool? _showBookmarks = true;
private string? _person = "pedro";
}Features
- Anchored to the pointer position, with collision-aware flipping and clamping.
- Opens on right click, long press (touch), and the
ContextMenu/Shift+F10keys. - Supports submenus with configurable reading direction.
- Supports items, labels, groups of items, checkbox items, and radio groups.
- Full keyboard navigation with roving focus and type-ahead.
- Dismissing and layering behavior is highly customizable.
Installation
Install the brain, or copy just this primitive in with the CLI.
navius add context-menuAnatomy
Import the parts and assemble them.
@using Navius.Primitives.Components.ContextMenu
<NaviusContextMenu>
<NaviusContextMenuTrigger />
<NaviusContextMenuPositioner>
<NaviusContextMenuPopup>
<NaviusContextMenuLabel />
<NaviusContextMenuItem />
<NaviusContextMenuGroup>
<NaviusContextMenuItem />
</NaviusContextMenuGroup>
<NaviusContextMenuCheckboxItem>
<NaviusContextMenuItemIndicator />
</NaviusContextMenuCheckboxItem>
<NaviusContextMenuRadioGroup>
<NaviusContextMenuRadioItem>
<NaviusContextMenuItemIndicator />
</NaviusContextMenuRadioItem>
</NaviusContextMenuRadioGroup>
<NaviusContextMenuSub>
<NaviusContextMenuSubTrigger />
<NaviusContextMenuSubContent />
</NaviusContextMenuSub>
<NaviusContextMenuSeparator />
<NaviusContextMenuArrow />
</NaviusContextMenuPopup>
</NaviusContextMenuPositioner>
</NaviusContextMenu>API Reference
Root
Contains all the parts of a context menu. Owns open state; renders no DOM.
| Prop | Type | Default |
|---|---|---|
| Open | bool | false |
| OpenChanged | EventCallback<bool> | - |
| DefaultOpen | bool | false |
| ChildContent | RenderFragment? | - |
Trigger
The area that opens the context menu. Wrap the target and right-click within its bounds. Renders a div.
| Prop | Type | Default |
|---|---|---|
| Disabled | bool | false |
| ChildContent | RenderFragment? | - |
Data attributes
| Data attribute | Values |
|---|---|
| [data-popup-open] | Present while the menu is open. |
| [data-disabled] | Present when disabled |
Popup
The menu surface that pops out when the context menu is open. Wrapped by the Positioner and self-portaled to the document body. Renders a div with role="menu". The Side / Align / SideOffset / AlignOffset / AvoidCollisions / CollisionPadding / Sticky / HideWhenDetached / ArrowPadding props are set on the enclosing Positioner.
| Prop | Type | Default |
|---|---|---|
| Side | string | "right" |
| Align | string | "start" |
| SideOffset | double | 2 |
| AlignOffset | double | 0 |
| AvoidCollisions | bool | true |
| CollisionPadding | double? | - |
| Sticky | string? | - |
| HideWhenDetached | bool | false |
| ArrowPadding | double | 0 |
| Loop | bool | false |
| Modal | bool | true |
| KeepMounted | bool | false |
| Container | string? | - |
| Dir | string? | - |
| OnEscapeKeyDown | EventCallback<NaviusEscapeKeyDownEventArgs> | - |
| OnPointerDownOutside | EventCallback<NaviusPointerDownOutsideEventArgs> | - |
| OnFocusOutside | EventCallback<NaviusFocusOutsideEventArgs> | - |
| OnInteractOutside | EventCallback<NaviusInteractOutsideEventArgs> | - |
| OnCloseAutoFocus | EventCallback<NaviusCloseAutoFocusEventArgs> | - |
| ChildContent | RenderFragment? | - |
Data attributes
| Data attribute | Values |
|---|---|
| [data-open] | Present while open. |
| [data-closed] | Present while closing (through the exit transition). |
| [data-starting-style] | Present at the start of the open transition. |
| [data-ending-style] | Present at the start of the close transition. |
| [data-side] | "left" | "right" | "top" | "bottom" |
| [data-align] | "start" | "center" | "end" |
Item
The component that contains the context menu items. Renders a div.
| Prop | Type | Default |
|---|---|---|
| Disabled | bool | false |
| TextValue | string? | - |
| OnSelect | EventCallback<NaviusSelectEventArgs> | - |
| ChildContent | RenderFragment? | - |
Data attributes
| Data attribute | Values |
|---|---|
| [data-highlighted] | Present when highlighted |
| [data-disabled] | Present when disabled |
Group
Used to group multiple items. Renders a div with role="group".
| Prop | Type | Default |
|---|---|---|
| ChildContent | RenderFragment? | - |
Label
Used to render a label. It won't be focusable using arrow keys. Renders a div.
| Prop | Type | Default |
|---|---|---|
| ChildContent | RenderFragment? | - |
CheckboxItem
An item that can be controlled and rendered like a checkbox. Renders a div.
| Prop | Type | Default |
|---|---|---|
| Checked | bool? | - |
| CheckedChanged | EventCallback<bool?> | - |
| DefaultChecked | bool? | - |
| Disabled | bool | false |
| TextValue | string? | - |
| OnSelect | EventCallback<NaviusSelectEventArgs> | - |
| ChildContent | RenderFragment? | - |
Data attributes
| Data attribute | Values |
|---|---|
| [data-checked] | Present when checked. |
| [data-unchecked] | Present when unchecked. |
| [data-indeterminate] | Present when in the indeterminate state. |
| [data-highlighted] | Present when highlighted |
| [data-disabled] | Present when disabled |
RadioGroup
Used to group multiple radio items. Renders a div with role="group".
| Prop | Type | Default |
|---|---|---|
| Value | string? | - |
| ValueChanged | EventCallback<string?> | - |
| DefaultValue | string? | - |
| ChildContent | RenderFragment? | - |
RadioItem
An item that can be controlled and rendered like a radio. Renders a div.
| Prop | Type | Default |
|---|---|---|
| Value | string | - |
| Disabled | bool | false |
| TextValue | string? | - |
| OnSelect | EventCallback<NaviusSelectEventArgs> | - |
| ChildContent | RenderFragment? | - |
Data attributes
| Data attribute | Values |
|---|---|
| [data-checked] | Present when checked. |
| [data-unchecked] | Present when unchecked. |
| [data-highlighted] | Present when highlighted |
| [data-disabled] | Present when disabled |
ItemIndicator
Renders when the parent CheckboxItem or RadioItem is checked. You can style this element directly, or use it as a wrapper to put an icon into. Renders a span.
| Prop | Type | Default |
|---|---|---|
| ForceMount | bool | false |
| ChildContent | RenderFragment? | - |
Data attributes
| Data attribute | Values |
|---|---|
| [data-checked] | Present when checked. |
| [data-unchecked] | Present when unchecked. |
| [data-indeterminate] | Present when in the indeterminate state. |
Separator
Used to visually separate items in the context menu. Renders a div with role="separator".
| Prop | Type | Default |
|---|---|---|
| - | - | - |
Data attributes
| Data attribute | Values |
|---|---|
| [data-orientation] | "horizontal" |
Arrow
An optional arrow element to render alongside the content. This can be used to help visually link the trigger point with the content. Renders an svg.
| Prop | Type | Default |
|---|---|---|
| Width | double | 10 |
| Height | double | 5 |
Sub
Contains all the parts of a submenu. Owns its own open state; renders no DOM.
| Prop | Type | Default |
|---|---|---|
| Open | bool | false |
| OpenChanged | EventCallback<bool> | - |
| DefaultOpen | bool | false |
| ChildContent | RenderFragment? | - |
SubTrigger
An item that opens a submenu. Must be rendered inside a Sub. Renders a div.
| Prop | Type | Default |
|---|---|---|
| Disabled | bool | false |
| TextValue | string? | - |
| Dir | string? | - |
| ChildContent | RenderFragment? | - |
Data attributes
| Data attribute | Values |
|---|---|
| [data-popup-open] | Present while the submenu is open. |
| [data-highlighted] | Present when highlighted |
| [data-disabled] | Present when disabled |
SubContent
The component that pops out when a submenu is open. Must be rendered inside a Sub. Renders a div.
| Prop | Type | Default |
|---|---|---|
| Side | string | "right" |
| Align | string | "start" |
| SideOffset | double | 0 |
| AlignOffset | double | 0 |
| AvoidCollisions | bool | true |
| CollisionPadding | double? | - |
| Loop | bool | false |
| ForceMount | bool | false |
| Dir | string? | - |
| ChildContent | RenderFragment? | - |
Data attributes
| Data attribute | Values |
|---|---|
| [data-open] | Present while open. |
| [data-closed] | Present while closing (through the exit transition). |
| [data-side] | "left" | "right" | "top" | "bottom" |
| [data-align] | "start" | "center" | "end" |
Accessibility
Adheres to the menu WAI-ARIA design pattern (without typeahead support) and uses roving tabindex to manage focus movement among menu items.
Keyboard interactions
| Key | Description |
|---|---|
| Space | Activates the focused item. |
| Enter | Activates the focused item. |
| ArrowDown | Moves focus to the next item. |
| ArrowUp | Moves focus to the previous item. |
| ArrowRight | When focus is on a SubTrigger, opens the submenu and moves focus to its first item. |
| ArrowLeft | When focus is inside a submenu, closes it and moves focus to the parent SubTrigger. |
| Home | Moves focus to the first item. |
| End | Moves focus to the last item. |
| Esc | Closes the context menu and returns focus to the trigger. |