question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot pass through directive attributes, so components can't support @on{EVENT}:stopPropagation etc.

See original GitHub issue

Is your feature request related to a problem? Please describe.

This feature relates to https://github.com/dotnet/aspnetcore/issues/18460 where I’m building a custom styled component and want to keep all features for a simple tag, eg:

<div class="@boxClass" @attributes="AdditionalAttributes">
    @ChildContent
</div>

@code  {
    [Parameter(CaptureUnmatchedValues = true)]
    public IDictionary<string, object>? AdditionalAttributes { get; set; }
}

where, @boxClass is generated somewhere else. All attributes works except the @on{EVENT} in combination with @on{EVENT}:stopPropagation and/or @on{EVENT}:preventDefault.

Describe the solution you’d like

I’d expect that if I’m able to pass down the event handler it should be possible to pass the event custom attributes also since it’s not practical (could be for a workaround) to have two flags for each possible event and declare them into the component.

Additional context

None

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:5
  • Comments:20 (13 by maintainers)

github_iconTop GitHub Comments

5reactions
stefanloerwaldcommented, Apr 27, 2021

Hi @SteveSandersonMS,

I came across this issue (again) and decided to look at code this time. For library developers who want to support arbitrary event listeners on their components, there’s just no way around attribute splatting, but that mechanism doesn’t support @onclick="Handler" @onclick:stopPropagation="Stop", because RZ10010 (“The component parameter 'onclick' is used two or more times for this component. Parameters must be unique (case-insensitive). The component parameter 'onclick' is generated by the '@onclick:stopPropagation' directive attribute.”) will prevent compiling that code.

The thing is: I don’t at all see why that’s the case! This is what I observed with the razor compiler:

  1. @{eventName}:stopPropagation="value" (and similarly preventDefault), will generate the code __builder.AddEventStopPropagationAttribute(seq, eventName, value);
  2. AddEventStopPropagationAttribute is implemented in WebRenderTreeBuilderExtensions with a one-liner: https://github.com/dotnet/aspnetcore/blob/a4154ac58b07bce7ddb086427c1bc8a13073babd/src/Components/Web/src/Web/WebRenderTreeBuilderExtensions.cs#L50
  3. Since AddEventStopPropagationAttribute is implemented as builder.AddAttribute(sequence, $"__internal_stopPropagation_{eventName}", value);, @{eventName}:stopPropagation="value" could be replaced by __internal_stopPropagation_{eventName}="@value" with the exact same effect as AddEventStopPropagationAttribute.
  4. Testing this with .NET 5 suggests that replacing the directive by the internal attribute works perfectly fine: the stopPropagation behavior is correctly applied.

This makes me wonder why RZ10010 is there in the first place. The implementation does not use the same attribute name twice at all (because one attribute is named onclick and the other __internal_stopPropagation_onclick).

Could it perhaps be that this is an error that was necessary in some old version of blazor, but now is just obsolete because implementation changed?

2reactions
javiercncommented, Mar 24, 2023

You don’t have to increment the sequence numbers.

I apologize in advance, that I have not figured out the details, and unfortunately, I don’t have time to spend on this as I am heads down on Blazor united work. Take my suggestion as a rough sketch and play with it if you want, it might not pan out, but I think it can potentially be done.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can you prevent an Angular component's host click from ...
You could do the following: Redefine the click event of the component, and emit this event when the button is clicked; Set the...
Read more >
Event: stopPropagation() method - Web APIs | MDN
The stopPropagation () method of the Event interface prevents further propagation of the current event in the capturing and bubbling phases.
Read more >
Optimizing Events handling in Angular
Let's explore how to implement our own Event Manager Plugin to optimize event handling. Then I'll show how to use it to automatically...
Read more >
Handling Events with Vue.js
Handling events with Vue. We can intercept an event by adding the v-on directive to the relevant DOM element. Let's say we want...
Read more >
Modal | Components
preventDefault () method of the event object passed to your ok (OK button), cancel (Cancel button), close (modal header close button) and hide...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found