Skip to content
IzzyBlazor

Enter all results ↑ ↓ move Esc close

ContextMenu

Since 0.1.0-alpha.22
Opens a menu at the pointer by right-click, with no button.

A menu with no button, opened at the pointer by right-click, a long press, the menu key or Shift+F10 anywhere in the element For names, such as a file list or a canvas. Links, fields and selected text keep the browser's menu, as does Shift+right-click.

For is an id, or a CSS selector when it starts with "#", "." or "[", so one menu serves every card of a list. Each card says which it is with data-menu-value, and the menu's items take it: {0} in an item's Value or Href is the value of the card pressed, and OnOpen gets it for items that run C#.

A folder's menu

Set For to the id of the element that opens the menu, and Label to name it. Right-click can't be seen, so give its actions another way in, here the toolbar. For a menu with a button that also opens by right-click, use a Menu's ContextFor.

Static

Preview

Designs

Brief.docx

Timeline.xlsx

Right-click the folder, or use the toolbar.

Code

Usage.razor
@inject NavigationManager Navigation

@* Right-click, long press or Shift+F10 anywhere in the folder opens its menu. The toolbar has the
   same actions, the way in that can be seen. *@
<Stack Gap="3" Grow>
    <Toolbar Label="Folder">
        <ToolbarButton Href="@To("new-folder")" StartIcon="Lucide.FolderPlus">New folder</ToolbarButton>
        <ToolbarButton Href="@To("upload")" StartIcon="Lucide.Upload">Upload</ToolbarButton>
        <ToolbarButton Href="@To("refresh")" StartIcon="Lucide.RefreshCw">Refresh</ToolbarButton>
    </Toolbar>
    <Card id="project-files" tabindex="0" role="group" aria-label="Project files" Size="ControlSize.Sm">
        <Stack Gap="2">
            @foreach (var (name, icon) in Files)
            {
                <Row Gap="2" Align="Align.Center">
                    <Icon Name="icon" />
                    <TextBlock>@name</TextBlock>
                </Row>
            }
        </Stack>
    </Card>
    <ContextMenu For="project-files" Label="Folder actions">
        <MenuItem Href="@To("new-folder")" Icon="Lucide.FolderPlus">New folder</MenuItem>
        <MenuItem Href="@To("upload")" Icon="Lucide.Upload">Upload</MenuItem>
        <MenuDivider />
        <MenuItem Href="@To("refresh")" Icon="Lucide.RefreshCw">Refresh</MenuItem>
    </ContextMenu>
    <TextBlock Size="TextSize.Sm" Color="Color.Muted">
        @(Action switch
        {
            "new-folder" => "Created New folder.",
            "upload" => "Choose files to upload.",
            "refresh" => "Refreshed the folder.",
            _ => "Right-click the folder, or use the toolbar.",
        })
    </TextBlock>
</Stack>

@code {
    [SupplyParameterFromQuery(Name = "action")] private string? Action { get; set; }

    private string To(string action) => Navigation.GetUriWithQueryParameter("action", action);

}

Static: IzzyBlazor's script opens the menu at the pointer. Its items are links.

Add this example to a page with the izzy CLI:

bash
izzy add component ContextMenu --at <file>:<line>

One menu for every card

A selector as For, such as .file-card, opens one menu from every element it matches. Each says which it is with data-menu-value, and {0} in an item's Value or Href is the value of the one pressed. On interactive pages OnOpen gets it, for items that run C#.

Static

Preview

Q3 report.pdf

2.4 MB

Budget 2027.xlsx

860 KB

Logo.svg

12 KB

Open

Right-click a file.

Code

Shared.razor
@inject NavigationManager Navigation

@* One menu for every file: the card pressed says which with data-menu-value. Each card takes focus,
   so Shift+F10 opens the menu from the keyboard. *@
<form method="post" @onsubmit="Posted" @formname="shared-files" data-enhance>
    <AntiforgeryToken />
    <Stack Gap="3" Grow>
        <Grid MinChildWidth="13.Rem" Gap="3">
            @foreach (var file in Files)
            {
                <Card Class="shared-file" data-menu-value="@file.Name" tabindex="0" role="group" aria-label="@file.Name" Size="ControlSize.Sm">
                    <Row Gap="2" Align="Align.Center">
                        <Icon Name="file.Icon" />
                        <Stack Gap="0" Grow>
                            <TextBlock Weight="FontWeight.Medium">@file.Name</TextBlock>
                            <TextBlock Size="TextSize.Sm" Color="Color.Muted">@file.Size</TextBlock>
                        </Stack>
                    </Row>
                </Card>
            }
        </Grid>
        <ContextMenu For=".shared-file" Label="File actions" OnOpen="file => _file = file">
            <MenuItem Href="@Navigation.GetUriWithQueryParameter("open", "{0}")" Icon="Lucide.ExternalLink">Open</MenuItem>
            <MenuItem Submit Name="duplicate" Value="{0}" OnClick="() => _pressed = (_file, false)" Icon="Lucide.Copy">Duplicate</MenuItem>
            <MenuDivider />
            <MenuItem Submit Name="delete" Value="{0}" OnClick="() => _pressed = (_file, true)" Icon="Lucide.Trash" Accent="Accent.Danger">Delete</MenuItem>
        </ContextMenu>
        <TextBlock Size="TextSize.Sm" Color="Color.Muted">@Result</TextBlock>
    </Stack>
</form>

@code {
    [SupplyParameterFromQuery(Name = "open")] private string? Opened { get; set; }

    // Static pages: the chosen item posts the pressed card's value.
    [SupplyParameterFromForm(FormName = "shared-files", Name = "duplicate")] private string? Duplicated { get; set; }

    [SupplyParameterFromForm(FormName = "shared-files", Name = "delete")] private string? Deleted { get; set; }

    // Interactive pages post nothing: OnOpen says which card, OnClick which item.
    private string _file = "";
    private (string File, bool Delete)? _pressed;
    private string? _posted;

    private string Result => _posted ?? (Opened is not null ? $"Opened {Opened}." : "Right-click a file.");

    private void Posted()
    {
        var (file, delete) = _pressed ?? (Deleted ?? Duplicated ?? "", Deleted is not null);
        _posted = delete ? $"Deleted {file}." : $"Duplicated {file}.";
    }

}

Static: IzzyBlazor's script puts the pressed card's value into the items as the menu opens. Open is a link; Duplicate and Delete post the form. Each card takes focus (`tabindex="0"`, named by `aria-label`), so Shift+F10 opens its menu.

Details

Right-click can't be seen, so its actions need another way in, such as a toolbar. For the usual case, a menu with a button that also opens by right-click, use Menu's ContextFor.

Parts

Written inside ContextMenu. The API page lists their parameters.

  • MenuItem: one entry in a Menu.
  • MenuGroup: a labeled section of a Menu.
  • MenuDivider: a line between groups of items in a Menu.
  • MenuRadioGroup: a set of menu options where one is chosen (menuitemradio), each a MenuItem with a Value.
  • MenuSub: a submenu: an item that opens a second menu beside it.

Mode support

What works in each mode, ContextMenu
FeatureStaticInteractive
Opens by right-click, long press, the menu key or Shift+F10YesYes
Links and form posts as itemsYesYes
One menu for every card of a list, told which was pressedYesYes
Focus goes back where it wasYesYes

Static: static server-side rendering, with no circuit and no WebAssembly. Interactive: Server, WebAssembly or Auto.

Without JavaScript

Every page starts as plain HTML: before IzzyBlazor's script loads on a static page, while an interactive page prerenders, and with JavaScript off. With JavaScript off, right-click shows the browser's own menu, so give the actions another way in.

Accessibility

  • With script, it's a menu named by Label, with menuitem, menuitemcheckbox and menuitemradio items; a MenuGroup is a group named by its Label.
  • Opening it focuses its first item; Escape or a choice puts focus back where it was.

Keyboard navigation

In the For element

ContextMenu keys, In the For element
Press this keyTo do this
Shift+F10 or Menu keyOpen the menu under the focused element

In the menu

ContextMenu keys, In the menu
Press this keyTo do this
Up or DownMove through the items, wrapping
Home or EndGo to the first or last item
LettersJump to the next item starting with them
Enter or SpaceChoose the item
EscapeClose the menu and go back to where focus was; in a submenu, close just the submenu and go back to its item
TabClose the menu and move on
RightOn a MenuSub's item, open the submenu at its first item (Left on right-to-left pages)
LeftIn a submenu, close it (Right on right-to-left pages)

Parameters

ContextMenu parameters
PropTypeDefault
ForThe element whose right-click, long press, menu key or Shift+F10 opens the menu: its id, or a CSS selector when it starts with #, . or [, such as .file-card for every card of a list.stringRequired
LabelThe menu's name for screen readers, such as "Folder actions".stringRequired
PanelWidthThe width of the menu panel, such as Length.Rem(16) for a column of long labels.Length?computed
ChildContentThe MenuItems, MenuGroups and MenuDividers.RenderFragment?
@bind-OpenWhether it is open.boolfalse
IdThe element id.string?
OnOpenRuns when the menu opens on an interactive page, with the data-menu-value of the element pressed, for items that run C#.EventCallback<string>

See also

  • Menu: opens a popover menu of links, form posts and actions.
  • Popover: shows non-modal content beside a trigger.

Connection lost

Rejoining the server…