Skip to content

Commit e03c85a

Browse files
committed
add duration option and remove delay option from Frame
1 parent b18639a commit e03c85a

File tree

4 files changed

+36
-32
lines changed

4 files changed

+36
-32
lines changed

gulpfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ gulp.task('compile', function () {
1616
plugins: [
1717
'syntax-async-functions',
1818
'transform-async-to-generator',
19-
"transform-class-properties",
19+
'transform-class-properties',
2020
'transform-runtime'
2121
]
2222
}))

lib/frame.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import React, { PropTypes } from 'react';
22

33
export default class Frame extends React.Component {
44
static propTypes = {
5-
children: PropTypes.arrayOf(PropTypes.element).isRequired,
5+
children: PropTypes.array.isRequired,
66
component: PropTypes.string,
7-
delay: PropTypes.number,
7+
duration: PropTypes.number,
88
onRender: PropTypes.func
99
};
1010

1111
static defaultProps = {
1212
component: null,
13-
delay: 0,
13+
duration: 0,
1414
onRender: () => {}
1515
};
1616

lib/keyframes.js

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

test/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ test.after((t) => {
1414
clock.uninstall();
1515
});
1616

17-
test('return value', (t) => {
17+
test('animate', (t) => {
1818
const container = document.createElement('div');
1919
const component = render(
2020
<Keyframes>
21-
<Frame>foo</Frame>
22-
<Frame delay={100}>bar</Frame>
23-
<Frame delay={200}>baz</Frame>
21+
<Frame duration={100}>foo</Frame>
22+
<Frame duration={200}>bar</Frame>
23+
<Frame>baz</Frame>
2424
</Keyframes>,
2525
container
2626
);

0 commit comments

Comments
 (0)