As far as I understood its possible to add only one class to the element by doing className={styles.className} even if it's composed of many.
So at the current moment the code uses ternary operator in order to render different element styles depending on the state.cross value.
export default class Header extends Component {
constructor(props) {
super(props);
this.state = { cross: false };
this.showCross = this.showCross.bind(this);
this.showLine = this.showLine.bind(this);
}
showCross() {
this.setState({cross: true});
}
showLine() {
this.setState({cross: false});
}
render() {
return (
<div onMouseEnter={this.showCross} onMouseLeave={this.showLine} className={styles.blockClose}>
<a className={this.state.close ? styles.closeCross : styles.closeLine}> </a>
</div>
)
}
}
What it actually does it makes 2 lines to look like a cross after state has been changed and transform has been applied.
:local(.closeLine) {
...20px line
&::before {
...equal line
}
}
:local(.closeCross) {
composes: closeLine;
transform: rotate(-45deg);
&::before {
transform: rotate(90deg);
}
}
My question is:
Is it possible instead of conditional rendering just toggle class by doing smth like element.classList.toggle(className) to manage the styling of the element.
:local(.closeCross) {
transform: rotate(-45deg);
&::before {
transform: rotate(90deg);
}
}
element.classList.toggle(className)directly in your click handler or in componentDidMount, however you'd be losing the benefits of Reacts declarative programming model, therefore taking control from React.