styled-components / styled-components

Visual primitives for the component age. Use the best bits of ES6 and CSS to style your apps without stress 💅
https://styled-components.com
MIT License
40.28k stars 2.49k forks source link

theme prop is not passed down to styled component #4182

Open jakubsuszynski opened 10 months ago

jakubsuszynski commented 10 months ago

Environment

npx envinfo --system --binaries --npmPackages styled-components,babel-plugin-styled-components --markdown

System:

Steps to reproduce

Create component which accepts theme prop, for example enum or string Style it using styled-components Pass prop theme to styled component

Expected Behavior

Prop passed to styled component should work

Actual Behavior

Theme prop is not getting passed. Probably because styled-component expects it to be Theme object, instead of anything else. Currently I'm using an external dependency with component which accepts theme prop. I cannot style it with styled components, because string theme prop is not being passed down.

bigkho commented 9 months ago

No external dependencies are required. Theme props work differently with styled-components. You can find the documentation here.

jakubsuszynski commented 9 months ago

No external dependencies are required. Theme props work differently with styled-components. You can find the documentation here.

You don't understand. You can check my codesandbox link for reference. I have an external component which requires theme prop, which is a string ("red", "blue" etc). Besides that, I want to style it using styled-components. After wrapping it in styled, my theme prop is not being passed to that external component, because it's taken by styled wrapper...

bigkho commented 9 months ago

You're correct, I see what's going on. It seems that styled-components may need something to distinguish between their own theme prop. However, it may be inconsistent in the first place to use a theme in an non-styled-component and then wrapping it with styled. In your codesandbox example, simply changing theme to any other prop name will suffice. Ideally, when working with themes with styled-components, you're using a ThemeProvider where you can use the theme prop to override colors. If you're attempting to use theme in the context you've provided, it's likely just worth naming it something else to avoid the issue entirely. Please let me know if there's something I am missing here, and I will look into styled-components to see if there's something better that could be done if this is problem.

jakubsuszynski commented 9 months ago

You're correct, I see what's going on. It seems that styled-components may need something to distinguish between their own theme prop. However, it may be inconsistent in the first place to use a theme in an non-styled-component and then wrapping it with styled. In your codesandbox example, simply changing theme to any other prop name will suffice. Ideally, when working with themes with styled-components, you're using a ThemeProvider where you can use the theme prop to override colors. If you're attempting to use theme in the context you've provided, it's likely just worth naming it something else to avoid the issue entirely. Please let me know if there's something I am missing here, and I will look into styled-components to see if there's something better that could be done if this is problem.

The case is that component is external, so I cannot change name of it. Workaround would be to create wrapper component which'd pass this prop with different name, but yeah - it's an workaround. The problem is that it's used really widely in my case and replacing it would be a pain in the ass ;)

DmitryBerdnikov commented 9 months ago

I faced the same problem. Styled-components version 6 doesn't pass prop name theme to styled component .

Working solution is wrapping components with different prop name, like buttonTheme and then pass buttonTheme as theme to other component, but it's frustrating

Maybe this code does it

Maybe it's better to make configurable theme prop name to styled components using StyleSheetManager

nitzcard commented 4 months ago

I also have this problem in a third party library where I can't change the prop name.