When you hover over them, chips transform their color

I am currently using a chip in my code and I would like to change its color when the mouse hovers over it. I attempted to achieve this by using:

hover:{
            backgroundColor: 'red',
        }

In addition, I incorporated

const StyledChip = withStyles( ...

However, I encountered an issue as it is not working as expected. Can someone provide assistance? Thank you in advance!

The code snippet looks something like this:

import React from 'react';
import PropTypes from 'prop-types';
import { withStyles } from '@material-ui/core/styles';
import Avatar from '@material-ui/core/Avatar';
import Chip from '@material-ui/core/Chip';

const styles = theme => ({
  root: {
    display: 'flex',
    justifyContent: 'center',
    flexWrap: 'wrap',
  },
  chip: {
    margin: theme.spacing.unit,
  },
});

const StyledChip = withStyles({
  root: {
    backgroundColor: 'white',
  },
  '&:hover': {
    backgroundColor: 'red',
  }
})(Chip);

function Chips(props) {
  const { classes } = props;
  return (
    <div className={classes.root}>

      <StyledChip
        avatar={
          <Avatar>
            <FaceIcon />
          </Avatar>
        }
        label="Clickable Deletable Chip"
      />
    </div>
  );
}

Chips.propTypes = {
  classes: PropTypes.object.isRequired,
};

export default withStyles(styles)(Chips);

I have explored alternative solutions but unfortunately, none of them seem to work effectively.

Answer №1

When passing an object to the withStyles function, the first level of keys represents the classes that can be accessed through the classes prop within the component. These keys correspond to the CSS class names generated by withStyles.

For example, consider the code snippet below:

const StyledButton = withStyles({
  root: {
    backgroundColor: 'blue',
  },
  '&:hover': {
    color: 'red',
  }
})(Button);

In this scenario, the StyledButton component will have access to two classes. The classes.root will set the background color to blue, while classes['&:hover'] will change the text color to red when hovered over. However, the Button component ignores the second class definition.

If the syntax is modified as shown below:

const StyledButton = withStyles({
  root: {
    backgroundColor: "blue",
    "&:hover": {
      color: "red"
    }
  }
})(Button);

The "&:hover" is now included in the definition of the root class. This results in CSS being generated like:

.root-generated-class-name {
  background-color: blue;
}
.root-generated-class-name:hover {
  color: red;
}

Here, props.classes.root would hold the value of root-generated-class-name.

Feel free to check out a live example here.

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

Enhance your visual content by incorporating text onto images with CSS/Bootstrap5

I'm having trouble getting my rows and columns to display on top of my background image. I've been struggling with this issue for a while now. Any assistance on this matter would be greatly appreciated! I'm fairly new to this, so if it&apos ...

People picker in Office UI Fabric React does not populate the suggested drop down menu

While utilizing the office ui fabric react component for people picker, I'm facing an issue where the suggested people are not populating in the dropdown. The two errors that I am encountering are: Error in CommandButton.render(): TypeError: Cannot ...

Stylish Responsive Design with HTML and CSS Centering

I am tasked with creating a landing page that features an image at the top with text, followed by a couple of videos and a 2-pixel strip line image that must stay at the bottom. <html> ... <body> <img src="topimage.png" alt="" /> <t ...

Create a boundary behind the title on a see-through backdrop

How can I create a design where there is a line to the left and right of a headline? Usually, I would use border-top on the surrounding element and style the headline with some CSS properties like this: h2 { margin-top: -10px; padding: 0 5px; b ...

Can you explain the variances between ngx-translate and ngx-i18next for me?

As ngx-i18next serves as a wrapper for i18next, I am curious about the specific differences in translation capabilities between ngx-translate and i18next. ...

Overabundance of Recursive Calls in Node.js Project Dependencies

After a tiring day at work, I noticed an alert for Windows SkyDrive showing that files couldn't be uploaded due to the path being too long. The lengthy directory structure made me chuckle at the technological limitation. However, it got me thinking: ...

Utilizing a single v-model for several elements

I am having an issue with a dropdown that is set to v-model="compose.Recipient". Based on the value of "compose.Recipient", I need another dropdown to appear as shown below: <div class="form-group" v-if="compose.Recipient==2" title="Select Class"> ...

JavaScript - returning a false error

I'm experiencing an issue with my form validation function that is supposed to check for empty fields by looping through the form elements. Here's the code snippet: function validateForm(ourform){ var formElements = document.getElementById(our ...

Leveraging Internationalization in Next.js 14 - A Comprehensive Guide

Embarking on a brand new project with Next.js 14, I've decided to use the /app directory instead of the conventional src folder. However, I've hit a roadblock while attempting to implement i18n following the guidelines provided by Vercel. Refere ...

A JavaScript function that yields no value is undefined

In my AngularJS project, I have a function that makes a GET request to the backend multiple times and returns data. Here is an example of how the function is used: function getFunction(inputData) { $http.get('request URL', { params: 'so ...

Guide to aligning a component in the middle of the screen - Angular

As I delve into my project using Angular, I find myself unsure about the best approach to rendering a component within the main component. Check out the repository: https://github.com/jrsbaum/crud-angular See the demo here: Login credentials: Email: [e ...

What is the best way to distinguish between various objects that are all in a single line?

After creating an array of objects with data such as name and id, I used the res.json() method to convert it into json format for browser use. However, when I input the array of object data, it looked like this: [ { id: 1, name: 'albany sof ...

When you utilize the overflow:hidden property in an inline-block list, you may notice some

Referring to this example: http://jsfiddle.net/CZk8L/4/ I'm puzzled by why the CSS style overflow:hidden is creating extra space at the bottom of the first li. Can anyone shed some light on this? This has been bothering me for hours, as I need the p ...

Interactive material design drop-down menu

Currently, I am working on a dynamic drop-down menu that utilizes material-ui js. However, I have encountered an issue where clicking on one menu opens all the menus simultaneously, and vice versa when trying to close them. If you would like to view the c ...

Showing off HTML tags within react-json-tree

I have incorporated the following npm package into my project: https://www.npmjs.com/package/react-json-tree My goal is to showcase a json tree in react, but I am facing a challenge on how to include an HTML tag as a JSON value. Are there any alternative ...

Ways to export redux store data to an external JSON file?

My Redux store is structured as object key value pairs listed below: { "appElements": { "layers": { "layer_1": { "scene": { "width": "100px", "height": "100px", "bgColor": "#aaaaaa", "bgImage": " ...

What is the best way to pass the values of two interlinked drop-down menus through an AJAX post request to a Django view?

Presently, I am encountering an issue with sending the values of two dropdowns to a django view. My code would have functioned correctly if the dropdowns were independent. Unfortunately, this is not the case as the first one updates the second one. Therefo ...

Maintaining the active status of section 1 after the page is refreshed using Javascript and CSS

I have a total of 3 buttons and 3 hidden sections available, which can be seen in the image below: click here for image description Whenever one of the buttons is clicked, the respective section transitions from being hidden to becoming visible: click he ...

Generating exports while utilizing the UseReducer hook method for a React application

My React hooks application includes a special actions file when userReducer is used, as shown below: export namespace PrepareReviewActions { export enum Types { TOGGLE_CONFIRMATION, TOGGLE_ALL_CHECKED, SET_EXCEPTION_TYPES, SET_ACTION_ ...

How can I differentiate between an unreachable server and a user navigating away in a $.ajax callback function?

Situation: You have a situation where several $.ajax requests to the server are still in progress. All of them end with xhr.status === 0 and xhr.readyState === 0. Possible reasons for this issue: The server might be down (EDIT: meaning it is unreachabl ...