Repository navigation
Conversation
d94384e to
5558deb
Compare
| static componentStyle: Object | ||
| static warnTooManyClasses: Function | ||
| static propTypes = { | ||
| sc: PropTypes.object, |
There was a problem hiding this comment.
Let’s not enforce this so anything can be passed in 😅 I think that’d be great for theme switching with styled theming at least for some users
There was a problem hiding this comment.
Hmm idk. If you make it an any situation, then it becomes hard for us to perform any kind of useful dev-time checks to verify you're using it correctly.
| if ( | ||
| process.env.NODE_ENV !== 'production' && | ||
| this.props.sc && | ||
| Object.prototype.toString.call(this.props.sc) !== '[object Object]' && |
There was a problem hiding this comment.
This is potentially a really expensive check, even if dev which might throw some people off; also see above :)
There was a problem hiding this comment.
You think so? Should be fast enough... I'd rather get a useful warning than no warning... this becomes especially important if someone happened to be using an "sc" prop in their codebase already.
There was a problem hiding this comment.
Yea, but an object is an easy false positive anyway. And that comes on top of this prop name being highly unlikely in my opinion as everyone would assume it being related to us.
So if we assume an object we just constrain the user and we’re not reliably detecting misuse anyway :/ seems like a suboptimal compromis
There was a problem hiding this comment.
prop name being highly unlikely in my opinion as everyone would assume it being related to us
Maybe. I've learned never to assume things in this industry, haha.
Yea, but an object is an easy false positive anyway.
Not sure I follow. There's no surefire way to detect if it's being used correctly, but at least verifying it's a plain object seems like an OK plan over doing nothing. I originally was thinking about trying to detect if the prop was used inside an interpolation, but that would be hard to do without wrapping it in an ES6 Proxy, which would be a compatibility nightmare.
There was a problem hiding this comment.
Well, you could define a getter on the interpolation props we are generating, at least in dev. But I disagree that doing sth here is better than nothing ;) this would unnecessarily restrict the API to lock out theming/type switches that are already really popular :)
| )}. this is now a reserved attribute in styled-components and will not be passed through to the underlying component or DOM node` | ||
| ) | ||
|
|
||
| warnedImproperSCPropUsage = true |
There was a problem hiding this comment.
We have a once utility so this can be extracted which would be nice here, right?
mxstbr
left a comment
There was a problem hiding this comment.
How much of a bundle-size change will this bring to the table?
|
@mxstbr near-zero since we already have to consume |
209d253 to
b6c3371
Compare
since it doesn't appear that JSX namespace attributes will land in the near future, moving to use of a reserved, memorable prop allows us to drop the attribute whitelist in a future major version
b6c3371 to
d27c2eb
Compare
|
@kitten removed all the controversial bits |
|
I don't think Component props are its public API. If a component uses SC, it used to be its implementation detail, but now it’s exposed. This makes it harder to swap SC with other implementations, and leaves people who migrate away from SC with technical debt in the form of a weird property that they might have to keep in their API for backward compat or because they don’t have time to refactor. It also makes the API of any SC slightly more confusing. I understand the temptation to save typing but I’m not sure it’s the right instinct here. If you go this road I would suggest picking a name that has some meaning outside of SC. For example: const Thing = styled.div`
color: ${p => p.variant.color};
`;
<Thing variant={{ color: 'red' }} /> |
|
That sounds sensible to me @gaearon |
|
@gaearon how would you address the technical requirement of trying to avoid a prop that may already be in their codebase? This change effectively punches out that prop such that the underlying component or JSX would not receive it. That’s why I specifically picked “sc”, since it’s unlikely it would be used for other purposes and is memorable. In terms of switching libraries, that’s a moot point unless other libs implement similar functionality, and even then is very easy to codemod. |
|
I think it’s more important to create a good API for all future users than to lock them into an intentionally obscure name because of past users. You’re going to do a breaking change anyway. |
|
Definitely agreed here; I'd love to finally put an end to the issue that is being addressed here, and I can accept that not everyone will be happy (heck, I think it's fine as it is), but That said, |
|
Going to think on the naming more... I'm sure there's a middle ground that's a little more unique. @mxstbr dropping the validAttr code after this change reduces the bundle size by 2.5kB gzipped 😲 |
I still consider this to be more of a lazy workaround than a real solution, and it kills a big part of styled-component's appeal to me. :( I assume you're not considering anymore to add a method for specifying (near the component definition) which props not to pass through? And if not, do you really expect any shared component library to make your reserved prop part of their public API? Or is your position that if we want to hide the reserved prop, we should "just" create an intermediate component that maps top-level props to nested props? |
|
@diondiondion why would this kill some appeal? You can still use regular props and reference them in your styles... the only difference would be “variant” or whatever isn’t passed through to the underlying component/DOM. Purely a way to opt out of garbage in the DOM. |
|
@probablyup Isn't the idea to get rid of the prop whitelist? So, correct me if I'm wrong, but it seems like the choice will be between using the variant prop and having garbage in the DOM, which of course isn't acceptable normally, so there's not much of a choice in reality. That's why I was asking:
|
|
Yes, to get rid of the whitelist and make our prop behavior a little less magical / maintenance-heavy. As it is, the current strategy is kind of cruddy because it misses all-lowercase prop names used for styling. Adding a prop to specify which props to not pass along is interesting. I could see that evolving into a community pattern perhaps. The nice thing is that can be added as an enhancement down the line, since it would involve a lot of manual setup. |
|
Yeah, I understand the intent, but the more “static” the final scheme is, the better. Anything that involves dynamic transformations, heuristics based on names or prop order, will cause problems later down the road. |
Glamourous has that option, it's called In my mind this would be the perfect solution for styled-components as well. It requires a little bit more typing when creating the component, but lets us define a component's API freely, with no downsides. Plus it'll work equally well with html tags and wrapped components and is easy to teach. When I suggested this last year, @gaearon has pointed out that he'd prefer an approach where the filtered props aren't passed as strings in an array, but rather a function like So, yeah.. sorry if I'm derailing the PR discussion a bit, I just care about this issue a lot and thought it'd be worth reiterating my concerns. If you want to read more about this, check out issue #439, but it is a bit of a long read. ;) |
|
@diondiondion the issue in #439 is frankly hopeless as it's gone through every single option with every single opinion. That's not saying that this PR is useless or what we'll definitely go for, but at this point it's a tough call either way. @probablyup Let's park this for a bit please 😉 I think it's been clear that we need another angle to this — which I don't believe will be easy — or some time to decide whether we'll go for this temporarily. Either way, I'm not sure whether we can get rid of |
|
Yeah I wasn’t planning on landing this until 4.0 anyway. Sounds like more discussion is needed. |
|
Another possibility that came up during a convo: Make the special prop Example: const StyledButton = styled.button`
background-color: ${p => p.theme.backgroundColor};
color: ${p => p.theme.color};
`;
// usage
// <StyledButton theme={{ color: 'red' }} />
// with a ThemeProvider, the local theme prop is shallow applied...
<ThemeProvider theme={{ color: 'green', backgroundColor: 'blue' }}>
<StyledButton theme={{ color: 'red' }} />
</ThemeProvider>
// the button would be red
// and the background would remain bluePros
Considerations
|
|
Adding a con to the list above - no shorthand for boolean theme props (as in |
Another con for using the A global theme should be something you only access intentionally, and this solution blurs the line between component props & global theme props. |
|
@diondiondion added your point to the comment. That's true. We could do something like reassign the theme from context to Then you'd use it like this: const Component = styled.div`
color: ${p => p.theme.context.color};
background: ${p => p.theme.background};
`
<ThemeProvider theme={{ color: 'red' }}>
<Component background="blue" />
</ThemeProvider> |
|
Why does the component theme have to pollute the main theme? I would see it working like this, where the outer const Component = styled.div`
color: ${p => p.theme.color};
background: ${p => p.theme.background};
`
<ThemeProvider theme={{ color: 'red', background: 'red' }}>
<Component theme={{ background: 'blue' }}>
<Component />
</Component>
</ThemeProvider>If the component theme does pollute the main theme, then why not just use a nested |
|
@srilq yeah your example is the original proposal for how it should work. I agree with you. |
|
I love the Anyone using SC doesn't use I second that, in my opinion, it should never affect the global theme for other styled components. P.S: It would be nice to think about the typing upfront if that's possible :) |
|
I'm currently leaning toward a prefix-based approach for denoting private variables. Either "$" or "_". I wanted to do "#" to mirror what's going on with private class fields but that unfortunately is a syntax error. While "theme" would be nice, it definitely is more cumbersome in some ways and makes it less elegant to add one-off theme properties. |
|
Closing in favor of #2093 |
since it doesn't appear that JSX namespace attributes will land
in the near future, moving to use of a reserved, memorable prop
allows us to drop the attribute whitelist in a future major version