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

What is the best way to display JSX/HTML content that is stored in a variable or passed through

const Footer = () => { let text="<b>Hey</b>" return ( <div> Displaying bold text: {text} </div> ) } The code above will only display Displaying bold text: <b>Hey</b>. How can I ensure that the bol ...

What level of trust can be placed in MUI Global Class names when using JSS?

Here is my current code snippet: const formControlStyles = { root: { '&:hover .MuiFormLabel-root': { } } } Would it be considered secure to utilize the class name in theme overrides for connecting with other components? Furthe ...

Differentiating page URLs in NextJS based on language

I'm currently developing a website with multiple languages and I would like the page URLs to vary based on the language. For instance, www.website.com/anasayfa and www.website.com/home lead to the same page but have different URLs. I've been us ...

Combining left-aligned and centered elements within a grid using flexbox

Looking to create a fluid responsive grid layout using flexbox, without the need for media queries. The number of elements in the grid can vary and each item should have a fixed, equal width with left alignment. The entire group should have equal left and ...

Is it possible for media queries to effectively support mobile devices?

I have a specific requirement for my <input type="number"> element where I need the up and down spinner buttons to be displayed for selecting the next value. However, on mobile devices, these spinner buttons are not visible. To address this ...

Retrieve information and update state right before component display in React Native

Consider this scenario: I have a modal that displays data from its state, which is an array. The state is set in the componentDidMount() function as recommended in the documentation. My goal is to display updated data every time the modal opens. I was able ...

Dynamically altering the CSS4 variables in real time

I am facing the challenge of managing multiple CSS4 variables, including primary, for various companies. How can I dynamically change the primary CSS4 variable color based on the company? Note: My specific requirement is to update the primary variable glo ...

What makes the nav class different from the navbar class in Bootstrap?

Recently, I delved into learning about bootstrap and web development. To my surprise, I stumbled upon the nav and navbar classes in bootstrap 4. I'm curious to know what sets them apart from each other and when it's best to use one over the other ...

Expanding Side Panel feature in Bootstrap 4+

Attempting to create a collapsible sidebar using Bootstrap 4+. I managed to find code for Bootstrap 3.7.3 from here, but after converting it to Bootstrap 4+, the layout doesn't appear as intended. I'm not well-versed in HTML styling and would gre ...

Unable to locate the 'worker_threads' module using Node version 16.14.2

I'm facing an issue with my monorepo project which contains multiple next.js apps managed by NX. Whenever I attempt to run the command sudo npx nx serve appname on Ubuntu WSL, I encounter the error message Cannot find module 'worker_threads' ...

How about designing a button with a dual-layered border for a unique touch

Looking for a specific button: My attempted CSS code that's not working: button { font-family: 'Ubuntu', sans-serif; font-size: 1em; font-weight: bold; color: white; border: 3px double #f26700; background: #f26700; ...

Setting a default currency value (either in dollars or euros) in an input field

Is there a way to automatically insert a currency symbol (€, $) in a text field by default when entering a number? Are there any default values for this feature? Thank you Here is an example ...

Attempting to imitate the hover effect of a scroll-down button

Looking for advice on creating a scroll down button with a hovering effect similar to the one found on this website - . Any tips or suggestions would be greatly appreciated. ...

Is there a way to incorporate an 'input' type within an Icon Button component from Material UI in a React project?

Kindly review my code below. I have revamped it for clarity so you won't need to stress about important details! const test = () => { return ( <label > <input type='file' multiple style={{ display: &ap ...

Troubleshooting tips for resolving issues when launching Nx React + Express

[webpack-dev-server] [HPM] Encountered an issue while forwarding request localhost:4200/api to http://localhost:3333/ [ECONNREFUSED] (https://nodejs.org/api/errors.html#errors_common_system_errors) While setting up my nx app with react and express, I face ...

Why is my array.sort statement in ReactJS not functioning properly?

This question has been puzzling me for ages, despite the fact that it has probably been answered countless times. I have an array called products that contains various product objects, each with properties like name, price, amount, store name, and image UR ...

Effortless gliding towards the left

I am looking to incorporate buttons for smooth horizontal scrolling within my container. Currently, the functionality is in place but I would like to enhance its smoothness. How can I improve the scrolling experience? Should I consider using a different p ...

What is the method for attaching a keypress event to an HTML document?

Looking to add an interactive touch to my website by creating a "press any key" page. When a key is pressed, I want it to kick off animations that bring the page to life - like sliding elements in from different directions. Open to using jQuery or plain ...

`How can you indicate a clicked linkbutton from a group of linkbuttons in asp.net?`

There is a single aspx page with a set of link buttons on it. Link Button 1 Link Button 2 Link Button 3 Link Button 4 Link Button 5 When any of the link buttons are clicked, they should be highlighted. All these link buttons are contained within a table ...

Are DIV elements really impossible to click using selenium Web Driver?

Can DIV elements be clicked using selenium Web Driver? For example, I'm having trouble clicking the delete button in Gmail. https://i.stack.imgur.com/zsyio.png I've been trying to locate the element using the XPATH = //div[@aria-label='De ...