Is it possible to modify the button icon on hover by utilizing chakra-ui and vanilla-extract?

  • My stack includes nextjs13, chakra-ui, and vanilla-extract

Seeking guidance on how to update both icon and text within a button upon hover, utilizing chakra-ui and vanilla-extract.

The current setup of my button is as follows:

<Button >
 <svg width="16" height="18">
  <use xlinkHref={path/to/iconA}></use>
 </svg>
{!isFollowing ? <span>follow</span> : <span>following</span>}
</Button>

I aim to display the 'follow' button with icon A if the logged-in user is not following the individual.
Likewise, I want to showcase "following" with icon A if the logged-in user already follows the individual.

When hovering over the button (and the current user follows the individual), I desire a transition in the button's text: from "following" to "unfollow" with red text color, as well as an alteration in the icon from iconA to iconB.

I managed to adjust the text color by applying CSS to span:

<span className={textCss}>following</span>

const textCss = style({
"":hover":{
color:"red"
}})

However, I am unsure of how to modify both the text and the icon simultaneously.

Answer №1

One possible approach is:

import * as customStyles from './custom-styles.css'
<Button className={customStyles.button}>
 <svg width="16" height="18">
  <use xlinkHref={isFollowing ? 'path/to/iconA' : 'path/to/iconAB'}></use>
 </svg>
<span className={customStyles.buttonText}>{isFollowing ? 'unfollow' : 'follow'}</span>
</Button>

To style the button and text using vanilla extract, you can do:

export const button = style({})
export const buttonText = style({
    selectors: {
        `${button}:hover &`: { 
             color: 'red'
         }
     }
})

You can refer to Vanilla extract's thorough documentation here.

Answer №2

One way I resolved the problem was by introducing a new state variable called 'isHover'.

const [isHover, setIsHover] = useState(false);

<Button onMouseEnter={() => setIsHover(true)}
        onMouseLeave={() => setIsHover(false)}>
 <svg width="16" height="18">
  <use xlinkHref={ !isSelected && isHover? path/to/imageB: path/to/imageA }> 
  </use>
 </svg>
 {!isSelected ? <span>select</span> : <span>selected</span>}
</Button>

Answer №3

Should you find yourself in need of altering the hue of an icon that already exists, a quick fix is to update the SVG file by replacing all instances of fill="curentColor" throughout the document. After making this adjustment, you can apply the following CSS code: svg:hover { fill: red; }, which will allow you to change the color upon hovering over the icon.

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

Challenges with Spreading Props in TextField Component After MUIv4 Upgrade with TypeScript

Latest Material-UI Version: 4.1.0 I'm encountering difficulties in passing props to an abstracted <TextField /> component that I've developed. Below is the snippet of code: PasswordInput.tsx import * as React from 'react' impo ...

Choose items that do not contain ::before or ::after pseudo-elements

I am looking to apply a specific font style to all text on a page except for Font Icons (specifically Font Awesome) which do not share a common class. In order to achieve this, I need to target elements in one of the following ways: Elements that do not ...

Avoid the use of the escape key

Is there a way to prevent users from submitting data if they press the escape button, even though the code for e.keyCode === 27 is not working in material ui Dialog component? I want to make sure that the data is not saved to the database if the user pre ...

How can I implement a SWR refresh from a button click in a child component using a callback function?

I am currently working on a shopping cart component in Next.js and facing an issue with refreshing the cart data after making updates. My cart component is a function as per the recommendations in the Next.js documentation (https://nextjs.org/docs/basic-fe ...

The issue with jspdf is that it is failing to generate PDF documents of

I'm currently developing a resume builder app using ReactJS. One of the functionalities I'm working on is enabling users to download their resumes as PDFs. However, I've encountered an issue with the generated PDFs when using jsPDF. The down ...

How to Create a Hover Drop Down Menu that Disappears When Not Hovered Over

When I hover over the drop down menus on my Navbar, they display perfectly. However, it's impossible to click on the items in the menu because as soon as I move the cursor away from the main nav, it disappears. I've tried adding display:block and ...

The JQuery loading symbol does not prevent keyboard interactions

I successfully implemented a Jquery busy indicator in my application using the following link: However, I noticed that even though it prevents users from clicking on input elements while loading, I can still navigate to these elements by pressing the tab ...

Repair the masthead background during overscroll

The Dilemma At the top of my webpage, I have a sleek masthead with a captivating background image that scrolls along with the page. However, there is an issue when users overscroll upwards, causing an undesirable white overflow to appear. To rectify this ...

Error in React + Jest unit test caused by Material-UI typography issue

When I run my React web app in the browser, no warnings are displayed. However, when running unit tests, I am encountering a Material-UI typography error. Warning: Material-UI: you are using the deprecated typography variants that will be removed in the ...

What methods can I implement to ensure a button illuminates only when correct information is provided?

I have developed a calculator for permutations and combinations. When either permutation or combination is selected, and the required values are entered, I want the calculate button to light up. How can I achieve this without using setInterval in my curren ...

Error: The specified module could not be located and resolved: 'child_process'

Encountering a webpack error while using ReactJS, specifically when running npm start. The error message states: `./~/csvtojson/libs/core/workerMgr.js Module not found: Can't resolve 'child_process' in '/var/www/html/ReactLogin ...

Reactjs, encountering a hitch in utilizing material UI: Incompatible hook call detected

As a newcomer to React, I decided to incorporate Material UI components into my project. After installing the components locally using npm install and importing them into my project, I encountered an error when trying to run start: Error: Invalid hook call ...

The integration of Next.js with a Custom Express Server results in incorrect content types being generated for woff and woff2 files

I recently migrated my Next.js app to a new dedicated cpu server on Digital Ocean, and now I'm facing an issue where my fonts are being served with Content-Type: text/html; charset=utf-8, resulting in a 500 error. Strangely enough, this was not a prob ...

Rating component in Material UI accommodating lengthy values

Currently, I am facing a challenge with the Material UI rating component and how to implement flex-wrap when the icons exceed the width of the parent component. When I try to apply flex-wrap: wrap to the rating component, the icons wrap as expected but un ...

Steps for changing the language in KeyboardDatePicker material ui

Currently, I am utilizing material ui on my website and leveraging the KeyboardDatePicker API with successful results. The only issue is that the months' names and button text are displayed in English, whereas I would prefer them to be in Spanish. Des ...

Tips for Aligning Images of Various Sizes in the Center of a div

My images are displayed in a horizontal line, but some have different sizes. I want to center all the images within their container div for a better look. Is there a way to do this automatically with CSS so that the images remain centered regardless of siz ...

The MUI component with hideBackDrop={true} is preventing me from clicking outside of the Drawer component to close it

I implemented a Drawer component which opens when an icon on the Map is clicked. The drawer menu appears along with a backshadow that drops over the map. Interestingly, clicking on the map while the backshadow is displayed closes the drawer without any i ...

The rule 'import/no-cycle' definition could not be located

After removing my npm package along with the package.lock.json file, I proceeded to run 'npm install' and followed up with 'npm update'. However, upon starting my application using 'npm run start', an error occurred. Upon lau ...

"Implementation of Google+ button causing the appearance of a horizontal scrollbar

Adding Facebook and Twitter sharing buttons was easy, but now I'm having trouble with Google+. No matter where I place the code on my page (using a Bootstrap grid), it always adds 2-3 pixels on the right side, creating a horizontal scrollbar: <div ...

Is there a way to conditionally redirect to a specific page using NextAuth?

My website has 2 points of user login: one is through my app and the other is via a link on a third-party site. If a user comes from the third-party site, they should be redirected back to it. The only method I can come up with to distinguish if a user is ...