Class TwContextMenu
- Namespace
- TwBlazor.Components
- Assembly
- TwBlazor.dll
An area that opens a menu of actions where a person right-clicks it, such as the actions for a file, a row or a canvas. The menu takes the same items as a TwMenu.
public class TwContextMenu : TwBlazorComponentBase, IComponent, IHandleEvent, IHandleAfterRender, ITwComponent, IAsyncDisposable
- Inheritance
-
TwContextMenu
- Implements
- Inherited Members
Remarks
The menu opens at the pointer on a right-click (or a long press, on touch screens that support it) and is kept inside the window. Keyboard users open it with Shift+F10 or the Menu key while focus is in the area; it then opens against the focused element with focus on its first item. Inside the menu the keys are the same as in a TwMenu. Escape closes it and returns focus to where it was; a click elsewhere, a scroll or choosing an action close it too. Outside the area the browser keeps its own context menu. Put TwMenuItem, TwMenuCheckboxItem, TwMenuRadioGroup<TValue>, TwSubmenu, TwMenuDivider and TwMenuGroup in MenuContent.
Constructors
TwContextMenu()
public TwContextMenu()
Properties
ChildContent
Gets or sets the area that opens the menu when it is right-clicked.
[Parameter]
public RenderFragment? ChildContent { get; set; }
Property Value
Disabled
Whether the menu is turned off, so a right-click shows the browser's own context menu instead.
[Parameter]
public bool Disabled { get; set; }
Property Value
Focusable
Whether the area is a Tab stop of its own, so keyboard users can reach it and open the menu with Shift+F10 or
the Menu key. Set it when nothing inside the area can take focus. The area then tells screen readers how to open
the menu; give it a name with an aria-label attribute. Defaults to false.
[Parameter]
public bool Focusable { get; set; }
Property Value
IsOpen
Gets whether the menu is open.
public bool IsOpen { get; }
Property Value
MenuClass
Gets or sets extra classes for the menu panel.
[Parameter]
public string? MenuClass { get; set; }
Property Value
MenuContent
Gets or sets the menu's items.
[Parameter]
public RenderFragment? MenuContent { get; set; }
Property Value
OpenChanged
Raised when the menu opens or closes.
[Parameter]
public EventCallback<bool> OpenChanged { get; set; }
Property Value
Methods
BuildRenderTree(RenderTreeBuilder)
Renders the component to the supplied RenderTreeBuilder.
protected override void BuildRenderTree(RenderTreeBuilder __builder)
Parameters
__builderRenderTreeBuilder
CloseAsync()
Closes the menu and returns focus to where it was before the menu opened. Escape and choosing an item call this too.
[JSInvokable("Close")]
public Task CloseAsync()
Returns
- Task
A task that completes once the menu is closed.
Dismiss()
Closes the menu without moving focus, because a click elsewhere, a scroll or Tab is already moving it on.
[JSInvokable]
public Task Dismiss()
Returns
- Task
A task that completes once the menu is closed.
DisposeAsync()
Removes the area's and the menu's listeners and releases the reference the script held to the component.
public ValueTask DisposeAsync()
Returns
- ValueTask
A task that completes once the component is cleaned up.
OnAfterRenderAsync(bool)
Method invoked after each time the component has been rendered interactively and the UI has finished updating (for example, after elements have been added to the browser DOM). Any ElementReference fields will be populated by the time this runs.
This method is not invoked during prerendering or server-side rendering, because those processes are not attached to any live browser DOM and are already complete before the DOM is updated.
Note that the component does not automatically re-render after the completion of any returned Task, because that would cause an infinite render loop.
protected override Task OnAfterRenderAsync(bool firstRender)
Parameters
firstRenderboolSet to
trueif this is the first time OnAfterRender(bool) has been invoked on this component instance; otherwisefalse.
Returns
Remarks
The OnAfterRender(bool) and OnAfterRenderAsync(bool) lifecycle methods
are useful for performing interop, or interacting with values received from @ref.
Use the firstRender parameter to ensure that initialization work is only performed
once.
OpenAtAsync(double, double, bool)
Opens the menu at a point in the window, or moves it there when it is already open. A right-click, Shift+F10 and the Menu key call this.
[JSInvokable("OpenAt")]
public Task OpenAtAsync(double clientX, double clientY, bool fromKeyboard = false)
Parameters
clientXdoubleThe horizontal position, in CSS pixels from the left of the window.
clientYdoubleThe vertical position, in CSS pixels from the top of the window.
fromKeyboardboolWhether the menu is opened from the keyboard, which moves focus to its first item. Opened by a pointer, the menu itself takes focus, so the arrow keys still reach the items.
Returns
- Task
A task that completes once the menu is open.