Skip to content

Commit

Permalink
[material-ui][StepConnector] deprecate composed classes (#41740)
Browse files Browse the repository at this point in the history
  • Loading branch information
sai6855 authored Apr 10, 2024
1 parent 44c88a0 commit ae45647
Show file tree
Hide file tree
Showing 16 changed files with 368 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1012,3 +1012,43 @@ The StepLabel's `componentsProps` was deprecated in favor of `slotProps`:
+ slotProps={{ label: labelProps }}
/>
```

## StepConnector

Use the [codemod](https://github.com/mui/material-ui/tree/HEAD/packages/mui-codemod#step-connector-classes) below to migrate the code as described in the following sections:

```bash
npx @mui/codemod@next deprecations/step-connector-classes <path>
```

### Composed CSS classes

The CSS classes that composed the `line` CSS class and `orientation` prop values were removed.

Here's how to migrate:

```diff
- .MuiStepConnector-lineHorizontal
+.MuiStepConnector-horizontal > .MuiStepConnector-line
- .MuiStepConnector-lineVertical
+.MuiStepConnector-vertical > .MuiStepConnector-line
```

```diff
import { stepConnectorClasses } from '@mui/material/StepConnector';

MuiStepConnector: {
styleOverrides: {
root: {
- [`& .${stepConnectorClasses.lineHorizontal}`]: {
+ [`&.${stepConnectorClasses.horizontal} > .${stepConnectorClasses.line}`]: {
color: 'red',
},
- [`& .${stepConnectorClasses.lineVertical}`]: {
+ [`&.${stepConnectorClasses.vertical} > .${stepConnectorClasses.line}`]: {
color: 'red',
},
},
},
},
```
10 changes: 6 additions & 4 deletions docs/pages/material-ui/api/step-connector.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,16 @@
{
"key": "lineHorizontal",
"className": "MuiStepConnector-lineHorizontal",
"description": "Styles applied to the root element if `orientation=\"horizontal\"`.",
"isGlobal": false
"description": "Styles applied to the line element if `orientation=\"horizontal\"`.",
"isGlobal": false,
"isDeprecated": true
},
{
"key": "lineVertical",
"className": "MuiStepConnector-lineVertical",
"description": "Styles applied to the root element if `orientation=\"vertical\"`.",
"isGlobal": false
"description": "Styles applied to the line element if `orientation=\"vertical\"`.",
"isGlobal": false,
"isDeprecated": true
},
{
"key": "root",
Expand Down
10 changes: 6 additions & 4 deletions docs/translations/api-docs/step-connector/step-connector.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@
"line": { "description": "Styles applied to {{nodeName}}.", "nodeName": "the line element" },
"lineHorizontal": {
"description": "Styles applied to {{nodeName}} if {{conditions}}.",
"nodeName": "the root element",
"conditions": "<code>orientation=\"horizontal\"</code>"
"nodeName": "the line element",
"conditions": "<code>orientation=\"horizontal\"</code>",
"deprecationInfo": "Combine the <a href=\"/material-ui/api/step-connector/#step-connector-classes-horizontal\">.MuiStepConnector-horizontal</a> and <a href=\"/material-ui/api/step-connector/#step-connector-classes-line\">.MuiStepConnector-line</a> classes instead. <a href=\"/material-ui/migration/migrating-from-deprecated-apis/\">How to migrate</a>"
},
"lineVertical": {
"description": "Styles applied to {{nodeName}} if {{conditions}}.",
"nodeName": "the root element",
"conditions": "<code>orientation=\"vertical\"</code>"
"nodeName": "the line element",
"conditions": "<code>orientation=\"vertical\"</code>",
"deprecationInfo": "Combine the <a href=\"/material-ui/api/step-connector/#step-connector-classes-vertical\">.MuiStepConnector-vertical</a> and <a href=\"/material-ui/api/step-connector/#step-connector-classes-line\">.MuiStepConnector-line</a> classes instead. <a href=\"/material-ui/migration/migrating-from-deprecated-apis/\">How to migrate</a>"
},
"root": { "description": "Styles applied to the root element." },
"vertical": {
Expand Down
36 changes: 36 additions & 0 deletions packages/mui-codemod/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1008,6 +1008,42 @@ npx @mui/codemod@latest deprecations/step-label-props <path>

```

#### `step-connector-classes`

JS transforms:

```diff
import { stepConnectorClasses } from '@mui/material/StepConnector';

MuiStepConnector: {
styleOverrides: {
root: {
- [`& .${stepConnectorClasses.lineHorizontal}`]: {
+ [`&.${stepConnectorClasses.horizontal} > .${stepConnectorClasses.line}`]: {
color: 'red',
},
- [`& .${stepConnectorClasses.lineVertical}`]: {
+ [`&.${stepConnectorClasses.vertical} > .${stepConnectorClasses.line}`]: {
color: 'red',
},
},
},
},
```

CSS transforms:

```diff
- .MuiStepConnector-lineHorizontal
+.MuiStepConnector-horizontal > .MuiStepConnector-line
- .MuiStepConnector-lineVertical
+.MuiStepConnector-vertical > .MuiStepConnector-line
```

```bash
npx @mui/codemod@next deprecations/step-connector-classes <path>
```

### v6.0.0

### v5.0.0
Expand Down
2 changes: 2 additions & 0 deletions packages/mui-codemod/src/deprecations/all/deprecations-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import transformAlertClasses from '../alert-classes';
import transformToggleButtonGroupClasses from '../toggle-button-group-classes';
import transformStepLabelProps from '../step-label-props';
import transformBackdropProps from '../backdrop-props';
import transformStepConnectorClasses from '../step-connector-classes';

/**
* @param {import('jscodeshift').FileInfo} file
Expand All @@ -28,6 +29,7 @@ export default function deprecationsAll(file, api, options) {
file.source = transformToggleButtonGroupClasses(file, api, options);
file.source = transformStepLabelProps(file, api, options);
file.source = transformBackdropProps(file, api, options);
file.source = transformStepConnectorClasses(file, api, options);

return file.source;
}
2 changes: 2 additions & 0 deletions packages/mui-codemod/src/deprecations/all/postcss.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const { plugin: chipClassesPlugin } = require('../chip-classes/postcss-plugin');
const {
plugin: paginationItemClassesPlugin,
} = require('../pagination-item-classes/postcss-plugin');
const { plugin: stepConnectorClassesPlugin } = require('../step-connector-classes/postcss-plugin');
const {
plugin: toggleButtonGroupClassesPlugin,
} = require('../toggle-button-group-classes/postcss-plugin');
Expand All @@ -20,6 +21,7 @@ module.exports = {
buttonGroupClassesPlugin,
chipClassesPlugin,
paginationItemClassesPlugin,
stepConnectorClassesPlugin,
toggleButtonGroupClassesPlugin,
],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './step-connector-classes';
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const classes = [
{
deprecatedClass: ' .MuiStepConnector-lineHorizontal',
replacementSelector: '.MuiStepConnector-horizontal > .MuiStepConnector-line',
},
{
deprecatedClass: ' .MuiStepConnector-lineVertical',
replacementSelector: '.MuiStepConnector-vertical > .MuiStepConnector-line',
},
];

const plugin = () => {
return {
postcssPlugin: `Replace deperecated StepConnector classes with new classes`,
Rule(rule) {
const { selector } = rule;

classes.forEach(({ deprecatedClass, replacementSelector }) => {
const selectorRegex = new RegExp(`${deprecatedClass}$`);

if (selector.match(selectorRegex)) {
rule.selector = selector.replace(selectorRegex, replacementSelector);
}
});
},
};
};
plugin.postcss = true;

module.exports = {
plugin,
classes,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const { plugin } = require('./postcss-plugin');

module.exports = {
plugins: [plugin],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import { classes } from './postcss-plugin';

/**
* @param {import('jscodeshift').FileInfo} file
* @param {import('jscodeshift').API} api
*/
export default function transformer(file, api, options) {
const j = api.jscodeshift;
const root = j(file.source);
const printOptions = options.printOptions;
classes.forEach(({ deprecatedClass, replacementSelector }) => {
const replacementSelectorPrefix = '&';
root
.find(j.ImportDeclaration)
.filter((path) => path.node.source.value.match(/^@mui\/material\/StepConnector$/))
.forEach((path) => {
path.node.specifiers.forEach((specifier) => {
if (
specifier.type === 'ImportSpecifier' &&
specifier.imported.name === 'stepConnectorClasses'
) {
const deprecatedAtomicClass = deprecatedClass.replace(
`${deprecatedClass.split('-')[0]}-`,
'',
);
root
.find(j.MemberExpression, {
object: { name: specifier.local.name },
property: { name: deprecatedAtomicClass },
})
.forEach((memberExpression) => {
const parent = memberExpression.parentPath.parentPath.value;
if (parent.type === j.TemplateLiteral.name) {
const memberExpressionIndex = parent.expressions.findIndex(
(expression) => expression === memberExpression.value,
);
const precedingTemplateElement = parent.quasis[memberExpressionIndex];
const atomicClasses = replacementSelector
.replaceAll('MuiStepConnector-', '')
.replaceAll(replacementSelectorPrefix, '')
.replaceAll(' > ', '')
.split('.')
.filter(Boolean);

if (
precedingTemplateElement.value.raw.endsWith(
deprecatedClass.startsWith(' ')
? `${replacementSelectorPrefix} .`
: `${replacementSelectorPrefix}.`,
)
) {
const atomicClassesArgs = [
memberExpressionIndex,
1,
...atomicClasses.map((atomicClass) =>
j.memberExpression(
memberExpression.value.object,
j.identifier(atomicClass),
),
),
];
parent.expressions.splice(...atomicClassesArgs);

if (replacementSelector.includes(' > ')) {
const quasisArgs = [
memberExpressionIndex,
1,
j.templateElement(
{
raw: precedingTemplateElement.value.raw.replace(' ', ''),
cooked: precedingTemplateElement.value.cooked.replace(' ', ''),
},
false,
),
j.templateElement({ raw: ' > .', cooked: ' > .' }, false),
];

if (atomicClasses.length === 3) {
quasisArgs.splice(
3,
0,
j.templateElement({ raw: '.', cooked: '.' }, false),
);
}

parent.quasis.splice(...quasisArgs);
} else {
parent.quasis.splice(
memberExpressionIndex,
1,
j.templateElement(
{
raw: precedingTemplateElement.value.raw,
cooked: precedingTemplateElement.value.cooked,
},
false,
),

j.templateElement({ raw: '.', cooked: '.' }, false),
);
}
}
}
});
}
});
});

const selectorRegex = new RegExp(`${replacementSelectorPrefix}${deprecatedClass}$`);
root
.find(
j.Literal,
(literal) => typeof literal.value === 'string' && literal.value.match(selectorRegex),
)
.forEach((path) => {
path.replace(
j.literal(
path.value.value.replace(
selectorRegex,
`${replacementSelectorPrefix}${replacementSelector}`,
),
),
);
});
});
return root.toSource(printOptions);
}
Loading

0 comments on commit ae45647

Please sign in to comment.