-
-
Notifications
You must be signed in to change notification settings - Fork 408
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
Enhance contextual-components & helpers #179
Comments
From the previous requirements I conclude that if we want to handle components and helpers with the same syntax, we need two keywords. The name is not very important at this point, but let's pretend the keywords are We know that it has to support glimmer components. The syntax of glimmer components is, according to the last rumours, going to have Aternative 1: curly-braces syntax for components and helpersThe idea is that the helpers receive the first argument, determine if it's a component or a helper and pass the arguments as received, which means that Invocation: {{#invoke-component "my-button" class="btn btn-success" @loadingMessage="Wait please ..."}}
Submit!
{{/invoke-component}}
{{!-- equivalent to <my-button class="btn btn-success" @loadingMessage="Wait please ...">Submit!</my-button> --}}
{{invoke-component "moment-format" today "YYYY-MM-DD" locale="fr"}}
{{!-- equivalent to {{moment-format today "YYYY-MM-DD" locale="fr"}} --}}
{{invoke-component "my-component" title=(invoke-component "moment-format" date)}}
{{!-- equivalent to <my-component class={{moment-format date}}>Submit!</my-component> --}} Contextual components/helpers creation: <my-form class="my-form" @submitBtn={{wrap-component "x-button" class="btn btn-success" @loadingMsg="Please wait..."}}></my-form>
{{!-- Later on, inside the my-form.hbs template --}}
{{invoke-component submitBtn @anotherAttr="foo" aria-hidden="true"}}
<my-calendar @format-date={{wrap-component "moment-format" locale="fr" format="YYYY-MM-DD"}}></my-calendar>
{{!-- Later on, inside the my-calendar.hbs template --}}
Today is {{invoke-component format-date today}} and tomorrow is {{invoke-component format-date tomorrow}} Shorthand invocation of contextual glimmer-components: {{!-- my-form.hbs --}}
{{yield hash=(
format-created-at=(wrap-component "moment-format" model.createdAt)
cancel-btn=(wrap-component "form-button" class="btn-warning" @onClick=(action "cancel"))
submit-btn=(wrap-component "form-button" class="btn-sucess" @onClick=(action "submit"))
)}}
<my-form @model={{model}} as |f|>
<p>This moment was created at {{f.format-created-at "YYYY-MM-DD"}}</p> {{!-- Invoke a helper --}}
<f::cancel-button>Abort</f::cancel-button>
<f::submit-button>Save</f::submit-button>
</my-form> |
I am all in favor of adding this feature since it would allow to create better public APIs for components like the ones in the ember-power family. One option to avoid the I would need to know more about current implementation of contextual components, since last time I checked (July? August?) it was partially coded as an special case. Maybe @zackthehuman can help a bit here. I'm all in for investigating this further. Great work, @mixonic! |
@Serabe What do you mean to restrict it to the dot syntax? With components is easy, but components are eager, so |
This was replaced by #432 which is merged, and awaiting implementation. Closing this issue in favor of tracking the progress of that RFC. |
I found myself in need of a way of creating something similar to contextual components but instead of creating a component and curry arguments, I wanted to do the same with a helper.
After drafting an RFC, checking previous art of the subject and chat about it on slack I realized that the matter is a bit more complicated that I naively thought at first.
I want to push this forward on an RFC but I want to write this issue first so we can list the motivation an requirement that the RFC has to consider.
Motivation
Essentially the same motivation that anyone would have to desire contextual components:
The naive idea that wouldn't work, and why
It seemed obvious that if we have a
{{component}}
keyword that allows to dynamically invoke components by its name and also to wrap components and attribututes into anComponentDefinition
, the only feature we need is a symmetrical{{helper}}
keyword that doesthe same thing with helpers.
You can read this thread in discuss, but the basic problem with this idea is:
{{component}}
helper is used to invoke a component and when it's used to create a contextual component, given the "eager" nature of helpers, there is no way of know the intention of the user. Example:The
closeButton
attribute is clearly creating a closure component with anonClick
option bundling it together in aComponentDefinition
that can later on be rendered providing extra elements.The
(helper "moment-format")
part is not clear tho. The intention of the user is to invoke themoment-format
helper with the given arguments or is instead creating a contextual helper and pass it tomy-component
?There is no way of telling what the user wants to do with helpers using a single keyword like
{{component}}
. We'd need a different syntax to disambiguate.Constraints and other relevant information for the solution
Listed here without any particular order.
Object.assign
. Options passed in invocation win over those the component/helper has closed over.components
collection (if that is the correct term). This reinforces my idea that the keywords for wrap/invoke components/helper should be the same.{{component}}
helper works that would require a new approach anyway. I'm not sure if that fundamental problem is the one I mentioned in the previous point or there is more to the story.Element Modifiers
: I know little-to-nothing about them apart that they exist in @mixonic's mind. How can they affect this proposal? Can they also be wrapped/invoked?{{component}}
keyword be statically analysable. Perhaps it's not relevant for this RFC, but it worth mentioning it.The action plan, teaching issues and such would be discussed in an eventual RFC.
Pinging people that might want to read this: @mixonic @Serabe @locks
The text was updated successfully, but these errors were encountered: