-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
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
[Alert] Filled variant text color should be using theme.palette[color].contrastText and not theme.palette.getContrastText #33512
Comments
Thanks for the report. #29813 did change the behavior, but it was for solving an issue. The contrast color in the cases where the colors changed is because the black gives better contrast than the white color for the text. We could use index 5cfcd0492e..6ce845f83e 100644
--- a/packages/mui-material/src/Alert/Alert.js
+++ b/packages/mui-material/src/Alert/Alert.js
@@ -95,11 +95,14 @@ const AlertRoot = styled(Paper, {
theme.palette.mode === 'dark'
? theme.palette[color].dark
: theme.palette[color].main,
- color: theme.palette.getContrastText(
- theme.palette.mode === 'dark'
- ? theme.palette[color].dark
- : theme.palette[color].main,
- ),
+ color:
+ theme.palette.mode === 'light' && theme.palette[color].contrastText
+ ? theme.palette[color].contrastText
+ : theme.palette.getContrastText(
+ theme.palette.mode === 'dark'
+ ? theme.palette[color].dark
+ : theme.palette[color].main,
+ ),
}),
}),
}; @siriwatknp what do you think about this change? |
So the problem maybe is that theme.palette[color].contrastText only with theme.palette[color].main.
It would leave you with
Or this should be resolve in theme palette after createPalette
And you only left with
|
We haven't had so far the need to have a contrast text color for each color in the palette, so I wouldn't add it until we see this required in more than one place. I feel this would be overengineering for the problem we have. For the Let's also consider what @siriwatknp thinks. |
I won't do anything at this point because the contrast ratio is expected (it should be black): What we could do is add a comment to the Alert page that the const theme = createTheme({
components: {
MuiAlert: {
styleOverrides: {
root: ({ ownerState }) => ({
...(ownerState.variant === "filled" && {
color: "#fff"
})
})
}
}
}
}); Here: https://codesandbox.io/s/admiring-shape-rzm6lw?file=/src/App.tsx:341-884 |
@siriwatknp @mnajdova I agreed not to do anything. But do we need extra logic in components on how to resolve contrastColor if it already is calculated in the palette itself
theme.palette.mode === 'light' ? theme.palette[color].contrastText : theme.palette[color].darkContrastText which pobably can decrease bundle size also at some components.
Right now I see only code duplication for no reason in each component with contrast text colours and the same logic all over the place. The other option is to remove contrastText form theme and changed it to getModeContrastText and use it in all places also. You do not need 2 ways on how colour should be resolved. |
Thanks @povilass this is a good point. We are likely going to hit this in the future with CSS vars. @povilass currently we don't need this in other components, this is why I don't think we should add it right now. cc @siriwatknp did you need so far something like this for joy? |
Apologies if this is the wrong place to ask the question, but does this mean that the example in the docs is no longer supported - is the contrast text always either white or black now? And does the contrastText property have no effect when using a custom theme? |
Sorry, ignore me - I've realised the above example is achievable by not using the filled variant. |
Duplicates
Latest version
Current behavior 😯
Currenly theme.palette.getContrastText is used to calculate text color.
Expected behavior 🤔
Expected color to be provided from theme.palette[color].contrastText
Steps to reproduce 🕹
https://codesandbox.io/s/clever-almeida-u36ek8?file=/src/App.tsx
Context 🔦
Pull request broke Alert #29813 text colour. If contrastText is provided it should not calculate theme.palette.getContrastText or should account on provided contrastText
Your environment 🌎
No response
The text was updated successfully, but these errors were encountered: