Skip to content

Commit 75f5a8a

Browse files
[IconButton] custom color causes type error (#34521)
Co-authored-by: siriwatknp <[email protected]>
1 parent 5292e98 commit 75f5a8a

File tree

2 files changed

+53
-31
lines changed

2 files changed

+53
-31
lines changed

packages/mui-material/src/IconButton/IconButton.js

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -69,40 +69,43 @@ const IconButtonRoot = styled(ButtonBase, {
6969
marginRight: ownerState.size === 'small' ? -3 : -12,
7070
}),
7171
}),
72-
({ theme, ownerState }) => ({
73-
...(ownerState.color === 'inherit' && {
74-
color: 'inherit',
75-
}),
76-
...(ownerState.color !== 'inherit' &&
77-
ownerState.color !== 'default' && {
78-
color: (theme.vars || theme).palette[ownerState.color].main,
79-
...(!ownerState.disableRipple && {
80-
'&:hover': {
81-
backgroundColor: theme.vars
82-
? `rgba(${theme.vars.palette[ownerState.color].mainChannel} / ${
83-
theme.vars.palette.action.hoverOpacity
84-
})`
85-
: alpha(theme.palette[ownerState.color].main, theme.palette.action.hoverOpacity),
86-
// Reset on touch devices, it doesn't add specificity
87-
'@media (hover: none)': {
88-
backgroundColor: 'transparent',
72+
({ theme, ownerState }) => {
73+
const palette = (theme.vars || theme).palette?.[ownerState.color];
74+
return {
75+
...(ownerState.color === 'inherit' && {
76+
color: 'inherit',
77+
}),
78+
...(ownerState.color !== 'inherit' &&
79+
ownerState.color !== 'default' && {
80+
color: palette?.main,
81+
...(!ownerState.disableRipple && {
82+
'&:hover': {
83+
...(palette && {
84+
backgroundColor: theme.vars
85+
? `rgba(${palette.mainChannel} / ${theme.vars.palette.action.hoverOpacity})`
86+
: alpha(palette.main, theme.palette.action.hoverOpacity),
87+
}),
88+
// Reset on touch devices, it doesn't add specificity
89+
'@media (hover: none)': {
90+
backgroundColor: 'transparent',
91+
},
8992
},
90-
},
93+
}),
9194
}),
95+
...(ownerState.size === 'small' && {
96+
padding: 5,
97+
fontSize: theme.typography.pxToRem(18),
9298
}),
93-
...(ownerState.size === 'small' && {
94-
padding: 5,
95-
fontSize: theme.typography.pxToRem(18),
96-
}),
97-
...(ownerState.size === 'large' && {
98-
padding: 12,
99-
fontSize: theme.typography.pxToRem(28),
100-
}),
101-
[`&.${iconButtonClasses.disabled}`]: {
102-
backgroundColor: 'transparent',
103-
color: (theme.vars || theme).palette.action.disabled,
104-
},
105-
}),
99+
...(ownerState.size === 'large' && {
100+
padding: 12,
101+
fontSize: theme.typography.pxToRem(28),
102+
}),
103+
[`&.${iconButtonClasses.disabled}`]: {
104+
backgroundColor: 'transparent',
105+
color: (theme.vars || theme).palette.action.disabled,
106+
},
107+
};
108+
},
106109
);
107110

108111
/**

packages/mui-material/src/IconButton/IconButton.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as React from 'react';
22
import { expect } from 'chai';
33
import PropTypes from 'prop-types';
44
import { createRenderer, describeConformance } from 'test/utils';
5+
import { ThemeProvider, createTheme } from '@mui/material/styles';
56
import IconButton, { iconButtonClasses as classes } from '@mui/material/IconButton';
67
import Icon from '@mui/material/Icon';
78
import ButtonBase from '@mui/material/ButtonBase';
@@ -100,4 +101,22 @@ describe('<IconButton />', () => {
100101
);
101102
}).toErrorDev(['MUI: You are providing an onClick event listener']);
102103
});
104+
105+
it('should not throw error for a custom color', () => {
106+
expect(() => (
107+
<ThemeProvider
108+
theme={createTheme({
109+
components: {
110+
MuiIconButton: {
111+
defaultProps: {
112+
color: 'custom',
113+
},
114+
},
115+
},
116+
})}
117+
>
118+
<IconButton />
119+
</ThemeProvider>
120+
)).not.to.throw();
121+
});
103122
});

0 commit comments

Comments
 (0)