-
Notifications
You must be signed in to change notification settings - Fork 10k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is there a way to stop propagation of events? #16105
Comments
Not presently. We will add something along these lines if it becomes a common request though. The main difficulty is that if we wanted to run the .NET code in a worker thread, then the bubbling and propagation decisions would all have to happen asynchronously. |
Without it, having an onclick on an A tag doesn't seem to work |
@chanan Do you have repro steps? I just checked that the following both work (as in, they invoke the method): <a onclick="@MyMethod">First tag</a>
<a href="http://example.com" onclick="@MyMethod">Second tag</a> |
This doesn't work for me. It changes the location to "/":
|
Not being able to stop event propagation is pretty bad, especially inside a framework that encourages you to use nested components. There is simply no way to prevent unwanted events from bubbling up to parent components. In the following example I have 3 buttons. My goal is to handle a click on any of these buttons directly in their onclick handlers, without the event bubbling up to the topmost <div> tag.
Currently the only idea I have to prevent this, is handling the event in JS only, and calling into C# from there. I don't even know yet how I am supposed to pass my component instance into that handler. Definately a +1 from me for a Blazor way to stop event propagation.
|
@SteveSandersonMS your answer to this question:
was:
I think that it is already a common request (problem): #5544, #15908, #15932, #15965, #16045, aspnet/Blazor#715. People must be able to stop the propagation of events. There are situations in which events should always propagate (default behaviour) or always not propagate. Maybe this can be solved by adding a new attribute like
|
OK, yes, that is pretty clear. We will come up with some way of doing it. Like you suggest, it will probably be a variation on the event-registration syntax, e.g.,
Filed #5545 |
BTW, about this:
Could you describe scenarios for this? It's a nontrivial bit of functionality to implement, so we'd need more info to know how much to prioritise it. |
I think this would also resolve #15908 |
@SteveSandersonMS <input bind="@TestValue" onkeydown="@KeyHandler" /> int TestValue { get; set; }
void KeyHandler(UIKeyboardEventArgs ev)
{
if (ev.Key.CompareTo("+") == 0)
TestValue++;
else if (ev.Key.CompareTo("-") == 0)
TestValue--;
} Press I have to do something like this to handle keyboard events correctly: // By default events are always passed to element
void KeyHandler(UIKeyboardEventArgs ev)
{
if (ev.Key.CompareTo("+") == 0)
{
TestValue++;
ev.Handled = true;
}
else if (ev.Key.CompareTo("-") == 0)
{
TestValue--;
ev.Handled = true;
}
} or // By default events aren't passed to elements if C# event handler is attached
// (preventDefault() is called in JS before event is send to C# code)
void KeyHandler(UIKeyboardEventArgs ev)
{
if (ev.Key.CompareTo("+") == 0)
TestValue++;
else if (ev.Key.CompareTo("-") == 0)
TestValue--;
else
DispatchEvent(ev);
} Maybe the second solution is easier to implement. I have no experience in JavaScript programming but maybe this will be useful: New DispatchEvent function should have some additional parameters, for example |
@SteveSandersonMS Further example for you steve, this one causes more problems too, beacuse of the frequency of the events, it actually trashes the GC fairly quickly.
And functions:
The idea is that on a mouseOver on the parent, the class on the sub menu is toggled to open the menu, and if the menu is open the handler exits immediately. The reality is, that handler gets fired for onmouse over on every element inside the li tag, and the li itself, which menas you move on the menu, the pop opens, then you move down to your menu option, and in the process generate maybe 10 or even 20 events all in rapid succession, which leads to GC nursery full error in the run time. |
Learning from years of game development (DirectX and OpenGL) and custom UI engines, I believe having With above solution, it turns out i have to bind I believe having the same mechanism like what HTML and WinForms and WPF do are would make more sense and they look more intuitive. More or less the all do something similar to |
@shawty GC nursery full is not an error.
We are all waiting for better event system in Blazor. |
@Andrzej-W I don't know the internals of the Blazor GC Andrezej, so when I see messages like that appearing in my browser console, it looks like an error (To me and the many others who see it). All I was doing was adding another common(ish) example to the list so the guys have more to work with in order to help them achive the goal. |
@shawty the first time I saw this message a few months ago I also thought that it was an error - so don't worry and be happy that we can finally write web applications in C# 😃 |
@xclud I totally agree. I would rather have my code decide to prevent or not than having extra noise with additional attributes in my tags. |
@Andrzej-W don't worry dude, it's all good. My reply was meant to sound the way it did, I'd had some clients winding me up erlier that day, so I wasn't in a perticularly patient or forgiving mood :-) Lessons to be learned, never write angry when on git-hub. Blazor rocks, no two ways about that. |
Is there a way in onclick (I guess using UIMouseEventArgs?) to stop the click from propagating?
The text was updated successfully, but these errors were encountered: