Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

reactjs - Using material ui createStyles

I'm trying to transform my css from a less file to material createStyles and i can't get my head around how it works.

I understand the basics of the createstyles but i can't my child selector working

All i want is to be able to access the css class missionStatusLibelle

.missionStatus {
  display: flex;
  align-items: center;
  height: 34px;
  width: 100%;

  .missionStatusLibelle {
    align-items: flex-start;
    justify-content: flex-start;
    margin-left: 10px;
    font-size: 14px;
    font-weight: 500;
    line-height: 20px;
  }

}

like

<div className={styles.missionStatusLibelle}>

And transform it into something like this

const useStyles = makeStyles((theme: Theme) =>
  createStyles({
    missionStatus: {
      display: "flex",
      alignItems: "center",
      height: "34px",
      width: "100%",

      missionStatusLibelle: {
        alignItems: "flex-start",
        justifyContent: "flex-start",
        marginLeft: "10px",
        fontSize: "14px",
        fontWeight: 500,
        lineHeight: "20px"
      }
    }
}));

But i can't get to missionStatusLibelle apart from using pseudo-selectors.

const styles = useStyles();
<div className={styles.missionStatusLibelle}>

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You want something like the following:

const useStyles = makeStyles((theme: Theme) =>
  createStyles({
    missionStatus: {
      display: "flex",
      alignItems: "center",
      height: "34px",
      width: "100%",

      "& $missionStatusLibelle": {
        alignItems: "flex-start",
        justifyContent: "flex-start",
        marginLeft: "10px",
        fontSize: "14px",
        fontWeight: 500,
        lineHeight: "20px"
      }
    },
    missionStatusLibelle: {}
  }
));

Here's the relevant documentation: https://cssinjs.org/jss-plugin-nested/?v=v10.0.0-alpha.24#use-rulename-to-reference-a-local-rule-within-the-same-style-sheet


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...