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] Add applyStyles() to theme #40667

Merged
merged 6 commits into from
Jan 24, 2024

Conversation

siriwatknp
Copy link
Member

Motivation

  • This is a new utility to create styles for a color mode that will smooth the transition from Material UI v5 -> v6.
  • Improve the DX for customization. Currently, customization is different between basic theme and CSS theme variables. This utility abstracts the implementation so developers don't need to think about it.

Note: it replaces the existing theme.applyDarkStyles from #40324

Examples

- styled('div')(({ theme }) => ({
-   background: theme.palette.mode === 'light' ? theme.palette.grey[700] : theme.palette.grey[300],
- }))
+ styled('div')(({ theme }) => [
+   { background: theme.palette.grey[700] },
+   theme.applyStyles('dark', {
+      background: theme.palette.grey[300],
+   })
+ ])
- styled('div')(({ theme }) => ({
-   background: theme.vars.palette.grey[700],
-   [theme.getColorSchemeSelector('dark')]: {
-      background: theme.vars.palette.grey[300],
-   }
- }))
+ styled('div')(({ theme }) => [
+   { background: theme.vars.palette.grey[700] },
+   theme.applyStyles('dark', {
+      background: theme.vars.palette.grey[300],
+   })
+ ])

Docs

I won't add this utility to the docs for now to battle test with zero-runtime a bit more.


@siriwatknp siriwatknp marked this pull request as ready for review January 18, 2024 06:41
@mui-bot
Copy link

mui-bot commented Jan 18, 2024

Netlify deploy preview

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

Bundle size report

Details of bundle changes (Toolpad)
Details of bundle changes

Generated by 🚫 dangerJS against 3a91783

@siriwatknp siriwatknp added package: system Specific to @mui/system customization: theme Centered around the theming features labels Jan 18, 2024
* })
*```
*/
export default function applyStyles<K extends string>(key: K, styles: CSSObject) {
Copy link
Contributor

@brijeshb42 brijeshb42 Jan 18, 2024

Choose a reason for hiding this comment

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

Can this be more generic? ie, can this be also used to apply styles for say, dir="rtl/ltr" ?
Or do we need a separate function for that?

Copy link
Member Author

Choose a reason for hiding this comment

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

I might be wrong but I don't think it's the responsibility of applyStyles() because applyStyles is for customization that's related to colors.

I have never seen a case where rtl/ltr are differ in terms of colors. Moreover, the rtl/ltr should be done at a higher level. In emotion, it's done by a stylis plugin that changes CSS properties, e.g. from marginLeft to marginRight.

// If CssVarsProvider is used as a provider,
// returns ':where([data-mui-color-scheme="light|dark"]) &'
const selector = (this as Theme)
.getColorSchemeSelector('dark')
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 verify if this is defined in material package ? I stumbled upon this when solving another issue.

Copy link
Member Author

Choose a reason for hiding this comment

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

getColorSchemeSelector is defined when CssVarsProvider is used.

I think it should be attached to the theme by extendTheme. I will create another issue to tackle this separately.

@@ -45,7 +45,6 @@ export interface Theme extends BaseTheme {
components?: Components<BaseTheme>;
unstable_sx: (props: SxProps<Theme>) => CSSObject;
unstable_sxConfig: SxConfig;
applyDarkStyles: (css: CSSObject) => CSSObject;
Copy link
Member Author

Choose a reason for hiding this comment

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

applyStyles is already declared in the SystemTheme.

@siriwatknp siriwatknp merged commit 2a29460 into mui:master Jan 24, 2024
22 checks passed
@andreassk80
Copy link

Shouldn't this have been a breaking change since it's renaming a function and be part of a major version?

@siriwatknp
Copy link
Member Author

Shouldn't this have been a breaking change since it's renaming a function and be part of a major version?

This is our mistake. The new API is introduced in 5.15.6 and the name change is in 5.15.7, ideally this PR should be part of 5.15.6 but I missed it.

Anyway, since we haven't documented it yet, I hope that it won't affect anyone. Sorry for the mistake.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
customization: theme Centered around the theming features package: system Specific to @mui/system
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants