-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
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
[zero] Add useThemeProps
processor
#40648
Conversation
Netlify deploy previewhttps://deploy-preview-40648--material-ui.netlify.app/ Bundle size reportDetails of bundle changes (Toolpad) |
useThemeProps
processoruseThemeProps
processor
…/fix-defaultprops
What's the use case to have RSC compatible components? We use the Badge in the PR, but I guess it's not the objective (I don't see why a developer would want this with his component, there would be no state change animations, the onClick listener wouldn't works, etc.), but I guess the Badge is meant as a supplement for layout components. For instance, it makes a lot of sense to me with a Now, I don't think this change is needed, we can have context (maybe with nesting support but not sure) emotion-js/emotion#2978 (comment).
But them, how can we support theme nesting? I don't see this mentioned, but I think it matters. For example, how is the documentation of Material UI supposed to be able to show components in their default form while the docs has a MUI branded theme? At first sight, I would recommend:
This way, we get:
Opportunity moved to #43443 |
Motivation
Currently, all components are client components and will never be server components because they will always access to the theme context to look for default props (via system
useThemeProps
).This PR aims to remove the usage of React context to make some components RSC compatible while having backward compatible with emotion.
How
Lift the current
useThemeProps
inside a component to the outer scope so that zero-runtime can transform the code by creating an adapter function calledcreateUseThemeProps
.At build-time, zero-runtime will replace the
createUseThemProps
argument with thedefaultProps
object defined in the theme config that passed to zero runtime.After the transformation, the Badge will be:
At runtime, the
createUseThemeProps
from zero runtime will merge props with the same logic as in system'suseThemeProps
.Those components that haven't used
createUseThemeProps
, will continue to use React context as is.In summary, we remove the React context usage by providing the defaultProps directly to components at build-time.
Trade-off
Some components, e.g. Select, allow developers to provide a React component for replacing icons. This is possible in React context approach but I won't work with zero-runtime because it is not serializable. cc @brijeshb42