Skip to content

Commit

Permalink
feat: onSettle callback
Browse files Browse the repository at this point in the history
  • Loading branch information
rgommezz committed May 11, 2020
1 parent 0e679ed commit 34a1534
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
3 changes: 3 additions & 0 deletions example/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ export default function App() {
animatedPosition={animatedPosition.current}
snapPoints={snapPointsFromTop}
initialSnapIndex={2}
onSettle={index => {
console.log('Next snap index: ', index);
}}
renderHandle={() => (
<View style={styles.headerContainer}>
<View style={styles.header}>
Expand Down
13 changes: 12 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ export class ScrollBottomSheet<T extends any> extends Component<Props<T>> {
* Animated value that keeps track of the position: 0 => closed, 1 => opened
*/
private position: Animated.Node<number>;
private nextSnapIndex: Animated.Value<number>;
private decelerationRate: Animated.Value<number>;

private scrollComponent: React.ComponentType<
Expand All @@ -174,6 +175,7 @@ export class ScrollBottomSheet<T extends any> extends Component<Props<T>> {
const closedPosition = snapPoints[snapPoints.length - 1];
const initialSnap = snapPoints[initialSnapIndex];
const tempDestSnapPoint = new Value(0);
this.nextSnapIndex = new Value(initialSnapIndex);
const isAndroid = new Value(Number(Platform.OS === 'android'));
const initialDecelerationRate = Platform.select({
android:
Expand Down Expand Up @@ -302,6 +304,7 @@ export class ScrollBottomSheet<T extends any> extends Component<Props<T>> {
),
[
set(tempDestSnapPoint, add(snapPoints[i], extraOffset)),
set(this.nextSnapIndex, i),
calculateNextSnapPoint(i + 1),
],
calculateNextSnapPoint(i + 1)
Expand Down Expand Up @@ -342,7 +345,13 @@ export class ScrollBottomSheet<T extends any> extends Component<Props<T>> {
cond(
state.finished,
[
// Resetting appropriate values
onChange(
this.nextSnapIndex,
call([this.nextSnapIndex], ([value]) => {
this.props.onSettle?.(value);
})
),
// Resetting appropriate valuesc
set(drawerOldGestureState, GestureState.END),
set(handleOldGestureState, GestureState.END),
set(prevTranslateYOffset, state.position),
Expand Down Expand Up @@ -380,8 +389,10 @@ export class ScrollBottomSheet<T extends any> extends Component<Props<T>> {
didScrollUpAndPullDown,
setTranslationY,
set(tempDestSnapPoint, add(snapPoints[0], extraOffset)),
set(this.nextSnapIndex, 0),
set(destSnapPoint, calculateNextSnapPoint()),
set(dragY, 0),
set(velocityY, 0),
set(
lastSnap,
sub(
Expand Down

0 comments on commit 34a1534

Please sign in to comment.