@@ -6,20 +6,22 @@ export default class Keyframes extends React.Component {
66 static propTypes = {
77 children : PropTypes . arrayOf ( PropTypes . element ) . isRequired ,
88 component : PropTypes . string ,
9+ delay : PropTypes . number ,
910 onStart : PropTypes . func ,
1011 onEnd : PropTypes . func
1112 } ;
1213
1314 static defaultProps = {
1415 component : 'span' ,
16+ delay : 0 ,
1517 onStart : noop ,
1618 onEnd : noop
1719 } ;
1820
1921 constructor ( props ) {
2022 super ( props ) ;
21- this . state = { frameNum : - 1 } ;
22- this . delayTimer = null ;
23+ this . state = { frameNum : this . props . delay ? - 1 : 0 } ;
24+ this . timer = null ;
2325 }
2426
2527 shouldComponentUpdate ( nextProps , nextState ) {
@@ -29,12 +31,14 @@ export default class Keyframes extends React.Component {
2931 }
3032
3133 componentWillMount ( ) {
32- const frameNum = this . state . frameNum + 1 ;
33- const frame = this . getFrame ( frameNum ) ;
34+ if ( 0 === this . state . frameNum ) {
35+ this . props . onStart ( ) ;
36+ }
37+ }
3438
35- // render the first frame if it has no delay
36- if ( frame && ! frame . props . delay ) {
37- this . setState ( { frameNum } ) ;
39+ componentWillUpdate ( ) {
40+ if ( 0 === this . state . frameNum ) {
41+ this . props . onStart ( ) ;
3842 }
3943 }
4044
@@ -47,7 +51,7 @@ export default class Keyframes extends React.Component {
4751 }
4852
4953 componentWillUnmount ( ) {
50- clearTimeout ( this . delayTimer ) ;
54+ clearTimeout ( this . timer ) ;
5155 }
5256
5357 render ( ) {
@@ -59,25 +63,25 @@ export default class Keyframes extends React.Component {
5963 }
6064
6165 requestNextFrame ( ) {
62- const frameNum = this . state . frameNum + 1 ;
63- const frame = this . getFrame ( frameNum ) ;
64- if ( ! frame ) {
65- this . props . onEnd ( ) ;
66- return ;
67- }
68-
69- const { delay } = frame . props ;
70-
71- clearTimeout ( this . delayTimer ) ;
72- this . delayTimer = setTimeout ( ( ) => {
73- if ( 0 === frameNum ) {
74- this . props . onStart ( ) ;
66+ this . waitForDelay ( ( ) => {
67+ const frameNum = this . state . frameNum + 1 ;
68+ if ( this . props . children . length <= frameNum ) {
69+ this . props . onEnd ( ) ;
70+ return ;
7571 }
72+
7673 this . setState ( { frameNum } ) ;
77- } , delay ) ;
74+ } ) ;
75+ }
76+
77+ waitForDelay ( fn ) {
78+ const currentFrame = this . getFrame ( ) ;
79+ const delay = currentFrame ? currentFrame . props . duration : this . props . delay ;
80+ clearTimeout ( this . timer ) ;
81+ this . timer = setTimeout ( fn , delay ) ;
7882 }
7983
80- getFrame ( frameNum = this . state . frameNum ) {
81- return this . props . children [ frameNum ] ;
84+ getFrame ( ) {
85+ return this . props . children [ this . state . frameNum ] ;
8286 }
8387}
0 commit comments