Skip to content

Instantly share code, notes, and snippets.

@sjorsvanheuveln
Created August 26, 2017 23:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sjorsvanheuveln/7631eacdfa87e2be1301e55ef9d3743b to your computer and use it in GitHub Desktop.
Save sjorsvanheuveln/7631eacdfa87e2be1301e55ef9d3743b to your computer and use it in GitHub Desktop.
Easy Example for ReactTransitionGroup animation in Node
import React from 'react';
import { TweenMax } from 'gsap';
export default class Box extends React.Component {
componentWillEnter(callback) {
const el = this.container;
TweenMax.fromTo(el, 0.3, { y: 100, opacity: 0 }, { y: 0, opacity: 1, onComplete: callback });
}
componentWillLeave(callback) {
const el = this.container;
TweenMax.fromTo(el, 0.3, { y: 0, opacity: 1 }, { y: -100, opacity: 0, onComplete: callback });
}
render() {
return (
<div className="box" ref={c => this.container = c} />
);
}
}
import React from 'react';
import { TransitionGroup } from 'react-transition-group';
import Box from './Box';
export default class Page extends React.Component {
constructor(props) {
super(props);
this.state = {
shouldShowBox: true,
};
this.toggleBox = this.toggleBox.bind(this);
}
toggleBox() {
this.setState({
shouldShowBox: !this.state.shouldShowBox,
});
}
render() {
return (
<div className="page">
<TransitionGroup>
{ this.state.shouldShowBox && <Box />}
</TransitionGroup>
<button className="toggle-btn" onClick={this.toggleBox}>toggle</button>
</div>
);
}
}
.page {
height: 100%;
}
.box {
width: 100px;
height: 100px;
background-color: #3498DB;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
}
.toggle-btn {
position: absolute;
top: 20px;
right: 0;
left: 0;
margin: auto;
width: 80px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment