Closed dariuszwisniewski closed 5 years ago
Hi @WisienDot !
This is because styleName is actually a proxy for className. It will try to find a styles object, and then use it to generate a className property (see: https://github.com/gajus/babel-plugin-react-css-modules). This is intended behaviour, and there is no way to combine className and styleName on a single component.
As for solutions, you could consider the following:
:global {}
, then import this file in your component-specific stylesheet using SASS-@import
, then use styleName for both local and global styles.BTW, another approach could be to scope any global styles in a separate React component, and adding a className prop onto that component. You could then combine both classes using some in-component magic.
container.jsx
import styles from './styles.scss';
component Container extends Component {
render() {
return (
<div className={`${styles.container} ${this.props.className}`}>{this.props.children}</div>
);
}
}
RandomComponent.jsx
import styles from './styles.scss';
import Container from 'components/Container';
class RandomComponent extends Component {
render() {
return (
<Container styleName="rand-component">Magic!</Container>
);
}
}
Description: I'm trying to make use of styleName property combined with className to separate global and local styles. It looks like styleName property doesn't work as expected or I'm missing something. Only classes from className property are attached into the html element.
Css:
React Component:
Rendered HTML:
My webpack.mix.js file:
It looks like styleName property is removed completely and not attached into the classes list. Thanks in advance for any suggestions.