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

[styled-engine-sc] Fix types for css function of styled-engine-sc #40640

Closed

Conversation

delijah
Copy link

@delijah delijah commented Jan 16, 2024

@mui-bot
Copy link

mui-bot commented Jan 16, 2024

Netlify deploy preview

https://deploy-preview-40640--material-ui.netlify.app/

Bundle size report

No bundle size changes (Toolpad)
No bundle size changes

Generated by 🚫 dangerJS against f2d8503

@delijah delijah force-pushed the fix-styled-engine-sc-css-func-types branch from 35597b7 to f77674d Compare January 16, 2024 15:46
@mnajdova
Copy link
Member

Can you please create a sandbox using the build packages from the PR, that will validate the fix? These are the package version:

You can find them here: https://ci.codesandbox.io/status/mui/material-ui/pr/40640/builds/461016

@zannager zannager added the package: styled-engine-sc Specific to styled-components label Jan 17, 2024
@delijah
Copy link
Author

delijah commented Jan 17, 2024

Can you please create a sandbox using the build packages from the PR, that will validate the fix? These are the package version:

To be honest: This codesandbox situation, when using styled-engine-sc, overwhelms me a bit. The parcel version does not work correctly and if i create a devbox myself, .. i could also not figure out how to get this working. Even created an issue some time ago: #40497.

Is there any existing, working, codesandbox out there, using typescript and styled-engine-sc, which i could re-use, in order to satisfy your request?

@delijah
Copy link
Author

delijah commented Jan 17, 2024

Can you please create a sandbox using the build packages from the PR, that will validate the fix? These are the package version:

Ok, somehow i was able to create something that might work 🙂

https://codesandbox.io/p/devbox/styled-engine-sc-css-function-types-49tz5g?file=%2Fsrc%2FApp.tsx%3A7%2C12

@delijah delijah force-pushed the fix-styled-engine-sc-css-func-types branch from ef9f918 to f2d8503 Compare January 18, 2024 19:07
@delijah
Copy link
Author

delijah commented Jan 24, 2024

Let me know if i can be of any help in order to get this merged ? @mnajdova

styles:
| TemplateStringsArray
| CSSObject
| InterpolationFunction<ThemedStyledProps<object, DefaultTheme>>,
Copy link
Member

Choose a reason for hiding this comment

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

Won't using DefaultTheme in this type mean that the theme in the css prop will have the wrong type? It won't be the Material UI's theme. Ideally I feel like we should export from here a "generator" for this type and re-exprot it from Material UI using the Material UI's Theme. We would do the same for the other package as well (@mui/styled-engine).

Copy link
Author

Choose a reason for hiding this comment

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

I basically agree. That is what my other PR is actually doing/solving.

The issue here is, the actual theme variable we receive within the css-function, is not the material theme. We have to use theme[THEME_ID] in order to access the MUI theme. So this problem already exists currently. But at least, with this fix/PR, we have solved the typing issue for variables. Step 2 would be getting the value and types of the theme prop right (which is solved in the other PR mentioned above).

Copy link
Author

Choose a reason for hiding this comment

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

As long as we do not fix the theme variable thing properly (other PR), i do not know what other type we could use here.

Copy link
Contributor

Choose a reason for hiding this comment

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

I tried code in your repo and added a theme with a ThemeProvider. Added this extra code (disabled type-checking temporarily)

const complexMixin = css<ComponentProps>`
  color: ${(props) => {
    console.log(props);
    return props.$whiteColor ? "white" : "black";
  }};
`;

const StyledComp = styled("div")<ComponentProps>`
  /* This is an example of a nested interpolation */
  ${(props) => (props.$complex ? complexMixin : "color: blue")}
`;

and I found that the theme in props is actually logged correctly and does not need to be accessed using theme[THEME_ID]. Do you have a repro of the above case?

Screenshot 2024-02-06 at 1 21 04 AM

Copy link
Member

Choose a reason for hiding this comment

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

@delijah I got your point. Would it make sense to do it in the same PR? This way we prevent the scenario where 2 PRs are not in the same release.

From what I see, both of your PRs do not contain huge changes so I think they can be combined.

@mnajdova
Copy link
Member

mnajdova commented Feb 5, 2024

Tagging @siriwatknp and @brijeshb42 to help with pushing this, together with https://github.com/mui/material-ui/pull/40656/files to the finish line. I won't have bandwidth to look into it this week, and I don't want to need to postpone it any further.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
package: styled-engine-sc Specific to styled-components
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[styled-engine-sc] Types are not taken into account when using styled function with a template string
7 participants