forked from meliorence/react-native-snap-carousel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
473 lines (414 loc) · 14.7 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
import React, { Component, PropTypes } from 'react';
import { ScrollView, Animated, Platform, Easing } from 'react-native';
import shallowCompare from 'react-addons-shallow-compare';
export default class Carousel extends Component {
static propTypes = {
...ScrollView.propTypes,
/**
* Width in pixels of your elements
*/
itemWidth: PropTypes.number.isRequired,
/**
* Width in pixels of your slider according
* to your styles
*/
sliderWidth: PropTypes.number.isRequired,
/**
* Animated animation to use. Provide the name
* of the method, defaults to timing
*/
animationFunc: PropTypes.string,
/**
* Animation options to be merged with the
* default ones. Can be used w/ animationFunc
*/
animationOptions: PropTypes.object,
/**
* Trigger autoplay
*/
autoplay: PropTypes.bool,
/**
* Delay before enabling autoplay on startup and
* after releasing the touch
*/
autoplayDelay: PropTypes.number,
/**
* Delay until navigating to the next item
*/
autoplayInterval: PropTypes.number,
/**
* Global wrapper's style
*/
containerCustomStyle: ScrollView.propTypes.style,
/**
* Content container's style
*/
contentContainerCustomStyle: ScrollView.propTypes.style,
/**
* If enabled, snapping will be triggered once
* the ScrollView stops moving, not when the
* user releases his finger
*/
enableMomentum: PropTypes.bool,
/**
* If enabled, releasing the touch will scroll
* to the center of the nearest/active item
*/
enableSnap: PropTypes.bool,
/**
* Index of the first item to display
*/
firstItem: PropTypes.number,
/**
* Opacity value of the inactive slides
*/
inactiveSlideOpacity: PropTypes.number,
/**
* Scale factor of the inactive slides
*/
inactiveSlideScale: PropTypes.number,
/**
* Style of each item's container
*/
slideStyle: Animated.View.propTypes.style,
/**
* whether to implement a `shouldComponentUpdate`
* strategy to minimize updates
*/
shouldOptimizeUpdates: PropTypes.bool,
/**
* Snapping on android is kinda choppy, especially
* when swiping quickly so you can disable it
*/
snapOnAndroid: PropTypes.bool,
/**
* Delta x when swiping to trigger the snap
*/
swipeThreshold: PropTypes.number,
/**
* Fired when snapping to an item
*/
onSnapToItem: PropTypes.func
};
static defaultProps = {
animationFunc: 'timing',
animationOptions: {
easing: Easing.elastic(1)
},
autoplay: false,
autoplayDelay: 5000,
autoplayInterval: 3000,
containerCustomStyle: {},
contentContainerCustomStyle: {},
enableMomentum: false,
enableSnap: true,
firstItem: 0,
inactiveSlideOpacity: 1,
inactiveSlideScale: 0.9,
slideStyle: {},
shouldOptimizeUpdates: true,
snapOnAndroid: true,
swipeThreshold: 20
}
constructor (props) {
super(props);
this.state = {
activeItem: props.firstItem,
interpolators: [],
};
this._positions = [];
this._calcCardPositions(props);
this._onTouchStart = this._onTouchStart.bind(this);
this._onScroll = this._onScroll.bind(this);
this._onScrollEnd = this._snapEnabled ? this._onScrollEnd.bind(this) : false;
this._onScrollBegin = this._snapEnabled ? this._onScrollBegin.bind(this) : false;
this._initInterpolators = this._initInterpolators.bind(this);
this._onTouchRelease = this._onTouchRelease.bind(this);
// This bool aims at fixing an iOS bug due to scrolTo that triggers onMomentumScrollEnd.
// onMomentumScrollEnd fires this._snapScroll, thus creating an infinite loop.
this._ignoreNextMomentum = false;
}
componentDidMount () {
const { firstItem, autoplay } = this.props;
this._initInterpolators(this.props);
setTimeout(() => {
this.snapToItem(firstItem, false, false, true);
}, 0);
if (autoplay) {
this.startAutoplay();
}
}
shouldComponentUpdate (nextProps, nextState) {
if (this.props.shouldOptimizeUpdates === false) {
return true;
} else {
return shallowCompare(this, nextProps, nextState);
}
}
componentWillReceiveProps (nextProps) {
const { firstItem } = nextProps;
const { interpolators } = this.state;
if (interpolators && interpolators.length !== nextProps.children.length) {
this._positions = [];
this._calcCardPositions(nextProps);
this._initInterpolators(nextProps);
this.setState({ activeItem: firstItem });
}
}
componentWillUnmount () {
this.stopAutoplay();
}
get _snapEnabled () {
const { enableSnap, snapOnAndroid } = this.props;
return enableSnap && (Platform.OS === 'ios' || snapOnAndroid);
}
get _nextItem () {
const { activeItem } = this.state;
return this._positions[activeItem + 1] ? activeItem + 1 : 0;
}
_calcCardPositions (props = this.props) {
const { itemWidth } = props;
this.props.children.map((item, index) => {
this._positions[index] = {
start: index * itemWidth
};
this._positions[index].end = this._positions[index].start + itemWidth;
});
}
_initInterpolators (props = this.props) {
const { firstItem } = props;
let interpolators = [];
this.props.children.map((item, index) => {
interpolators.push(new Animated.Value(index === firstItem ? 1 : 0));
});
this.setState({ interpolators });
}
_getActiveItem (centerX, offset = 25) {
for (let i = 0; i < this._positions.length; i++) {
const { start, end } = this._positions[i];
if (centerX + offset >= start && centerX - offset <= end) {
return i;
}
}
return 0;
}
_getCenterX (event) {
const { sliderWidth, itemWidth } = this.props;
const containerSideMargin = (sliderWidth - itemWidth) / 2;
return event.nativeEvent.contentOffset.x + sliderWidth / 2 - containerSideMargin;
}
_onScroll (event) {
const { animationFunc, animationOptions, enableMomentum } = this.props;
const { activeItem } = this.state;
const newActiveItem = this._getActiveItem(this._getCenterX(event));
if (enableMomentum) {
clearTimeout(this._snapNoMomentumTimeout);
}
if (activeItem !== newActiveItem) {
Animated[animationFunc](
this.state.interpolators[activeItem],
{ ...animationOptions, toValue: 0 }
).start();
this.setState({ activeItem: newActiveItem });
Animated[animationFunc](
this.state.interpolators[newActiveItem],
{ ...animationOptions, toValue: 1 }
).start();
}
}
_onTouchStart () {
if (this._autoplaying) {
this.stopAutoplay();
}
}
_onScrollBegin (event) {
this._scrollStartX = event.nativeEvent.contentOffset.x;
this._scrollStartActive = this.state.activeItem;
this._ignoreNextMomentum = false;
}
_onScrollEnd (event) {
const { autoplayDelay, autoplay } = this.props;
if (this._ignoreNextMomentum) {
// iOS fix
this._ignoreNextMomentum = false;
return;
}
this._scrollEndX = event.nativeEvent.contentOffset.x;
this._scrollEndActive = this.state.activeItem;
const deltaX = this._scrollEndX - this._scrollStartX;
if (this._snapEnabled) {
this._snapScroll(deltaX);
}
if (autoplay) {
// Restart autoplay after a little while
// This could be done when releasing touch
// but the event is buggy on Android...
clearTimeout(this._enableAutoplayTimeout);
this._enableAutoplayTimeout =
setTimeout(() => {
this.startAutoplay(true);
}, autoplayDelay + 1000);
}
}
// Due to a bug, this event is only fired on iOS
// https://github.com/facebook/react-native/issues/6791
// it's fine since we're only fixing an iOS bug in it, so ...
_onTouchRelease (event) {
const { enableMomentum } = this.props;
if (enableMomentum && Platform.OS === 'ios') {
this._snapNoMomentumTimeout =
setTimeout(() => {
this._snapScroll(0);
}, 100);
}
}
_snapScroll (deltaX) {
const { swipeThreshold } = this.props;
// When using momentum and releasing the touch with
// no velocity, scrollEndActive will be undefined (iOS)
if (!this._scrollEndActive && Platform.OS === 'ios') {
this._scrollEndActive = this._scrollStartActive;
}
if (this._scrollStartActive !== this._scrollEndActive) {
// Snap to the new active item
this.snapToItem(this._scrollEndActive);
} else {
// Snap depending on delta
if (deltaX > 0) {
if (deltaX > swipeThreshold) {
this.snapToItem(this._scrollStartActive + 1);
} else {
this.snapToItem(this._scrollEndActive);
}
} else if (deltaX < 0) {
if (deltaX < -swipeThreshold) {
this.snapToItem(this._scrollStartActive - 1);
} else {
this.snapToItem(this._scrollEndActive);
}
} else {
// Snap to current
this.snapToItem(this._scrollEndActive);
}
}
}
get currentIndex () {
return this.state.activeItem;
}
startAutoplay (instantly = false) {
const { autoplayInterval, autoplayDelay } = this.props;
if (this._autoplaying) {
return;
}
setTimeout(() => {
this._autoplaying = true;
this._autoplayInterval =
setInterval(() => {
if (this._autoplaying) {
this.snapToItem(this._nextItem);
}
}, autoplayInterval);
}, instantly ? 0 : autoplayDelay);
}
stopAutoplay () {
this._autoplaying = false;
clearInterval(this._autoplayInterval);
}
snapToItem (index, animated = true, fireCallback = true, initial = false) {
const itemsLength = this._positions.length;
if (index >= itemsLength) {
index = itemsLength - 1;
fireCallback = false;
} else if (index < 0) {
index = 0;
fireCallback = false;
}
const snapX = this._positions[index].start;
// Make sure the component hasn't been unmounted
if (this.refs.scrollview) {
this.refs.scrollview.scrollTo({x: snapX, y: 0, animated});
this.props.onSnapToItem && fireCallback && this.props.onSnapToItem(index);
// iOS fix, check the note in the constructor
if (!initial && Platform.OS === 'ios') {
this._ignoreNextMomentum = true;
}
}
}
snapToNext (animated = true) {
const itemsLength = this._positions.length;
let newIndex = this.currentIndex + 1;
if (newIndex > itemsLength - 1) {
newIndex = 0;
}
this.snapToItem(newIndex, animated);
}
snapToPrev (animated = true) {
const itemsLength = this._positions.length;
let newIndex = this.currentIndex - 1;
if (newIndex < 0) {
newIndex = itemsLength - 1;
}
this.snapToItem(newIndex, animated);
}
_children (children = this.props.children) {
return React.Children.map(children, (child) => child);
}
_childSlides () {
const { slideStyle, inactiveSlideScale, inactiveSlideOpacity } = this.props;
if (!this.state.interpolators || !this.state.interpolators.length) {
return false;
};
return this._children().map((child, index) => {
const animatedValue = this.state.interpolators[index];
if (!animatedValue) {
return false;
}
return (
<Animated.View
key={`carousel-item-${index}`}
style={[
slideStyle,
{
opacity: animatedValue.interpolate({ inputRange: [0, 1], outputRange: [inactiveSlideOpacity, 1] }),
transform: [{
scale: animatedValue.interpolate({ inputRange: [0, 1], outputRange: [inactiveSlideScale, 1] })
}]
}
]}>
{ child }
</Animated.View>
);
});
}
render () {
const { sliderWidth, itemWidth, containerCustomStyle, contentContainerCustomStyle, enableMomentum } = this.props;
const containerSideMargin = (sliderWidth - itemWidth) / 2;
const style = [
{ paddingHorizontal: Platform.OS === 'ios' ? containerSideMargin : 0 },
containerCustomStyle || {}
];
const contentContainerStyle = [
{ paddingHorizontal: Platform.OS === 'android' ? containerSideMargin : 0 },
contentContainerCustomStyle || {}
];
return (
<ScrollView
decelerationRate={0.9}
style={style}
contentContainerStyle={contentContainerStyle}
ref={'scrollview'}
horizontal={true}
onScrollBeginDrag={this._onScrollBegin}
onMomentumScrollEnd={enableMomentum ? this._onScrollEnd : undefined}
onScrollEndDrag={!enableMomentum ? this._onScrollEnd : undefined}
onResponderRelease={this._onTouchRelease}
onScroll={this._onScroll}
onTouchStart={this._onTouchStart}
scrollEventThrottle={50}
{...this.props}
>
{ this._childSlides() }
</ScrollView>
);
}
}