Recently, I've been experimenting with incorporating animations and transitions into my React.js projects. In the animated GIF provided below, you can see that when I close a Message component, it smoothly fades out. Once the fade out animation completes, I then hide the component using onAnimationEnd.
My goal is to have the Components below slide up when a Message disappears. I'm currently exploring how to achieve this conceptually, whether through a CSS animation/transition approach or a React-specific method.
https://i.sstatic.net/zfRQI.gif
Message.js
import React, {PropTypes, Component} from 'react';
import MessageTypes from '../constants/MessageTypes';
export default class Message extends Component {
constructor(props, context) {
super(props, context);
this.handleClick = this.handleClick.bind(this);
this.onAnimationEnd = this.onAnimationEnd.bind(this);
this.state = {
animate: false,
hide: false
}
}
handleClick(e) {
e.preventDefault();
this.setState({animate: true});
}
onAnimationEnd() {
console.log('fdsfd');
this.setState({hide: true});
}
render() {
return (
<div ref="message" onAnimationEnd={this.onAnimationEnd} className={`card message ${this.state.hide ? 'hide open' : ''} ${this.state.animate ? 'message-transition-fade-out' : ''}`}>
<span className={`icon icon-${this.iconLevel()}`} style={{color: `#${this.iconColor()}`}}></span>
<p>
<strong>{this.props.title}</strong><br />
<span dangerouslySetInnerHTML={{ __html: this.props.message }} />
</p>
<a href="#" onClick={this.handleClick}>
<span className="icon icon-close"></span>
</a>
</div>
);
}
}
Message.scss
.message {
max-height: 150px;
transition: height 2s ease;
&.open {
height: 0; //define your height or use max-height
}
&-transition {
&-fade-out {
@include animation(0s, .3s, fadeout);
}
}
}