Griffel: The Issue of Child Class Rules Overriding Parent Element Hover in CSS-in-JS

My first experience with Griffel is proving to be a challenge. I am struggling to achieve a simple task where a button is hidden by default and should become visible when the user hovers over its parent element.

In my React application, the button component resides within the parent div:

const styles = useStyles();

<div className={styles.userContainer}>
  <Button className={styles.removeUserButton} />
</div>

CSS:

export const useStyles = makeStyles({
  removeUserButton: {
    display: 'none'
  },
  userContainer: {
    backgroundColor: '#fefefe',
    '&:hover': {
      '& removeUserButton': {
        display: 'inline-block'
      } 
    }
  }
})

I tried creating a variable for the class name as well, but it did not work:

const removeUserClassName = 'removeUserButton';

In CSS:

[removeUserClassName]: {
  display: 'none'
},
userContainer: {
  backgroundColor: '#fefefe',
  '&:hover': {
    [`& ${removeUserClassName}`]: {
      display: 'inline-block'
    }
  }
}

Unfortunately, this approach also failed to produce the desired outcome. If anyone has experience with Griffel and knows how to solve this issue, your guidance would be greatly appreciated. Thank you!

Link to sandbox: https://codesandbox.io/s/griffel-hover-issue-gwx1sj

Answer №1

You may encounter an issue when trying to use a classname specified in your configuration (removeUserClassName). Griffel utilizes dynamic class names, meaning you will need to employ a different selector.

To understand the nature of dynamic class names better, check out this example in this playground.

If you want a working demo, here is a StackBlitz that uses the button as a selector instead of a class name, and it functions correctly.

For your reference, here is the code snippet:

import { makeStyles } from '@griffel/react';

export const useStyles = makeStyles({
  removeUserButton: {
    display: 'none',
  },
  userContainer: {
    width: '150px',
    height: '50px',
    backgroundColor: 'black',
    ':hover': {
      '& button': { display: 'inline-block' },
      backgroundColor: '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

Discovering the most recently selected element in jQuery

Currently reading a jQuery tutorial from the Head First series and practicing with an example where I need to identify the last selected element. In Chapter 2, there is a task involving four images on a page. When a user clicks on any of these images, the ...

Utilizing tag keys for inserting text and adjusting text sizes within a Web application

Creating an online editing interface for coursework where keyboard events are used. The goal is to have the tab key insert text, while also reducing the size of the text on that line. However, upon using getElementById, an error message pops up stating: ...

What is the best way to retrieve a variable within an arrow function?

I'm attempting to pass the value of score, which is inside the getScoreText arrow function, as a prop to a child component. However, I am struggling to even console log it from outside. Is what I want to achieve possible? const getScore = () => { ...

Is there a way to specifically import only variables and mixins from Sass stylesheets?

Using the Zurb Foundation 4 (S)CSS framework has presented a challenge with massively duplicated styles. Each file that I import 'foundation' into brings along all of Foundation's styles, resulting in redundant declarations for body, .row, . ...

Leverage NGinx reverse proxy to enable create-react-app

I am currently in the process of setting up a ReactJS application on a remote Ubuntu server. To be able to test it in the browser, I am utilizing the NGinx reverse-proxy functionalities as shown below. server { listen 80; server_name mentalg.com; ...

Tips on updating arrow button icon when clicked using jquery

I am currently working on a project where I have a button icon that I want to change upon clicking it. I am using the following jQuery code: <script> $('div[id^="module-tab-"]').click(function(){ $(this).next('.hi').sl ...

The Ant Design form.setFieldsValue method does not trigger a re-render when a Form.Item contains multiple elements

Struggling to update a Cascader field value using an "onClick" event within a Form.Item element in the Antd Form Component. It seems that the issue lies in the inability of Form.Item to update the field value when there are multiple elements inside the ta ...

Trouble centering a list within a parent div using CSS

Even though this issue seems straightforward and many others have asked similar questions on the site, I find myself stuck after reading through several solutions and attempting various fixes. Why is this happening? My problem involves centering a navigat ...

Can google-spreadsheet-to-json be utilized and deployed as a firebase function successfully?

I'm encountering difficulties retrieving data via http client using firebase function. I consistently receive the following error after deployment: Error: getaddrinfo ENOTFOUND spreadsheets.google.com spreadsheets.google.com:443 at errnoException ...

What could be causing my Apollo useLazyQuery to be triggered unexpectedly within a React hook?

import { useLazyQuery } from '@apollo/client'; import { useEffect, useState } from 'react'; import { ContestSessionResponseInfoObject, GetSessionDocument, HasAccessToRoundDocument, } from '@/graphql/generated/shikho-private- ...

The error mentioned above was triggered by the <app> module

import './App.css'; import React from 'react'; import axios from 'axios'; import {useState, useEffect} from 'react'; function App() { const [data, setData] = useState([]); useEffect(() => { axios . ...

Minimize the gap between the input text and the lower border

Is there a way to bring the input text field and text closer together by reducing the space between the bottom border and the text? #a { padding: 1px 3px; background: transparent; border: 0; outline: 0; border-bottom: 0.8px soli ...

Designing a card flip animation featuring front and back faces without using absolute positioning

Looking to enhance my website with a new feature that incorporates the card-flipper schema found on David Walsh's site: https://davidwalsh.name/css-flip. The challenge arises from the fact that the content within my cards is dynamic, making the height ...

Service Denial - React - HTTP Proxy

This error seems to have popped up just today, as it hasn't shown up before. While creating a new react app, I encountered 1 high vulnerability: High: Denial of Service Package: http-proxy Patched in: No patch available Depen ...

Displaying the value of a jquery variable in an HTML document

I'm tackling a problem differently today compared to yesterday, but my knowledge of jQuery and JavaScript is quite basic. My goal is to increment the transform value of a div every 5 seconds: <div style="transform: translateX(0px);" id="slide_ima ...

The clearCookie function does not seem to be effectively clearing cookies when viewed in the browser's developer tools

I'm having trouble implementing a Sign Out feature on my React-Express Web App. The cookie is not getting cleared as expected. Here is the code used to set the cookie: res.cookie('!', t, { maxAge: 1000 * 60 * 60 * 24 * 30, httpOnly ...

Tips on sending values to register in react-hook-form 7

As of version 7, react-hook-forms no longer use refs. I have been attempting to create a dynamic form by taking field props from a JSON file, but every time I try to register the useForm, I encounter a type issue. const {register , and so on..}= useForm({d ...

When downloading with PDF React Renderer, images fail to appear

I am facing a peculiar issue with React PDF Renderer. I have a document with an Image located in the public folder. The image displays perfectly fine using the PDFViewer component. However, when I try to download the document, the image always disappears. ...

Undo changes in Material UI Data Grid cell editing

Utilizing Material UI's Datagrid for cell editing has been a smooth experience so far. However, there is an issue where if an error occurs during persisting a value, I need to revert back to the initial state. I initially attempted to refetch data fr ...

Looking to utilize Material UI's features without relying on pre-built components

I'm exploring Material UI for the first time. I'm working with React and using both class and functional components. However, when trying to incorporate Material UI components and features like makeStyles into a class component, I encountered a h ...