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

[muiStyled] Support default theme when none is available #22791

Merged
merged 40 commits into from Oct 2, 2020

Conversation

mnajdova
Copy link
Member

@mnajdova mnajdova commented Sep 28, 2020

This PR adds support for default theme in muiStyled, utility in @material-ui/core.

In addition to this, ThemeContext is exported from both @material-ui/styled-engine and @material-ui/styled-engine-sc, which is used in our ThemeProvider. This simplifies the DX when using muiStyled, in means that developers no longer need to add in addition to our ThemeProvider the ThemeProvider from their styled engine - emotion or styled-components.

@mui-pr-bot
Copy link

mui-pr-bot commented Sep 28, 2020

@material-ui/core: parsed: +0.25% , gzip: +0.26%
@material-ui/lab: parsed: +0.28% , gzip: -0.12% 😍

Details of bundle changes

Generated by 🚫 dangerJS against dcc97fa

Copy link
Member

@eps1lon eps1lon left a comment

Choose a reason for hiding this comment

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

I don't really understand how customStyled relates to styled.

@@ -1 +1,2 @@
export { default } from '@emotion/styled';
export * from 'emotion-theming';
Copy link
Member

Choose a reason for hiding this comment

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

We should specify what we need in case emotion-theming adds new modules that we don't need.

Copy link
Member

Choose a reason for hiding this comment

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

What problem does emotion-theming solve?

Copy link
Member Author

Choose a reason for hiding this comment

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

By adding the emotion's/styled-components' ThemeProvider in our ThemeProvider component, developers no longer need to specify ThemeProvider from emotion/styled component together with ours. Also, in the styled utility when custom theme is used, it will automatically be available on props.theme. In addition to this, when no ThemeProvider is used, the styled utility uses the defaultTheme

Copy link
Member

Choose a reason for hiding this comment

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

What if they already use emotion-theming?

Copy link
Member

Choose a reason for hiding this comment

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

I think that using a different ThemeProvider (enforcing the usage of two) makes incremental adoption easier. Developers don't have to worry about conflicts of theme structure with existing usage of emotion/sc.

However, for using the css prop, it seems required https://emotion.sh/docs/theming#css-prop.

It seems that theme-ui imports the ThemeContext of emotion directly (without emotion-theming), https://github.com/system-ui/theme-ui/blob/af57d4ce97c0e8a3289115f33cef1d17bc65adad/packages/core/src/index.ts#L3. They also have emotion as a direct dependency.

Copy link
Member Author

Choose a reason for hiding this comment

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

Just a clarification, for our components, like Slider, Button, muiStyled + our ThemeProvider is working, it won't work for the scenarios of using muiStyled + div, span or other primitive elements as we do not have access to change the behavior there.

Copy link
Member

Choose a reason for hiding this comment

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

Oh, I see, thanks for the clarification. +1 for including the styled-engine theme provider in Material-UI's ThemeProvider component. It's simpler. For the theme structure conflict, developers can workaround the issue by nesting <MuiThemeProvider> > <Emotion ThemeProvider>.

The only point I would suggest to explore is: Using the ThemeContext exported from @emotion/core vs. emotion-theming.

Copy link
Member Author

Choose a reason for hiding this comment

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

The only point I would suggest to explore is: Using the ThemeContext exported from @emotion/core vs. emotion-theming.

Yeah, let me spend some time on this, just to make sure I am not missing something 👍

Copy link
Member Author

Choose a reason for hiding this comment

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

Refactored to use only the ThemeContext (both emotion & styled-components expose this). Tested with both engines, tests are passing, so I think everything is covered. No new dependencies added with this 👍

Copy link
Member

Choose a reason for hiding this comment

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

No new dependencies added with this

Nice :).

packages/material-ui/src/styles/muiStyled.d.ts Outdated Show resolved Hide resolved
packages/material-ui/src/styles/customStyled.d.ts Outdated Show resolved Hide resolved
@mnajdova mnajdova changed the title [styled] Add customStyled utililty in core [styled] Add styled utililty in core Sep 29, 2020
Copy link
Member

@oliviertassinari oliviertassinari left a comment

Choose a reason for hiding this comment

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

I think that it would be been better to name the new helper experimentalStyled, as Sebastian proposed, so we could iterate on it, without breaking developers' using v5-alpha code until we find something we happy with. It also removes the need to consider the migration story from the changes here, to be handled later on.

packages/material-ui/src/styled/styled.js Outdated Show resolved Hide resolved
packages/material-ui/src/styles/styled.js Outdated Show resolved Hide resolved
@mnajdova
Copy link
Member Author

I think that it would be been better to name the new helper experimentalStyled, as Sebastian proposed, so we could iterate on it, without breaking developers' using v5-alpha code until we find something we happy with. It also removes the need to consider the migration story from the changes here, to be handled later on.

Let's do that, I have too many changes on this PR that are not really related to the problem I am solving. We can do the rename/deprecation later on.


// with yarn
yarn add @material-ui/styles
yarn add @material-ui/styles @emotion/core @emotion/styled
Copy link
Member

Choose a reason for hiding this comment

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

Does the styles package really need these new dependencies? I was under the assumption that we would try not to interfer with it, progressively making it legacy, and dropping it.

Copy link
Member Author

Choose a reason for hiding this comment

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

The only reason there were added is because the ThemeProvider is in this package. Once we move it out of the package we can drop these dependencies

Copy link
Member

Choose a reason for hiding this comment

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

We only document the import of ThemeProvider from the core. Would it work if we moved the theme provider nesting to the core?

My concern is about the migration experience for v4 developers that are using JSS. We could imagine having developers migrate their v4 imports from @material-ui/core/styles to @material-ui/styles and have still everything working. We could no longer have @material-ui/styles as a dependency in @material-ui/core. Would that work? Do you have an alternative story in mind? A migration from @material-ui/core/styles to react-jss won't be as smooth.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah I see what is the concern. Let me try then to migrate the nesting to the core 👍

Copy link
Member Author

Choose a reason for hiding this comment

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

Done 👍

mnajdova and others added 3 commits October 1, 2020 12:54
Co-authored-by: Sebastian Silbermann <silbermann.sebastian@gmail.com>
Co-authored-by: Sebastian Silbermann <silbermann.sebastian@gmail.com>
packages/material-ui/src/styles/ThemeProvider.js Outdated Show resolved Hide resolved
packages/material-ui/src/styles/ThemeProvider.js Outdated Show resolved Hide resolved
mnajdova and others added 3 commits October 2, 2020 10:18
Co-authored-by: Matt <github@nospam.33m.co>
Co-authored-by: Matt <github@nospam.33m.co>
@mnajdova mnajdova requested a review from mbrookes October 2, 2020 11:03
@mnajdova mnajdova merged commit aa68974 into mui:next Oct 2, 2020
export { default as styled, ComponentCreator, StyledProps } from './styled';
export {
default as MuiThemeProvider,
Copy link
Member

Choose a reason for hiding this comment

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

We have stopped documenting MuiThemeProvider in v4. I would propose we keep pushing in the same direction: we drop MuiThemeProvider in v5. The motivation for using ThemeProvider over MuiThemeProvider is to signal that a developer only needs one theme provider on the page, not one from Material-UI and one from styled-components. I have created https://trello.com/c/q6cdyyaP/2744-remove-muithemeprovider to keep track of this proposal.

@zannager zannager added the package: styles Specific to @mui/styles. Legacy package, @material-ui/styled-engine is taking over in v5. label Feb 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
package: styles Specific to @mui/styles. Legacy package, @material-ui/styled-engine is taking over in v5.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants