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

[Button] Fix contained with inherit prop not adapting on dark mode #34508

Merged
10 changes: 8 additions & 2 deletions packages/mui-material/src/Button/Button.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,10 @@ const ButtonRoot = styled(ButtonBase, {
},
}),
...(ownerState.variant === 'contained' && {
backgroundColor: (theme.vars || theme).palette.grey.A100,
backgroundColor:
(theme.vars || theme).palette.mode === 'dark'
Copy link
Member

Choose a reason for hiding this comment

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

When using CSS variables, ideally we would use the same variable for both dark and light mode, and we would only switch the value of the variable. cc @siriwatknp should we add a variable for the inherit background of the button?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mnajdova @siriwatknp should we use this approach here? or is it good to go as it is?

Copy link
Member

Choose a reason for hiding this comment

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

I would tend to use CSS variables, for e.g. as it is done for other components: https://github.com/mui/material-ui/blob/master/packages/mui-material/src/styles/experimental_extendTheme.js#L137. cc @siriwatknp

? (theme.vars || theme).palette.grey[700]
: (theme.vars || theme).palette.grey[300],
boxShadow: (theme.vars || theme).shadows[4],
// Reset on touch devices, it doesn't add specificity
'@media (hover: none)': {
Expand Down Expand Up @@ -186,7 +189,10 @@ const ButtonRoot = styled(ButtonBase, {
? // this is safe because grey does not change between default light/dark mode
theme.vars.palette.text.primary
: theme.palette.getContrastText?.(theme.palette.grey[300]),
backgroundColor: (theme.vars || theme).palette.grey[300],
backgroundColor:
(theme.vars || theme).palette.mode === 'dark'
? (theme.vars || theme).palette.grey[800]
: (theme.vars || theme).palette.grey[400],
boxShadow: (theme.vars || theme).shadows[2],
}),
...(ownerState.variant === 'contained' &&
Expand Down
36 changes: 36 additions & 0 deletions test/regressions/fixtures/Button/Button.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import * as React from 'react';
import { useTheme } from '@mui/material/styles';
import Button from '@mui/material/Button';
import IconButton from '@mui/material/IconButton';
import Box from '@mui/material/Box';
import Brightness4Icon from '@mui/icons-material/Brightness4';
import Brightness7Icon from '@mui/icons-material/Brightness7';

export default function MulticoloredButton() {
const theme = useTheme();
const ColorModeContext = React.createContext({ toggleColorMode: () => {} });

const colorMode = React.useContext(ColorModeContext);
return (
<Box
sx={{
display: 'flex',
width: '100%',
alignItems: 'center',
justifyContent: 'center',
bgcolor: 'background.default',
color: 'text.primary',
borderRadius: 1,
p: 3,
}}
>
{theme.palette.mode} mode
<IconButton sx={{ ml: 1 }} onClick={colorMode.toggleColorMode} color="inherit">
{theme.palette.mode === 'dark' ? <Brightness7Icon /> : <Brightness4Icon />}
</IconButton>
<Button variant="contained" color="inherit">
Button
</Button>
</Box>
);
}