Skip to content

Commit

Permalink
Add support for '@mui/styles/makeStyles' imports
Browse files Browse the repository at this point in the history
Fixes #32935
  • Loading branch information
joshkel committed May 31, 2022
1 parent 93b0aba commit 4f22c14
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 2 deletions.
10 changes: 8 additions & 2 deletions packages/mui-codemod/src/v5.0.0/jss-to-tss-react.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,10 @@ export default function transformer(file, api, options) {
);
importsChanged = true;
}
} else if (importSource === '@material-ui/styles/makeStyles') {
} else if (
importSource === '@material-ui/styles/makeStyles' ||
importSource === '@mui/styles/makeStyles'
) {
foundMakeStyles = true;
path.replace(
j.importDeclaration(
Expand All @@ -223,7 +226,10 @@ export default function transformer(file, api, options) {
),
);
importsChanged = true;
} else if (importSource === '@material-ui/styles/withStyles') {
} else if (
importSource === '@material-ui/styles/withStyles' ||
importSource === '@mui/styles/withStyles'
) {
foundWithStyles = true;
path.replace(
j.importDeclaration(
Expand Down
13 changes: 13 additions & 0 deletions packages/mui-codemod/src/v5.0.0/jss-to-tss-react.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,19 @@ describe('@mui/codemod', () => {
const expected = read('./jss-to-tss-react.test/expected-from-mui-styles.js');
expect(actual).to.equal(expected, 'The transformed version should be correct');
});
it('transforms @mui/styles/makeStyles to use tss-react', () => {
const actual = transform(
{
source: read('./jss-to-tss-react.test/actual-from-mui-styles-makeStyles.js'),
path: require.resolve('./jss-to-tss-react.test/actual-from-mui-styles-makeStyles.js'),
},
{ jscodeshift },
{},
);

const expected = read('./jss-to-tss-react.test/expected-from-mui-styles-makeStyles.js');
expect(actual).to.equal(expected, 'The transformed version should be correct');
});
it('transforms typescript makeStyles with nested selectors to use tss-react', () => {
const actual = transform(
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import React from "react";
import makeStyles from "@mui/styles/makeStyles";

const useStyles = makeStyles({
test: {
backgroundColor: "purple",
color: "white"
}
});

const useStyles2 = makeStyles({
test: {
backgroundColor: "purple",
color: "white",
"& $test2": {
backgroundColor: "lime",
color: "blue"
}
},
test2: {
backgroundColor: "blue",
color: "lime"
}
});

function InnerComponent() {
const classes = useStyles2();
return <div className={classes.test2}>Inner Test</div>;
}
export default function ComponentUsingStyles(props) {
const classes = useStyles();
return <div className={classes.test}>Test<InnerComponent/></div>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import React from "react";
import { makeStyles } from 'tss-react/mui';

const useStyles = makeStyles()({
test: {
backgroundColor: "purple",
color: "white"
}
});

const useStyles2 = makeStyles()((_theme, _params, classes) => ({
test: {
backgroundColor: "purple",
color: "white",
[`& .${classes.test2}`]: {
backgroundColor: "lime",
color: "blue"
}
},
test2: {
backgroundColor: "blue",
color: "lime"
}
}));

function InnerComponent() {
const { classes } = useStyles2();
return <div className={classes.test2}>Inner Test</div>;
}
export default function ComponentUsingStyles(props) {
const { classes } = useStyles();
return <div className={classes.test}>Test<InnerComponent/></div>;
}

0 comments on commit 4f22c14

Please sign in to comment.