ContextMenu
Since 0.1.0-alpha.22A 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.
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#.
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 aMenuItemwith aValue.MenuSub: a submenu: an item that opens a second menu beside it.
Mode support
| Feature | Static | Interactive |
|---|---|---|
| Opens by right-click, long press, the menu key or Shift+F10 | Yes | Yes |
| Links and form posts as items | Yes | Yes |
| One menu for every card of a list, told which was pressed | Yes | Yes |
| Focus goes back where it was | Yes | Yes |
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
menunamed byLabel, withmenuitem,menuitemcheckboxandmenuitemradioitems; aMenuGroupis agroupnamed by itsLabel. - Opening it focuses its first item; Escape or a choice puts focus back where it was.
Keyboard navigation
In the For element
| Press this key | To do this |
|---|---|
| Shift+F10 or Menu key | Open the menu under the focused element |
In the menu
| Press this key | To do this |
|---|---|
| Up or Down | Move through the items, wrapping |
| Home or End | Go to the first or last item |
| Letters | Jump to the next item starting with them |
| Enter or Space | Choose the item |
| Escape | Close the menu and go back to where focus was; in a submenu, close just the submenu and go back to its item |
| Tab | Close the menu and move on |
| Right | On a MenuSub's item, open the submenu at its first item (Left on right-to-left pages) |
| Left | In a submenu, close it (Right on right-to-left pages) |
Parameters
| Prop | Type | Default |
|---|---|---|
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. | string | Required |
LabelThe menu's name for screen readers, such as "Folder actions". | string | Required |
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. | bool | false |
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> |
Plus 17 item props and 4 common props.
Full APISee also
- Menu: opens a popover menu of links, form posts and actions.
- Popover: shows non-modal content beside a trigger.