Skip to content
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

Fix don't omit theme at runtime #4129

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

takurinton
Copy link
Contributor

#4074

Include the theme in the execution props.

Some UI component libraries require a theme at runtime. (e.g. react-select)
If you skip here, you will not be able to refer to the theme when overwriting the theme, resulting in a runtime error.

In fact, if you try using the codesandbox in the issue comment, A runtime error occurs because the theme cannot be referenced.

Cannot read properties of undefined (reading 'spacing')

@takurinton takurinton changed the title Fix include theme to execution props Fix don Aug 14, 2023
@takurinton takurinton changed the title Fix don Fix don't omit theme at runtime Aug 14, 2023
@@ -143,7 +143,7 @@ function useStyledComponentImpl<Props extends object>(
if (context[key] === undefined) {
// Omit undefined values from props passed to wrapped element.
// This enables using .attrs() to remove props, for example.
} else if (key[0] === '$' || key === 'as' || key === 'theme') {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd rather not do this, can use useTheme to get the theme in your component if needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, I didn't know how...
Could you please show me an example??

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link

Choose a reason for hiding this comment

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

@probablyup I think there is misunderstanding about the intention of this change.
It is not about to acquire the theme via the theme prop or via the useTheme hook. It is not about the styled-components theming mechanism at all.
It is about other libs relying on the "theme" prop which is now "killed" by the styled-components wrapper.

Many internal components of react-select passing "theme" props around. When you style them via styled-components, this prop is gone and we ran into a runtime error.
This was also mentioned in #4074.

So changing the line 146 to not remove the "theme" prop fixes that issue with react-select.

Copy link

Choose a reason for hiding this comment

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

@quantizor Could we please have this PR added or implement other change to fix the #4074. issue due to which it is impossible to use react-select with styled components?

Copy link
Contributor

Choose a reason for hiding this comment

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

The only thing I'm not sure about is what should happen if a theme prop is passed that is incompatible with the styled-components theme. The styles still need the context theme to work, whereas the theme being described here has nothing to do with styled-components.

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe the theme should be a second argument to style interpolations instead of being lumped in with all props. That would disambiguate the two, but we wouldn't be able to remove the s-c theme from the props object until v7 since it would be a breaking change.

Copy link
Contributor

Choose a reason for hiding this comment

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

Further complicating this, we allow the s-c theme to be overridden via theme prop as a general feature...

Copy link

Choose a reason for hiding this comment

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

@quantizor Not sure that I understand in details what was changed in s-c v6 but it worked in v5. Here is the sandbox. If you downgrade s-c to 5.3.11 there is no any error.

@quantizor
Copy link
Contributor

I prepared an alternate implementation that will not leak the theme prop in normal scenarios. Passing theme always would be kind of a breaking change for v6 since the current behavior has been established, but using attrs to feed an override theme prop seems like a reasonable workaround.

@AhmedMuhammedElsaid
Copy link

ThanQ

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.

None yet

5 participants