10

i upgraded from material ui version 3 to 4 and would like to override: .MuiOutlinedInput-root:hover .MuiOutlinedInput-notchedOutline

since I think this update introduces the hover state which is changing my current UI.

I used createMuiTheme()

and have tried the following but none of those worked:

MuiOutlinedInput: {
    root: {
        '&:hover': {
            '&$notchedOutline': {
                borderColor: '#f00',
            }
        },
    },
}


MuiOutlinedInput: {
    root: {
        '&$hover $notchedOutline': {
            borderColor: '#f00',
        },
    },
}

what am I doing wrong, hope someone can help

Ryan Cogswell
  • 75,046
  • 9
  • 218
  • 198
ducci
  • 351
  • 1
  • 4
  • 15

1 Answers1

13

You were quite close. The correct syntax is a combination of aspects from your two attempts.

The "hover" state is controlled via the ":hover" pseudo-class (it is not a rule name as referenced in your second example with $hover), so your first example correctly uses &:hover to match the hover state of the input; however the $notchedOutline class is applied to a descendant of the root element (not the root element itself) so you need the space between the root reference and the $notchedOutline reference as in your second example.

Here is a working example:

import React from "react";
import ReactDOM from "react-dom";
import TextField from "@material-ui/core/TextField";
import { createMuiTheme, MuiThemeProvider } from "@material-ui/core/styles";
const theme = createMuiTheme({
  overrides: {
    MuiOutlinedInput: {
      root: {
        "& $notchedOutline": {
          borderColor: "green"
        },
        "&:hover $notchedOutline": {
          borderColor: "red"
        },
        "&$focused $notchedOutline": {
          borderColor: "purple"
        }
      }
    }
  }
});
function App() {
  return (
    <MuiThemeProvider theme={theme}>
      <TextField variant="outlined" defaultValue="My Text" />
    </MuiThemeProvider>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

Edit OutlinedInput

Related answers:

Ryan Cogswell
  • 75,046
  • 9
  • 218
  • 198
  • hi Ryan, argh so close, thanks a lot for pointing out. Hope this can help others as well – ducci Sep 27 '19 at 14:07
  • Thanks a lot, it was helpful for me too, if you can tell me how to set the same property using classes obj instead of overwrite – jsLearner Aug 10 '21 at 09:52