-
Notifications
You must be signed in to change notification settings - Fork 15
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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'; | ||
import React, { | ||
forwardRef, | ||
Ref, | ||
|
@@ -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; | ||
|
@@ -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( | ||
|
@@ -71,6 +72,8 @@ const CardsSwipe = forwardRef( | |
onSwipedLeft = () => {}, | ||
onSwipedRight = () => {}, | ||
onNoMoreCards = () => {}, | ||
onDragAround = () => {}, | ||
onFinish = () => {}, | ||
}: CardsSwipeProps, | ||
ref: Ref<CardsSwipeRefObject> | ||
) => { | ||
|
@@ -275,12 +278,14 @@ const CardsSwipe = forwardRef( | |
cardContainerStyle, | ||
}} | ||
> | ||
{renderCard(cards[secondIndex])} | ||
{renderCard(cards[secondIndex], index)} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
@@ -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}> | ||
|
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, | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please, update documentation with all these changes. |
||
} | ||
|
||
type AnimatedGHContext = { | ||
|
@@ -43,6 +46,8 @@ const SwipePan = ({ | |
onStart, | ||
onChangeDirection, | ||
onEnd, | ||
onDragAround, | ||
onFinish, | ||
originY, | ||
children, | ||
}: Props) => { | ||
|
@@ -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) { | ||
|
@@ -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; | ||
|
@@ -82,6 +88,7 @@ const SwipePan = ({ | |
y.value = withSpring(0); | ||
} | ||
}, | ||
onFinish: (event) => runOnJS(onFinish)(event), | ||
}); | ||
|
||
return ( | ||
|
There was a problem hiding this comment.
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.