Skip to content

Instantly share code, notes, and snippets.

@adjohu
Created September 10, 2015 23:52
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save adjohu/6d519dd395f3b0aec02c to your computer and use it in GitHub Desktop.
Save adjohu/6d519dd395f3b0aec02c to your computer and use it in GitHub Desktop.
import React, {PropTypes} from 'react';
import TransitionGroup from '../transitionGroup';
class StaggeredTransitionGroup extends React.Component {
constructor(props) {
super(props);
this.state = {
count: 0
};
}
getTotal() {
return React.Children.count(this.props.children);
}
getInterval() {
return this.props.totalTime / this.getTotal();
}
componentDidMount() {
this.interval = setInterval(::this.updateCount, this.getInterval());
}
componentWillUnmount() {
clearInterval(this.interval);
}
updateCount() {
let {count} = this.state;
const total = this.getTotal();
if (++count === total) {
clearInterval(this.interval);
}
this.setState({count});
}
render() {
const {count} = this.state;
const {children, ...props} = this.props;
const childrenToShow = _.take(children, count);
return (
<TransitionGroup {...props}>
{childrenToShow}
</TransitionGroup>
);
}
}
StaggeredTransitionGroup.propTypes = {
totalTime: PropTypes.number
};
StaggeredTransitionGroup.defaultProps = {
totalTime: 400
};
export default StaggeredTransitionGroup;
@i8ramin
Copy link

i8ramin commented Sep 11, 2015

nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment