Skip to content
GitHub11 stars

Context Menu

A menu that appears at the pointer on right click or long press.

Right click here
<ContextMenuRoot>
    <ContextMenuTrigger class="context-menu-trigger">
        Right click here

Usage guidelines

  • Use context menus as an enhancement: Don't make a context menu the only way to perform actions. People may not discover or be able to open one, especially on touch devices or with assistive technology. Always provide visible controls for the actions the context menu offers.

Anatomy

Import the primitives and assemble the parts:

@using Blazix.Primitives

<ContextMenuRoot>
    <ContextMenuTrigger />
    <ContextMenuPortal>
        <ContextMenuBackdrop />
        <ContextMenuPositioner>
            <ContextMenuPopup>
                <ContextMenuArrow />
                <ContextMenuItem />
                <ContextMenuLinkItem />
                <ContextMenuSeparator />

                <ContextMenuSubmenuRoot>
                    <ContextMenuSubmenuTrigger />
                </ContextMenuSubmenuRoot>

                <ContextMenuGroup>
                    <ContextMenuGroupLabel />
                </ContextMenuGroup>

                <ContextMenuRadioGroup>
                    <ContextMenuRadioItem>
                        <ContextMenuRadioItemIndicator />
                    </ContextMenuRadioItem>
                </ContextMenuRadioGroup>

                <ContextMenuCheckboxItem>
                    <ContextMenuCheckboxItemIndicator />
                </ContextMenuCheckboxItem>
            </ContextMenuPopup>
        </ContextMenuPositioner>
    </ContextMenuPortal>
</ContextMenuRoot>

API Reference

ContextMenuRoot

Doesn't render its own HTML element.

ParameterTypeDefault
Disabled

Whether the component should ignore user interaction.

bool false
LoopFocus bool true
Modal bool true
HighlightItemOnHover

Whether moving the pointer over an item highlights it.

bool true
Open bool false
OpenChanged

Called with the new value when Open changes, for @bind-Open.

EventCallback<bool> -
OnOpenChange EventCallback<OpenChangeEventArgs> -
OnOpenChangeComplete EventCallback<bool> -
Handle PopupHandle? -
ContentTemplate RenderFragment<object?>? -
ChildContent

The part's content.

RenderFragment? -

ContextMenuTrigger

Renders a <div> element.

ParameterTypeDefault
Render RenderFragment<MenuTriggerRenderProps>? -
ChildContent

The part's content.

RenderFragment? -

Other attributes are passed to the element it renders.

AttributeType
data-popup-open

Present when the corresponding context menu is open.

-
data-pressed

Present when the corresponding context menu is open.

-

ContextMenuPortal

Doesn't render its own HTML element.

ParameterTypeDefault
KeepMounted bool false
ChildContent

The part's content.

RenderFragment? -

ContextMenuBackdrop

Renders a <div> element.

ParameterTypeDefault
Render RenderFragment<PopupRenderProps>? -
ChildContent

The part's content.

RenderFragment? -

Other attributes are passed to the element it renders.

AttributeType
data-open

Present when the menu is open.

-
data-closed

Present when the menu is closed.

-
data-starting-style

Present when the menu begins animating in.

-
data-ending-style

Present when the menu is animating out.

-

ContextMenuPositioner

Doesn't render its own HTML element.

ParameterTypeDefault
Side Side? -
Align Align? -
SideOffset int? -
AlignOffset int? -
CollisionPadding int? -
ArrowPadding int? -
Sticky bool? -
Anchor string? -
ChildContent

The part's content.

RenderFragment? -
AttributeType
data-open

Present when the menu popup is open.

-
data-closed

Present when the menu popup is closed.

-
data-anchor-hidden

Present when the anchor is hidden.

-
data-align

Indicates how the popup is aligned relative to the specified side.

'start' | 'center' | 'end'
data-side

Indicates which side the popup is positioned relative to the anchor.

'top' | 'bottom' | 'left' | 'right' | 'inline-end' | 'inline-start'
CSS variableType
--anchor-height

The anchor's height.

number
--anchor-width

The anchor's width.

number
--available-height

The available height between the anchor and the edge of the viewport.

number
--available-width

The available width between the anchor and the edge of the viewport.

number
--positioner-height

The height of the menu's positioner. It is important to set height to this value when using CSS to animate size changes.

number
--positioner-width

The width of the menu's positioner. It is important to set width to this value when using CSS to animate size changes.

number
--transform-origin

The coordinates that this element is anchored to. Used for animations and transitions.

string

ContextMenuPopup

Renders a <div> element.

ParameterTypeDefault
Side Side? -
Align Align? -
SideOffset int? -
AlignOffset int? -
CollisionPadding int? -
ArrowPadding int 5
Sticky bool false
Anchor string? -
FallbackAxis bool? -
KeepMounted bool false
InitialFocus string? -
FinalFocus string? -
PositionerAttributes IReadOnlyDictionary<string, object>? -
Render RenderFragment<MenuPopupRenderProps>? -
ChildContent

The part's content.

RenderFragment? -

Other attributes are passed to the element it renders.

AttributeType
data-open

Present when the menu is open.

-
data-closed

Present when the menu is closed.

-
data-align

Indicates how the popup is aligned relative to specified side.

'start' | 'center' | 'end'
data-instant

Present if animations should be instant.

'click' | 'dismiss' | 'group' | 'trigger-change'
data-side

Indicates which side the popup is positioned relative to the anchor.

'top' | 'bottom' | 'left' | 'right' | 'inline-end' | 'inline-start'
data-starting-style

Present when the menu begins animating in.

-
data-ending-style

Present when the menu is animating out.

-

ContextMenuArrow

Renders a <div> element.

ParameterTypeDefault
Render RenderFragment<PopupRenderProps>? -
ChildContent

The part's content.

RenderFragment? -

Other attributes are passed to the element it renders.

AttributeType
data-open

Present when the menu popup is open.

-
data-closed

Present when the menu popup is closed.

-
data-uncentered

Present when the menu arrow is uncentered.

-
data-align

Indicates how the popup is aligned relative to specified side.

'start' | 'center' | 'end'
data-side

Indicates which side the popup is positioned relative to the anchor.

'top' | 'bottom' | 'left' | 'right' | 'inline-end' | 'inline-start'

ContextMenuItem

Renders a <div> element.

ParameterTypeDefault
OnClick EventCallback<MouseEventArgs> -
CloseOnClick bool true
Disabled

Whether the component should ignore user interaction.

bool false
Label string? -
Render RenderFragment<MenuItemRenderProps>? -
ChildContent

The part's content.

RenderFragment? -

Other attributes are passed to the element it renders.

AttributeType
data-highlighted

Present when the menu item is highlighted.

-
data-disabled

Present when the menu item is disabled.

-

ContextMenuGroup

Renders a <div> element.

ParameterTypeDefault
Render RenderFragment<MenuGroupRenderProps>? -
ChildContent

The part's content.

RenderFragment? -

Other attributes are passed to the element it renders.

ContextMenuGroupLabel

Renders a <div> element.

ParameterTypeDefault
Render RenderFragment<MenuGroupLabelRenderProps>? -
ChildContent

The part's content.

RenderFragment? -

Other attributes are passed to the element it renders.

ContextMenuSeparator

Renders a <div> element.

ParameterTypeDefault
Render RenderFragment<MenuSeparatorRenderProps>? -
ChildContent

The part's content.

RenderFragment? -

Other attributes are passed to the element it renders.

AttributeType
data-orientation

Indicates the orientation of the separator.

'horizontal' | 'vertical'

ContextMenuSubmenuRoot

Doesn't render its own HTML element.

ParameterTypeDefault
Disabled

Whether the component should ignore user interaction.

bool false
LoopFocus bool true
Modal bool true
HighlightItemOnHover

Whether moving the pointer over an item highlights it.

bool true
Open bool false
OpenChanged

Called with the new value when Open changes, for @bind-Open.

EventCallback<bool> -
OnOpenChange EventCallback<OpenChangeEventArgs> -
OnOpenChangeComplete EventCallback<bool> -
Handle PopupHandle? -
ContentTemplate RenderFragment<object?>? -
ChildContent

The part's content.

RenderFragment? -

ContextMenuSubmenuTrigger

Renders a <div> element.

ParameterTypeDefault
Delay int 100
CloseDelay int 0
OpenOnHover bool true
Disabled

Whether the component should ignore user interaction.

bool false
Label string? -
Render RenderFragment<MenuItemRenderProps>? -
ChildContent

The part's content.

RenderFragment? -

Other attributes are passed to the element it renders.

AttributeType
data-popup-open

Present when the corresponding submenu is open.

-
data-highlighted

Present when the submenu trigger is highlighted.

-
data-disabled

Present when the submenu trigger is disabled.

-

ContextMenuRadioGroup

Renders a <div> element.

ParameterTypeDefault
Value string? -
ValueChanged

Called with the new value when Value changes, for @bind-Value.

EventCallback<string?> -
OnValueChange

Runs before the value changes, with the reason; call Cancel() to keep it.

EventCallback<ValueChangeEventArgs<string?>> -
Disabled

Whether the component should ignore user interaction.

bool false
Render RenderFragment<MenuGroupRenderProps>? -
ChildContent

The part's content.

RenderFragment? -

Other attributes are passed to the element it renders.

ContextMenuRadioItem

Renders a <div> element.

ParameterTypeDefault
Valuerequired string -
CloseOnClick bool false
Disabled

Whether the component should ignore user interaction.

bool false
Label string? -
Render RenderFragment<MenuRadioItemRenderProps>? -
ChildContent

The part's content.

RenderFragment? -

Other attributes are passed to the element it renders.

AttributeType
data-checked

Present when the menu radio item is selected.

-
data-unchecked

Present when the menu radio item is not selected.

-
data-highlighted

Present when the menu radio item is highlighted.

-
data-disabled

Present when the menu radio item is disabled.

-

ContextMenuRadioItemIndicator

Renders a <span> element.

ParameterTypeDefault
KeepMounted bool false
Render RenderFragment<MenuRadioItemIndicatorRenderProps>? -
ChildContent

The part's content.

RenderFragment? -

Other attributes are passed to the element it renders.

AttributeType
data-checked

Present when the menu radio item is selected.

-
data-unchecked

Present when the menu radio item is not selected.

-
data-disabled

Present when the menu radio item is disabled.

-
data-starting-style

Present when the radio indicator begins animating in.

-
data-ending-style

Present when the radio indicator is animating out.

-

ContextMenuCheckboxItem

Renders a <div> element.

ParameterTypeDefault
Checked bool false
CheckedChanged

Called with the new value when Checked changes, for @bind-Checked.

EventCallback<bool> -
OnCheckedChange

Runs before the checked state changes; call Cancel() to keep it.

EventCallback<ValueChangeEventArgs<bool>> -
CloseOnClick bool false
Disabled

Whether the component should ignore user interaction.

bool false
Label string? -
Render RenderFragment<MenuCheckboxItemRenderProps>? -
ChildContent

The part's content.

RenderFragment? -

Other attributes are passed to the element it renders.

AttributeType
data-checked

Present when the menu checkbox item is checked.

-
data-unchecked

Present when the menu checkbox item is not checked.

-
data-highlighted

Present when the menu checkbox item is highlighted.

-
data-disabled

Present when the menu checkbox item is disabled.

-

ContextMenuCheckboxItemIndicator

Renders a <span> element.

ParameterTypeDefault
KeepMounted bool false
Render RenderFragment<MenuCheckboxItemIndicatorRenderProps>? -
ChildContent

The part's content.

RenderFragment? -

Other attributes are passed to the element it renders.

AttributeType
data-checked

Present when the menu checkbox item is checked.

-
data-unchecked

Present when the menu checkbox item is not checked.

-
data-disabled

Present when the menu checkbox item is disabled.

-
data-starting-style

Present when the indicator begins animating in.

-
data-ending-style

Present when the indicator is animating out.

-

ContextMenuLinkItem

Renders a <a> element.

ParameterTypeDefault
CloseOnClick bool true
Disabled

Whether the component should ignore user interaction.

bool false
Label string? -
Render RenderFragment<MenuItemRenderProps>? -
ChildContent

The part's content.

RenderFragment? -

Other attributes are passed to the element it renders.

AttributeType
data-highlighted

Present when the link is highlighted.

-