Is it possible to modify the :hover effects of a material-ui outlined select component?

I am currently developing a react application using Material UI. My goal is to change the color of a selector to white when hovering over it. However, I have encountered difficulties in applying the style as intended. Even when directly adding classes, the desired effect is not achieved.

After trying several methods, I found a partial solution:

':hover &.MuiOutlinedInput-root .MuiOutlinedInput-notchedOutline': {
      borderColor: WhiteAlien,
    },

Unfortunately, this approach caused the styles to be applied to other components, leading to issues with timing and functionality.

Below is the code snippet that I am working with:

import React from "react";
import { makeStyles } from "@material-ui/core/styles";
import InputLabel from "@material-ui/core/InputLabel";
import MenuItem from "@material-ui/core/MenuItem";
import FormControl from "@material-ui/core/FormControl";
import Select from "@material-ui/core/Select";
import { useTranslation } from "react-i18next";

const WhiteAlien = "#fcfcfc";
const SpaceRed = "#d90429";

const useStyles = makeStyles((theme) => ({
  formControl: {
    margin: theme.spacing(4),
    minWidth: 120,
  },
  select: {
    color: WhiteAlien,
    borderColor: WhiteAlien,
    "&.MuiOutlinedInput-root.Mui-focused .MuiOutlinedInput-notchedOutline": {
      borderColor: WhiteAlien,
    },
    "& .MuiOutlinedInput-notchedOutline": {
      borderColor: "gray",
      "& :hover": {
        borderColor: WhiteAlien,
      },
    },
  },

  inputLabel: {
    color: WhiteAlien,
    "&.MuiFormLabel-root.Mui-focused": {
      color: WhiteAlien,
    },
  },
}));

export default function SimpleSelect() {
  const { t, i18n } = useTranslation();
  const classes = useStyles();

  const changeLanguage = (lng) => {
    i18n.changeLanguage(lng);
  };

  const handleChange = (event) => {
    changeLanguage(event.target.value);
  };

  return (
    <div>
      <FormControl variant="outlined" className={classes.formControl}>
        <InputLabel
          className={classes.inputLabel}
          id="lenguage-selector-input-label"
        >
          {t("Navbar.select_title")}
        </InputLabel>
        <Select
          className={classes.select}
          labelId="lenguage-selector-label"
          id="lenguage-selector"
          value={i18n.language}
          onChange={handleChange}
          label={t("Navbar.select_title")}
        >
          <MenuItem value={"es_ES"}>Spanish</MenuItem>
          <MenuItem value={"en_EN"}>English</MenuItem>
        </Select>
      </FormControl>
    </div>
  );
}

Answer №1

After some trial and error, I managed to fix the issue by making adjustments to the theme using specific functions provided by material. Ultimately, the code ended up looking like this:

import React from 'react';
import { makeStyles, withStyles } from '@material-ui/core/styles';
import InputLabel from '@material-ui/core/InputLabel';
import MenuItem from '@material-ui/core/MenuItem';
import FormControl from '@material-ui/core/FormControl';
import Select from '@material-ui/core/Select';
import { createMuiTheme, MuiThemeProvider } from '@material-ui/core/styles';
import { useTranslation } from 'react-i18next';

const WhiteAlien = '#fcfcfc';

const useStyles = makeStyles((theme) => ({
  formControl: {
    margin: theme.spacing(4),
    minWidth: 120,
  },
  select: {
    color: WhiteAlien,
  },
  inputLabel: {
    color: WhiteAlien,
    '&.MuiFormLabel-root.Mui-focused': {
      color: WhiteAlien,
    },
  },
}));

const theme = createMuiTheme({
  overrides: {
    MuiOutlinedInput: {
      root: {
        '& $notchedOutline': {
          borderColor: 'gray',
        },
        '&:hover $notchedOutline': {
          borderColor: WhiteAlien,
        },
        '&$focused $notchedOutline': {
          borderColor: WhiteAlien,
        },
      },
    },
  },
});

export default function SimpleSelect() {
  const { t, i18n } = useTranslation();
  const classes = useStyles();

  const changeLanguage = (lng) => {
    i18n.changeLanguage(lng);
  };

  const handleChange = (event) => {
    changeLanguage(event.target.value);
  };

  return (
    <div>
      <MuiThemeProvider theme={theme}>
        <FormControl variant="outlined" className={classes.formControl}>
          <InputLabel className={classes.inputLabel} id="lenguage-selector-input-label">
            {t('Navbar.select_title')}
          </InputLabel>
          <Select
            className={classes.select}
            labelId="lenguage-selector-label"
            id="lenguage-selector"
            value={i18n.language}
            onChange={handleChange}
            label={t('Navbar.select_title')}
          >
            <MenuItem value={'es_ES'}>Castellano</MenuItem>
            <MenuItem value={'en_EN'}>English</MenuItem>
          </Select>
        </FormControl>
      </MuiThemeProvider>
    </div>
  );
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Unable to locate React npm package

I'm facing a seemingly simple issue that I just can't seem to solve. I am using React and webpack, and I've recently installed a new package (braintree-web-drop-in). However, whenever I import it into my React module and try to log it out, i ...

My project is experiencing issues with the execution of JavaScript codes

Greetings, I'm having trouble with my JavaScript codes. The following code works perfectly: function Choice() { var box = document.getElementById('searchh'); if (!count) { var count = 0; } box.onclick = function () ...

Customizing React configurations for production environments

Our organization utilizes the standard Development, Test, and Production environments. The create-react-app based application we have is integrated as a content item within our CMS, requiring the execution of the npm run build command to deploy it to any e ...

Creating visual content on a website

I'm currently working on a project where I need to showcase the results of a numerical model I am operating. My goal is to gather user input in the form of latitude/longitude coordinates, utilize php (or a similar tool) to trigger a python script that ...

For the past two days, there has been an ongoing issue that I just can't seem to figure out when running npm start

After multiple failed attempts, I have exhausted all troubleshooting steps including executing npm clear cache --force, deleting node_modules/ and package-lock.json, followed by running npm install, npm build, and eventually npm run dev. The errors encoun ...

Screen size is incompatible with flash player

I've been using this code to try and make a swf file fit my screen or any screen it's on, but I've run into an issue. When using this code, the player in both Chrome and IE stretches to fit the screen width, but the height remains the same, ...

Arranging an array of objects based on a specific keyword and its corresponding value

I have an array of objects that looks like this: [ { "type": "Exam", "value": 27 }, { "type": "Lesson", "value": 17 }, { "type": "Lesson", &qu ...

Incorporated React component from Rails-assets.org into a Rails application

I have been trying to incorporate a react component from rails-assets.org into my Rails application. Currently, I am using the react-rails gem to write react view in my application. Now, I want to use other react components such as react-router, react-sel ...

The Zoom CSS property seems to be experiencing some issues on the Firefox browser

After researching the issue, I discovered several solutions involving css3 transition techniques. Currently, I have been using {zoom:1.5} for all of my buttons. However, this method does not work on Firefox. When attempting to implement the transition pro ...

Ensure child images in a flex row are adjusted to have a height equivalent to 100% of the surrounding content

Consider this example: .row { background: #F88; margin-top: 20px; } .cont { display: block; height: 82px; width: 82px; background-color: #0AF; } .txt { background-color: #0C0; } <link href="https://stackpath.bootstrapcdn.com/bootstr ...

Adjust the positioning of a class element

I have an eye icon that changes position when clicked. Initially, it is set to left: 10%;, but if there is a DOM element with the ID login-section, it should be repositioned to left: 50%;. I attempted to use document.getElementsByClassName('fa-eye-sl ...

Error occurred during JSON parsing: String in the JSON object is not properly terminated

Can someone help me with this problem I'm facing? I am trying to fetch a JSON object from my Node.js backend that is the result of a MySQL operation. Here's the code snippet: app.get('/get_events/:playgroundId', cors(), function(req,r ...

sticky header on pinned tables in a React data grid

I have combined 3 tables together, with the middle table containing a minimum of 15 columns. This setup allows users to horizontally scroll through the additional columns conveniently. However, I am facing a challenge in implementing a sticky header featu ...

The function useCollectionData in react-firebase-hooks fails to return any value

I am encountering an issue while attempting to query a collection using useCollectionData with a where query. Every time I try, it just returns undefined. I have reviewed the documentation available on GitHub, but unfortunately, I haven't had any succ ...

What could be causing the React Todo List to consistently delete the final item from the list?

I've been experimenting with creating a Todo List App using React Hooks. When I utilize <span>{todo}</span>, the app functions perfectly by deleting the clicked element. However, when I switch from <span>{todo}</span> to <i ...

V-toolip using a different design [vuetify]

I have a v-data-table configured with 5 columns and I want to add a text as a v-tooltip component. However, since it's all in one template, I am unable to use the tooltip on it without the <template #activator="{ on }">. Can anyone adv ...

Do you know the name of the jQuery tool that Mashable employs for showcasing their top 5 images and videos?

Recently, I came across an image scroller on Mashable that caught my eye and I am interested in incorporating it into a new project: I saw it in action on the website, but I am curious about what it actually is. Does anyone know if it is available as a W ...

Call a function following the dispatch action within a separate component in my React Native application

I need to trigger a function after dispatching something in another component. I cannot use promises as a solution. For instance, I want to dispatch an action in Component 1 and then call a function in the corresponding component that starts an animated ...

The following middleware is not functioning properly on a local SSL server

When I run my Nextjs app without SSL using "next dev", the middleware functions as expected without any errors. However, if I attempt to run the app with SSL enabled, an empty middleware function triggers an error. The code for the middleware function (l ...

Using jQuery to create a flawless animation

I am currently working on an animation project, and I have shared my progress on jsfiddle. Below is the code snippet I have utilized: /* JavaScript: */ var app = function () { var self = this; var allBoxes = $('.box&apos ...