-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
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
MuiFormLabel Focused color problem #11244
Comments
@luiscdz We have recently documented the specificity change introduced: #11227. It applies to the import React from "react";
import { TextField, createMuiTheme, ThemeProvider } from "@material-ui/core";
const theme = createMuiTheme({
overrides: {
MuiInputLabel: { // Name of the component ⚛️ / style sheet
root: { // Name of the rule
color: "orange",
"&$focused": { // increase the specificity for the pseudo class
color: "blue"
}
}
}
}
});
function OverridesCss() {
return (
<ThemeProvider theme={theme}>
<TextField value="value" label="label" />
</ThemeProvider>
);
}
export default OverridesCss; |
@marsonmao Yes, you can. |
@oliviertassinari Ohh got it! Finally I realized the rule, thanks! But I modified the key name to make it easier to write the style: formLabelRoot: { // must provide all of formLabelRoot && '&$formLabelFocused' && formLabelFocused
'&$formLabelFocused': { color: theme.palette.primary.main },
},
formLabelFocused: {
// color: 'green', // won't affect anything
},
<TextField
InputLabelProps={{
FormLabelClasses: {
root: classes.formLabelRoot,
focused: classes.formLabelFocused,
},
}}
/> By the way...I found this part of
Is it a typo? (I mean should it be 'light' : 'dark'?) |
@marsonmao I'm happy you have found a solution. No, it's not a typo. Does the specification ask for something else? |
@oliviertassinari Ok then it's fine. I was just wondering if the light type should match the light theme, if it's the opposite then it's an intentionally made art decision. |
the only part that I didn't try before googling... thanks. |
Is there any reason why the focused class doesn't apply styling to focused elements without stating the element with the class currently set as focused, being in focus? Seems pointlessly redundant, why have a focused class at all and just add the internal state to the original control |
@RyRy79261 We handle the focus class like a pseudo class (:focused, :hover, etc.) you have to increase specificity so you can scope the override to a specific state without messing the default one. |
@oliviertassinari Thanks for the reply, but I'm failing to understand why the focused class would be assigned to an element that isnt in a focused state thus requiring being that specific. I'm also struggling to see the scoping issue as applying styles to the root element made no difference, only an element with the class focus, that is also explicitly in focus had the css applied to it. Is there a situation where an form element has the focus class applied to its label by the default behaviour of focusing on an input without said form element actually being in a state of focused? |
I don't understand what you don't understand. The focused class is only applied when the input is focused. If the focused styles color had a specificity of one, it would be overriden when you change the non focused color. The prevent this problem, it has a specificity of two. The CSS specification used the same logic to define the pseudo classes specificity impact. |
As an example There's a label, when the label's input is in focus, the class name If you then unfocus, this class is removed, implying that the However, the definition here states, that even though that class, called logically: .focused{
// Focused styles
} Actual: .focused{
"&$focused":{
// styles
}
} This means that either specifying the state of focused, in a focus class is redundant, or having the focus class to apply in that state is redundant |
@RyRy79261 That's not how it should be done. It's should be something like this (in equivalent CSS #15140): .mui-input-label.focused {
color: red;
} I have updated my misleading comment, sorry. |
Thank you, however my issue is that the code you added there does not render. .focused{
"&$focused":{
// styles
}
}```
Only when I do it like that does it work |
@RyRy79261 Check this comment: #11244 (comment). |
is not working for me. I can't find the styles in Chrome debugger at all, I don't think it's a case of them being overrided by more specific styles. Help? |
@jonasms Make sure your custom theme is correctly applied. |
Just to add a bit of insight here which might be useful for some. Some of the component might allow for things like this:
Hope this might help someone. |
This is hell 🙃 |
I have fixed this issue by adding global css file and I put there this |
To custom focused label on TextField locally :
Hope it helps ;) |
mui 5 example import { formLabelClasses } from "@mui/material";
import { createTheme } from "@mui/material/styles";
import { grey } from "@mui/material/colors";
export const theme = createTheme({
components: {
MuiFormLabel: {
styleOverrides: {
root: {
color: grey[400],
[`&.${formLabelClasses.focused}`]: {
color: grey[800]
}
}
}
}
}
}) |
lord have mercy trying to use mui theme and overriding with a css file is a catastrophe and just breaks the css. |
@vitaly-ivanov-navidium c You need to make sure that its wrapped in component, also missing a bracket at the beginning of formlabelclass string. also I wonder why focused doesnt change the color of it if its in the api?
|
thanks @JahnoelRondon, I corrected the code to look accurate. Regarding your question - that was just part of our design to retain label color even on focus (updated as well to avoid confusion). |
Hello, @oliviertassinari, and any other MUI experts, I need some help! I have a highly customized text field which requires overrides for basically every concievable state 😅 . I was able to get every customization to work properly by adding the following overrides to the theme:
Unfortunately, now that I created a global override that works perfectly, I have to account for an additional text input which requires different background and border styles. How would you handle this? Refactor the TextInput overrides to exclude border/background styling, and then handle the background/border override using styled components? Is this the preferred way to handle this? Also Is there a way to bundle all of these overrides as a variants of a TextInput instead? Thanks in advance for any time and energy sent my way! |
Expected Behavior
When I create a new theme and override focused prop of MuiFormLabel, MaterialUI not used this new class because it create high new class that override my new class.
Current Behavior
Steps to Reproduce (for bugs)
https://codesandbox.io/s/52poq578vn
Context
I need to change color label when the input is focus, in beta 41 it is work.
Your Environment
The text was updated successfully, but these errors were encountered: