You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Is it possible to extend a component and modify functional behavior while maintaining the component API? #2448
Basically, I want to extend a components styles and include all the API behavior of a styled-component (as prop, string interpolation, etc), but add some functional differences to the render function.
The above captures the styling, but breaks when using the as prop. I would love to still be able to do <IconButton as="a">. I'm not sure if there is a pattern that will allow me to do this.
Just to chime in on this, we're having the exact same issue.
At Artsy we have an admittedly complex set of Typography components for controlling the text styles across our site. Ultimately it uses a styled wrapper with a functional component wrapper. Unfortunately using as on that kills all the child styles.
Basically, we need a way to tell styled-components to ignore the as prop and just pass it on to the wrapped component.
In your example, it would then be passed on to the Button component and everything would work as expected again. To visualise this here's an example of the prop being passed down explicitly:
Oh my. I wish this had been implemented as a static option for the styled function, now there's one more prop to be aware of for component users while they'd arguably very rarely need to use both as and forwardedAs at the same time. And they have to be aware of the problem to know when to use which prop.
In other words: it seems like "prop-handling wrapper components all the way" continues to be something you have to live with when using styled components.
@diondiondion please refrain from commenting if you're not adding anything constructive to the conversation.
arguably very rarely need to use both as and forwardedAs at the same time
Actually it comes up often if you're using third party component libraries. Your use case isn't everyone's so please keep it classy and use whatever tool is best for your use case while we work toward a more permanent, sustainable solution.
@probablyup I'd like to consider my comment constructive feedback, even if I didn't hide my disappointment with the new API.
I'm glad to hear you're working on a more permanent solution, but it's not like that's been communicated, maybe that's something that could be improved in the future. After all, all I'm seeing & reacting to is the final API, not the reasoning behind it or what's still to come.
I've spoken about JSX prop namespaces many times in these issues. I'm taking a crack at writing a babel transform for it which will be released separately from s-c and in v5 we'll look for the output and punch it out appropriately. This'll be a general-purpose pattern any JSX library can use and my goal will be to get the babel plugin added to the general React babel preset.
I've spoken about JSX prop namespaces many times in these issues.
I know. However most of my nagging can be boiled down to "Please reduce the need for wrapper components when working with styled-components", and as far as I understand, JSX namespaces don't address that at all. They're basically a more powerful version of the forwardedAs prop.
Is it not in your interest to allow directly defining styled components that are effectively "transparent" with any other react/s-c component? ("Transparent" meaning interchangeable without having to change the component API, "direct" meaning without having to create wrapper components that work as adapters for said transparency.)
They're basically a more powerful version of the forwardedAs prop.
Not really, it entirely replaces the need to ever do filtering on more than a library basis by cordoning off sets of props into buckets. If you only want to use a prop for styling purposes, you'd put it in the sc: namespace and we'd exclude it from being passed down to any component below. Other libraries would also be free to the do the same thing and chose their own namespace key. It's a much more elegant solution to this entire problem than anything that currently exists.
With it forwardedAs can go away and be replaced by sc:as. You could start writing arbitrary prop names (e.g. sc:color) that don't have to be camelCased anymore because with the namespace we know specifically that the prop is used for styling and not intended for the DOM or any underlying component.
Is it not in your interest to allow directly defining styled components that are effectively "transparent" with any other react/s-c component?
Absolutely, and the proposed namespacing accomplishes that elegantly. You still have to be aware of className being passed to a wrapped custom component but that's just the nature of the beast.
The end goal here is to make it explicit what's being used for styling and what isn't so component libraries will not have conflicts or have to ship extra filtering code to try and exclude stuff that shouldn't end up in the DOM.
@probablyup, sorry, but I don't think I understand how namespacing gets us any closer to fixing the original issue. It helps with prop filtering, but how does it help with this scenario:
I get that you can now use <StrongButton forwardedAs="a"> to make this work, but that means the consumer needs to know when to use as and when to use forwardedAs -- ideally the consumer should only need to know about as. As a library creator, it would be amazing if I had a mechanism like React's forwardRef to control where the as value went to make this possible.
I really like the as API (or sc:as with namespacing), and I would love it to work with more complex components.
I get that you can now use to make this work, but that means the consumer needs to know when to use as and when to use forwardedAs -- ideally the consumer should only need to know about as.
It's the same problem if you then wrap the styled component with another layer that does its own complex logic. This sort of thing is very common and why we have to move toward partitioning props off per use case so they don't clash and lead to this sort of confusion.
Basically need to stop thinking about this so much as styled-components being the uppermost component layer and only looking one level deep, because in reality it could be anywhere in the component stack with arbitrary props passing by/through it.
It's the same problem if you then wrap the styled component with another layer that does its own complex logic.
I would imagine it would be the responsibility of the wrapping layer to handle that as well. That's how it works with refs.
Basically need to stop thinking about this so much as styled-components being the uppermost component layer and only looking one level deep, because in reality it could be anywhere in the component stack with arbitrary props passing by it.
So are namespaced props automatically passed down with something like context? Or are they still props that need to be spread at each level?
Or are they still props that need to be spread at each level?
Still have to be spread, as a best practice if you're implementing a layer in a component tree you should always spread unknown props to the next layer down and only extract the props you need for your use case.
With it forwardedAs can go away and be replaced by sc:as.
Isn't it as that would be replaced by sc:as, and forwardAs by an unprefixed as?
Is it not in your interest to allow directly defining styled components that are effectively "transparent" with any other react/s-c component?
Absolutely, and the proposed namespacing accomplishes that elegantly.
It really doesn't though, on the contrary. Namespacing props moves the burden of handling prop filtering to the component user, who will have to be aware of the component's implementation details to know when and when not to use a prefix.
This may be fine for one-off components but it's not a viable solution for component library creators who will have to create wrapper components that hide away the prefixes (e.g. map primary to sc:primary) to prevent exposing implementation details in the library API.
I honestly think you're betting on the wrong horse with this. I feel like I'm asking for a screw driver and being told "wait while we work on this sledge hammer, it'll totally take care of your screws, too". To me this approach even conflicts with styled-components' core philosophy which was all about encoding styling best practices in the library API even if that meant imposing some limitations on what people can do. No component should need more than one as prop (if they do, it's due to composability limitations). But you won't convince me that instantiating a component instance with both an as and a forwardedAs prop isn't an anti-pattern (or an escape hatch if we're being generous) that's quite hard to wrap your head around if you want to understand what's being rendered in the end.
I absolutely see the value of prefixes for ephemeral props like ref and key (which could be react:ref and react:key), but handling all styling props like this will create more complexity than we have even now. Not all props that affect styling & layout will be only for s-c to consume (think props that affect which elements are rendered), so to component users the distinction between the two will seem arbitrary. Wrapper components will be needed more than ever if you go through with this.
Basically need to stop thinking about this so much as styled-components being the uppermost component layer and only looking one level deep, because in reality it could be anywhere in the component stack with arbitrary props passing by/through it.
Going back to the "encoding best practices" point, do you really think exposing all the complexity that may be hidden in any given component in its API is the way forward?
Why don't you instead give us the tools to define statically how each component layer handles its props? (Just like you'd do when composing vanilla React components.)
Defining prop forwarding rules along with the component definition would be BY FAR the most robust & predictable solution here, no matter how many levels deep you wrap your components.
The main problem with this approach is that your component is no longer a "styled" component. That means you can't reference the component by name in other styles.
Activity
ahtee commentedon Mar 15, 2019
This issue seems related to #2419. From that issue it seems that
asis WIP or being considered for an upcoming release.just-be-dev commentedon Mar 15, 2019
Just to chime in on this, we're having the exact same issue.
At Artsy we have an admittedly complex set of Typography components for controlling the text styles across our site. Ultimately it uses a styled wrapper with a functional component wrapper. Unfortunately using
ason that kills all the child styles.Here's a replication of the issue we're seeing https://codesandbox.io/s/182ovy2ylq. A more detailed write up of the issue and our temporary workaround can be found at artsy/palette#327.
diondiondion commentedon Mar 17, 2019
Related feature request: #2129
Basically, we need a way to tell styled-components to ignore the
asprop and just pass it on to the wrapped component.In your example, it would then be passed on to the
Buttoncomponent and everything would work as expected again. To visualise this here's an example of the prop being passed down explicitly:Unfortunately, the request wasn't received very well by the s-c team so far.
jiafra commentedon Jun 28, 2019
As of 4.3
diondiondion commentedon Jun 28, 2019
Oh my. I wish this had been implemented as a static option for the styled function, now there's one more prop to be aware of for component users while they'd arguably very rarely need to use both
asandforwardedAsat the same time. And they have to be aware of the problem to know when to use which prop.In other words: it seems like "prop-handling wrapper components all the way" continues to be something you have to live with when using styled components.
quantizor commentedon Jun 28, 2019
@diondiondion please refrain from commenting if you're not adding anything constructive to the conversation.
Actually it comes up often if you're using third party component libraries. Your use case isn't everyone's so please keep it classy and use whatever tool is best for your use case while we work toward a more permanent, sustainable solution.
diondiondion commentedon Jun 28, 2019
@probablyup I'd like to consider my comment constructive feedback, even if I didn't hide my disappointment with the new API.
I'm glad to hear you're working on a more permanent solution, but it's not like that's been communicated, maybe that's something that could be improved in the future. After all, all I'm seeing & reacting to is the final API, not the reasoning behind it or what's still to come.
quantizor commentedon Jun 28, 2019
I've spoken about JSX prop namespaces many times in these issues. I'm taking a crack at writing a babel transform for it which will be released separately from s-c and in v5 we'll look for the output and punch it out appropriately. This'll be a general-purpose pattern any JSX library can use and my goal will be to get the babel plugin added to the general React babel preset.
stonebk commentedon Jun 28, 2019
What are the drawbacks to following the same pattern that React did with refs?
It seems like we already have a well-established pattern for handling these types of props in React, why wouldn't we just follow that pattern?
diondiondion commentedon Jul 1, 2019
I know. However most of my nagging can be boiled down to "Please reduce the need for wrapper components when working with styled-components", and as far as I understand, JSX namespaces don't address that at all. They're basically a more powerful version of the
forwardedAsprop.Is it not in your interest to allow directly defining styled components that are effectively "transparent" with any other react/s-c component? ("Transparent" meaning interchangeable without having to change the component API, "direct" meaning without having to create wrapper components that work as adapters for said transparency.)
quantizor commentedon Jul 1, 2019
Not really, it entirely replaces the need to ever do filtering on more than a library basis by cordoning off sets of props into buckets. If you only want to use a prop for styling purposes, you'd put it in the
sc:namespace and we'd exclude it from being passed down to any component below. Other libraries would also be free to the do the same thing and chose their own namespace key. It's a much more elegant solution to this entire problem than anything that currently exists.With it
forwardedAscan go away and be replaced bysc:as. You could start writing arbitrary prop names (e.g.sc:color) that don't have to be camelCased anymore because with the namespace we know specifically that the prop is used for styling and not intended for the DOM or any underlying component.Absolutely, and the proposed namespacing accomplishes that elegantly. You still have to be aware of
classNamebeing passed to a wrapped custom component but that's just the nature of the beast.The end goal here is to make it explicit what's being used for styling and what isn't so component libraries will not have conflicts or have to ship extra filtering code to try and exclude stuff that shouldn't end up in the DOM.
stonebk commentedon Jul 1, 2019
@probablyup, sorry, but I don't think I understand how namespacing gets us any closer to fixing the original issue. It helps with prop filtering, but how does it help with this scenario:
With that component I can do the following:
That works great. Now if I have a component that defines some internal behavior:
The
asprop now breaks because it replaces the entire component implementation:If we were to have a
forwardAsfunction, it would solve this:I get that you can now use
<StrongButton forwardedAs="a">to make this work, but that means the consumer needs to know when to useasand when to useforwardedAs-- ideally the consumer should only need to know aboutas. As a library creator, it would be amazing if I had a mechanism like React'sforwardRefto control where theasvalue went to make this possible.I really like the
asAPI (orsc:aswith namespacing), and I would love it to work with more complex components.quantizor commentedon Jul 1, 2019
It's the same problem if you then wrap the styled component with another layer that does its own complex logic. This sort of thing is very common and why we have to move toward partitioning props off per use case so they don't clash and lead to this sort of confusion.
Basically need to stop thinking about this so much as styled-components being the uppermost component layer and only looking one level deep, because in reality it could be anywhere in the component stack with arbitrary props passing by/through it.
stonebk commentedon Jul 1, 2019
I would imagine it would be the responsibility of the wrapping layer to handle that as well. That's how it works with refs.
So are namespaced props automatically passed down with something like context? Or are they still props that need to be spread at each level?
quantizor commentedon Jul 1, 2019
Still have to be spread, as a best practice if you're implementing a layer in a component tree you should always spread unknown props to the next layer down and only extract the props you need for your use case.
diondiondion commentedon Jul 2, 2019
Isn't it
asthat would be replaced bysc:as, andforwardAsby an unprefixedas?It really doesn't though, on the contrary. Namespacing props moves the burden of handling prop filtering to the component user, who will have to be aware of the component's implementation details to know when and when not to use a prefix.
This may be fine for one-off components but it's not a viable solution for component library creators who will have to create wrapper components that hide away the prefixes (e.g. map
primarytosc:primary) to prevent exposing implementation details in the library API.I honestly think you're betting on the wrong horse with this. I feel like I'm asking for a screw driver and being told "wait while we work on this sledge hammer, it'll totally take care of your screws, too". To me this approach even conflicts with styled-components' core philosophy which was all about encoding styling best practices in the library API even if that meant imposing some limitations on what people can do. No component should need more than one
asprop (if they do, it's due to composability limitations). But you won't convince me that instantiating a component instance with both anasand aforwardedAsprop isn't an anti-pattern (or an escape hatch if we're being generous) that's quite hard to wrap your head around if you want to understand what's being rendered in the end.I absolutely see the value of prefixes for ephemeral props like
refandkey(which could bereact:refandreact:key), but handling all styling props like this will create more complexity than we have even now. Not all props that affect styling & layout will be only for s-c to consume (think props that affect which elements are rendered), so to component users the distinction between the two will seem arbitrary. Wrapper components will be needed more than ever if you go through with this.Going back to the "encoding best practices" point, do you really think exposing all the complexity that may be hidden in any given component in its API is the way forward?
Why don't you instead give us the tools to define statically how each component layer handles its props? (Just like you'd do when composing vanilla React components.)
Defining prop forwarding rules along with the component definition would be BY FAR the most robust & predictable solution here, no matter how many levels deep you wrap your components.
stonebk commentedon Jul 2, 2019
Namespacing aside, it seems like we still need a mechanism for forwarding
as.stonebk commentedon Aug 28, 2019
I create a little proof of concept for a forwardAs API:
https://github.com/zillow/styled-forward-as
Here you can see it in action:
https://codesandbox.io/s/still-fast-kkpjl
The main problem with this approach is that your component is no longer a "styled" component. That means you can't reference the component by name in other styles.