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

[material-ui][CircularProgress] Deprecate composed classes #42076

Merged
merged 10 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,46 @@ Here's how to migrate:

```

## CircularProgress

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

```bash
npx @mui/codemod@next deprecations/circular-progress-classes <path>
```

### Composed CSS classes

The CSS classes that composed the `circle` CSS class and `variant` prop values were removed.

Here's how to migrate:

```diff
- .MuiCircularProgress-circleDeterminate
+.MuiCircularProgress-determinate > .MuiCircularProgress-circle
- .MuiCircularProgress-circleIndeterminate
+.MuiCircularProgress-indeterminate > .MuiCircularProgress-circle
```

```diff
import { circularProgressClasses } from '@mui/material/CircularProgress';

MuiCircularProgress: {
styleOverrides: {
root: {
- [`& .${circularProgressClasses.circleDeterminate}`]: {
+ [`&.${circularProgressClasses.determinate} > .${circularProgressClasses.circle}`]: {
color: 'red',
},
- [`& .${circularProgressClasses.circleIndeterminate}`]: {
+ [`&.${circularProgressClasses.indeterminate} > .${circularProgressClasses.circle}`]: {
color: 'red',
},
},
},
},
```

## Divider

Use the [codemod](https://github.com/mui/material-ui/tree/HEAD/packages/mui-codemod#divider-props) below to migrate the code as described in the following sections:
Expand Down
6 changes: 4 additions & 2 deletions docs/pages/material-ui/api/circular-progress.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
"key": "circleDeterminate",
"className": "MuiCircularProgress-circleDeterminate",
"description": "Styles applied to the `circle` svg path if `variant=\"determinate\"`.",
"isGlobal": false
"isGlobal": false,
"isDeprecated": true
},
{
"key": "circleDisableShrink",
Expand All @@ -55,7 +56,8 @@
"key": "circleIndeterminate",
"className": "MuiCircularProgress-circleIndeterminate",
"description": "Styles applied to the `circle` svg path if `variant=\"indeterminate\"`.",
"isGlobal": false
"isGlobal": false,
"isDeprecated": true
},
{
"key": "colorPrimary",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"circleDeterminate": {
"description": "Styles applied to {{nodeName}} if {{conditions}}.",
"nodeName": "the <code>circle</code> svg path",
"conditions": "<code>variant=\"determinate\"</code>"
"conditions": "<code>variant=\"determinate\"</code>",
"deprecationInfo": "Combine the <a href=\"/material-ui/api/circular-progress/#circular-progress-classes-circle\">.MuiCircularProgress-circle</a> and <a href=\"/material-ui/api/circular-progress/#circular-progress-classes-determinate\">.MuiCircularProgress-determinate</a> classes instead. See <a href=\"/material-ui/migration/migrating-from-deprecated-apis/\">Migrating from deprecated APIs</a> for more details."
},
"circleDisableShrink": {
"description": "Styles applied to {{nodeName}} if {{conditions}}.",
Expand All @@ -40,7 +41,8 @@
"circleIndeterminate": {
"description": "Styles applied to {{nodeName}} if {{conditions}}.",
"nodeName": "the <code>circle</code> svg path",
"conditions": "<code>variant=\"indeterminate\"</code>"
"conditions": "<code>variant=\"indeterminate\"</code>",
"deprecationInfo": "Combine the <a href=\"/material-ui/api/circular-progress/#circular-progress-classes-circle\">.MuiCircularProgress-circle</a> and <a href=\"/material-ui/api/circular-progress/#circular-progress-classes-indeterminate\">.MuiCircularProgress-indeterminate</a> classes instead. See <a href=\"/material-ui/migration/migrating-from-deprecated-apis/\">Migrating from deprecated APIs</a> for more details."
},
"colorPrimary": {
"description": "Styles applied to {{nodeName}} if {{conditions}}.",
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 @@ -845,6 +845,42 @@ CSS transforms:
npx @mui/codemod@next deprecations/chip-classes <path>
```

#### `circular-progress-classes`

JS transforms:

```diff
import { circularProgressClasses } from '@mui/material/CircularProgress';

MuiCircularProgress: {
styleOverrides: {
root: {
- [`& .${circularProgressClasses.circleDeterminate}`]: {
+ [`&.${circularProgressClasses.determinate} > .${circularProgressClasses.circle}`]: {
color: 'red',
},
- [`& .${circularProgressClasses.circleIndeterminate}`]: {
+ [`&.${circularProgressClasses.indeterminate} > .${circularProgressClasses.circle}`]: {
color: 'red',
},
},
},
},
```

CSS transforms:

```diff
- .MuiCircularProgress-circleDeterminate
+.MuiCircularProgress-determinate > .MuiCircularProgress-circle
- .MuiCircularProgress-circleIndeterminate
+.MuiCircularProgress-indeterminate > .MuiCircularProgress-circle
```

```bash
npx @mui/codemod@next deprecations/circular-progress-classes <path>
```

#### `divider-props`

```diff
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 @@ -12,6 +12,7 @@ import transformToggleButtonGroupClasses from '../toggle-button-group-classes';
import transformStepLabelProps from '../step-label-props';
import transformBackdropProps from '../backdrop-props';
import transformStepConnectorClasses from '../step-connector-classes';
import transformCircularProgressClasses from '../circular-progress-classes';
import transformSpeedDialProps from '../speed-dial-props';

/**
Expand All @@ -33,6 +34,7 @@ export default function deprecationsAll(file, api, options) {
file.source = transformStepLabelProps(file, api, options);
file.source = transformBackdropProps(file, api, options);
file.source = transformStepConnectorClasses(file, api, options);
file.source = transformCircularProgressClasses(file, api, options);
file.source = transformSpeedDialProps(file, api, options);

return file.source;
Expand Down
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\/CircularProgress$/))
.forEach((path) => {
path.node.specifiers.forEach((specifier) => {
if (
specifier.type === 'ImportSpecifier' &&
specifier.imported.name === 'circularProgressClasses'
) {
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('MuiCircularProgress-', '')
.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);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import path from 'path';
import { expect } from 'chai';
import postcss from 'postcss';
import { jscodeshift } from '../../../testUtils';
import jsTransform from './circular-progress-classes';
import { plugin as postcssPlugin } from './postcss-plugin';
import readFile from '../../util/readFile';

function read(fileName) {
return readFile(path.join(__dirname, fileName));
}

const postcssProcessor = postcss([postcssPlugin]);

describe('@mui/codemod', () => {
describe('deprecations', () => {
describe('circular-progress-classes', () => {
describe('js-transform', () => {
it('transforms props as needed', () => {
const actual = jsTransform(
{ source: read('./test-cases/actual.js') },
{ jscodeshift },
{ printOptions: { quote: 'double', trailingComma: true } },
);

const expected = read('./test-cases/expected.js');
expect(actual).to.equal(expected, 'The transformed version should be correct');
});

it('should be idempotent', () => {
const actual = jsTransform(
{ source: read('./test-cases/expected.js') },
{ jscodeshift },
{},
);

const expected = read('./test-cases/expected.js');
expect(actual).to.equal(expected, 'The transformed version should be correct');
});
});

describe('css-transform', () => {
it('transforms classes as needed', async () => {
const actual = await postcssProcessor.process(read('./test-cases/actual.css'), {
from: undefined,
});

const expected = read('./test-cases/expected.css');
expect(actual.css).to.equal(expected, 'The transformed version should be correct');
});

it('should be idempotent', async () => {
const actual = await postcssProcessor.process(read('./test-cases/expected.css'), {
from: undefined,
});

const expected = read('./test-cases/expected.css');
expect(actual.css).to.equal(expected, 'The transformed version should be correct');
});
});

describe('test-cases', () => {
it('should not be the same', () => {
const actualJS = read('./test-cases/actual.js');
const expectedJS = read('./test-cases/expected.js');
expect(actualJS).not.to.equal(expectedJS, 'The actual and expected should be different');

const actualCSS = read('./test-cases/actual.css');
const expectedCSS = read('./test-cases/expected.css');
expect(actualCSS).not.to.equal(
expectedCSS,
'The actual and expected should be different',
);
});
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './circular-progress-classes';
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const classes = [
{
deprecatedClass: ' .MuiCircularProgress-circleDeterminate',
replacementSelector: '.MuiCircularProgress-determinate > .MuiCircularProgress-circle',
},
{
deprecatedClass: ' .MuiCircularProgress-circleIndeterminate',
replacementSelector: '.MuiCircularProgress-indeterminate > .MuiCircularProgress-circle',
},
];

const plugin = () => {
return {
postcssPlugin: `Replace deprecated CircularProgress 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,
};
DiegoAndai marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const { plugin } = require('./postcss-plugin');

module.exports = {
plugins: [plugin],
};
Loading
Loading