ContextMenu
A menu opened at the pointer by right click, long press, Shift+F10 or the ContextMenu key.
Examples
Context menu
Same items and keyboard behavior as DropdownMenu.
Right click, long press, or focus here and press Shift+F10
Last action: none
Razor
<div class="flex w-full max-w-md flex-col items-center gap-3">
<KlyContextMenu Items="Items" OnSelect="item => _last = item.Label" Label="Document actions" aria-label="Document, press Shift+F10 for actions" Class="w-full">
<div class="flex h-36 w-full items-center justify-center rounded-(--klyroo-radius) border border-dashed border-(--klyroo-border-accented) px-4 text-center text-sm text-(--klyroo-text-muted)" data-testid="context-area">
Right click, long press, or focus here and press Shift+F10
</div>
</KlyContextMenu>
<p class="text-sm text-(--klyroo-text-muted)" data-testid="context-state">Last action: @(_last ?? "none")</p>
</div>
@code {
private string? _last;
private static readonly KlyMenuItem[] Items =
[
new() { Label = "Open", Shortcut = "Enter" },
new() { Label = "Rename", Shortcut = "F2" },
new() { Label = "Share", Href = "/docs" },
KlyMenuItem.Separator(),
new() { Label = "Archive", Disabled = true },
new() { Label = "Delete", Color = KlyColor.Error },
];
}Bound open state
@bind-Open follows the menu without moving it.
Right click here: the open state is bound
Open: no
Razor
<div class="flex w-full max-w-md flex-col items-center gap-3">
<KlyContextMenu Items="Items" @bind-Open="_open" Label="Document actions" aria-label="Document, press Shift+F10 for actions" Class="w-full">
<div class="flex h-36 w-full items-center justify-center rounded-(--klyroo-radius) border border-dashed border-(--klyroo-border-accented) px-4 text-center text-sm text-(--klyroo-text-muted)" data-testid="bound-context-area">
Right click here: the open state is bound
</div>
</KlyContextMenu>
<p class="text-sm text-(--klyroo-text-muted)" data-testid="bound-context-state">Open: @(_open ? "yes" : "no")</p>
</div>
@code {
private bool _open;
private static readonly KlyMenuItem[] Items =
[
new() { Label = "Open" },
new() { Label = "Rename" },
new() { Label = "Delete", Color = KlyColor.Error },
];
}API
KlyContextMenu
Menu that opens at the pointer on right click, long press or the keyboard, over the content it wraps.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
Class |
string |
Classes added to the root element, merged last so they win over the theme. | |
Disabled |
bool |
false |
Lets the browser show its own context menu in the area. |
Focusable |
bool |
true |
Makes the area reachable with Tab, so that Shift+F10 and the ContextMenu key work from the keyboard. |
Items (required) |
IReadOnlyList |
The entries of the menu, as KlyMenuItem . | |
Label |
string |
Accessible name of the menu. | |
Open (@bind-Open) |
bool |
false |
Open state, reported with @bind-Open . Setting it to true opens the menu at the center of the area, since there is no pointer to open it at. |
Ui |
ContextMenuUi |
Per-instance class overrides of the slots of the context menu, see ContextMenuUi . They are merged after the theme and the Class parameter wins on the root slot. |
Events
| Name | Type | Description |
|---|---|---|
OnSelect |
EventCallback |
Called with the activated item. Link items navigate instead. |
Slots
| Name | Type | Description |
|---|---|---|
ChildContent |
RenderFragment |
The area that reacts to the context menu gesture. |
Ui slots
Ui takes a ContextMenuUi with one class string per slot, merged after the theme.
| Slot | Styles |
|---|---|
Root |
Classes merged into the root slot of KlyContextMenu , after the theme and the app configuration. |
Trigger |
Classes merged into the trigger slot of KlyContextMenu , after the theme and the app configuration. |
Content |
Classes merged into the content slot of KlyContextMenu , after the theme and the app configuration. |
Group |
Classes merged into the group slot of KlyContextMenu , after the theme and the app configuration. |
Label |
Classes merged into the label slot of KlyContextMenu , after the theme and the app configuration. |
Separator |
Classes merged into the separator slot of KlyContextMenu , after the theme and the app configuration. |
Item |
Classes merged into the item slot of KlyContextMenu , after the theme and the app configuration. |
ItemIcon |
Classes merged into the itemIcon slot of KlyContextMenu , after the theme and the app configuration. |
ItemLabel |
Classes merged into the itemLabel slot of KlyContextMenu , after the theme and the app configuration. |
ItemShortcut |
Classes merged into the itemShortcut slot of KlyContextMenu , after the theme and the app configuration. |