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

[system] Make @emotion/* fully supported in the System #33205

Merged
merged 9 commits into from
Aug 1, 2022
2 changes: 1 addition & 1 deletion packages/mui-material/src/Badge/Badge.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ const Badge = React.forwardRef(function Badge(inProps, ref) {
Badge: BadgeBadge,
...components,
}}
className={clsx(className, classes.root, componentsProps.root?.className)}
className={clsx(componentsProps.root?.className, classes.root, className)}
componentsProps={{
root: {
...componentsProps.root,
Expand Down
5 changes: 3 additions & 2 deletions packages/mui-material/src/Button/Button.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,8 @@ const Button = React.forwardRef(function Button(inProps, ref) {
variant,
};

const classes = useUtilityClasses(ownerState);
// eslint-disable-next-line @typescript-eslint/naming-convention
const { root: classes_root, ...classes } = useUtilityClasses(ownerState);
mnajdova marked this conversation as resolved.
Show resolved Hide resolved

const startIcon = startIconProp && (
<ButtonStartIcon className={classes.startIcon} ownerState={ownerState}>
Expand All @@ -339,7 +340,7 @@ const Button = React.forwardRef(function Button(inProps, ref) {
return (
<ButtonRoot
ownerState={ownerState}
className={clsx(className, contextProps.className)}
className={clsx(contextProps.className, classes_root, className)}
mnajdova marked this conversation as resolved.
Show resolved Hide resolved
mnajdova marked this conversation as resolved.
Show resolved Hide resolved
component={component}
disabled={disabled}
focusRipple={!disableFocusRipple}
Expand Down
61 changes: 61 additions & 0 deletions packages/mui-material/src/Button/Button.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { describeConformance, act, createRenderer, fireEvent, screen } from 'tes
import { ThemeProvider, createTheme } from '@mui/material/styles';
import Button, { buttonClasses as classes } from '@mui/material/Button';
import ButtonBase, { touchRippleClasses } from '@mui/material/ButtonBase';
import { ClassNames } from '@emotion/react';

describe('<Button />', () => {
const { render, renderToString } = createRenderer();
Expand Down Expand Up @@ -634,4 +635,64 @@ describe('<Button />', () => {
);
expect(container.querySelectorAll(`.${touchRippleClasses.root}`)).to.have.length(1);
});

describe('Emotion compatibility', () => {
it('classes.root should overwrite builtin styles.', () => {
// This is pink
const color = 'rgb(255, 192, 204)';

const { getByRole } = render(
<ClassNames>
{({ css }) => (
<Button color="primary" classes={{ root: css({ color }) }}>
This text should be pink
</Button>
)}
</ClassNames>,
);
const button = getByRole('button');

expect(getComputedStyle(button).color).to.equal(color);
});

it('className should overwrite classes.root and builtin styles.', () => {
const colorPink = 'rgb(255, 192, 204)';
const colorRed = 'rgb(255, 0, 0)';

const { getByRole } = render(
<ClassNames>
{({ css }) => (
<Button
color="primary"
className={css({ color: colorRed })}
classes={{ root: css({ color: colorPink }) }}
>
This text should be red
</Button>
)}
</ClassNames>,
);
const button = getByRole('button');

expect(getComputedStyle(button).color).to.equal(colorRed);
});

it('classes.* should overwrite builtin styles.', () => {
// This is pink
const color = 'rgb(255, 192, 204)';

const { getByRole } = render(
<ClassNames>
{({ css }) => (
<Button color="primary" classes={{ text: css({ color }) }}>
This text should be pink
</Button>
)}
</ClassNames>,
);
const button = getByRole('button');

expect(getComputedStyle(button).color).to.equal(color);
});
});
});
2 changes: 1 addition & 1 deletion packages/mui-material/src/ButtonBase/TouchRipple.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ const TouchRipple = React.forwardRef(function TouchRipple(inProps, ref) {

return (
<TouchRippleRoot
className={clsx(classes.root, touchRippleClasses.root, className)}
className={clsx(touchRippleClasses.root, classes.root, className)}
ref={container}
{...other}
>
Expand Down
6 changes: 3 additions & 3 deletions packages/mui-material/src/Drawer/Drawer.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ const Drawer = React.forwardRef(function Drawer(inProps, ref) {
if (variant === 'permanent') {
return (
<DrawerDockedRoot
className={clsx(classes.root, classes.docked, className)}
className={clsx(classes.docked, classes.root, className)}
garronej marked this conversation as resolved.
Show resolved Hide resolved
ownerState={ownerState}
ref={ref}
{...other}
Expand All @@ -233,7 +233,7 @@ const Drawer = React.forwardRef(function Drawer(inProps, ref) {
if (variant === 'persistent') {
return (
<DrawerDockedRoot
className={clsx(classes.root, classes.docked, className)}
className={clsx(classes.docked, classes.root, className)}
ownerState={ownerState}
ref={ref}
{...other}
Expand All @@ -251,7 +251,7 @@ const Drawer = React.forwardRef(function Drawer(inProps, ref) {
...BackdropPropsProp,
transitionDuration,
}}
className={clsx(classes.root, classes.modal, className)}
className={clsx(classes.modal, classes.root, className)}
open={open}
ownerState={ownerState}
onClose={onClose}
Expand Down
2 changes: 1 addition & 1 deletion packages/mui-material/src/ImageList/ImageList.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ const ImageList = React.forwardRef(function ImageList(inProps, ref) {
return (
<ImageListRoot
as={component}
className={clsx(classes.root, classes[variant], className)}
className={clsx(classes[variant], classes.root, className)}
garronej marked this conversation as resolved.
Show resolved Hide resolved
ref={ref}
style={style}
ownerState={ownerState}
Expand Down
2 changes: 1 addition & 1 deletion packages/mui-material/src/ImageListItem/ImageListItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ const ImageListItem = React.forwardRef(function ImageListItem(inProps, ref) {
return (
<ImageListItemRoot
as={component}
className={clsx(classes.root, classes[variant], className)}
className={clsx(classes[variant], classes.root, className)}
garronej marked this conversation as resolved.
Show resolved Hide resolved
ref={ref}
style={{
height,
Expand Down
2 changes: 1 addition & 1 deletion packages/mui-material/src/Select/SelectInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ const SelectInput = React.forwardRef(function SelectInput(props, ref) {
onFocus={onFocus}
{...SelectDisplayProps}
ownerState={ownerState}
className={clsx(classes.select, className, SelectDisplayProps.className)}
className={clsx(SelectDisplayProps.className, classes.select, className)}
// The id is required for proper a11y
id={buttonId}
>
Expand Down
8 changes: 4 additions & 4 deletions packages/mui-utils/src/composeClasses/composeClasses.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ describe('composeClasses', () => {
},
),
).to.deep.equal({
root: 'MuiTest-root standardOverride MuiTest-standard',
slot: 'slotOverride MuiTest-slot',
root: 'MuiTest-root MuiTest-standard standardOverride',
slot: 'MuiTest-slot slotOverride',
});
});

Expand All @@ -51,8 +51,8 @@ describe('composeClasses', () => {
},
),
).to.deep.equal({
root: 'MuiTest-root standardOverride MuiTest-standard',
slot: 'slotOverride MuiTest-slot',
root: 'MuiTest-root MuiTest-standard standardOverride',
slot: 'MuiTest-slot slotOverride',
});
});
});
2 changes: 1 addition & 1 deletion packages/mui-utils/src/composeClasses/composeClasses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ export default function composeClasses<ClassKey extends string>(
output[slot] = slots[slot]
.reduce((acc, key) => {
if (key) {
acc.push(getUtilityClass(key));
if (classes && classes[key]) {
acc.push(classes[key]);
}
acc.push(getUtilityClass(key));
}
return acc;
}, [] as string[])
Expand Down
27 changes: 27 additions & 0 deletions test/regressions/fixtures/Button/EmotionCompat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import * as React from 'react';
import { ClassNames } from '@emotion/react';
import Button from '@mui/material/Button';

export default function EmotionCompat() {
return (
<ClassNames>
{({ css }) => (
<React.Fragment>
<Button color="primary" classes={{ root: css({ color: 'pink' }) }}>
This text should be pink
</Button>
<Button
color="primary"
className={css({ color: 'red' })}
classes={{ root: css({ color: 'pink' }) }}
>
This text should be red
</Button>
<Button color="primary" classes={{ text: css({ color: 'pink' }) }}>
This text should be pink
</Button>
</React.Fragment>
)}
</ClassNames>
);
}