JSS plugin for classes composition
npm install jss-composeThis plugin allows you to use CSS frameworks and legacy code together with JSS as well as reuse Rules more granularly.
Make sure you read how to use
plugins
in general.

To combine JSS with CSS framework like Material Design Lite or Bootstrap and others.
``javascript`
const styles = {
button: {
// Using space separated class names.
composes: 'btn btn-primary',
color: 'red'
}
buttonActive: {
// Using an array of class names.
composes: ['btn', 'btn-primary'],
color: 'blue'
}
}
Compiles to:
`css`
.button-123456 {
color: red;
}
.buttonActive-123456 {
color: blue;
}
When you use it:
`javascript``
It renders to:html`
Manage element states without rules duplication.
To reference a local rule, prefix the rule name with $ symbol.
`javascript
const styles = {
button: {
color: 'black'
},
// You can chain compositions
buttonActive: {
composes: '$button',
color: 'red'
},
buttonActiveDisabled: {
composes: '$buttonActive',
opacity: 0.5
},
// Or use arrays
disabled: {
opacity: 0.5
},
active: {
color: 'red'
},
buttonDisabled: {
composes: ['$button', '$active', '$disabled']
}
}
`
Compiles to:
`cssbuttonDisabled
.button-123456 {
color: black;
}
.buttonActive-123456 {
color: red;
}
.buttonActiveDisabled-123456 {
opacity: 0.5;
}
.disabled-123456 {
opacity: 0.5;
}
.active-123456 {
color: red;
}
/ Rule is not compiled to CSS, because it has no own properties. /`
When you use it:
`javascript``
It renders to:html`
You can compose both local and global classes at the same time.
`javascript`
const styles = {
active: {
color: 'red'
},
button: {
composes: ['$active', 'btn', 'btn-primary'],
color: 'blue'
}
}
Compiles to:
`css`
.active-123456 {
color: red;
}
.button-123456 {
color: blue;
}
When you use it:
`javascript``
It renders to:html`
- Doesn't work within global Style Sheets.
- Does not work inside of nested rules.
- When composing local rules, they need to be defined first. Otherwise you get wrong css selector order and specificity.
File a bug against [cssinjs/jss prefixed with \[jss-compose\]](https://github.com/cssinjs/jss/issues/new?title=[jss-compose]%20).
`bash``
npm i
npm test
MIT