Skip to content

Instantly share code, notes, and snippets.

@muZk
Created December 12, 2017 16:42
Show Gist options
  • Save muZk/cdc402c973c2c1e3918f81c392be5c66 to your computer and use it in GitHub Desktop.
Save muZk/cdc402c973c2c1e3918f81c392be5c66 to your computer and use it in GitHub Desktop.
How to use hover with react-jss

How to use hover with react-jss

Let's say we have the following style:

const styles = (theme) => ({
  title: {
    color: theme.color,
  },
})

and we want to change the color when the title is hovered.

In plain CSS we can use psudo classes:

.title:hover{
  color: 'red',
}

While in SASS/LESS:

.title{
  &:hover{
    color: 'red',
  }
}

To accomplish this with react-jss, we can use & operator just like in SASS/LESS:

const styles = (theme) => ({
  title: {
    color: theme.color,
    '&:hover': {
      color: theme.hoverColor,
    }
  },
})

There is a working example: https://codesandbox.io/s/kw3wx5ymkv

@vaibhavhrt
Copy link

thanks

@blakmer
Copy link

blakmer commented Oct 30, 2019

Does this example work with :checked?

@kysen
Copy link

kysen commented Nov 15, 2019

Thanks

@muZk
Copy link
Author

muZk commented Nov 15, 2019

Does this example work with :checked?

If that works with CSS, should work here 👍

@volodymyr-sakharov-exa
Copy link

volodymyr-sakharov-exa commented Jan 3, 2020

And what about sub elements? For example, I have 3 text elements:

    <div class="wrapper">
      <div class="text1">text 1</div>
      <div class="text2">text 2</div>
      <div class="text3">text 3</div>
    </div>

And I want to hover .wrapper, and to have all 3 text blocks changed their color. What should I do?
Also, I use class hashing, so final classes will be like .wrapper-dfg346

@Hungreeee
Copy link

Hungreeee commented Apr 18, 2020

i want to ask sir how can I change other classes when one is hovered.
I tried to use '&:hover > + ~ ' but it never worked

@vnoitkumar
Copy link

vnoitkumar commented May 27, 2020

i want to ask sir how can I change other classes when one is hovered.
I tried to use '&:hover > + ~ ' but it never worked

prefix the class with a $. For example: "&:hover $someClass"

check this link

@albertBarsegyan
Copy link

thank brother

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment