1

I'm using Griffel https://griffel.js.org/ for the first time and I am struggling to achieve this simple thing. By default this button is hidden and when the user hover's over the parent I want to make the child visible.

I am using React so I have this button component inside 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 was not sure if it will know what the removeUserButton class is so I also tried by making it variable. So I added:

const removeUserClassName = 'removeUserButton';

on top of the file and then in CSS:

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

But this still does not work. And in case you are wondering I have also tried the first piece of code by adding . before removeUserButton.

Has anyone used Griffel and knows how to handle this? Appreciate any guidance. Thank you!

Here is a sandbox I created: https://codesandbox.io/s/griffel-hover-issue-gwx1sj

1
  • I can try to create a sandbox, issue is that hover part does not work. Also this worked fine when I was using regular CSS modules. Right now I am in a process of moving code to Griffel and cannot get this to work Commented Feb 3, 2023 at 0:34

1 Answer 1

1

The issue is that you are attempting to use a classname that is in your configuration (removeUserClassName). Yet, Griffel uses dynamic class names. So you'll need to use a different selector.

You can see the nature of the dynamic class names in this playground example.

Here is a working StackBlitz that uses the button as selector rather than class name and it works fine.

Here is the code for reference:

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',
    },
  },
});
Sign up to request clarification or add additional context in comments.

11 Comments

sorry that was a typo here, in my code I do have quotes.
I have added quotes to the code in my question
I've changed the answer. Hope this helps.
yeah works with a button, guess if I cannot figure it with the class I can change code to this
There are many, many ways to use CSS selectors that can allow you to select any element very precisely. Using the classname is unnecessary really.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.