How to reuse the same style rule with multiple selectors
Asked Answered
M

2

10

I want this using jss styling.

.a{
height: 20px;
}
.b{
height: 40px;
}
.a,.b{
width: 100px;
}

Try 1

Make a rule c and add the class to both a and b

c: {
width: '100px'
}

Try 2

Make a object common and merge them to both a and b rule

const common = {
   width: '100px',
};

a: {
height: '20px',
...common
}
b: {
height: '40px',
...common
}

Is there any better way possible ?

Macassar answered 7/8, 2017 at 22:20 Comment(0)
S
10

How about extend plugin (enabled by default)?

https://cssinjs.org/jss-plugin-extend

const styles = {
  buttonColor: {
    background: 'red'
  },
  button: {
    extend: 'buttonColor',
    fontSize: '20px'
  }
}
Seton answered 16/12, 2018 at 22:50 Comment(0)
A
6

A simpler alternative that I feel is easier to parse would be to set the key to be a String by wrapping in quotes:

'.a, .b': {
   width: 100px; 
}
Akkadian answered 19/2, 2020 at 19:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.