Tips for updating the hover effect color on Material UI select component options in a React JS application

I've been trying to customize the hover effect for the options in the mui Auto Complete component drop-down menu, but I'm having trouble finding the right method to do so.

Here is the hover effect that I want to change: Image

I'd like to use my own color choice for the hover effect.

Below is the code for my component [please excuse any errors, as I am new to react].

I have attempted various solutions from different sources, including Stack Overflow and other websites. Unfortunately, none of them have worked for me, possibly due to a lack of understanding on my part.

My primary goal is to modify the hover effect color when the mouse hovers over the select component options. However, I am struggling to figure out how to accomplish this.

This is an image of my component

export default function SelectBox ( { ...props } ) {

    return (

        <Autocomplete
            autoComplete={ true }
            disablePortal
            id="combo-box-demo"
            options={ props.options }
            ChipProps={ { backgroundColor: "green" } } // Uncertain about the purpose of this
            sx={ {
                width: { xs: 100, sm: 130, md: 150, lg: 170 },

               // Unsure about this as well
               "& + .MuiAutocomplete-popper .MuiAutocomplete-option[aria-selected='true']" :
                {
                    backgroundColor: "#FF8787",
                },

            } }

            renderInput={ ( params ) => <TextField { ...params } label={ props.label } size='small' className='color-change' 
             sx={ {
                width: "80%", backgroundColor: "#F1F1F1", 
                '.MuiOutlinedInput-notchedOutline': {
                    borderColor: '#C6DECD',
                }, borderRadius: 2,
              "&:hover .MuiOutlinedInput-notchedOutline": {
                    borderColor: "green"
                }, "&:hover": {
                    "&& fieldset": {
                        border: "1px solid green"
                    }
                }
            } } /> }
           
        />

    );
}

Answer №1

If you want to change the background color of options when they are hovered over, you just need to add :hover to a selector for the sx prop of Autocomplete.

For example, you can test out a simplified version here: stackblitz

Modify the following selector:

"& + .MuiAutocomplete-popper .MuiAutocomplete-option[aria-selected='true']": {
  backgroundColor: "#FF8787",
};

Add :hover to select the hovered item:

// 👇 Select the hover item here
'& + .MuiAutocomplete-popper .MuiAutocomplete-option:hover': {
  // 👇 Customize the hover bg color here
  backgroundColor: "#FF8787",
};

In the full example for Autocomplete, the original selector is included to customize the selected item to match the hover effect, but this step is optional.

export default function SelectBox(props) {
  return (
    <Autocomplete
      autoComplete={true}
      disablePortal
      id="combo-box-demo"
      options={props.options}
      ChipProps={{ backgroundColor: "green" }}
      sx={{
        width: { xs: 100, sm: 130, md: 150, lg: 170 },
        // 👇 Select the hover item here
        "& + .MuiAutocomplete-popper .MuiAutocomplete-option:hover": {
          // 👇 Customize the hover bg color here
          backgroundColor: "hotpink",
        },
        // 👇 Optional: keep this one to customize the selected item when hovered
        "& + .MuiAutocomplete-popper .MuiAutocomplete-option[aria-selected='true']:hover":
          {
            backgroundColor: "hotpink",
          },
      }}
      renderInput={(params) => (
        <TextField
          {...params}
          label={props.label}
          size="small"
          className="color-change"
          sx={{
            width: "80%",
            backgroundColor: "#F1F1F1",
            ".MuiOutlinedInput-notchedOutline": {
              borderColor: "#C6DECD",
            },
            borderRadius: 2,
            "&:hover .MuiOutlinedInput-notchedOutline": {
              borderColor: "green",
            },
            "&:hover": {
              "&& fieldset": {
                border: "1px solid green",
              },
            },
          }}
        />
      )}
    />
  );
}

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

Could you assist me in retrieving information from an API request?

I can't seem to pinpoint my mistake, but I know it's there. After the user provides their state and city information and submits it, a fetch request is supposed to retrieve latitude and longitude values. These values are then used in another API ...

Obtaining media images from a Django URL using React.js

How to configure static and media files in settings.py for a Django project: STATIC_URL = '/static/' STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'frontend','build', 'static') ] MEDIA_URL='/media/' M ...

Customizing table row background color on hover or mouseover in Material UI datatable

Completely new to React and Material-UI. I'm attempting to modify the color of table rows when hovering over them with the mouse. I have tried various solutions from different posts but none have worked for me. (e.g. Root, cell, and tableRow) The x ...

React Native does not support Laravel Echo's listenForWhisper functionality

I have successfully implemented private channels and messaging in my Laravel App using websockets:serve. Now, I am attempting to enable whisper functionality for the typing status but encountering some issues. When sending a whisper: Echo.private('c ...

Can JavaScript be utilized to dynamically adjust the size of all elements on the screen to a specified percentage of their initial height and width when a certain event occurs?

I'm fairly new to the world of JavaScript, but I have a basic understanding of it. I want to optimize my personal website for mobile devices. I've already taken care of screen orientation and element positioning, everything is centered nicely and ...

Transform a hexadecimal string into a hexadecimal color representation within SCSS

I have a plethora of colors stored in JSON format. I am utilizing rootbeer with gulp to convert them into Sass maps for processing by SCSS: { "blue": "33A2FF" } into $colors: ( "blue": "33A2FF" ); I am able to use ...

Is there a way to set the menu to automatically open when the page loads?

Looking for some help with a slide out menu that I want to be always open and cannot be closed. I tried changing the open=true setting to open=false but it didn't work as expected. Here's the code snippet below. I aim to remove the open and close ...

In React, the functionality of rendering components conditionally is not functioning properly

I am looking to conditionally display either the Login component or Menubar component based on the value of the isLogin state. If the isLogin state is false, I want to render the login page. If it is true, I want to render the Menubar page. To achieve thi ...

Using multiple header tags in HTML

I have been tasked with developing a webpage featuring a prominent header at the top of the page. This header will include the navigation bar, logo, and website title. As the user begins to scroll, I want the header to transform into a compact version that ...

Utilizing version 4 of Bootstrap to create a visually appealing full

I am currently using the latest version of bootstrap, but I have encountered an issue with my sidebar being too short and not reaching full height. You can view the problem here: Is this problem caused by my CSS or is it a bootstrap issue? And how can ...

The length of the LinearProgress bar does not match the length of the navbar

Utilizing the LinearProgress component from Material UI, I created a customized ColoredLinearProgress to alter its color: import React, { Component } from 'react'; import { withStyles } from '@material-ui/core/styles'; import { LinearP ...

The default export called 'Cookies' could not be found in the 'react-cookie' library. It is possible that the specific exports are unknown

I keep encountering an error when attempting to access a cookie from the browser using 'react-cookie'. Below is the code snippet I am attempting to use: import Cookies from 'react-cookie'; function App() { const ...

What is the best way to make the SPA load with the tab displaying the highest value?

I have a React single-page application using Typescript and Material UI. One challenge I'm facing is creating a tab menu with the current month and all previous months, where the last month should be active when the page loads. Despite researching on ...

Exploring the Features of Bottom and Right in jQuery

Here is a snippet of jQuery code that I included in a sample program: $('#left').click(function(){ $('.box').animate({ left: "-=40px", }, function(){/* End of Animation*/}); }); $('#right ...

Connecting event listeners to offspring elements, the main element, and the entire document

My request is as follows: I have multiple boxes displayed on a webpage, each containing clickable divs and text. When a user clicks on a clickable div, an alert should appear confirming the click. Clicking on the text itself should not trigger any action. ...

Exploring the different color variations of React Material-UI primary and secondary colors: A guide

The React Material-UI documentation mentions that it will automatically calculate light and dark variants of your primary and secondary colors. (From the documentation: https://material-ui.com/customization/palette/) const theme = createMuiTheme({ pal ...

Learn the process of utilizing signed URLs in conjunction with ReactJS to securely store files in AWS

I am currently working on a form where I need to attach images along with regular text inputs in order to submit data to my MongoDB. Here is the code for my post creation function: const [postData, setPostData] = useState({ text: '', i ...

Troubleshooting: Issue with Bootstrap 4 navbar toggle button functionality

Is it possible to manually control the opening and hiding of the navbar when the toggle button is clicked in bootstrap 4? Currently, my navbar automatically opens and closes when clicking the toggle button. Any suggestions on how to modify this behavior wo ...

adjust the size of a form field to match the background image dimensions

I'm encountering an issue while trying to solve this particular challenge: My goal is to integrate a login box onto a full-screen image. Although I've come across numerous methods for incorporating an image into a form field, my task requires me ...

Embedding CSS stylesheets in a Django template

I'm currently working on a large Django website and in the process of adding the HTML templates. Here is the file tree for the main part: C:. +---forums | | admin.py etc | | | +---migrations | | | ... | | | +---templa ...