Created
September 10, 2015 23:52
-
-
Save adjohu/6d519dd395f3b0aec02c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nice!