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

[system] Move stylesheet generator to extendTheme #41446

Merged
merged 44 commits into from
Mar 20, 2024

Conversation

siriwatknp
Copy link
Member

@siriwatknp siriwatknp commented Mar 11, 2024

A part of #40225

closes #40178

Summary

  • This PR moves the stylesheet generation to extendTheme for all packages (Material UI and next, Joy UI, Pigment CSS) via generateStyleSheets().
  • The generateCssVars is refactored to generateThemeVars to remove the duplicated logic across packages.
  • The switch between @media prefers-color-scheme, classes, or data attributes can be fully controlled by getSelector

Next step


@siriwatknp siriwatknp added package: system Specific to @mui/system package: material-ui Specific to @mui/material package: joy-ui Specific to @mui/joy enhancement This is not a bug, nor a new feature labels Mar 11, 2024
@mui-bot
Copy link

mui-bot commented Mar 11, 2024

Netlify deploy preview

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

@material-ui/core: parsed: +0.15% , gzip: +0.13%
@mui/joy: parsed: +0.11% , gzip: +0.12%

Bundle size report

Details of bundle changes (Toolpad)
Details of bundle changes

Generated by 🚫 dangerJS against bb8759f

Comment on lines 84 to 93
getSelector: function getSelector(colorScheme, css) {
if (colorScheme && colorScheme === 'dark') {
return {
'@media (prefers-color-scheme: dark)': {
':root': css,
},
};
}
return ':root';
},
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 default extendTheme use data-* attribute for creating selectors. This custom getSelector proves that it can be customized.

@siriwatknp siriwatknp marked this pull request as ready for review March 13, 2024 03:24
Signed-off-by: Siriwat K <siriwatkunaporn@gmail.com>
Signed-off-by: Siriwat K <siriwatkunaporn@gmail.com>
Signed-off-by: Siriwat K <siriwatkunaporn@gmail.com>
@@ -2,34 +2,8 @@ import { serializeStyles } from '@emotion/serialize';
import { Theme } from './extendTheme';

export function generateTokenCss(theme: Theme) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you also add tests for these files now that the testing is set-up.

Copy link
Member Author

@siriwatknp siriwatknp Mar 13, 2024

Choose a reason for hiding this comment

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

Done in 0bba432. Thanks for the request, I am able to prevent the error with the tests if theme is not created by extendTheme()

@siriwatknp
Copy link
Member Author

I will move this to next branch instead.

@siriwatknp siriwatknp closed this Mar 13, 2024
@siriwatknp siriwatknp reopened this Mar 20, 2024
@siriwatknp siriwatknp changed the base branch from master to next March 20, 2024 04:11
@siriwatknp siriwatknp merged commit d700e5c into mui:next Mar 20, 2024
22 checks passed
pluvio72 pushed a commit to pluvio72/material-ui that referenced this pull request Mar 29, 2024
Comment on lines +186 to +188
themeProp.defaultColorScheme = resolvedDefaultColorScheme;
themeProp.colorSchemeSelector = colorSchemeSelector;
themeProp.attribute = attribute;
Copy link
Member

@oliviertassinari oliviertassinari Jun 8, 2024

Choose a reason for hiding this comment

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

Is this normal? We are violating the immutability principal. Can we remove/deprecate this? At the minimum, I think that there must be a comment in the code to explain why for the next wave of thousand of developers who will read the source and be confused.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for pointing out. It needs to be cleanup, I will open a separate PR.

Copy link
Member

Choose a reason for hiding this comment

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

Great thanks. If it's expected, a comment would be great to help understand.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This is not a bug, nor a new feature package: joy-ui Specific to @mui/joy package: material-ui Specific to @mui/material package: system Specific to @mui/system v6.x
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[system] theme.getColorSchemeSelector is not a function
4 participants