Skip to content
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

Compiler support for @oneventname:preventDefault and @oneventname:stopPropagation #14517

Closed
SteveSandersonMS opened this issue Sep 27, 2019 · 3 comments
Assignees
Labels
area-blazor Includes: Blazor, Razor Components Done This issue has been fixed enhancement This issue represents an ask for new feature or an enhancement to an existing one

Comments

@SteveSandersonMS
Copy link
Member

I looked into doing this myself, but after talking with @rynowak, it seems likely that we're missing some support in the compiler for having @directiveattribute:param appearing on its own (not alongside @directiveattribute).

The overall summary is:

  • For each @oneventname, there's also @oneventname:preventDefault and @oneventname:stopPropagation. These can be used either alongside @oneventname or independently of it.
  • They take a boolean expression value, or can appear minimized, which is the same as giving them the value true
  • @foo:preventDefault compiles as builder.AddEventPreventDefaultAttribute(sequence, "foo", boolValueExpression). This new method is added here.
  • @foo:stopPropagation compiles as builder.AddEventStopPropagationAttribute(sequence, "foo", boolValueExpression). This new method is added here.
  • These new method calls can appear among the AddAttribute calls for the element (internally they work by calling into AddAttribute).
  • Ideally, these new directive attributes would only be available if you have referenced Microsoft.AspNetCore.Components.Web, since that's where the runtime implementation is, and won't produce compiling code if you haven't referenced that.

Examples:

Minimized preventDefault

<div @onclick:preventDefault>...</div>

Generates:

builder.OpenElement(0, "div");
// ... possibly other attributes here ...
builder.AddEventPreventDefaultAttribute(2, "onclick", true);
// ... possibly other attributes here ...
builder.CloseElement();

Note that you can also have a regular @onclick event handler on the same element, but that's totally independent.

Non-minimized preventDefault

<div @onclick:preventDefault="@someBoolExpression">...</div>

Generates:

builder.OpenElement(0, "div");
// ... possibly other attributes here ...
builder.AddEventPreventDefaultAttribute(2, "onclick", someBoolExpression);
// ... possibly other attributes here ...
builder.CloseElement();

Note that you can also have a regular @onclick event handler on the same element, but that's totally independent.

Minimized stopPropagation

<div @onkeydown:stopPropagation>...</div>

Generates:

builder.OpenElement(0, "div");
// ... possibly other attributes here ...
builder.AddEventStopPropagationAttribute(2, "onkeydown", true);
// ... possibly other attributes here ...
builder.CloseElement();

Note that you can also have a regular @onkeydown event handler on the same element, but that's totally independent.

Non-minimized stopPropagation

<div @onmouseover:stopPropagation="@someBoolExpression">...</div>

Generates:

builder.OpenElement(0, "div");
// ... possibly other attributes here ...
builder.AddEventStopPropagationAttribute(2, "onmouseover", someBoolExpression);
// ... possibly other attributes here ...
builder.CloseElement();

Note that you can also have a regular @onmouseover event handler on the same element, but that's totally independentl

@SteveSandersonMS SteveSandersonMS added the area-blazor Includes: Blazor, Razor Components label Sep 27, 2019
@mkArtakMSFT mkArtakMSFT added enhancement This issue represents an ask for new feature or an enhancement to an existing one PRI: 1 - Required labels Sep 27, 2019
@mkArtakMSFT mkArtakMSFT added this to Triage in Blazor 3.1 via automation Sep 27, 2019
@mkArtakMSFT mkArtakMSFT moved this from Triage to 3.1.0 Preview3 in Blazor 3.1 Sep 27, 2019
@mkArtakMSFT mkArtakMSFT moved this from To Do (3.1.0 Preview2) to In progress in Blazor 3.1 Oct 7, 2019
@NTaylorMullen NTaylorMullen added Done This issue has been fixed and removed Working labels Oct 18, 2019
NTaylorMullen added a commit that referenced this issue Oct 18, 2019
- For our current world Blazor both of these properties are always `true`. However, for other non-web Blazor scenarios the `EventHandler` attributes at that layer will turn off these features because they're specific to web.

#14517
NTaylorMullen added a commit that referenced this issue Oct 18, 2019
- For our current world Blazor both of these properties are always `true`. However, for other non-web Blazor scenarios the `EventHandler` attributes at that layer will turn off these features because they're specific to web.

#14517
@mkArtakMSFT mkArtakMSFT moved this from In progress to Done in Blazor 3.1 Oct 19, 2019
@MarkusRodler
Copy link
Contributor

@SteveSandersonMS I have a question about preventDefault. Is it now possible to preventDefault only for some keys? For example i want to preventDefault on a textfield if the enter key is pressed. In JavaScript I would write it like this:

document.getElementById('div').keypress(function (event) {
    if (event.which === 13) {
        evt.preventDefault();
    }
)};

I hope it is legit to ask that here.

@SteveSandersonMS
Copy link
Member Author

@MarkusRodler For that scenario you would still need to use a JS event handler, since the .NET handlers are async.

@MarkusRodler
Copy link
Contributor

@SteveSandersonMS Thanks for the reply!

@ghost ghost locked as resolved and limited conversation to collaborators Dec 27, 2019
@jaredpar jaredpar removed this from Done in Blazor 3.1 Jan 7, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components Done This issue has been fixed enhancement This issue represents an ask for new feature or an enhancement to an existing one
Projects
None yet
Development

No branches or pull requests

5 participants