Skip to content

Commit

Permalink
fix: re-snap to current position when snap points get updated
Browse files Browse the repository at this point in the history
  • Loading branch information
gorhom committed May 27, 2021
1 parent 4bc40d9 commit bb8e202
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 14 deletions.
9 changes: 2 additions & 7 deletions example/src/components/weather/Weather.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,11 @@ import { LOCATION_DETAILS_HEIGHT } from '../locationDetails';
interface WeatherProps {
animatedPosition: Animated.SharedValue<number>;
animatedIndex: Animated.SharedValue<number>;
snapPoints: number[];
}

const { height: SCREEN_HEIGHT } = Dimensions.get('window');

const Weather = ({
animatedIndex,
animatedPosition,
snapPoints,
}: WeatherProps) => {
const Weather = ({ animatedIndex, animatedPosition }: WeatherProps) => {
// hooks
const { colors } = useShowcaseTheme();
const { bottom: bottomSafeArea } = useSafeAreaInsets();
Expand Down Expand Up @@ -55,7 +50,7 @@ const Weather = ({
},
],
}),
[snapPoints, lockedYPosition]
[lockedYPosition]
);
const containerStyle = useMemo(
() => [
Expand Down
11 changes: 7 additions & 4 deletions example/src/screens/integrations/MapExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,17 @@ const MapExample = () => {
const data = useMemo(() => createLocationListMockData(15), []);
const poiListSnapPoints = useMemo(
() => [
bottomSafeArea === 0 ? SEARCH_HANDLE_HEIGHT / 2 : bottomSafeArea,
bottomSafeArea + SEARCH_HANDLE_HEIGHT,
LOCATION_DETAILS_HEIGHT + bottomSafeArea,
'100%',
],
[bottomSafeArea]
);
const poiDetailsSnapPoints = useMemo(
() => [LOCATION_DETAILS_HEIGHT + bottomSafeArea, SCREEN_HEIGHT],
() => [
LOCATION_DETAILS_HEIGHT + bottomSafeArea + SEARCH_HANDLE_HEIGHT,
'100%',
],
[bottomSafeArea]
);
const mapInitialCamera = useMemo(
Expand Down Expand Up @@ -129,7 +132,7 @@ const MapExample = () => {

//#region effects
useLayoutEffect(() => {
poiListModalRef.current?.present();
requestAnimationFrame(() => poiListModalRef.current?.present());
}, []);
//#endregion

Expand Down Expand Up @@ -170,7 +173,6 @@ const MapExample = () => {
<Weather
animatedIndex={weatherAnimatedIndex}
animatedPosition={weatherAnimatedPosition}
snapPoints={poiListSnapPoints}
/>
<BottomSheetModal
ref={poiListModalRef}
Expand All @@ -181,6 +183,7 @@ const MapExample = () => {
handleHeight={SEARCH_HANDLE_HEIGHT}
topInset={headerHeight}
enableDismissOnClose={false}
enablePanDownToClose={false}
keyboardBehavior="extend"
animatedPosition={animatedPOIListPosition}
animatedIndex={animatedPOIListIndex}
Expand Down
1 change: 1 addition & 0 deletions example/src/screens/screens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export const screens = [
slug: 'Integrations/MapExample',
getScreen: () => require('./integrations/MapExample').default,
screenOptions: {
headerTintColor: 'black',
headerTransparent: true,
},
},
Expand Down
28 changes: 25 additions & 3 deletions src/components/bottomSheet/BottomSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
animateOnMount ? -1 : _providedIndex
);
const animatedPosition = useSharedValue(INITIAL_POSITION);
const animatedNextPosition = useSharedValue(0);
const animatedNextPositionIndex = useSharedValue(0);
//#endregion

//#region conditional variables
Expand Down Expand Up @@ -503,6 +505,13 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
*/
cancelAnimation(animatedPosition);

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

/**
* set animation state to running
*/
Expand Down Expand Up @@ -915,8 +924,10 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
let nextPosition;
if (_providedIndex === -1) {
nextPosition = animatedContainerHeight.value;
animatedNextPositionIndex.value = -1;
} else {
nextPosition = animatedSnapPoints.value[_providedIndex];
animatedNextPositionIndex.value = _providedIndex;
}

/**
Expand Down Expand Up @@ -944,6 +955,7 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
if (animateOnMount) {
animateToPosition(nextPosition);
} else {
animatedNextPosition.value = nextPosition;
animatedPosition.value = nextPosition;
}
isAnimatedOnMount.value = true;
Expand All @@ -961,8 +973,7 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
JSON.stringify(_animatedSnapPoints) ===
JSON.stringify(_previousAnimatedSnapPoints) ||
!isLayoutCalculated.value ||
!isAnimatedOnMount.value ||
animatedAnimationState.value === ANIMATION_STATE.RUNNING
!isAnimatedOnMount.value
) {
return;
}
Expand All @@ -977,7 +988,18 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
});

let nextPosition;
if (animatedCurrentIndex.value === -1) {

/**
* if snap points changed while sheet is animating, then
* we stop the animation and animate to the updated point.
*/
if (animatedPosition.value !== animatedNextPosition.value) {
cancelAnimation(animatedPosition);
nextPosition =
animatedNextPositionIndex.value !== -1
? _animatedSnapPoints[animatedNextPositionIndex.value]
: animatedNextPosition.value;
} else if (animatedCurrentIndex.value === -1) {
nextPosition = animatedContainerHeight.value;
} else {
nextPosition = _animatedSnapPoints[animatedCurrentIndex.value];
Expand Down

0 comments on commit bb8e202

Please sign in to comment.