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

Suggestion: Adding Directive #5607

Closed
asyncoder opened this issue Oct 11, 2018 · 24 comments
Closed

Suggestion: Adding Directive #5607

asyncoder opened this issue Oct 11, 2018 · 24 comments
Labels
affected-few This issue impacts only small number of customers area-blazor Includes: Blazor, Razor Components enhancement This issue represents an ask for new feature or an enhancement to an existing one feature-razor.language not-on-roadmap Priority:2 Work that is important, but not critical for the release severity-minor This label is used by an internal tool
Milestone

Comments

@asyncoder
Copy link

I'd love to see the ability of supporting also directives, same principle as Angular.

Thanks!

@RemiBou
Copy link
Contributor

RemiBou commented Oct 12, 2018

Honestly, I think one of the serious advantage of Blazor compared to other frameworks is its simplicity : there is a small amount of concept in the framework (page / component). In the angular world you learn a new one every day (https://angular.io/guide/glossary).

When asking a feature maybe we could tell which case cannot be resolved with the current framework state instead of grabbing concept from others.

Do you have any case in mind where the current framework cannot help you ?

@asyncoder
Copy link
Author

I wouldn't say grabbing features from others rather than taking advantages. However, the simplicity and reusing C# skills are one of the reasons that Blazor got my heart :-)

Directives are really powerful when you want for example to extend new features and behaviours to an existing components. Let's say that you have a native component "input" and you'd like it behaves somehow when the user focus on it. It wouldn't be so nice to create a new abstract component to do this and then you have to manage all others attributes including accessibility and so on.

Hope I made it clear.

Happy blazoring!

@srowan
Copy link

srowan commented Oct 15, 2018

Agree, some concept of attribute directives (aka custom bindings in knockout) is critical. Components are good, but they don't go nearly far enough.

@asyncoder asyncoder reopened this Dec 14, 2018
@asyncoder
Copy link
Author

Any chance to have this feature ASAP? 🙄
@danroth27

@danroth27
Copy link
Member

@asyncoder This one is a bit further down our backlog right now. We need to first land getting the basics of Razor Components into ASP.NET Core 3.0. But if anyone wants to submit some design proposals for what cross cutting directives in Blazor might look like that would be fine.

@aspnet-hello aspnet-hello transferred this issue from dotnet/blazor Dec 17, 2018
@aspnet-hello aspnet-hello added this to the Backlog milestone Dec 17, 2018
@aspnet-hello aspnet-hello added area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates enhancement This issue represents an ask for new feature or an enhancement to an existing one area-blazor Includes: Blazor, Razor Components labels Dec 17, 2018
@didii
Copy link

didii commented Dec 21, 2018

So I'd like to simply give some input about what I like and don't like about the general use of directives and how I feel would be a good investment:

  • They should be explicit
    • The MVC tag helpers are imo not explictit. You don't know what tag helper is available and you'll have to think of all the prefixes to add to make sure you don't overlap with another existing one or attribute.
      <label asp-for="Item.Name">Name</label>
    • Angular directives are explict. You directly say what directive should be applied. However, since they are simple attribtues could still overlap with existing attributes or other directives so you need again a prefix while we have namespaces for a reason.
      <button appConfirm [message]='"Are you sure?"'>Delete</button>
    • Proposal 1: I don't know if this is possible with razor, but explicitely invoking it could help to identify directives. This way there is no confusion about what this attribute does and you are not left with finding abbreviated and possibly confusing prefixes. Directives that have the same name can be prefixed by its namespace. In this example Confirm or ConfirmDirective should be the class name of the directive.
      <MyComponent @Confirm/> or <MyComponent @MyLibrary.Confirm/>
    • Proposal 2: Again, I don't know if this is possible, but since directives share a lot of common traits with C# attributes, we might find a way to apply them like that. Not as an html attribute, but as a C# one.
      @[Confirm]
      <MyComponent/>
  • They should be succinct
    • Angular directives can add as many parameters as they like. This eventually gives an overload of attributes and it gets hard to identify what attributes belongs to which component/directive. Does [message] belong to the directive of the component?
      <app-show-message appConfirm [message]="'Are you sure?'">Blah<app-show-message/>
    • Proposal: Only allow a single options object per directive:
      <MyComponent @MyDirective="@_myDirectiveOptions">
    • Note that if from the previous block proposal 2 is possible, this is obsolete since we simply apply how attributes normally get their options and adding multiple is also the same. This keeps them nicely seperated and easy to understand.
      @[MyDirective("message", Title = "Tile")]
      @[MyOtherDirective]
      <input/>

Feel free to comment or criticise.

@didii
Copy link

didii commented Dec 21, 2018

@RemiBou why directives are sometimes preferred:

I want to extend the <select> attribute such that you can use it like this:
<select Items="@_items" Changed="@OnChanged"/>
Expected behavior is that options are generated automatically based on the collection _items and that I get notified and get the object back from this select rather than an ID as a string (i.e. OnChanged is an Action<TItem>).

This can still be easily implemented using a component. But now I want to style it and add the class attribute to the select. Blazor/Razor will now complain that the parameter class is not defined. Well, I could write a parameter class in the component to support this. But then I want to be able to hide it using the hidden attribute. So you'll have to add that paramter too etc.

This is the result of blazor components not existing in the DOM itself. Which keeps the DOM clean and readable, but now in this case also results in some responsibilities shifted to a child component since the parent has no way of editing it, or a lot of code needs to be added to the child component to support it. Both are not preferred in this case.

@mkArtakMSFT mkArtakMSFT removed area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates labels May 9, 2019
@Liero
Copy link

Liero commented Aug 28, 2020

@didii:

  • They should be succinct

    • Angular directives can add as many parameters as they like. This eventually gives an overload of attributes and it gets hard to identify what attributes belongs to which component/directive. Does [message] belong to the directive of the component?
      <app-show-message appConfirm [message]="'Are you sure?'">Blah<app-show-message/>
    • Proposal: Only allow a single options object per directive:
      <MyComponent @MyDirective="@_myDirectiveOptions">
    • Note that if from the previous block proposal 2 is possible, this is obsolete since we simply apply how attributes normally get their options and adding multiple is also the same. This keeps them nicely seperated and easy to understand.
      @[MyDirective("message", Title = "Tile")]
      @[MyOtherDirective]
      <input/>

What about following?

<MyComponent @MyDirective(option1, option2)>
<MyComponent @MyDirective="(option1, option2)">

@SteveSandersonMS SteveSandersonMS added affected-few This issue impacts only small number of customers not-on-roadmap severity-minor This label is used by an internal tool labels Oct 6, 2020 — with ASP.NET Core Issue Ranking
@gojanpaolo
Copy link

gojanpaolo commented Dec 8, 2020

In Angular, we've used ng2-trim-directive which lets us trim user inputs neatly like so:

<input trim="blur">

Hoping to get something similar with Blazor

Closest I got so far is..

<input @bind="Foo" onblur="this.value=this.value.trim()">

@TonyLugg
Copy link

TonyLugg commented Jan 11, 2021

I switched from Angular to Aurelia a few years ago because of Aurelia's simplicity. Aurelia has "Custom Attributes" which allows you to add behavior to any element in the DOM. The Blazor team should look at how Aurelia implements this as it is simple and intuitive. http://aurelia.io/docs/templating/custom-attributes

@Gopichandar
Copy link

Gopichandar commented Jul 17, 2021

Is there any plan for this in near feature. ?

@rekna1
Copy link

rekna1 commented Aug 16, 2021

I would also strongly favor the possibility to be able to add behavior to existing html elements :

It is preferred when possible to use existing html elements over custom elements to keep html as close to the standard as possible. Input elements should remain input elements ... buttons should remain buttons.

Main problem is, when you wrap eg. a button inside a custom button component, you have to expose almost every aspect of an html button element to be as flexible as possible (properties and events). Also , you'd be more future proof, when html adds new attributes/properties/events, you would be able to use them, which is not the case when the button is wrapped inside a component. (and when this custom component is not under your control you have to wait until the external library updates)

@TanayParikh TanayParikh modified the milestones: Backlog, .NET 7 Planning Oct 18, 2021
@TanayParikh TanayParikh added the Priority:2 Work that is important, but not critical for the release label Oct 22, 2021
@mkArtakMSFT mkArtakMSFT modified the milestones: .NET 7 Planning, Backlog Nov 11, 2021
@ghost
Copy link

ghost commented Nov 11, 2021

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

@rekna1
Copy link

rekna1 commented Nov 11, 2021

I don't know for sure, but suppose you have an input element wrapped inside a component, and this component is inside a fieldset. When the fieldset is set to disabled, the component should be disabled as well... Could be more difficult.

I also wonder about aria attributes, custom styling, ... which maybe would require additional coding ...

@PavloParafiloCricut
Copy link

Nice feature to have.
Can help to avoid nesting hell and workarounds.
Should help to organize code better.

@ParadiseFallen
Copy link

This will also helps MBB.
+1 on this feature

@vgb1993
Copy link

vgb1993 commented Aug 23, 2022

Sounds like a reasonable proposition to me, I think directives can help you organize better and improve readability

@justdox
Copy link

justdox commented Sep 10, 2022

Honestly, I think one of the serious advantage of Blazor compared to other frameworks is its simplicity : there is a small amount of concept in the framework (page / component). In the angular world you learn a new one every day (https://angular.io/guide/glossary).

When asking a feature maybe we could tell which case cannot be resolved with the current framework state instead of grabbing concept from others.

Do you have any case in mind where the current framework cannot help you ?

So, here is a typical case, that is to apply drag-movable feature to any HTML element like that in Angular —— by just attach a directive to the element.

In fact, the so-called "simplicity" is truely Weak, Crude, and NOT Finished. It makes that very hard to achieve AOP, to reuse Common functionality codes.

@mkArtakMSFT
Copy link
Member

Thanks for contacting us.
This suggestion seems to be too general. We are open to more specific and detailed proposals.

@mkArtakMSFT mkArtakMSFT closed this as not planned Won't fix, can't repro, duplicate, stale Sep 28, 2022
@ParadiseFallen
Copy link

@mkArtakMSFT we want attached properties for blazor like in XAML, thats all

@mikestam
Copy link

I thought this was already a Blazor feature.
Aurelia has great feature and examples of directives. @show, @if, @disabled and whatever you implement.
This makes code clean and nice. Easy and intuitive to write and follow.
Yes, you can achieve this in different ways in Blazor but do not want to use component to wrap button or div for this, or numerous @if{) nor to use css framework dependent classes.
Attached properties could be the way too.

@Gaulomatic
Copy link

So this issue, although mentioning Angular 12 times and Aurelia six times, is now closed after .. four years - because it is not too specific. Great job.

@TonyLugg
Copy link

@mkArtakMSFT If you just looked at custom attributes in Aurelia like I suggest a long time ago, you would perhaps understand. Custom attributes are somewhat "general", which is what we are all looking for.

@ghost ghost locked as resolved and limited conversation to collaborators Nov 30, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affected-few This issue impacts only small number of customers area-blazor Includes: Blazor, Razor Components enhancement This issue represents an ask for new feature or an enhancement to an existing one feature-razor.language not-on-roadmap Priority:2 Work that is important, but not critical for the release severity-minor This label is used by an internal tool
Projects
None yet
Development

No branches or pull requests