How to prevent event propagation in blazor #49308
-
I am developing a library, there is a function being called whenever the component's (oncontextmenu) is triggered, I can't modify or see that component I am just providing this function and I am curious if there is a way to call some methods to stop the event from propagating. Because it so happens the component is currently a child element of another component that also has oncontextmenu to do something, by design, so if this component's oncontextmenu is triggered that component gets triggered as well right so how do I prevent that |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Just to clarify, so since you would not have access to the component itself in this situation, and thus would not be able to use ...addEventListener("contextmenu", (ev) => {
// do stuff
ev.stopPropagation();
}); but handled in the event handler that's passed to Unfortunately I don’t believe that this is possible in Blazor since the event would have already occurred (in JS) by the time it reaches the Note: An API for something similar was requested in PreventDefault as a method on Blazor event arguments #45949, although based on the discussion there it seems like implementing this would be rather complicated. |
Beta Was this translation helpful? Give feedback.
Just to clarify, so since you would not have access to the component itself in this situation, and thus would not be able to use
@oncontextmenu:preventDefault="true”
to stop propagation, you would need something roughly equivalent to this:but handled in the event handler that's passed to
@oncontextmenu
, correct?Unfortunately I don’t believe that this is possible in Blazor since the event would have already occurred (in JS) by the time it reaches the
@oncontextmenu
handler in .NET. Although I'm not an expert on Blazor, so others might be aware of workarounds or ways to achieve a similar effect.Note: A…