Using a sibling combinator in material ui usestyles
Asked Answered
D

1

6

I'm trying to change the background of one div on the hover of another using material ui.

The standard css is:

#a:hover ~ #b {
   background: #ccc;
}

This is my attempt in material ui.

const useStyles = makeStyles(theme => ({
a: {
  background:'#fff',

},
b: {
    background:'#fff'
    '&:hover ~ #a': {
       background:'#eee'
    }
}));
Disability answered 13/12, 2019 at 18:33 Comment(0)
T
11

$ is the character to use when referencing another rule name ("b" in this case). You can find this documented here. #b would be the CSS syntax for matching an element with an id of "b".

Below is a working example.

import React from "react";
import ReactDOM from "react-dom";

import { makeStyles } from "@material-ui/core/styles";

const useStyles = makeStyles({
  a: {
    "&:hover ~ $b": {
      backgroundColor: "#ccc"
    }
  },
  b: {}
});
function App() {
  const classes = useStyles();
  return (
    <div>
      <div className={classes.a}>This is a</div>
      <div className={classes.b}>This is b</div>
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

Edit Sibling selector JSS

Trustworthy answered 13/12, 2019 at 19:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.