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] Support CSS Extraction using codemod #41935

Merged
merged 21 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
70 changes: 46 additions & 24 deletions packages/mui-codemod/src/v6.0.0/styled/styled-v6.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,27 @@ export default function styledV6(file, api, options) {
const root = j(file.source);
const printOptions = options.printOptions;

function createBuildStyle(key, upperBuildStyle) {
return function buildStyle(styleExpression) {
if (key) {
if (key.type === 'Identifier' || key.type === 'StringLiteral') {
return upperBuildStyle(j.objectExpression([j.objectProperty(key, styleExpression)]));
}
if (key.type === 'TemplateLiteral') {
return upperBuildStyle(
j.objectExpression([
{
...j.objectProperty(key, styleExpression),
computed: true,
},
]),
);
}
}
return upperBuildStyle ? upperBuildStyle(styleExpression) : styleExpression;
};
}

/**
*
* @param {import('ast-types').namedTypes.MemberExpression | import('ast-types').namedTypes.Identifier} node
Expand Down Expand Up @@ -309,7 +330,7 @@ export default function styledV6(file, api, options) {
}
}

recurseObjectExpression({ node: objectExpression });
recurseObjectExpression({ node: objectExpression, buildStyle: createBuildStyle() });

if (variants.length) {
objectExpression.properties.push(
Expand Down Expand Up @@ -343,13 +364,19 @@ export default function styledV6(file, api, options) {
node: prop.value,
parentNode: data.node,
key: prop.key,
buildStyle: createBuildStyle(prop.key, data.buildStyle),
replaceValue: (newValue) => {
prop.value = newValue;
},
modeStyles,
});
} else {
recurseObjectExpression({ ...data, node: prop, parentNode: data.node });
recurseObjectExpression({
...data,
node: prop,
parentNode: data.node,
buildStyle: createBuildStyle(prop.key, data.buildStyle),
});
}
});
appendPaletteModeStyles(data.node, modeStyles);
Expand All @@ -367,6 +394,8 @@ export default function styledV6(file, api, options) {
style: data.node.argument.right,
};

const lastLength = variants.push({}); // preserve the order of the recursive calls

const modeStyles = {}; // to collect styles from `theme.palette.mode === '...'`
variant.style.properties.forEach((prop) => {
if (prop.type === 'ObjectProperty') {
Expand All @@ -376,6 +405,7 @@ export default function styledV6(file, api, options) {
parentNode: variant.style,
props: variant.props,
key: prop.key,
buildStyle: createBuildStyle(prop.key, data.buildStyle),
replaceValue: (newValue) => {
prop.value = newValue;
},
Expand All @@ -387,15 +417,21 @@ export default function styledV6(file, api, options) {
node: prop,
parentNode: variant.style,
props: variant.props,
buildStyle: createBuildStyle(prop.key, data.buildStyle),
});
}
});
appendPaletteModeStyles(variant.style, modeStyles);
variants.push(buildObjectAST(variant));
variant.style = data.buildStyle(variant.style);
variants[lastLength - 1] = buildObjectAST(variant);
removeProperty(data.parentNode, data.node);
}
if (data.node.argument.type === 'ConditionalExpression') {
recurseObjectExpression({ ...data, node: data.node.argument, parentNode: data.node });
recurseObjectExpression({
...data,
node: data.node.argument,
parentNode: data.node,
});
removeProperty(data.parentNode, data.node);
}
}
Expand All @@ -417,17 +453,10 @@ export default function styledV6(file, api, options) {
if (data.props) {
props = mergeProps(data.props, props);
}
const styleVal = data.node.consequent;
let newStyle = styleVal;
if (
data.key &&
(data.key.type === 'Identifier' || data.key.type === 'StringLiteral')
) {
newStyle = j.objectExpression([j.objectProperty(data.key, styleVal)]);
}
const styleVal = data.buildStyle(data.node.consequent);
const variant = {
props,
style: newStyle,
style: styleVal,
};
variants.push(buildObjectAST(variant));

Expand All @@ -436,17 +465,10 @@ export default function styledV6(file, api, options) {
if (data.props) {
props2 = mergeProps(data.props, props2);
}
const styleVal2 = data.node.alternate;
let newStyle2 = styleVal2;
if (
data.key &&
(data.key.type === 'Identifier' || data.key.type === 'StringLiteral')
) {
newStyle2 = j.objectExpression([j.objectProperty(data.key, styleVal2)]);
}
const styleVal2 = data.buildStyle(data.node.alternate);
const variant2 = {
props: props2,
style: newStyle2,
style: styleVal2,
};
variants.push(buildObjectAST(variant2));
if (data.parentNode?.type === 'ObjectExpression') {
Expand Down Expand Up @@ -484,7 +506,7 @@ export default function styledV6(file, api, options) {
...data,
node: expression,
parentNode: data.parentNode,
key: data.key,
buildStyle: createBuildStyle(data.key, data.buildStyle),
replaceValue: (newValue) => {
data.node.expressions[index] = newValue;
},
Expand Down Expand Up @@ -523,7 +545,7 @@ export default function styledV6(file, api, options) {
j.stringLiteral(prop.key.name),
),
]),
style: j.objectExpression([j.objectProperty(data.key, prop.value)]),
style: data.buildStyle(prop.value),
}),
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,27 @@ const ImageListRoot = styled('ul')(({ ownerState }) => {
}),
};
});

const ImageListItemRoot = styled('li')(({ ownerState }) => ({
display: 'block',
position: 'relative',
[`& .${imageListItemClasses.img}`]: {
objectFit: 'cover',
width: '100%',
height: '100%',
display: 'block',
...(ownerState.variant === 'standard' && {
height: 'auto',
flexGrow: 1,
}),
'&:hover': {
'&[data-shape="circular"]': {
borderRadius: '50%',
...(ownerState.variant === 'unique' && {
height: 'auto',
flexGrow: 1,
}),
},
},
},
}));
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,44 @@ const ImageListRoot = styled('ul')({
}
}]
});

const ImageListItemRoot = styled('li')(({
display: 'block',
position: 'relative',
[`& .${imageListItemClasses.img}`]: {
objectFit: 'cover',
width: '100%',
height: '100%',
display: 'block',
'&:hover': {
'&[data-shape="circular"]': {
borderRadius: '50%'
},
}
},
variants: [{
props: {
variant: 'standard'
},
style: {
[`& .${imageListItemClasses.img}`]: {
siriwatknp marked this conversation as resolved.
Show resolved Hide resolved
height: 'auto',
flexGrow: 1,
}
}
}, {
props: {
variant: 'unique'
},
style: {
[`& .${imageListItemClasses.img}`]: {
'&:hover': {
'&[data-shape="circular"]': {
height: 'auto',
flexGrow: 1,
}
}
}
}
}]
}));
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ const LinearProgressBar1 = styled('span', {
theme
}) => ({
variants: [{
props: {
variant: 'buffer'
},
style: {
'&:hover': {}
}
}, {
props: (
{
variant,
Expand Down Expand Up @@ -56,13 +63,6 @@ const LinearProgressBar1 = styled('span', {
backgroundColor: 'currentColor',
}
}
}, {
props: {
variant: 'buffer'
},
style: {
'&:hover': {}
}
}, {
props: (
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ const Component = styled('div')(({
style: {
marginRight: -12
}
}, {
props: (
{
ownerState
}
) => ownerState.color !== 'inherit' &&
ownerState.color !== 'default',
style: {
color: palette?.main
}
}, {
props: (
{
Expand All @@ -84,16 +94,6 @@ const Component = styled('div')(({
},
},
}
}, {
props: (
{
ownerState
}
) => ownerState.color !== 'inherit' &&
ownerState.color !== 'default',
style: {
color: palette?.main
}
}]
};
});
26 changes: 18 additions & 8 deletions packages/mui-material/src/FormHelperText/FormHelperText.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const FormHelperTextRoot = styled('p', {
ownerState.filled && styles.filled,
];
},
})(({ theme, ownerState }) => ({
})(({ theme }) => ({
color: (theme.vars || theme).palette.text.secondary,
...theme.typography.caption,
textAlign: 'left',
Expand All @@ -55,13 +55,23 @@ const FormHelperTextRoot = styled('p', {
[`&.${formHelperTextClasses.error}`]: {
color: (theme.vars || theme).palette.error.main,
},
...(ownerState.size === 'small' && {
marginTop: 4,
}),
...(ownerState.contained && {
marginLeft: 14,
marginRight: 14,
}),
variants: [
{
props: {
size: 'small',
},
style: {
marginTop: 4,
},
},
{
props: ({ ownerState }) => ownerState.contained,
style: {
marginLeft: 14,
marginRight: 14,
},
},
],
}));

const FormHelperText = React.forwardRef(function FormHelperText(inProps, ref) {
Expand Down
29 changes: 17 additions & 12 deletions packages/mui-material/src/ImageList/ImageList.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,23 @@ const ImageListRoot = styled('ul', {

return [styles.root, styles[ownerState.variant]];
},
})(({ ownerState }) => {
return {
display: 'grid',
overflowY: 'auto',
listStyle: 'none',
padding: 0,
// Add iOS momentum scrolling for iOS < 13.0
WebkitOverflowScrolling: 'touch',
...(ownerState.variant === 'masonry' && {
display: 'block',
}),
};
})({
display: 'grid',
overflowY: 'auto',
listStyle: 'none',
padding: 0,
// Add iOS momentum scrolling for iOS < 13.0
WebkitOverflowScrolling: 'touch',
variants: [
{
props: {
variant: 'masonry',
},
style: {
display: 'block',
},
},
],
});

const ImageList = React.forwardRef(function ImageList(inProps, ref) {
Expand Down
Loading
Loading