RFC: using shorthand vs rendering callback prop functions#199
RFC: using shorthand vs rendering callback prop functions#199
Conversation
…resence-component
Codecov Report
@@ Coverage Diff @@
## master #199 +/- ##
==========================================
- Coverage 67.69% 67.18% -0.51%
==========================================
Files 103 106 +3
Lines 1461 1466 +5
Branches 288 281 -7
==========================================
- Hits 989 985 -4
- Misses 468 478 +10
+ Partials 4 3 -1
Continue to review full report at Codecov.
|
|
I like the power of Implementing it is not easy - you have to correctly apply styles, accessibility, keyboard handlers... do we really want to transfer all this responsibility to users? It looks like we are building a nuclear power plant just to light up a bulb. But I can imagine using it internally to implement 'abstract components' to be used in public components which would wrap the abstract component and implement renderXXX methods. |
|
The proposal sounds good to me. |
|
have discussion with @miroslavstastny, this one essentially distills to the following points. TL;DR TakeawayEssentially, we are still looking for the approach that will solve the following major problems
Below the reasoning behind this is provided. Using shorthand
<ElementType ... >
{childrenExist(children) ? (
children
) : (
<a
...
onClick={this.handleClick}
{...accessibility.attributes.anchor}
{...accessibility.keyHandlers.anchor}
>
{icon && Icon.create(...) }
{content}
</a>
)}
</ElementType>
<Avatar image={<Icon ...>}Using 'renderXXX'Although this one introduces all the possible customization mechanisms, it also imposes quite a lot of things that consumer should think about (styling, accessibility, potentially more will come later) - while, essentially, the only purpose of the client is to tweak rendered tree, not the behavior. There could be objections that at some cases (again, accessibility :) rendered tree might define behavior, but still it seems that we should investigate these cases more - to ensure that we are not introducing too much complexity for the client. |
|
Absolutely agreed with your point @miroslavstastny @kuzhelov Actually maybe the best proposal would be to introduce in the components that needs something like the renderXXX methods, the following: <ElementType ... >
{childrenExist(children) ? (
children
) : (
<div
...
{...accessibility.attributes.someComponentPart}
{...accessibility.keyHandlers.comeComponentPart}
>
renderXXX({...config, props })
</div>
)}
</ElementType>With this, we would be able to handle the accessibility, behavior and all other aspect we may introduce in the future, but we would have to introduce additional element in the DOM. I can try to introduce some implementation with the proposed example if you think would be useful. |
|
I don't think that would work - aria attributes must be on specific elements, you cannot move them to parent. Adding |
|
So, the final decision would be for us to proceed with the usage of shorthands? Maybe we should at least try to provide the config object from the renderComponent method to the shorthand function, so that we can have the component's props, styles, variables etc... It would like something like this: <Avatar image={({props, variables, styles}) => <Icon name='checked' color={variables.typePrimaryColor} />} /> |
|
I'm sorry I'm late to chime into this, but from the standpoint of implementing a very specific visual design, I don't see how we can legitimately restrict the ability of a person using Stardust to be unable to supply their own Image to Avatar. Absolutely (obviously?) Stardust should provide an Avatar component that is very quick and simple to consume by anyone using the shorthand, but I don't think it should restrict the possibility of someone to provide their own Image to be rendered in avatar, if that is critical to their visual design. Maybe I'm not understanding @mnajdova 's last comment though? In the case of Microsoft Teams visual design, Avatar Image needs to have some very specific work (clipping) done that does not make sense to implement it in Stardust. The desired effect can't be accomplished by css styles alone (mostly thanks to needing to support IE/Edge), so there has to be a way to provide a user's component to avatar to be the Image. I admit I'm still very weak in my understanding of React and Stardust at this point, but @levithomason and I talked about this last week and I think I'm articulating the idea he described to me. |
Absolutely agree - this is one of the key scenarios that needs to be supported. Please, be assured that whatever approach will be finally taken, it will support this scenario, specifically - to put some custom component into the image's slot. Currently we have different ways to achieve this - both via shorthand, as well as via |
|
Hey guys, so, I tried using the shorthand for generating different element then the one specified and I tried adding some additional variables in the props that may be used in the shorthand method. Here is the example: The drawback of this is that, when the image shorthand is actually used as an image, this properties are sent to the HTML dom element, because those are not handled props... Is there any suggestion about how this should be addressed? Other then this, another thing that I have I am not much happy with, is that the props that I am receiving are for the image component, so if I want to actually get some prop, or the styles and variables for the Avatar component, I would still have to add them as some unhandled props for the Image component in order to have access to them. This will again end up with the problem I mention above. @levithomason @kuzhelov @miroslavstastny PS: I guess I can add all these additional properties just if the shorthand for the image is function. Maybe this would kind a solve this problem? |
|
Closing this in favor of #270 |


Avatar
The motivation for this PR is to showcase some points about the different approaches we may implement for parts of the component by using shorthand and rendering callback functions. As an example, the Avatar component is used (the branch is created from #168 in order for the latest updates of the component to be reused ). The PR contains two Avatar classes (Avatar.tsx and AvatarWithoutRenderMethod.tsx). Let me briefly introduce the things that are implemented in both components.
AvatarWithoutRenderMethod
This component is improvement of the existing Avatar component that introduces shorthands for the image and label components which were previously hard-coded inside the Avatar component.
pros:
cons:
Avatar
This component, except for introducing the shorthands for the image and label, also introduces renderMethodAria prop, which is a rendering function that receives as a param the config object used inside the avatar's renderComponent method, together with the props (it is possible to include the state here as well, but there isn't state in this particular component). This method has a default implementation that has the same logic as the previous component (if the image is provided then the image is rendered, if not - then the label is rendered).
pros:
cons:
Example usages:
Used code: