Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): extend gesture handler #12

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions src/components/CardsSwipe/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import CardWrap from '../CardWrap';
import SwipePan, { SWIPE_DIRECTION } from '../SwipePan';
import styles from './styles';
Comment on lines +1 to +3
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, do not change order of imports. Keep the next order: library, own components, other stuff.

import React, {
forwardRef,
Ref,
Expand All @@ -17,17 +20,13 @@ import Animated, {
runOnJS,
withDelay,
} from 'react-native-reanimated';

import SwipePan, { SWIPE_DIRECTION } from '../SwipePan';
import CardWrap from '../CardWrap';

import styles from './styles';
import type { GestureHandleEvent } from 'lib/typescript/types';

const { width } = Dimensions.get('window');

interface CardsSwipeProps {
cards: Array<any>;
renderCard: (card: any) => React.ReactNode;
renderCard: (card: any, index: number) => React.ReactNode;
loop?: boolean;
renderNoMoreCard?: () => React.ReactNode;
renderYep?: () => React.ReactNode;
Expand All @@ -46,6 +45,8 @@ interface CardsSwipeProps {
onSwipedLeft?: (index: number) => void;
onSwipedRight?: (index: number) => void;
onNoMoreCards?: () => void;
onDragAround?: (event: GestureHandleEvent) => void;
onFinish?: (event: GestureHandleEvent) => void;
}

const CardsSwipe = forwardRef(
Expand All @@ -71,6 +72,8 @@ const CardsSwipe = forwardRef(
onSwipedLeft = () => {},
onSwipedRight = () => {},
onNoMoreCards = () => {},
onDragAround = () => {},
onFinish = () => {},
}: CardsSwipeProps,
ref: Ref<CardsSwipeRefObject>
) => {
Expand Down Expand Up @@ -275,12 +278,14 @@ const CardsSwipe = forwardRef(
cardContainerStyle,
}}
>
{renderCard(cards[secondIndex])}
{renderCard(cards[secondIndex], index)}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be in this case you should pass secondIndex instead index ? It would be the right, if you need index of card that render

</CardWrap>
) : null}
{index >= 0 ? (
<SwipePan
{...{
onDragAround,
onFinish,
onSnap: onCardSwiped,
onStart: onStartSwipe,
onChangeDirection: onChangeSwipeDirection,
Expand All @@ -296,7 +301,7 @@ const CardsSwipe = forwardRef(
cardContainerStyle,
}}
>
{renderCard(cards[index])}
{renderCard(cards[index], index)}
<Animated.View style={styles.overlay} pointerEvents={'none'}>
<View style={styles.row}>
<Animated.View style={likeOpacityStyle}>
Expand Down
19 changes: 13 additions & 6 deletions src/components/SwipePan/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as React from 'react';
import { Dimensions } from 'react-native';
import type { GestureHandleEvent } from 'lib/typescript/types';
import { PanGestureHandler } from 'react-native-gesture-handler';
import {
useAnimatedGestureHandler,
Expand All @@ -22,11 +23,13 @@ interface Props {
x: Value;
y: Value;
originY: Value;
children: React.ReactNode;
onSnap: (swipedRight: boolean) => void;
onStart: () => void;
onEnd: () => void;
onStart: (event: GestureHandleEvent) => void;
onEnd: (event: GestureHandleEvent) => void;
onChangeDirection: (direction: SWIPE_DIRECTION) => void;
children: React.ReactNode;
onDragAround: (event: GestureHandleEvent) => void;
onFinish: (event: GestureHandleEvent) => void;
Comment on lines +28 to +32
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, update documentation with all these changes.

}

type AnimatedGHContext = {
Expand All @@ -43,6 +46,8 @@ const SwipePan = ({
onStart,
onChangeDirection,
onEnd,
onDragAround,
onFinish,
originY,
children,
}: Props) => {
Expand All @@ -53,11 +58,13 @@ const SwipePan = ({
ctx.startY = y.value;

originY.value = event.y;
runOnJS(onStart)();
runOnJS(onStart)(event);
},
onActive: (event, ctx) => {
x.value = ctx.startX + event.translationX;
y.value = ctx.startY + event.translationY;
if (onDragAround) runOnJS(onDragAround)(event);

const direction =
Math.round(x.value) > 0 ? SWIPE_DIRECTION.RIGHT : SWIPE_DIRECTION.LEFT;
if (direction !== directionX.value) {
Expand All @@ -66,8 +73,7 @@ const SwipePan = ({
}
},
onEnd: (event, ctx) => {
runOnJS(onEnd)();

runOnJS(onEnd)(event);
const thresh = width * 0.4;
const diff = ctx.startX + event.translationX;
directionX.value = SWIPE_DIRECTION.DEFAULT;
Expand All @@ -82,6 +88,7 @@ const SwipePan = ({
y.value = withSpring(0);
}
},
onFinish: (event) => runOnJS(onFinish)(event),
});

return (
Expand Down