Skip to content

Commit ac2e144

Browse files
author
Benoît Delmaire
authored
Merge pull request #147 from archriss/refactor
Callback refactoring and miscellaneous updates
2 parents 6bb15c8 + a6982ef commit ac2e144

File tree

6 files changed

+226
-288
lines changed

6 files changed

+226
-288
lines changed

CHANGELOG.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
1+
## v3.2.0
2+
* Refactor callback handling. **Make sure to use the new prop `callbackOffsetMargin` if you experience missed callbacks.**
3+
* Make item's scale and opacity animation follow scroll value (thanks [@hammadj](https://github.com/hammadj))
4+
* `Pagination` component: make dots tappable with new props `tappableDots` and `carouselRef` (see the [example](https://github.com/archriss/react-native-snap-carousel/blob/master/example/src/index.js))
5+
* Fix state and scroll issues when the currently active item is being dynamically removed
6+
* Improve snap feeling when momentum is disabled (default)
7+
* Add prop `callbackOffsetMargin`
8+
* Remove props `animationFunc`, `animationOptions`, `scrollEndDragDebounceValue`, `snapOnAndroid`, and `useNativeOnScroll`
9+
110
## v3.1.0
211
* `Pagination` component: add new props for advanced customization
312

413
## v3.0.0
5-
###WARNING
14+
### WARNING
615
* **Do not use this version as some temporary code was pushed to `npm` by mistake. Make sure to use version `3.1.0` instead.**
716
### Breaking changes
817
* Plugin is now built on top of `FlatList`, which allows for huge performance optimizations. From now on, items must be rendered using props `data` and `renderItem`.

README.md

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -164,17 +164,15 @@ Prop | Description | Type | Default
164164

165165
Prop | Description | Type | Default
166166
------ | ------ | ------ | ------
167-
`activeSlideOffset` | From slider's center, minimum slide distance to be scrolled before being set to active | Number | `25`
167+
`activeSlideOffset` | From slider's center, minimum slide distance to be scrolled before being set to active. | Number | `20`
168168
`apparitionDelay` | `FlatList`'s init is a real mess, with lots of unneeded flickers and slides movement. This prop controls the delay during which the carousel will be hidden when mounted. | Number | `250`
169+
`callbackOffsetMargin` | Scroll events might not be triggered often enough to get a precise measure and, therefore, to provide a reliable callback. This usually is an Android issue, which might be linked to the version of React Native you're using (see ["Unreliable callbacks"](#unreliable-callbacks)). To work around this, you can define a small margin that will increase the "sweet spot"'s width. The default value should cover most cases, but **you will want to increase it if you experience missed callbacks**. | Number | `5`
169170
`enableMomentum` | See [momentum](#momentum) | Boolean | `false`
170171
`enableSnap` | If enabled, releasing the touch will scroll to the center of the nearest/active item | Boolean | `true`
171172
`firstItem` | Index of the first item to display | Number | `0`
172173
`hasParallaxImages` | Whether the carousel contains `<ParallaxImage />` components or not. Required for specific data to be passed to children. | Boolean | `false`
173-
`scrollEndDragDebounceValue` | **When momentum is disabled**, this prop defines the timeframe during which multiple callback calls should be "grouped" into a single one. This debounce also helps smoothing the snap effect by providing a bit of inertia when touch is released. **Note that this will delay callback's execution.** | Number | `50` for iOS, `150` for Android
174174
`shouldOptimizeUpdates` | Whether to implement a `shouldComponentUpdate` strategy to minimize updates | Boolean | `true`
175-
`snapOnAndroid` | Snapping on android is sometimes choppy, especially when swiping quickly, so you can disable it | Boolean | `true`
176175
`swipeThreshold` | Delta x when swiping to trigger the snap | Number | `20`
177-
`useNativeOnScroll` | Move `onScroll` events to the native thread in order to prevent the tiny lag associated with RN's JS bridge. **Activate this if you have a `transform` and/or `opacity` animation that needs to follow carousel's scroll position closely**. More info in [this post](https://facebook.github.io/react-native/blog/2017/02/14/using-native-driver-for-animated.html). Note that it will be activated if `hasParallaxImages` is set to `true` and/or if `scrollEventThrottle` is set to less than `16`. | Boolean | `false`
178176
`vertical` | Layout slides vertically instead of horizontally | Boolean | `false`
179177

180178
### Autoplay
@@ -190,8 +188,6 @@ Prop | Description | Type | Default
190188
Prop | Description | Type | Default
191189
------ | ------ | ------ | ------
192190
`activeSlideAlignment` | Determine active slide's alignment relative to the carousel. Possible values are: `'start'`, `'center'` and `'end'`. | String | `'center'`
193-
`animationFunc` | Animated animation to use; you must provide the name of the method. Note that it will only be applied to the scale animation since opacity's animation type will always be set to `timing` (no one wants the opacity to 'bounce' around) | String | `timing`
194-
`animationOptions` | Animation options to be merged with the default ones. Can be used without `animationFunc`. Note that opacity's easing will be kept linear. | Object | `{ duration: 600, easing: Easing.elastic(1) }`
195191
`containerCustomStyle` | Optional styles for Scrollview's global wrapper | View Style Object | `{}`
196192
`contentContainerCustomStyle` | Optional styles for Scrollview's items container | View Style Object | `{}`
197193
`inactiveSlideOpacity` | Value of the opacity effect applied to inactive slides | Number | `0.7`
@@ -321,6 +317,14 @@ const styles = Stylesheet.create({
321317
<Carousel sliderWidth={sliderWidth} itemWidth={itemWidth} />
322318
```
323319
320+
### Carousel's stretched height
321+
322+
Since `<Carousel />` is, ultimately, based on `<ScrollView />`, it inherits [its default styles](https://github.com/facebook/react-native/blob/master/Libraries/Components/ScrollView/ScrollView.js#L864) and particularly `{ flexGrow: 1 }`. This means that, by default, **the carousel container will stretch to fill up all available space**.
323+
324+
If this is not what you're after, you can prevent this behavior by passing `{ flexGrow: 0 }` to prop `containerCustomStyle`.
325+
326+
Alternatively, you can either use this prop to pass a custom height to the container, or wrap the carousel in a `<View />` with a fixed height.
327+
324328
### Understanding styles
325329
326330
Here is a screenshot that should help you understand how each of the above variables is used.
@@ -400,6 +404,10 @@ export class MyCarousel extends Component {
400404
401405
[This plugin](https://github.com/shichongrui/react-native-on-layout) can also prove useful.
402406
407+
### Native-powered animations
408+
409+
Scroll events have been moved to the native thread in order to prevent the tiny lag associated with React Native's JavaScript bridge. This is really useful when displaying a `transform` and/or `opacity` animation that needs to follow carousel's scroll position closely. You can find more info in [this post from Facebook](https://facebook.github.io/react-native/blog/2017/02/14/using-native-driver-for-animated.html).
410+
403411
## Known issues
404412
405413
### React Native version
@@ -427,9 +435,13 @@ We're trying to work around these issues, but the result is not always as smooth
427435
428436
### Unreliable callbacks
429437
430-
When `enableMomentum` is disabled, providing a reliable callback is really tricky since no `scrollEnd` event has been exposed yet for the `ScrollView` component. We can only rely on the `scrollEndDrag` event, which comes with a huge bunch of issues. See [#34](https://github.com/archriss/react-native-snap-carousel/issues/34) for more information.
438+
When `enableMomentum` is disabled (default behavior), providing a reliable callback is really tricky since no `scrollEnd` event has been exposed yet for the `ScrollView` component. We can only rely on the `scrollEndDrag` event, which comes with a huge bunch of issues. See [#34](https://github.com/archriss/react-native-snap-carousel/issues/34) for more information.
439+
440+
Version 2.3.0 tackled these issues with all sorts of flags and hacks. But you could still be facing the following one: **when you build a debug version of your app without enabling JS remote debugging, timers may desynchronize and cause a complete callback mess**. Try to either enable remote debugging or build a production version of your app, and everything should get back to normal.
441+
442+
Callback handling has been completely revamped in version 3.2.0, in a less hacky and more reliable way. There is one issue though: callbacks now rely on scroll events. Usually, this is not a problem since the plugin features a native-powered scroll. **But there has been [a regression in React Native 0.46.x](https://github.com/facebook/react-native/issues/15769), that has been fixed in version 0.48.2.**
431443
432-
Version 2.3.0 tackled these issues with a bunch of flags and hacks. But you could still be facing the following one: **when you build a debug version of your app without enabling JS remote debugging**, timers will desynchronize and callbacks will be a complete mess. Try to either enable remote debugging or build a production version of your app, and everything should get back to normal.
444+
If you're using an in-between version, you're in for some trouble since events won't be fired frequently enough (particularly on Android). **We've added a prop `callbackOffsetMargin` to help with this situation.**
433445
434446
### Error with Jest
435447
@@ -447,6 +459,7 @@ As such, this feature should be considered experimental since it might break wit
447459
448460
## TODO
449461
462+
- [ ] Implement a custom `PanResponder` for better control over carousel's callbacks and overall feeling
450463
- [ ] Implement 'loop' mode
451464
- [ ] Handle changing major props on-the-fly
452465
- [ ] Handle autoplay properly when updating children's length

example/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
},
88
"dependencies": {
99
"react": "16.0.0-alpha.12",
10-
"react-native": "~0.47.2",
10+
"react-native": "~0.48.3",
1111
"react-native-linear-gradient": "^2.3.0",
1212
"react-native-snap-carousel": "file:../"
1313
},
1414
"devDependencies": {
15-
"babel-preset-react-native": "3.0.1"
15+
"babel-preset-react-native": "3.0.2"
1616
}
1717
}

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-snap-carousel",
3-
"version": "3.1.0",
3+
"version": "3.2.0",
44
"description": "Swiper component for React Native with previews, snapping effect, parallax images, performant handling of huge numbers of items, and RTL support. Compatible with Android & iOS.",
55
"main": "src/index.js",
66
"repository": {
@@ -31,7 +31,6 @@
3131
"author": "Archriss <[email protected]> (github.com/archriss)",
3232
"license": "ISC",
3333
"dependencies": {
34-
"lodash.debounce": "4.0.8",
3534
"prop-types": "^15.5.10",
3635
"react-addons-shallow-compare": "15.6.0"
3736
},

0 commit comments

Comments
 (0)