Z
Z
zlodiak2020-11-08 19:52:06
React
zlodiak, 2020-11-08 19:52:06

How to describe nested styles in JSS?

There is a markup consisting of three levels of nesting:

<div className={classes.wrapper}>
    wrapper
    <h1 className={classes.hello}>
      Hello
      <a className={classes.world} href="#">
        world
      </a>
    </h1>
  </div>


The corresponding stylesheet looks like this:

wrapper: {
  background: "red",
  color: "blue"
},
hello: {
  color: "green"
},
world: {
  color: "magenta"
}


As you can see, due to the lack of nesting in JSS, you have to assign classes three times in the markup through the 'className' attribute. This is not convenient because if the nesting is from 10 levels, then you will have to assign classes 10 times through the 'className' attribute.

SCSS solves this problem like this:

.wrapper {
    background: "red",
    color: "blue"
  .hello {
    color: "green"
    .world {
      color: "magenta"
    }
  }
}


As a result, the class is written only once in the markup.

Please help me implement this approach in JSS.

LIVE DEMO

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2020-11-08
@zlodiak

const styles = () => ({
  wrapper: {
    background: 'red',
    color: 'blue',
    '& h1':  {
      color: 'green',
      '& a': {
        color: 'magenta',
      }
    },
  },
});

Accordingly, when rendering a component, only wrapper is specified.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question