Skip to content

Commit

Permalink
fix: updated detached bottom sheet handling
Browse files Browse the repository at this point in the history
  • Loading branch information
gorhom committed Jul 18, 2021
1 parent 14b66d7 commit 603f492
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
32 changes: 17 additions & 15 deletions src/components/bottomSheet/BottomSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
}
// handle component is null.
if (handleComponent === null) {
animatedHandleHeight.value = 0;
isHandleHeightCalculated = true;
}
// handle height did set.
Expand Down Expand Up @@ -593,10 +594,14 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
},
[_providedOnAnimate, animatedSnapPoints, animatedCurrentIndex]
);
const stopAnimation = useWorkletCallback(() => {
cancelAnimation(animatedPosition);
animatedAnimationSource.value = ANIMATION_SOURCE.NONE;
animatedAnimationState.value = ANIMATION_STATE.STOPPED;
}, [animatedPosition, animatedAnimationState, animatedAnimationSource]);
const animateToPositionCompleted = useWorkletCallback(
function animateToPositionCompleted(isFinished: boolean) {
if (!isFinished) {
animatedAnimationState.value = ANIMATION_STATE.INTERRUPTED;
return;
}
runOnJS(print)({
Expand All @@ -623,9 +628,9 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
configs?: Animated.WithTimingConfig | Animated.WithSpringConfig
) {
if (
animatedAnimationState.value !== ANIMATION_STATE.INTERRUPTED &&
(position === animatedPosition.value ||
position === undefined ||
position === animatedPosition.value ||
position === undefined ||
(animatedAnimationState.value === ANIMATION_STATE.RUNNING &&
position === animatedNextPosition.value)
) {
return;
Expand All @@ -640,25 +645,22 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
velocity,
},
});

stopAnimation();

/**
* cancel current running animation
* set animation state to running, and source
*/
cancelAnimation(animatedPosition);
animatedAnimationState.value = ANIMATION_STATE.RUNNING;
animatedAnimationSource.value = source;

// store
/**
* store next position
*/
animatedNextPosition.value = position;
animatedNextPositionIndex.value =
animatedSnapPoints.value.indexOf(position);

/**
* set animation state to running, and source
*/
animatedAnimationState.value = ANIMATION_STATE.RUNNING;
animatedAnimationSource.value = source;

/**
* fire `onAnimate` callback
*/
Expand Down Expand Up @@ -952,7 +954,7 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
function handleGestureStart(__, _, context: GestureEventContextType) {
'worklet';
// cancel current animation
cancelAnimation(animatedPosition);
stopAnimation();

// store current animated position
context.initialPosition = animatedPosition.value;
Expand Down Expand Up @@ -1467,7 +1469,7 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
component: BottomSheet.name,
method: 'useAnimatedReaction::OnMount',
params: {
_isLayoutCalculated,
isLayoutCalculated: _isLayoutCalculated,
animatedSnapPoints: animatedSnapPoints.value,
nextPosition,
},
Expand Down
12 changes: 11 additions & 1 deletion src/hooks/useBottomSheetDynamicSnapPoints.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { useCallback } from 'react';
import { useDerivedValue, useSharedValue } from 'react-native-reanimated';
import {
INITIAL_HANDLE_HEIGHT,
INITIAL_SNAP_POINT,
} from '../components/bottomSheet/constants';

/**
* Provides dynamic content height calculating functionalities, by
Expand All @@ -19,8 +23,14 @@ export const useBottomSheetDynamicSnapPoints = (
) => {
// variables
const animatedContentHeight = useSharedValue(0);
const animatedHandleHeight = useSharedValue(0);
const animatedHandleHeight = useSharedValue(INITIAL_HANDLE_HEIGHT);
const animatedSnapPoints = useDerivedValue(() => {
if (
animatedHandleHeight.value === INITIAL_HANDLE_HEIGHT ||
animatedContentHeight.value === 0
) {
return [INITIAL_SNAP_POINT];
}
const contentWithHandleHeight =
animatedContentHeight.value + animatedHandleHeight.value;

Expand Down

0 comments on commit 603f492

Please sign in to comment.