How to change outline color of Material UI React input component?
To find the class names and CSS properties that you can change, the documentation for the Component API shows a list.
TextField
is a special case though, because it combines and renders multiple sub-components, it allows you to pass CSS properties to the Input
component and the FormHelperText
component.
And the OutlinedInput is a very special case, because it actually uses NotchedInput for the input element which has its own CSS properties.
Looking at the code for the OutlinedInput you can see child selectors being used:
root: {
position: 'relative',
'& $notchedOutline': {
borderColor,
},
// ...
It looks like the issue is that the OutlinedInput doesn't set the styles for the NotchedOutline correctly
You may have some luck with this:
const theme = createMuiTheme({
// ...other code,
overrides: {
// ...
MuiOutlinedInput: {
focused: {
border: '1px solid #4A90E2'
},
'& $notchedOutline': {
border: '1px solid #4A90E2'
},
},
// ...
}
});
Thanks to Rudolf Olah's help and pointing me in the right direction! I was able to solve the issue with the following code:
overrides: {
MuiOutlinedInput: {
root: {
position: 'relative',
'& $notchedOutline': {
borderColor: 'rgba(0, 0, 0, 0.23)',
},
'&:hover:not($disabled):not($focused):not($error) $notchedOutline': {
borderColor: '#4A90E2',
// Reset on touch devices, it doesn't add specificity
'@media (hover: none)': {
borderColor: 'rgba(0, 0, 0, 0.23)',
},
},
'&$focused $notchedOutline': {
borderColor: '#4A90E2',
borderWidth: 1,
},
},
},
MuiFormLabel: {
root: {
'&$focused': {
color: '#4A90E2'
}
}
}