Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
piaskowyk committed Nov 14, 2023
1 parent 5cfa097 commit 53a93f5
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 45 deletions.
3 changes: 1 addition & 2 deletions Example/src/screens/SwipeBackAnimation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
NativeStackNavigationProp,
} from 'react-native-screens/native-stack';
import { Button } from '../shared';
// import { ScreenTransition } from 'react-native-reanimated';

type StackParamList = {
ScreenA: undefined;
Expand Down Expand Up @@ -62,7 +61,7 @@ const App = (): JSX.Element => (
goBackGesture: 'twoDimensionalSwipe',
}}
/>
<Stack.Screen name="ScreenC" component={ScreenC} options={{}} />
<Stack.Screen name="ScreenC" component={ScreenC} />
</Stack.Navigator>
);

Expand Down
2 changes: 1 addition & 1 deletion ios/RNSModule.mm
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ - (RNSScreenStackView *)getStackView:(nonnull NSNumber *)stackTag
{
RNSScreenStackView *view = [self getScreenStackView:stackTag];
if (![view isKindOfClass:[RNSScreenStackView class]]) {
RCTLogError(@"Invalid svg returned from registry, expecting RNSScreenStackView, got: %@", view);
RCTLogError(@"Invalid view type, expecting RNSScreenStackView, got: %@", view);
return nil;
}
return view;
Expand Down
92 changes: 56 additions & 36 deletions src/gesture-handler/GestureDetector.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React from 'react';
import { Dimensions } from 'react-native';

import {
GestureDetector,
Gesture,
Expand Down Expand Up @@ -46,41 +45,68 @@ const AnimationForGesture = {
twoDimensionalSwipe: ScreenTransition.TwoDimensional,
};

const EmptyGestureHandler = Gesture.Tap();

const SupportedGestures = [
'swipeRight',
'swipeLeft',
'swipeDown',
'swipeUp',
'horizontalSwipe',
'verticalSwipe',
'twoDimensionalSwipe',
];

const DefaultEvent: GestureUpdateEvent<PanGestureHandlerEventPayload> = {
absoluteX: 0,
absoluteY: 0,
handlerTag: 0,
numberOfPointers: 0,
state: 0,
translationX: 0,
translationY: 0,
velocityX: 0,
velocityY: 0,
x: 0,
y: 0,
};

const DefaultScreenDimensions = {
width: 0,
height: 0,
x: 0,
y: 0,
pageX: 0,
pageY: 0,
};

const TransitionHandler = ({
children,
stackRef,
goBackGesture,
nearByScreenEdgeGesture,
screenEdgeGesture,
transitionAnimation: userTransitionAnimation,
}: GestureProviderProps) => {
if (stackRef === undefined) {
throw new Error('[RNScreens] You have to specify `stackRef`');
}
stackRef.current = useAnimatedRef();
const defaultEvent: GestureUpdateEvent<PanGestureHandlerEventPayload> = {
absoluteX: 0,
absoluteY: 0,
handlerTag: 0,
numberOfPointers: 0,
state: 0,
translationX: 0,
translationY: 0,
velocityX: 0,
velocityY: 0,
x: 0,
y: 0,
};
const sharedEvent = useSharedValue(defaultEvent);
const startingGesturePosition = useSharedValue(defaultEvent);
const sharedEvent = useSharedValue(DefaultEvent);
const startingGesturePosition = useSharedValue(DefaultEvent);
const canPerformUpdates = useSharedValue(false);
let transitionAnimation = ScreenTransition.SwipeRight;
if (userTransitionAnimation) {
transitionAnimation = userTransitionAnimation;
if (!goBackGesture) {
throw new Error(
'You have to specify `goBackGesture` when using `transitionAnimation`'
'[RNScreens] You have to specify `goBackGesture` when using `transitionAnimation`'
);
}
} else {
if (goBackGesture !== undefined) {
if (!!goBackGesture && SupportedGestures.includes(goBackGesture)) {
transitionAnimation = AnimationForGesture[goBackGesture];
} else if (goBackGesture !== undefined) {
throw new Error(`[RNScreens] Unknown goBackGesture: ${goBackGesture}`);
}
}
const screenTransitionConfig = makeMutable(
Expand All @@ -91,17 +117,9 @@ const TransitionHandler = ({
sharedEvent,
startingGesturePosition,
screenTransition: transitionAnimation,
isSwipeGesture: true,
isTransitionCanceled: false,
goBackGesture: goBackGesture ?? 'swipeRight',
screenDimensions: {
width: 0,
height: 0,
x: 0,
y: 0,
pageX: 0,
pageY: 0,
},
screenDimensions: DefaultScreenDimensions,
onFinishAnimation: () => {
'worklet';
},
Expand All @@ -110,12 +128,12 @@ const TransitionHandler = ({
);
const refCurrent = stackRef.current;
if (refCurrent === null) {
throw new Error('You have to specify `stackRefWrapper`');
throw new Error('[RNScreens] You have to specify `stackRefWrapper`');
}

function onStart(event: GestureUpdateEvent<PanGestureHandlerEventPayload>) {
'worklet';
sharedEvent.value = defaultEvent;
sharedEvent.value = event;
const transitionConfig = screenTransitionConfig.value;
const animatedRef = refCurrent;
if (!animatedRef) {
Expand Down Expand Up @@ -253,7 +271,7 @@ const TransitionHandler = ({
.onUpdate(onUpdate)
.onEnd(onEnd);

if (nearByScreenEdgeGesture) {
if (screenEdgeGesture) {
const HIT_SLOP_SIZE = 50;
if (goBackGesture === 'swipeRight') {
panGesture = panGesture
Expand All @@ -266,18 +284,20 @@ const TransitionHandler = ({
} else if (goBackGesture === 'swipeDown') {
panGesture = panGesture
.activeOffsetY(20)
.hitSlop({ top: 0, height: Dimensions.get('window').height * 0.15 });
.hitSlop({ top: 0, height: Dimensions.get('window').height * 0.2 });
// workaround, because we don't have access to header height
} else if (goBackGesture === 'swipeUp') {
panGesture = panGesture
.activeOffsetY(-20)
.hitSlop({ bottom: 0, height: HIT_SLOP_SIZE });
}
}
if (!goBackGesture) {
return <>{children}</>;
}
return <GestureDetector gesture={panGesture}>{children}</GestureDetector>;
return (
<GestureDetector
gesture={goBackGesture ? panGesture : EmptyGestureHandler}>
{children}
</GestureDetector>
);
};

export default TransitionHandler;
4 changes: 2 additions & 2 deletions src/native-stack/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ export type NativeStackNavigationOptions = {

goBackGesture?: GoBackGesture;
transitionAnimation?: AnimatedScreenTransition;
nearByScreenEdgeGesture?: false;
screenEdgeGesture?: false;
};

export type NativeStackNavigatorProps =
Expand Down Expand Up @@ -519,5 +519,5 @@ export type GestureProviderProps = PropsWithChildren<{
stackRef: React.MutableRefObject<unknown>;
goBackGesture: GoBackGesture | undefined;
transitionAnimation: AnimatedScreenTransition | undefined;
nearByScreenEdgeGesture: false | undefined;
screenEdgeGesture: false | undefined;
}>;
5 changes: 1 addition & 4 deletions src/native-stack/views/NativeStackView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,6 @@ function NativeStackViewInner({
const { key, routes } = state;

const topScreenOptions = descriptors[state.routes[state.index].key].options;

const stackRef = React.useRef(null);
const GestureDetector = React.useContext(GHContext);

Expand All @@ -413,9 +412,7 @@ function NativeStackViewInner({
stackRef={stackRef}
goBackGesture={topScreenOptions?.goBackGesture}
transitionAnimation={topScreenOptions?.transitionAnimation}
nearByScreenEdgeGesture={
topScreenOptions?.nearByScreenEdgeGesture ?? false
}>
screenEdgeGesture={topScreenOptions?.screenEdgeGesture ?? false}>
<ScreenStack style={styles.container} stackRef={stackRef}>
{routes.map((route, index) => (
<RouteView
Expand Down

0 comments on commit 53a93f5

Please sign in to comment.