Skip to content

add reserved "sc" prop, handling - #1682

Closed
quantizor wants to merge 4 commits into
styled-components:developfrom
quantizor:add-protected-prop
Closed

quantizor wants to merge 4 commits into
styled-components:developfrom
quantizor:add-protected-prop

Conversation

@quantizor

@quantizor quantizor commented Apr 12, 2018 •

Copy link
Copy Markdown
Contributor

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

const Thing = styled.div`
  color: ${p => p.sc.color};
`;

<Thing sc={{ color: 'red' }} />

@quantizor
quantizor requested review from kitten and mxstbr April 12, 2018 04:40
@quantizor
quantizor force-pushed the add-protected-prop branch from d94384e to 5558deb Compare April 12, 2018 04:43
@quantizor quantizor changed the title add reserved sc attribute handling add reserved sc attribute, handling Apr 12, 2018
Comment thread src/models/StyledComponent.js Outdated
static componentStyle: Object
static warnTooManyClasses: Function
static propTypes = {
sc: PropTypes.object,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@quantizor quantizor Apr 12, 2018 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/models/StyledComponent.js Outdated
if (
process.env.NODE_ENV !== 'production' &&
this.props.sc &&
Object.prototype.toString.call(this.props.sc) !== '[object Object]' &&

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is potentially a really expensive check, even if dev which might throw some people off; also see above :)

@quantizor quantizor Apr 12, 2018 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@quantizor quantizor Apr 12, 2018 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have an opinion on this @mxstbr?

Comment thread src/models/StyledComponent.js Outdated
)}. this is now a reserved attribute in styled-components and will not be passed through to the underlying component or DOM node`
)

warnedImproperSCPropUsage = true

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a once utility so this can be extracted which would be nice here, right?

@mxstbr mxstbr left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How much of a bundle-size change will this bring to the table?

@quantizor

Copy link
Copy Markdown
Contributor Author

@mxstbr near-zero since we already have to consume PropTypes for the old context system

@quantizor
quantizor force-pushed the add-protected-prop branch 3 times, most recently from 209d253 to b6c3371 Compare April 16, 2018 04:30
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
@quantizor
quantizor force-pushed the add-protected-prop branch from b6c3371 to d27c2eb Compare April 16, 2018 04:30
@quantizor

Copy link
Copy Markdown
Contributor Author

@kitten removed all the controversial bits

@quantizor quantizor changed the title add reserved sc attribute, handling add reserved "sc" prop, handling Apr 16, 2018
kitten
kitten previously approved these changes Apr 17, 2018
@gaearon

gaearon commented Apr 18, 2018 •

Copy link
Copy Markdown
Member

I don't think sc is a good name for this.

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' }} />

@mxstbr

mxstbr commented Apr 18, 2018

Copy link
Copy Markdown
Member

That sounds sensible to me @gaearon

@quantizor

Copy link
Copy Markdown
Contributor Author

@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.

@gaearon

gaearon commented Apr 18, 2018

Copy link
Copy Markdown
Member

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.

@kitten

kitten commented Apr 18, 2018

Copy link
Copy Markdown
Member

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 sc can be improved upon.

That said, variant seems pretty good to me; The only problem would be that it's not quite suitable for every problem that one can throw at this API, but it's certainly suitable for the ones we'd like to be thrown at it 👍

@quantizor quantizor added the 4.0 label Apr 19, 2018
@quantizor

Copy link
Copy Markdown
Contributor Author

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 😲

@diondiondion

Copy link
Copy Markdown

I'd love to finally put an end to the issue that is being addressed here

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?

@quantizor

Copy link
Copy Markdown
Contributor Author

@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.

@diondiondion

Copy link
Copy Markdown

@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:

I assume you're not considering anymore to add a method for specifying (near the component definition) which props not to pass through?

@quantizor

Copy link
Copy Markdown
Contributor Author

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.

@gaearon

gaearon commented Apr 19, 2018

Copy link
Copy Markdown
Member

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.

@diondiondion

Copy link
Copy Markdown

Adding a prop to specify which props to not pass along is interesting.

Glamourous has that option, it's called filteredProps. You just pass it an array of prop names to filter (see glamourous PR).

glamorous(ChildComponent, {
  filterProps: ['foo', 'bar'],
})

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 filterProps(({props, to, filter, ...rest}) => rest), but that comes with its own downsides (even more typing, creates unused vars which linters don't like, it's less focused on this specific issue & can be (ab)used for other stuff).

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. ;)

@kitten

kitten commented Apr 19, 2018

Copy link
Copy Markdown
Member

@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 validAttrs quite so soon even with a new approach

@quantizor

Copy link
Copy Markdown
Contributor Author

Yeah I wasn’t planning on landing this until 4.0 anyway. Sounds like more discussion is needed.

@quantizor

quantizor commented Jun 22, 2018 •

Copy link
Copy Markdown
Contributor Author

Another possibility that came up during a convo:

Make the special prop "theme" and then shallow merge it against the outer theme if it exists.

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 blue

Pros

  • Only one place to look for theming-related props: props.theme
  • Very easy to do a local override of props.theme without having to use an interim <ThemeProvider> wrapper
  • Less chance of prop collision with other libraries & setups since this is an established pattern
  • Clear delineation of what is a "visual" prop vs what are custom attributes and/or real attributes

Considerations

  • props.theme must always be an object (I haven't seen people use non-object themes in the wild, but it's possible)
  • automatic shallow merge might be confusing
  • The global theme passed down via would pollute the "prop namespace" for nested components. Basically, if your global theme contains any properties of the same name as any one of your styled-component's props used for styling, there'll be overlap & conflicts & potentially accidentally applied styles.
  • no shorthand for boolean theme props <Box fullWidth />

@Andarist

Copy link
Copy Markdown
Contributor

Adding a con to the list above - no shorthand for boolean theme props (as in <Box fullWidth />)

@quantizor quantizor mentioned this pull request Aug 21, 2018
8 of 14 tasks
@diondiondion

Copy link
Copy Markdown

Make the special prop "theme" and then shallow merge it against the outer theme if it exists.

Another con for using the theme prop: The global theme passed down via <ThemeProvider> would massively pollute the "prop namespace" for nested components. Basically, if your global theme contains any properties of the same name as any one of your styled-component's props used for styling, there'll be overlap & conflicts & potentially accidentally applied styles.

A global theme should be something you only access intentionally, and this solution blurs the line between component props & global theme props.

@quantizor

quantizor commented Sep 11, 2018 •

Copy link
Copy Markdown
Contributor Author

@diondiondion added your point to the comment. That's true. We could do something like reassign the theme from context to theme.context.

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>

@stefee

stefee commented Sep 11, 2018

Copy link
Copy Markdown
Member

Why does the component theme have to pollute the main theme? I would see it working like this, where the outer Component instance has a blue background, and the inner Component goes back to being red again as the theme variation isn't passed down.

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 ThemeProvider? I'm not sure what the value of the prop is at that point, but maybe I'm missing context.

@quantizor

Copy link
Copy Markdown
Contributor Author

@srilq yeah your example is the original proposal for how it should work. I agree with you.

@morajabi

morajabi commented Oct 1, 2018 •

Copy link
Copy Markdown
Member

I love the theme proposal.

Anyone using SC doesn't use theme prop in their codebase, so totally safe.
Also, it's familiar to the eye as we already heavily use p => p.there.x in styled components.

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 :)

@quantizor

Copy link
Copy Markdown
Contributor Author

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.

@quantizor

Copy link
Copy Markdown
Contributor Author

Closing in favor of #2093

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants