-
Notifications
You must be signed in to change notification settings - Fork 221
fix(theme): contained buttons honor color=error/success/warning #1674
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,7 @@ export const MuiButton: Components<Theme>['MuiButton'] = { | |
| root: ({ theme }) => { | ||
| const { | ||
| palette: { | ||
| background: { brand, hover }, | ||
| background: { brand, hover, error, success, warning, info }, | ||
| text: { disabled, constant, neutral: TextNeutral }, | ||
| border: { neutral } | ||
| }, | ||
|
|
@@ -22,6 +22,49 @@ export const MuiButton: Components<Theme>['MuiButton'] = { | |
| backgroundColor: brand?.hover | ||
| } | ||
| }, | ||
| // Contained buttons default to the brand colour above. Honour the | ||
| // semantic `color` prop so error / success / warning / info actions are | ||
| // not silently painted brand (keppel) - the long-standing gap that made | ||
| // `<Button variant="contained" color="error">` render green. Text colour | ||
| // is derived per-background with getContrastText so a light background | ||
| // (notably the yellow warning) keeps a readable dark label instead of an | ||
| // unreadable white one (WCAG). These rules share the | ||
| // `&.MuiButton-contained` selector's (0,0,2,0) specificity and follow it | ||
| // in source order, so a semantic-colour button (which carries BOTH | ||
| // classes) resolves to its colour; the higher-specificity `.Mui-disabled` | ||
| // rules below still win when disabled. | ||
| '&.MuiButton-containedError': { | ||
| color: error?.default ? theme.palette.getContrastText(error.default) : constant?.white, | ||
| backgroundColor: error?.default, | ||
| '&:hover': { | ||
| backgroundColor: error?.hover | ||
| } | ||
| }, | ||
| '&.MuiButton-containedSuccess': { | ||
| color: success?.default | ||
| ? theme.palette.getContrastText(success.default) | ||
| : constant?.white, | ||
| backgroundColor: success?.default, | ||
| '&:hover': { | ||
| backgroundColor: success?.hover | ||
| } | ||
| }, | ||
| '&.MuiButton-containedWarning': { | ||
| color: warning?.default | ||
| ? theme.palette.getContrastText(warning.default) | ||
| : constant?.white, | ||
| backgroundColor: warning?.default, | ||
| '&:hover': { | ||
| backgroundColor: warning?.hover | ||
| } | ||
| }, | ||
|
Comment on lines
+36
to
+60
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This block has two main issues:
To solve both issues robustly, we can use MUI's built-in '&.MuiButton-containedError': {
color: error?.default ? theme.palette.getContrastText(error.default) : constant?.white,
backgroundColor: error?.default,
'&:hover': {
backgroundColor: error?.hover
}
},
'&.MuiButton-containedSuccess': {
color: success?.default ? theme.palette.getContrastText(success.default) : constant?.white,
backgroundColor: success?.default,
'&:hover': {
backgroundColor: success?.hover
}
},
'&.MuiButton-containedWarning': {
color: warning?.default ? theme.palette.getContrastText(warning.default) : constant?.white,
backgroundColor: warning?.default,
'&:hover': {
backgroundColor: warning?.hover
}
},
'&.MuiButton-containedInfo': {
color: info?.default ? theme.palette.getContrastText(info.default) : constant?.white,
backgroundColor: info?.default,
'&:hover': {
backgroundColor: info?.hover
}
},
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great catch on both. Adopted |
||
| '&.MuiButton-containedInfo': { | ||
| color: info?.default ? theme.palette.getContrastText(info.default) : constant?.white, | ||
| backgroundColor: info?.default, | ||
| '&:hover': { | ||
| backgroundColor: info?.hover | ||
| } | ||
| }, | ||
| '&.MuiButton-outlined': { | ||
| border: `1px solid ${neutral?.default}`, | ||
| '&:hover': { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The cast is deliberately scoped to the selector shape the test asserts (
color/backgroundColor/&:hover); the test only ever indexes selector keys, neverfontWeight/lineHeight. Broadening toRecord<string, unknown>would add a cast at every access without catching a real bug, so I've kept the narrower shape. Happy to switch if you'd prefer the stricter typing.