How to Customize Focus Color in Material-UI v5
If you're looking to change the focus color of the TextField
in Material-UI v5, you can do so by utilizing the color
props. However, this method has its limitations as it only accepts predefined values. If you wish to use a custom color, you can implement the following code snippet:
'primary' | 'secondary' | 'error' | 'info' | 'success' | 'warning'
To set a specific color as the focus color, you can use the provided code snippet:
const CssTextField = styled(TextField, {
shouldForwardProp: (props) => props !== "focusColor"
})((p) => ({
// input label when focused
"& label.Mui-focused": {
color: p.focusColor
},
// focused color for input with variant='standard'
"& .MuiInput-underline:after": {
borderBottomColor: p.focusColor
},
// focused color for input with variant='filled'
"& .MuiFilledInput-underline:after": {
borderBottomColor: p.focusColor
},
// focused color for input with variant='outlined'
"& .MuiOutlinedInput-root": {
"&.Mui-focused fieldset": {
borderColor: p.focusColor
}
}
}));
Implementation
<CssTextField focusColor='red' />
Demo
https://codesandbox.io/s/colortextfields-material-demo-forked-hpwc1?fontsize=14&hidenavigation=1&theme=dark&file=/demo.js
Customizing Input Styles in Material-UI v4
In Material-UI v4, you can modify the focused color border and label of the TextField
using the following approach:
const focusedColor = "orange";
const useStyles = makeStyles({
root: {
// input label when focused
"& label.Mui-focused": {
color: focusedColor
},
// focused color for input with variant='standard'
"& .MuiInput-underline:after": {
borderBottomColor: focusedColor
},
// focused color for input with variant='filled'
"& .MuiFilledInput-underline:after": {
borderBottomColor: focusedColor
},
// focused color for input with variant='outlined'
"& .MuiOutlinedInput-root": {
"&.Mui-focused fieldset": {
borderColor: focusedColor
}
}
}
});
export default function CustomizedInputs() {
const classes = useStyles();
return (
<div style={{ display: "flex", columnGap: 15 }}>
<TextField className={classes.root} variant="outlined" />
<TextField className={classes.root} variant="standard" />
<TextField className={classes.root} variant="filled" />
</div>
);
}
Demo
https://codesandbox.io/s/67139471how-to-change-the-focused-color-of-a-react-material-ui-textfield-qlgpf?file=/demo.tsx