22

I'm experimenting with JSS to see if it is realistic to migrate a Sass code base. I have a very basic example of a CSS style that, when hovered, modifies the style of a child node.

span {
  color: red;
}

button:hover span {
  color: blue;
}
<button>
  <span>Click Me</span>
</button>

I am unsure how to write this in JSS. Something I have tried looks like:

const styles = {
  button: {
    '&:hover': {
      span: {
        color: 'blue',
      }
    }
  },
  span: {
    color: 'red',
  }
}

const { classes } = jss.createStyleSheet(styles).attach()

document.body.innerHTML = `
  <button class=${classes.button}>
    <span class=${classes.span}>Click Here</span>
  </button>
`

Thanks!

KyleMit
  • 30,350
  • 66
  • 462
  • 664
Jon Sakas
  • 2,042
  • 1
  • 21
  • 26

2 Answers2

18

Have you tried doing:

const styles = {
  button: {
    '&:hover span': {
       color: 'blue',
    }
  },
  span: {
    color: 'red',
  }
}
deowk
  • 4,280
  • 1
  • 25
  • 34
13

As mentioned in the comment by @cwouter, if it was a class name, you can do something like this.

const styles = {
  button: {
    '&:hover $some_class_name': {
       color: 'blue',
    }
  },
  some_class_name: {
    color: 'red',
  }
}
VnoitKumar
  • 1,350
  • 15
  • 29