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

Fixes emotion compat, className overwriting more specific classes #33866

Closed
wants to merge 1 commit into from
Closed
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
8 changes: 5 additions & 3 deletions packages/mui-material/src/Tab/Tab.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import unsupportedProp from '../utils/unsupportedProp';
import tabClasses, { getTabUtilityClass } from './tabClasses';

const useUtilityClasses = (ownerState) => {
const { classes, textColor, fullWidth, wrapped, icon, label, selected, disabled } = ownerState;
const { classes, className, textColor, fullWidth, wrapped, icon, label, selected, disabled } =
ownerState;

const slots = {
root: [
'root',
'className',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'className',

This will generate class name: MuiTab-className

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will generate class name: MuiTab-className
Sorry my implementation was naive, obviousy we dont want MuiTab-className.

icon && label && 'labelIcon',
`textColor${capitalize(textColor)}`,
fullWidth && 'fullWidth',
Expand All @@ -25,7 +27,7 @@ const useUtilityClasses = (ownerState) => {
iconWrapper: ['iconWrapper'],
};

return composeClasses(slots, getTabUtilityClass, classes);
return composeClasses(slots, getTabUtilityClass, { ...classes, className });
};

const TabRoot = styled(ButtonBase, {
Expand Down Expand Up @@ -187,7 +189,7 @@ const Tab = React.forwardRef(function Tab(inProps, ref) {
return (
<TabRoot
focusRipple={!disableFocusRipple}
className={clsx(classes.root, className)}
className={classes.root}
ref={ref}
role="tab"
aria-selected={selected}
Expand Down
25 changes: 25 additions & 0 deletions packages/mui-material/src/Tab/Tab.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { expect } from 'chai';
import * as React from 'react';
import { spy } from 'sinon';
import { act, createRenderer, describeConformance, fireEvent } from 'test/utils';
import { ClassNames } from '@emotion/react';
import PhoneIcon from '@mui/icons-material/Phone';

describe('<Tab />', () => {
const { render } = createRenderer();
Expand Down Expand Up @@ -166,4 +168,27 @@ describe('<Tab />', () => {
expect(style).to.have.property('alignText', 'center');
});
});

describe('Emotion compatibility', () => {
it('className should not overwrite the the more specific classes that applies to root', () => {
// This is pink
const colorPink = 'rgb(255, 192, 204)';

const { getByRole } = render(
<ClassNames>
{({ css }) => (
<Tab
icon={<PhoneIcon />}
label="Background should be pink"
className={css({ backgroundColor: 'red' })}
classes={{ labelIcon: css({ backgroundColor: colorPink }) }}
/>
)}
</ClassNames>,
);
const tab = getByRole('tab');

expect(getComputedStyle(tab).backgroundColor).to.equal(colorPink);
});
});
});
35 changes: 35 additions & 0 deletions test/regressions/fixtures/Table/EmotionCompat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import * as React from 'react';
import { ClassNames } from '@emotion/react';
import Tab from '@mui/material/Tab';
import PhoneIcon from '@mui/icons-material/Phone';

export default function EmotionCompat() {
return (
<ClassNames>
{({ css }) => (
<React.Fragment>
<Tab
icon={<PhoneIcon />}
label="Background should be green"
classes={{
root: css({
backgroundColor: 'red',
}),
labelIcon: css({ backgroundColor: 'green' }),
}}
/>
<Tab
icon={<PhoneIcon />}
label="Background should be green"
className={css({ backgroundColor: 'red' })}
classes={{
labelIcon: css({
backgroundColor: 'green',
}),
}}
/>
</React.Fragment>
)}
</ClassNames>
);
}