Creating a dynamic feature in React where multiple icons change color individually when hovered over, all implemented

I'm looking to customize the icons in my footer by changing their colors when users hover over them. I have already created a CSS class with the necessary hover effects, but now I want to pass a parameter in my JSX file that specifies which color should be applied to each individual icon. Here's my current CSS class:

.icon-background {
    color: rgb(49, 45, 44, 0.8);
}

.icon-background:focus,
.icon-background:hover {
    background-color: var(--color);
    transition-duration: 0.2s;
    padding: 2.5px;
}

Answer №1

A simple method to achieve this task is by utilizing utility css classes. For example, you can add an icon in the jsx file as shown below:

<div className="blue default_icon">ICON</div>

Here is the corresponding css for the above code:

.blue:hover, 
.blue:focus {
  background-color: #0000ff;
}

.default_icon {
  color: rgb(49, 45, 44, 0.8);
}

.default_icon:hover {
  transition-duration: 0.2s;
  padding: 2.5px;
}

An alternative approach would be using React state. You can create an icon component and pass a color as a prop for better scalability with less css:

import { useState } from 'react';

const Icon = (props) => {
   const hoverColor = props.hoverColor;
   const [isHover, setIsHover] = useState(false);

   const handleMouseEnter = () => {
      setIsHover(true);
   };

   const handleMouseLeave = () => {
      setIsHover(false);
   };

   const hoverStyle = {
      backgroundColor: isHover ? hoverColor : "defaultColor",
   };

   return (
         <div
            style={hoverStyle}
            onMouseEnter={handleMouseEnter}
            onMouseLeave={handleMouseLeave}
         >
            ICON
         </div>
   );
};

export default Icon;

You can now use the icon anywhere in your project like this:

<Icon hoverColor={"#0000ff"} /> // Hovering over this would turn it blue

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

Icons are failing to display in the dropdown menu of MUI after an option is selected in ReactJS

There seems to be an issue with the icon in the select tag. Initially, it is not showing which is correct. However, when you click the dropdown, the icon appears as expected. But after selecting an option, if you click the dropdown again, the icon disapp ...

Creating duplicates of elements and generating unique IDs dynamically

I'm in the process of cloning some form elements and I need to generate dynamic IDs for them so that I can access their content later on. However, I'm not well-versed in Jquery/Javascript and could use some guidance. Here's a snippet of my ...

I noticed that my CSS style sheet is only working on three of my pages. What should I do to make sure it affects the fourth page as well?

I am currently facing an issue with my website where I have linked one .css stylesheet to 3 different pages. This is just a temporary template until I finalize the individual look of each page. After performing some div positioning on the site, most pages ...

Content that can be scrolled within a React.Box container

I am struggling with a particular view. The columnBox I have can potentially hold a large number of items, but for some reason it's not scrolling in the view. I've already tried setting overflowY to auto, but since I don't know the exact hei ...

Discovering the power of VBA in combination with Selenium to pinpoint and interact with a particular cell within a table

I am attempting to access a specific cell within a table on a webpage. The cell I need to reach has an id of "flowTile_6". It is highlighted below. I have tried multiple methods, but all resulted in errors. One approach I attempted was using the followi ...

Gulp does not generate any files

Hey there, I'm brand new to using node.js and coding in general. I recently attempted to convert SCSS to CSS using gulp. My gulpfile.js seems to be correct, but when I try running "gulp styles" in the node.js command prompt, I receive the following ou ...

When using Italics, the conflict arises between the specified line-height and the actual height of the

Recently, I encountered an issue that has me a bit perplexed: I have a span element with line-height set to 18px and font-size at 16px. Everything works perfectly when the text inside is regular; the height of the span remains at 18 pixels. The problem a ...

Error: Unable to execute function on blog mapping

I am facing an issue with my app where it fails to detect objects. Every time the component in my app calls ".map", I encounter an error message. I have double-checked that index.js is passing props correctly. Can anyone explain why this problem is occurri ...

What could be the reason my dropdown menu is not appearing on hover?

Currently, I am in the process of developing a menu using angularJS and google app script within a dialog box. I have been referring to this sample code for guidance. Instead of pasting all my code here, I can summarize what I have come up with: var a ...

Discovering MaterialUI breakpoints within CSS styling

Working on a create-react-app project, I have incorporated material-ui 1.x and customized the theme with unique breakpoints... import { createMuiTheme } from '@material-ui/core/styles'; let customTheme = createMuiTheme({ breakpoints:{ val ...

Unable to eliminate hover underline on link

Struggling to remove the underline from all links globally in my NextJS project has been quite a challenge. I've attempted various methods, including CSS (by importing the globals.css file into _app.tsx) and through ChakraUI. The only solution that ...

Enhancing the level of abstraction in selectors within Redux using TypeScript

Here is a custom implementation of using Redux with TypeScript and the connect method. import { connect, ConnectedProps } from 'react-redux' interface RootState { isOn: boolean } const mapState = (state: RootState) =&g ...

Angular select automatically saves the selected option when navigating between views

I would like the selected option in my dropdown menu to stay selected as I navigate through different views and then return back. Here is the view: <select ng-model="selectedSeason" class="form-control" ng-options="season as 'Season '+ seas ...

What is preventing me from mapping an array to Material UI Chip components?

I'm currently working with Next.js and I'm trying to map an array of elements to Material-UI chip components. However, when I compile the code, I encounter the following error: Error: Element type is invalid: expected a string (for built-in comp ...

Methods for inserting text into a WebBrowser Document without retrieving any Attributes in vb.net

Is it possible to retrieve text from a WebBrowser Document in vb.net without retrieving any attributes?! For example: <h1>text here</h1> Or: <h1 name="anything">text here</h1> How can I extract the "text here" within the <h1 ...

Changing direction of arrow upon dropdown menu opening with JavaScript

I have developed a piece of code that enables users to click on a div in order to reveal a dropdown menu containing radio buttons. My goal is to make the arrows rotate 180 degrees when the dropdown menus are opened, and then return to their original positi ...

Utilizing Azure Mobile Services with HTML

Is there a way to integrate my Windows Azure Mobile Services into an HTML page? I attempted to utilize the code snippets provided in the Azure portal, but unfortunately, they did not work for me. The prescribed code snippets that need to be included in m ...

How to efficiently retrieve a form's data from multiple forms with identical ids in JavaScript

I am facing a challenge with multiple forms on the html page that have the same ID. I need to send each form's information to the server based on user selection, but I haven't been able to find a relevant solution. For my Authorization polic ...

Encountering Nested CSS bugs while implementing Tailwind in a Next.js/React project

Just diving into the world of Next.js/React. I recently went through the Tailwind CSS for Next.js tutorial and successfully integrated Tailwind into my project by following these steps: npm install -D tailwindcss postcss autoprefixer npx tailwindcss init - ...

Changing the sliding underline effect in a jQuery navigation bar

Recently, I implemented a sliding underline element in my navigation bar. The idea is that when a link is hovered over, the underline smoothly transitions to that element. You can take a look at the codepen example here: https://codepen.io/lucasengnz/pen/e ...