Skip to content

Commit b28a66c

Browse files
committed
update 0.10.0
1 parent e2b05c6 commit b28a66c

File tree

6 files changed

+58
-63
lines changed

6 files changed

+58
-63
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,10 @@ React.render(<TweenOneGroup>
116116
| yoyo | boolean | false | if `true`, every other repeat cycle will run in the opposite direction so that the tween appears to go back and forth (forward then backward). |
117117
| ease | string | `easeInOutQuad` | animate ease. [refer](http://easings.net/en) |
118118
| bezier | object | null | bezier curve animate |
119-
| onStart | func | null | A function that should be called when the tween begins |
120-
| onUpdate | func | null | A function that should be called every time the animate updates |
121-
| onComplete | func | null | A function that should be called when the animate has completed |
122-
| onRepeat | func | null | A function that should be called each time the animate repeats |
119+
| onStart | func | null | A function that should be called when the tween begins, callback(e), e: { index, target } |
120+
| onUpdate | func | null | A function that should be called every time the animate updates, callback(e), e: { index, target, ratio } |
121+
| onComplete | func | null | A function that should be called when the animate has completed, callback(e), e: { index, target } |
122+
| onRepeat | func | null | A function that should be called each time the animate repeats, callback(e), e: { index, target } |
123123

124124

125125

@@ -157,4 +157,5 @@ svg polygon or path values: polygon is points, path is d; [demo](http://react-co
157157
| leave | object / array / func | `{ x: 30, opacity: 0 }` | leave anim twee-one data. when array is tween-one timeline, func refer to queue-anim |
158158
| onEnd | func | - | one animation end callback |
159159
| animatingClassName | array | `['tween-one-entering', 'tween-one-leaving']` | className to every element of animating |
160+
| resetStyleBool | boolean | true | whether to animation reset the style every time |
160161
| component | React.Element/String | div | component tag |

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rc-tween-one",
3-
"version": "0.9.5",
3+
"version": "0.10.0",
44
"description": "tween-one anim component for react",
55
"keywords": [
66
"react",

src/TimeLine.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -224,18 +224,22 @@ p.render = function () {
224224
return;
225225
}
226226
}
227+
const e = {
228+
index: i,
229+
target: this.target,
230+
};
227231
// onRepeat 处理
228232
if (item.repeat && repeatNum > 0
229233
&& progressTime + fromDelay >= 0 && progressTime < this.perFrame) {
230234
// 重新开始, 在第一秒触发时调用;
231-
item.onRepeat();
235+
item.onRepeat(e);
232236
}
233237
if (progressTime < 0 && progressTime + fromDelay > -this.perFrame) {
234238
this.setRatio(item.type === 'from' ? 1 : 0, item, i);
235239
} else if (progressTime >= duration && item.mode !== 'onComplete') {
236240
this.setRatio(item.type === 'from' || (repeatNum % 2 && item.yoyo) ? 0 : 1, item, i);
237241
if (item.mode !== 'reset') {
238-
item.onComplete();
242+
item.onComplete(e);
239243
}
240244
item.mode = 'onComplete';
241245
} else if (progressTime >= 0 && progressTime < duration) {
@@ -248,19 +252,16 @@ p.render = function () {
248252
}
249253
this.setRatio(ratio, item, i);
250254
if (progressTime <= this.perFrame) {
251-
item.onStart();
255+
item.onStart(e);
252256
} else {
253-
item.onUpdate(ratio);
257+
item.onUpdate({ ratio, ...e });
254258
}
255259
}
256260
if (progressTime >= 0 && progressTime < duration + this.perFrame) {
257261
this.onChange({
258262
moment: this.progressTime,
259-
item,
260-
tween: this.tween,
261-
index: i,
262263
mode: item.mode,
263-
target: this.target,
264+
...e,
264265
});
265266
}
266267
});

src/TweenOne.jsx

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,6 @@ class TweenOne extends Component {
2121
this.paused = this.props.paused;
2222
this.reverse = this.props.reverse;
2323
this.onChange = this.props.onChange;
24-
[
25-
'raf',
26-
'frame',
27-
'start',
28-
'play',
29-
'restart',
30-
].forEach((method) => this[method] = this[method].bind(this));
3124
}
3225

3326
componentDidMount() {
@@ -108,13 +101,13 @@ class TweenOne extends Component {
108101
this.cancelRequestAnimationFrame();
109102
}
110103

111-
restart() {
104+
restart = () => {
112105
this.startMoment = this.timeLine.progressTime;
113106
this.startFrame = ticker.frame;
114107
this.play();
115108
}
116109

117-
start() {
110+
start = () => {
118111
const props = this.props;
119112
if (props.animation && Object.keys(props.animation).length) {
120113
this.timeLine = new TimeLine(this.dom, dataToArray(props.animation), props.attr);
@@ -125,15 +118,15 @@ class TweenOne extends Component {
125118
}
126119
}
127120

128-
play() {
121+
play = () => {
129122
this.cancelRequestAnimationFrame();
130123
if (this.paused) {
131124
return;
132125
}
133126
this.rafID = ticker.add(this.raf);
134127
}
135128

136-
frame() {
129+
frame = () => {
137130
let moment = (ticker.frame - this.startFrame) * perFrame + (this.startMoment || 0);
138131
if (this.reverse) {
139132
moment = (this.startMoment || 0) - (ticker.frame - this.startFrame) * perFrame;
@@ -148,15 +141,15 @@ class TweenOne extends Component {
148141
this.timeLine.frame(moment);
149142
}
150143

151-
raf() {
144+
raf = () => {
152145
this.frame();
153146
if ((this.moment >= this.timeLine.totalTime && !this.reverse)
154147
|| this.paused || (this.reverse && this.moment === 0)) {
155148
return this.cancelRequestAnimationFrame();
156149
}
157150
}
158151

159-
cancelRequestAnimationFrame() {
152+
cancelRequestAnimationFrame = () => {
160153
ticker.clear(this.rafID);
161154
this.rafID = -1;
162155
}

src/TweenOneGroup.jsx

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,9 @@ class TweenOneGroup extends Component {
2121
this.isTween = {};
2222
// 第一进入,appear 为 true 时默认用 enter 或 tween-one 上的效果
2323
const children = toArrayChildren(getChildrenFromProps(this.props));
24-
children.forEach(child => {
25-
if (!child || !child.key) {
26-
return;
27-
}
28-
this.keysToEnter.push(child.key);
29-
});
3024
this.state = {
3125
children,
3226
};
33-
[
34-
'getChildrenToRender',
35-
'getCoverAnimation',
36-
'onChange',
37-
].forEach((method) => this[method] = this[method].bind(this));
3827
}
3928

4029
componentDidMount() {
@@ -43,7 +32,7 @@ class TweenOneGroup extends Component {
4332

4433
componentWillReceiveProps(nextProps) {
4534
const nextChildren = toArrayChildren(nextProps.children);
46-
const currentChildren = this.state.children;
35+
const currentChildren = toArrayChildren(this.state.children);
4736
const newChildren = mergeChildren(currentChildren, nextChildren);
4837

4938
this.keysToEnter = [];
@@ -74,27 +63,27 @@ class TweenOneGroup extends Component {
7463
});
7564
}
7665

77-
onChange(animation, key, type, obj) {
66+
onChange = (animation, key, type, obj) => {
7867
const length = dataToArray(animation).length;
7968
const animatingClassName = this.props.animatingClassName;
8069
const tag = obj.target;
70+
const isEnter = type === 'enter' || type === 'appear';
8171
if (obj.mode === 'onStart') {
8272
tag.className = tag.className
83-
.replace(animatingClassName[type === 'enter' ? 1 : 0], '').trim();
84-
if (tag.className.indexOf(animatingClassName[type === 'enter' ? 0 : 1]) === -1) {
85-
tag.className = `${tag.className} ${animatingClassName[type === 'enter' ? 0 : 1]}`.trim();
73+
.replace(animatingClassName[isEnter ? 1 : 0], '').trim();
74+
if (tag.className.indexOf(animatingClassName[isEnter ? 0 : 1]) === -1) {
75+
tag.className = `${tag.className} ${animatingClassName[isEnter ? 0 : 1]}`.trim();
8676
}
8777
} else if (obj.index === length - 1 && obj.mode === 'onComplete') {
88-
let children;
78+
let children = this.state.children;
8979
if (type === 'enter') {
90-
children = this.state.children;
9180
this.keysToEnter.splice(this.keysToEnter.indexOf(key), 1);
92-
} else {
81+
} else if (type === 'leave') {
9382
children = this.state.children.filter(child => key !== child.key);
9483
this.keysToLeave.splice(this.keysToLeave.indexOf(key), 1);
9584
}
9685
tag.className = tag.className
97-
.replace(animatingClassName[type === 'enter' ? 0 : 1], '').trim();
86+
.replace(animatingClassName[isEnter ? 0 : 1], '').trim();
9887
delete this.isTween[key];
9988
this.setState({
10089
children,
@@ -104,38 +93,46 @@ class TweenOneGroup extends Component {
10493
}
10594
}
10695

107-
getCoverAnimation(child, i, type) {
96+
getCoverAnimation = (child, i, type) => {
10897
let animation;
10998
let onChange;
110-
const appear = transformArguments(this.props.appear, child.key, i);
111-
if (appear || this.onEnterBool) {
112-
animation = type === 'leave' ? this.props.leave : this.props.enter;
113-
onChange = this.onChange.bind(this, animation, child.key, type);
99+
animation = type === 'leave' ? this.props.leave : this.props.enter;
100+
if (type === 'appear') {
101+
const appear = transformArguments(this.props.appear, child.key, i);
102+
animation = appear && this.props.enter || null;
114103
}
104+
onChange = this.onChange.bind(this, animation, child.key, type);
115105
const children = (<TweenOne
116106
{...child.props}
117107
key={child.key}
118108
component={child.type}
119109
animation={transformArguments(animation, child.key, i)}
120110
onChange={onChange}
121-
resetStyleBool={child.key in this.isTween}
111+
resetStyleBool={this.props.resetStyleBool}
122112
/>);
123-
if (this.keysToEnter.concat(this.keysToLeave).indexOf(child.key) >= 0) {
124-
this.isTween[child.key] = true;
113+
if (this.keysToEnter.concat(this.keysToLeave).indexOf(child.key) >= 0
114+
|| !this.onEnterBool && animation) {
115+
this.isTween[child.key] = type;
125116
}
126117
return children;
127118
}
128119

129-
getChildrenToRender(children) {
120+
getChildrenToRender = children => {
130121
return children.map((child, i) => {
131122
if (!child || !child.key) {
132123
return child;
133124
}
134125
const key = child.key;
135126
if (this.keysToLeave.indexOf(key) >= 0) {
136127
return this.getCoverAnimation(child, i, 'leave');
128+
} else if (this.keysToEnter.indexOf(key) >= 0) {
129+
return this.getCoverAnimation(child, i, 'enter');
130+
} else if (!this.onEnterBool) {
131+
return this.getCoverAnimation(child, i, 'appear');
137132
}
138-
return this.getCoverAnimation(child, i, 'enter');
133+
return this.isTween[child.key] &&
134+
this.getCoverAnimation(child, i, this.isTween[child.key]) ||
135+
React.createElement(TweenOne, { ...child.props, component: child.type, key: child.key });
139136
});
140137
}
141138

@@ -152,6 +149,7 @@ class TweenOneGroup extends Component {
152149
'leave',
153150
'animatingClassName',
154151
'onEnd',
152+
'resetStyleBool',
155153
].forEach(key => delete componentProps[key]);
156154
return createElement(this.props.component, componentProps, childrenToRender);
157155
}
@@ -169,6 +167,7 @@ TweenOneGroup.propTypes = {
169167
leave: objectOrArrayOrFunc,
170168
animatingClassName: PropTypes.array,
171169
onEnd: PropTypes.func,
170+
resetStyleBool: PropTypes.bool,
172171
};
173172

174173
TweenOneGroup.defaultProps = {
@@ -178,5 +177,6 @@ TweenOneGroup.defaultProps = {
178177
enter: { x: 50, opacity: 0, type: 'from' },
179178
leave: { x: -50, opacity: 0 },
180179
onEnd: noop,
180+
resetStyleBool: true,
181181
};
182182
export default TweenOneGroup;

src/util.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ export function mergeChildren(prev, next) {
100100
// the combined list
101101
const nextChildrenPending = {};
102102
let pendingChildren = [];
103+
let followChildrenKey;
103104
prev.forEach((c) => {
104105
if (!c) {
105106
return;
@@ -109,10 +110,14 @@ export function mergeChildren(prev, next) {
109110
nextChildrenPending[c.key] = pendingChildren;
110111
pendingChildren = [];
111112
}
113+
followChildrenKey = c.key;
112114
} else if (c.key) {
113115
pendingChildren.push(c);
114116
}
115117
});
118+
if (!followChildrenKey) {
119+
ret = ret.concat(pendingChildren);
120+
}
116121

117122
next.forEach((c) => {
118123
if (!c) {
@@ -122,13 +127,8 @@ export function mergeChildren(prev, next) {
122127
ret = ret.concat(nextChildrenPending[c.key]);
123128
}
124129
ret.push(c);
125-
});
126-
127-
// 保持原有的顺序
128-
pendingChildren.forEach((c) => {
129-
const originIndex = prev.indexOf(c);
130-
if (originIndex >= 0) {
131-
ret.splice(originIndex, 0, c);
130+
if (c.key === followChildrenKey) {
131+
ret = ret.concat(pendingChildren);
132132
}
133133
});
134134

0 commit comments

Comments
 (0)