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

[core] Generate vars in extendTheme #35739

Merged
merged 89 commits into from Mar 3, 2023

Conversation

mnajdova
Copy link
Member

@mnajdova mnajdova commented Jan 6, 2023

Breaking change

  • The shouldSkipGeneratingVar prop was moved from the createCssVarsProvider's option to the theme.
  • If the default theme does not use extendTheme from Material UI or Joy UI, it needs to be wrapped inside unstable_createCssVarsTheme - a util exported from the MUI System.
 import {
   unstable_createCssVarsProvider as createCssVarsProvider,
+   unstable_createCssVarsTheme as createCssVarsTheme,
 } from '@mui/system';

 const { CssVarsProvider } = createCssVarsProvider({
-  theme: {
+  theme: createCssVarsTheme({
     colorSchemes: {
       light: {
         typography: {
           htmlFontSize: '16px',
           h1: {
             fontSize: '1rem',
             fontWeight: 500,
           },
         },
       },
     },
+    shouldSkipGeneratingVar: (keys) => keys[0] === 'typography' && keys[1] === 'h1',
-  },
+  }),
   defaultColorScheme: 'light',
-  shouldSkipGeneratingVar: (keys) => keys[0] === 'typography' && keys[1] === 'h1',
});

Or define it directly in the theme prop:

 <CssVarsProvider 
+   theme={createCssVarsProvider({ 
+    // other theme keys
+    shouldSkipGeneratingVar: (keys) => keys[0] === 'typography' && keys[1] === 'h1'
+   })} />

As agreed upon in #35446 (comment)

Changes done in the PR:

  • extendTheme now adds the theme.vars object containing values in a form of: var(--joy-palette-primary-500, #aaaaaa) - containing default values from the light color scheme
  • CssVarsProvider now uses the shouldSkipGeneratingVar from the theme
  • updated the regexp for the CSS vars values to accept line height & color channel values
  • updated and added few other tests to reflect the changes

Result:

  1. Joy UI no longer requires the CssVarsProvider to be used 🎉 - https://codesandbox.io/s/staging-cache-85f1co?file=/demo.tsx
  2. Joy UI works with custom theme - https://codesandbox.io/s/icy-smoke-bmk4qw?file=/index.tsx
  3. Material Design 3 implementation does not require CssVarsProvider to be used (this was the case before too) - https://codesandbox.io/s/winter-haze-txd52g?file=/demo.tsx
  4. Material Design 3 with custom theme - https://codesandbox.io/s/laughing-frog-1tzo5w?file=/package.json
  5. Material Design 3 can co-exist with Material Design 2 - https://codesandbox.io/s/nifty-pine-gqn5uz?file=/demo.tsx
  6. Material Design 2 with custom theme - https://codesandbox.io/s/objective-hamilton-uzbhpo?file=/demo.tsx

@mnajdova mnajdova added the package: joy-ui Specific to @mui/joy label Jan 6, 2023
@mui-bot
Copy link

mui-bot commented Jan 6, 2023

Netlify deploy preview

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

@material-ui/core: parsed: +0.14% , gzip: +0.18%
@material-ui/system: parsed: +1.11% , gzip: +1.11%
@mui/material-next: parsed: +0.88% , gzip: +1.34%
@mui/joy: parsed: +1.32% , gzip: +1.36%

Bundle size report

Details of bundle changes

Generated by 🚫 dangerJS against d12e77b

shadowRing: theme.shadowRing,
shadowChannel: theme.shadowChannel,
};
theme.getColorSchemeSelector = () => '&';
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 same default implementation as in defaultTheme.

Comment on lines 649 to 652
theme.palette = deepmerge(themeOptions?.palette || {}, {
...(theme.colorSchemes.light.palette as RuntimeColorSystem['palette']),
colorScheme: 'light',
});
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if extendTheme() should not resolve the palette yet.

My initial intention is that extendTheme() should return sets of colors for your application but it should not decide what the current color scheme is. That logic will be handled by CssVarsProvider.

@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Feb 16, 2023
@mnajdova
Copy link
Member Author

We decided with generating the CSS vars in extendTheme with containing the css variable that defaults to the actual theme css value.

@mnajdova mnajdova changed the title [joy] Generate vars in extendTheme [core] Generate vars in extendTheme Feb 16, 2023
@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Feb 27, 2023
@mnajdova
Copy link
Member Author

@siriwatknp would be great if you can revalidate the screenshot differences, before I revert the change for adding back the CssVarsProvider. We can ignore the changes related to icons.

@siriwatknp
Copy link
Member

@siriwatknp would be great if you can revalidate the screenshot differences, before I revert the change for adding back the CssVarsProvider. We can ignore the changes related to icons.

Got it, this is next on my list.

Copy link
Member

@siriwatknp siriwatknp left a comment

Choose a reason for hiding this comment

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

✅ Looks great! pushed a minor fix.

@mnajdova mnajdova merged commit 1eddf71 into mui:master Mar 3, 2023
the-mgi pushed a commit to the-mgi/material-ui that referenced this pull request Mar 6, 2023
@mnajdova mnajdova mentioned this pull request Mar 6, 2023
1 task
@Rafcin
Copy link

Rafcin commented Mar 8, 2023

I think this change to unstable_createCssVarsTheme and Provider has messed up custom themes that use Experimental_CssVarsProvider as CssVarsProvider and experimental_extendTheme. This seems to have broken light and dark color schemes when using experimental.

@siriwatknp
Copy link
Member

I think this change to unstable_createCssVarsTheme and Provider has messed up custom themes that use Experimental_CssVarsProvider as CssVarsProvider and experimental_extendTheme. This seems to have broken light and dark color schemes when using experimental.

Can you open a new issue with reproduction? Feel free to tag me.

@Rafcin
Copy link

Rafcin commented Mar 9, 2023

@siriwatknp Just saw this, will do! Let me create a minimal reproduction first and I'll make a post!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change package: joy-ui Specific to @mui/joy package: material-ui Specific to @mui/material package: system Specific to @mui/system
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants