Reputation: 1506
I am just starting to use animations/transition in my React.js
projects. In the GIF below, when I close a Message
component, it fades out. After it fades, I hide the component using onAnimationEnd
.
What I want to happen is when a Message
disappears have the below Components slide up. I am unsure conceptually how I would accomplish this either via a CSS animation/transition or via a React specific way.
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);
}
}
}
Upvotes: 1
Views: 4071
Reputation: 1506
There is an npm
module called react-collapse
that does the trick.
<Collapse isOpened={this.state.isOpened} keepCollapsedContent={true}>
//content here
</Collapse>
Here are the results:
Upvotes: 1
Reputation: 19113
I would suggest you to use css transitions
.
Have a class called open
attached to the root of the Message
component. onAnimationEnd
remove the class open
. Now, use height
to animate that class.
Pseudo code.
.message {
height: 0px;
transition: height 0.3s ease;
}
.message.open {
height: 100px; //define your height or use max-height
}
Upvotes: 2