Goodmorning everybody,
I am trying to develop a website using ReactJS and Material UI.
I started from this template.
I would like to modify the color of the TextField when focused: now it is blue, let’s say that I want it to be green.
This was the original file (in the repo that i linked above, it is under src/components/MKInput/MKInputRoot):
import TextField from "@mui/material/TextField";
import { styled } from "@mui/material/styles";
export default styled(TextField)(({ theme, ownerState }) => {
const { palette, functions } = theme;
const { error, success, disabled } = ownerState;
const { grey, transparent, error: colorError, success: colorSuccess } = palette;
const { pxToRem } = functions;
// styles for the input with error={true}
const errorStyles = () => ({
backgroundImage:
"url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' fill='none' stroke='%23F44335' viewBox='0 0 12 12'%3E%3Ccircle cx='6' cy='6' r='4.5'/%3E%3Cpath stroke-linejoin='round' d='M5.8 3.6h.4L6 6.5z'/%3E%3Ccircle cx='6' cy='8.2' r='.6' fill='%23F44335' stroke='none'/%3E%3C/svg%3E")",
backgroundRepeat: "no-repeat",
backgroundPosition: `right ${pxToRem(12)} center`,
backgroundSize: `${pxToRem(16)} ${pxToRem(16)}`,
"& .Mui-focused": {
"& .MuiOutlinedInput-notchedOutline, &:after": {
borderColor: colorError.main,
},
},
"& .MuiInputLabel-root.Mui-focused": {
color: colorError.main,
},
});
// styles for the input with success={true}
const successStyles = () => ({
backgroundImage:
"url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 10 8'%3E%3Cpath fill='%234CAF50' d='M2.3 6.73L.6 4.53c-.4-1.04.46-1.4 1.1-.8l1.1 1.4 3.4-3.8c.6-.63 1.6-.27 1.2.7l-4 4.6c-.43.5-.8.4-1.1.1z'/%3E%3C/svg%3E")",
backgroundRepeat: "no-repeat",
backgroundPosition: `right ${pxToRem(12)} center`,
backgroundSize: `${pxToRem(16)} ${pxToRem(16)}`,
"& .Mui-focused": {
"& .MuiOutlinedInput-notchedOutline, &:after": {
borderColor: colorSuccess.main,
},
},
"& .MuiInputLabel-root.Mui-focused": {
color: colorSuccess.main,
},
});
return {
backgroundColor: disabled ? `${grey[200]} !important` : transparent.main,
pointerEvents: disabled ? "none" : "auto",
...(error && errorStyles()),
...(success && successStyles()),
};
});
I simplified it a little:
import TextField from "@mui/material/TextField";
import { styled } from "@mui/material/styles";
export default styled(TextField)(({ theme, ownerState }) => {
const { palette } = theme;
const { disabled } = ownerState;
const { grey, transparent } = palette;
const myStyles = () => ({
"& .Mui-focused": {
"& .MuiOutlinedInput-notchedOutline, &:after": {
borderColor: "#00ff00",
},
},
"& .MuiInputLabel-root.Mui-focused": {
color: "#00ff00",
},
});
return {
backgroundColor: disabled ? `${grey[200]} !important` : transparent.main,
pointerEvents: disabled ? "none" : "auto",
...myStyles(),
};
});
I removed the error && before ...myStyle() as I want this style to always be appliend, not only on error.
Now suppose I want to change from green to red, I replace the strings “#00ff00” with “#ff0000”, click on the text input on my webpage and the border becomes red when focused. If I refresh the page and do that again, the border is blue on focus. This happens every time I try to change color: I replace the color string with the desired one, and the correct color is applied until I refresh the page; after refresh, focus border color always goes back to blue.
Any suggestion on why this may happen?
Let me know if you need further details.
Thanks in advance