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 support for variants in the styled() util #39073

Merged
merged 16 commits into from
Oct 2, 2023

Conversation

mnajdova
Copy link
Member

@mnajdova mnajdova commented Sep 20, 2023

This PR adds an API that is available in the zero-runtime CSS in JS. The goal for backporting this API is to allow a smoother migration when both APIs need to be used alongside each other.

The new API is related to supporting the variants API that we have in the theme to the styled() utility too:

// JavaScript object syntax
const Button = styled('button')({
  cursor: 'pointer',
  variants: [{
    props: { variant: 'text', color: 'primary' },
    style: { color: 'blue' }
  }]
});

// Functions syntax
const Button = styled('button')(({ theme }) => ({
  cursor: 'pointer',
  variants: [{
    props: { variant: 'text', color: 'primary' },
    style: { color: theme.palette.primary.500 }
  }]
}));

I am reluctant to document this API just yet, maybe we can just try to use it internally first and them announce it together with the zero-runtime CSS in JS first alpha release.

@mnajdova mnajdova added new feature New feature or request package: system Specific to @mui/system labels Sep 20, 2023
@mui-bot
Copy link

mui-bot commented Sep 20, 2023

Netlify deploy preview

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

@material-ui/core: parsed: +0.11% , gzip: +0.13%
@material-ui/lab: parsed: +0.21% , gzip: +0.27%
@material-ui/system: parsed: +0.77% , gzip: +0.74%
@mui/material-next: parsed: +0.20% , gzip: +0.16%
@mui/joy: parsed: +0.13% , gzip: +0.17%

Bundle size report

Details of bundle changes (Toolpad)
Details of bundle changes

Generated by 🚫 dangerJS against 8cf423b

@mnajdova mnajdova force-pushed the system/variants-api-styled branch from 92fb0e2 to f4927a4 Compare September 25, 2023 10:56
@@ -28,43 +33,53 @@ const getStyleOverrides = (name, theme) => {
return null;
};

const transformVariants = (variants) => {
Copy link
Member Author

Choose a reason for hiding this comment

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

extracted so that it can be re-used in multiple places

};

const variantsResolver = (props, styles, theme, name) => {
const variantsResolver = (props, styles, variants) => {
Copy link
Member Author

Choose a reason for hiding this comment

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

Simplified signature to be re-used in both theme & styled's variants

}
});
}

return variantsStyles;
};

const themeVariantsResolver = (props, styles, theme, name) => {
Copy link
Member Author

Choose a reason for hiding this comment

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

This is the legacy variantsResolver

...props,
theme: resolveTheme({ ...props, defaultTheme, themeId }),
});
if (typeof stylesArg === 'function' && stylesArg.__emotion_real !== stylesArg) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Adding the variants styled in case arrays are used for defining multiple styles.

@@ -217,21 +330,7 @@ export default function createStyled(input = {}) {
// If the type is array, than we need to add placeholders in the template for the overrides, variants and the sx styles.
transformedStyleArg = [...styleArg, ...placeholders];
transformedStyleArg.raw = [...styleArg.raw, ...placeholders];
} else if (
Copy link
Member Author

Choose a reason for hiding this comment

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

This was moved above, so that we can do a transformation only once.

@mnajdova mnajdova marked this pull request as ready for review September 25, 2023 12:39
@mnajdova mnajdova requested a review from siriwatknp September 27, 2023 13:19
@mnajdova mnajdova merged commit beafaea into mui:master Oct 2, 2023
5 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature New feature or request package: system Specific to @mui/system
Projects
Status: Recently completed
Development

Successfully merging this pull request may close these issues.

4 participants