Alter the focus and hover color of Material-UI's outlined Chip component

Having trouble applying styles to a Material-UI chip with an outlined variant when hovering, but the expected results are not showing.

The border color changes to white as intended, but for some reason, the background color remains unchanged.

I'm starting to question whether backgroundColor is still the correct property to use in this case. Is there another property that should be used instead?

const CustomChip = withStyles(theme => ({
  root: {
    "&:hover": {
      borderColor: "white",
      backgroundColor: "green"
    }
  }
}))(Chip);

Answer №1

Here are the default background-color styles for the outlined version of Chip:

    /* Root element styles for `variant="outlined"`. */
    outlined: {
      backgroundColor: 'transparent',
      '$clickable&:hover, $clickable&:focus, $deletable&:focus': {
        backgroundColor: fade(theme.palette.text.primary, theme.palette.action.hoverOpacity),
      },

The code snippet above uses $clickable& to represent

.MuiChip-clickable.MuiChip-outlined
. Notice how it includes two class names plus a pseudo-class (:hover or :focus), giving it higher specificity than a single class name override rule. To successfully override these default styles, your custom rule must match or exceed this level of specificity.

An easy way to achieve this is by doubling the &, effectively duplicating the generated class name in the rule and boosting its specificity to align with the defaults.

For instance, consider the following working example:

import React from "react";
import { makeStyles, withStyles } from "@material-ui/core/styles";
import Avatar from "@material-ui/core/Avatar";
import Chip from "@material-ui/core/Chip";

const useStyles = makeStyles((theme) => ({
  root: {
    display: "flex",
    justifyContent: "center",
    flexWrap: "wrap",
    "& > *": {
      margin: theme.spacing(0.5)
    }
  }
}));

const StyledChip = withStyles({
  root: {
    "&&:hover": {
      backgroundColor: "purple"
    },
    "&&:focus": {
      backgroundColor: "green"
    }
  }
})(Chip);
export default function SmallChips() {
  const classes = useStyles();

  const handleClick = () => {
    console.info("You clicked the Chip.");
  };

  return (
    <div className={classes.root}>
      <StyledChip variant="outlined" size="small" label="Basic" />
      <StyledChip
        size="small"
        variant="outlined"
        avatar={<Avatar>M</Avatar>}
        label="Clickable"
        onClick={handleClick}
      />
    </div>
  );
}

https://codesandbox.io/s/override-outlined-chip-background-i9jz9?fontsize=14&hidenavigation=1&theme=dark

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

What could be causing the "directory not empty" error to appear in Podman when running the command "npm install"?

I recently started using Podman and I'm attempting to run a React application with Podman on WSL Ubuntu. Below is the Dockerfile I am using: #building react app FROM node:lts-alpine WORKDIR /home/dcaron/React-Admin\ Podman\ Test/client COPY ...

Create a new chart in React using Chart.js when the button is clicked

I am currently working on a bar chart that dynamically adjusts its data based on the selected year. The name of the year is passed from the main component to the chart component in the following manner: Main Component: <DonateChart year={this.state.ac ...

Disappearing Act: The Vanishing React.js Dialog Component that Lurks

Currently, I am utilizing a Dialog component from Material-UI and, overall, it is functioning correctly. However, there is an issue where if I click away from the Dialog, it disappears as expected, but occasionally it remains in the DOM with an opacity of ...

Create the columnHeader to match the width of the CSS column

Here's what I attempted: http://codepen.io/helloworld/pen/BcjCJ I aim for each column to have the same width as the column header visually. Currently, a slight difference in width exists between the column header and the actual column. Refer to the ...

When the React-bootstrap Popover arrow is oriented vertically, the arrow colors appear to be inverted compared to when it

Utilizing a react-bootstrap Popover component, I have implemented custom styling for the arrow with a gray color and 50% opacity. The CSS code that enables this customization is as shown below: .popover .popover-arrow::before, .popover .popover-arrow::afte ...

Apollo: An Abundance of Mutations

I'm struggling to find the right solution. I need assistance with incorporating two mutations in my export default. Unfortunately, I'm unsure of the correct approach to achieve this. Current code: import React from 'react'; import gq ...

The inline display property does not function properly with the <li> element

Here is the HTML code snippet I am working with: <div class="nav"> <ul class="nav-dots"> <li class='ni n1'><a href="" class='nia'>dot</a></li> <li class='ni n2'><a href="" clas ...

Ensuring Textfields in React Material UI Stay Aligned on a Single Row

I am trying to ensure that my three text fields always occupy the same row, even when resizing the page. These text fields are within a card element, and I attempted to use grid layout without success. Below is the code snippet: <CardContent style={{p ...

Exploring the capabilities of Angular 9 with mat-table's multiTemplateDataRows feature to enhance the user experience by enabling simultaneous hover effects on multiple rows

Seeking advice as a newcomer to Angular, I am encountering an issue with mat-table and multiTemplateDataRows. Essentially, each element from my data source is displayed on two rows in the table. When I hover over row 1, only row 1 is highlighted, and the s ...

updating view based on redux store changes using React Navigation

Currently seeking a effective solution for redux action based redirection with react-navigation Situation at hand: In App.js, the redux store is initialized and Main.js component is rendered. Within the Main component, waiting for redux-persist's reh ...

Show side by side using javascript

My dilemma lies in having a set of cards that are meant to be displayed inline, but I have to initially hide them using "display: none". When a specific button is clicked, I aim to reveal these cards; however, upon doing so, each card seems to occupy its o ...

Conceal the initial value in a dropdown menu in a React component

I've set up a codesandbox to demonstrate the issue (https://codesandbox.io/s/practical-flower-k6cyl?file=/src/App.tsx) Is there a way to prevent the "AGE" text (first option) in the select box from being selected again? It should only be visible when ...

Using Javascript/JQuery to apply pixel values in CSS styling

Recently, I've come across a discrepancy between two different ways of accessing CSS properties in jQuery: <foo>.css('marginTop') (which I always assumed was the standard notation) and <foo>.css('margin-top') (which ...

.then function not functioning properly in Axios DELETE request in a React project

I am currently facing an issue with calling a function to update the array of notes after deleting a note from the database. The function causing the error is called deleteNote, and the function I intend to call within the .then promise is getNotes. Here i ...

Autocomplete Dropdown failing to select default value when defaultValue option is set

stateNPAValue[formData.state.vale] = 0: "All",1: "959", 2: "203",3: "860", 4: "475" // API response for NPA data const [selectedNamesState, setSelectedNamesState] = useState([]); const transformedNpaData ...

Load React single-page application based on the specified route

I recently built a react-router-redux SPA and everything seems to be working great. However, I am facing an issue where regardless of the URL the user enters, the server always sends index.html and the application starts at the root route ('/'). ...

Exploring the versatility of complex data types within a MaterialUI drop-down menu: Prioritizing focus, seamless toggling

Edit: I have revised this to make the problem clearer. I am attempting to create an accessible drop-down menu using Material UI where the Menu options consist of elements like check boxes and switches. To ensure accessibility and usability, it must meet t ...

Revert button design to default after second click

Looking for some assistance here. I have a button that toggles between displaying and hiding information. When the information is displayed, it should have one style, and when it's hidden, it should have another. Currently, I'm able to change the ...

What is the best way to apply a conditional class to multiple elements with jQuery?

Hey there, I'm new here so please forgive any mistakes I might make or if I break any etiquette rules! I'm trying to create some cool buttons for radio inputs on a form using field yields. The idea is to have them start off in grayscale and then ...

The CORS preflight request was unsuccessful, requiring React to communicate with Express

Whenever I attempt to send a CORS request from my React application to my Node.JS and Express.js backend, I encounter the following error: Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:3001/ ...