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

[codemod] Add styled v6 transformation #41743

Merged
merged 39 commits into from
Apr 17, 2024

Conversation

siriwatknp
Copy link
Member

@siriwatknp siriwatknp commented Apr 2, 2024

This codemod is benefit for Material UI and any project that are using styled v5. The transformation will remove the use of props in the style argument and replace them with variants.

Review suggestion

Please focus on the test cases (not the codemod implementation). Feel free to suggest more test cases that we should cover.

Target components

See the result.

Out-of-scope transformations

  • ❌ Dynamic values, for example:

    const ResizableContainer = styled('div', {
       name: 'MuiGauge',
       slot: 'Container',
     })(({ ownerState, theme }) => ({
       width: ownerState.width ?? '100%',
       height: ownerState.height ?? '100%',
     }));

    Since Emotion does not support callback as a value, developers have to manually fix this by using inline style + CSS variables

  • ❌ dynamic reference from the theme, e.g. color palette:

     const Test = styled('div')(({ ownerState, theme }) => ({
        backgroundColor: (theme.vars || theme).palette[ownerState.color]?.main,
      }));

@siriwatknp siriwatknp added the package: codemod Specific to @mui/codemod label Apr 2, 2024
@mui-bot
Copy link

mui-bot commented Apr 2, 2024

Netlify deploy preview

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

Bundle size report

No bundle size changes (Toolpad)
No bundle size changes

Generated by 🚫 dangerJS against 83d052b

@mnajdova
Copy link
Member

mnajdova commented Apr 2, 2024

It would be great if we can create a draft POC PR that runs this on our repo (or at least in the @mui/material) package.

@siriwatknp
Copy link
Member Author

It would be great if we can create a draft POC PR that runs this on our repo (or at least in the @mui/material) package.

Here is the PR that runs the codemod on several components https://github.com/mui/material-ui/pull/41766/files#diff-eb5bb0f7d6fd1be741c79c303d033419ee5eefea4124632e06c5b0f4bcf5c0d4

@siriwatknp
Copy link
Member Author

siriwatknp commented Apr 8, 2024

It would be great if we can create a draft POC PR that runs this on our repo (or at least in the @mui/material) package.

Here is the PR that runs the codemod on several components https://github.com/mui/material-ui/pull/41766/files#diff-eb5bb0f7d6fd1be741c79c303d033419ee5eefea4124632e06c5b0f4bcf5c0d4

cc @mnajdova the transformation looks all correct with no visual regression.

Copy link
Member

@mnajdova mnajdova 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 this is a great first iteration. Can we add a description in the README.md under the v6 section? I would recommend writing out examples of what is supported, then when we add more features, we can extend the descrption.

cc @DiegoAndai do we have other place where we keep the descriptions of the codemods available for v6?

@siriwatknp
Copy link
Member Author

I think this is a great first iteration. Can we add a description in the README.md under the v6 section? I would recommend writing out examples of what is supported, then when we add more features, we can extend the descrption.

cc @DiegoAndai do we have other place where we keep the descriptions of the codemods available for v6?

Done.

@DiegoAndai
Copy link
Member

cc @DiegoAndai do we have other place where we keep the descriptions of the codemods available for v6?

Not for the codemods specifically, but we already have the migration guides for Material and System, so whenever we add breaking changes, we should point to the corresponding codemods.

Copy link
Member

@mnajdova mnajdova left a comment

Choose a reason for hiding this comment

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

I left few comments to be checked before merging. The transformations look good 👍

packages/mui-codemod/README.md Outdated Show resolved Hide resolved
packages/mui-codemod/README.md Show resolved Hide resolved
@@ -0,0 +1,60 @@
const FormHelperTextRoot = styled('p')(({ theme, ownerState, disabled }) => ({
Copy link
Member

Choose a reason for hiding this comment

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

Maybe in the future we can add support for aliased imports, e.g.

import { styled as muiStyled } from '@mui/material/styles';

variant: 'normal'
},
style: {
backgroundColor: (theme.vars || theme).palette[ownerState.color].light
Copy link
Member

Choose a reason for hiding this comment

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

can we add comment above this line?

medium: theme.typography.pxToRem(24),
large: theme.typography.pxToRem(36),
}[ownerState.fontSize],
// TODO v5 deprecate, v6 remove for sx
Copy link
Member

Choose a reason for hiding this comment

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

This got lost somewhere :)

@siriwatknp siriwatknp merged commit 7c7fd30 into mui:next Apr 17, 2024
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
package: codemod Specific to @mui/codemod
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants