-
Notifications
You must be signed in to change notification settings - Fork 734
Feat/new pan view improve animations #1574
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
Changes from 3 commits
536ed75
01d0b7e
08c99ab
a38f784
94a52b9
a6da607
d2e0ad7
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,16 +1,16 @@ | ||
import {isEmpty} from 'lodash'; | ||
import React, {useCallback} from 'react'; | ||
import {StyleProp, ViewStyle} from 'react-native'; | ||
import {StyleProp, View as RNView, ViewStyle} from 'react-native'; | ||
import {PanGestureHandler, PanGestureHandlerEventPayload} from 'react-native-gesture-handler'; | ||
import Animated, { | ||
useSharedValue, | ||
useAnimatedStyle, | ||
withSpring, | ||
withTiming, | ||
useAnimatedGestureHandler, | ||
runOnJS | ||
} from 'react-native-reanimated'; | ||
import {asBaseComponent} from '../../commons/new'; | ||
import {Constants} from '../../helpers'; | ||
import View, {ViewProps} from '../../components/view'; | ||
import { | ||
PanViewDirections, | ||
|
@@ -20,6 +20,7 @@ import { | |
getDismissVelocity, | ||
DEFAULT_THRESHOLD | ||
} from './panningUtil'; | ||
import useHiddenLocation from '../hooks/useHiddenLocation'; | ||
export {PanViewDirections, PanViewDismissThreshold}; | ||
|
||
export interface PanViewProps extends ViewProps { | ||
|
@@ -64,7 +65,7 @@ const PanView = (props: Props) => { | |
const { | ||
directions = [PanViewDirections.UP, PanViewDirections.DOWN, PanViewDirections.LEFT, PanViewDirections.RIGHT], | ||
dismissible, | ||
springBack, | ||
springBack: propsSpringBack, | ||
onDismiss, | ||
directionLock, | ||
threshold, | ||
|
@@ -82,6 +83,9 @@ const PanView = (props: Props) => { | |
}; | ||
}, []); | ||
|
||
const containerRef = React.createRef<RNView>(); | ||
const {onLayout, hiddenLocation} = useHiddenLocation({containerRef}); | ||
|
||
const getTranslationOptions = () => { | ||
'worklet'; | ||
return { | ||
|
@@ -105,27 +109,17 @@ const PanView = (props: Props) => { | |
runOnJS(onDismiss)(); | ||
} | ||
}, | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
[onDismiss]); | ||
|
||
const shouldDismissX = useCallback((isFinished: boolean) => { | ||
'worklet'; | ||
dismiss(isFinished); | ||
}, | ||
[dismiss]); | ||
|
||
const shouldDismissY = useCallback((isFinished: boolean) => { | ||
const springBack = useCallback(() => { | ||
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. Same here regarding the name... 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. I do find it confusing, any suggestion? 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. animateToOrigin anything that I can read and make sense without diving into the function implementation (: 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.
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.
I agree
Maybe we can have a slight difference in the name, something like 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. Done |
||
'worklet'; | ||
dismiss(isFinished); | ||
}, | ||
[dismiss]); | ||
|
||
const springBackIfNeeded = useCallback(() => { | ||
'worklet'; | ||
if (springBack) { | ||
if (propsSpringBack) { | ||
translationX.value = withSpring(0, SPRING_BACK_ANIMATION_CONFIG); | ||
translationY.value = withSpring(0, SPRING_BACK_ANIMATION_CONFIG); | ||
} | ||
}, [springBack]); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [propsSpringBack]); | ||
|
||
const onGestureEvent = useAnimatedGestureHandler({ | ||
onStart: (_event: PanGestureHandlerEventPayload, context: {initialTranslation: Frame}) => { | ||
|
@@ -139,28 +133,30 @@ const PanView = (props: Props) => { | |
const velocity = getDismissVelocity(event, directions, getTranslationOptions(), threshold); | ||
if (velocity) { | ||
waitingForDismiss.value = true; | ||
if (velocity.x !== 0) { | ||
const toX = Math.sign(translationX.value) * (Math.abs(translationX.value) + Constants.screenWidth); | ||
translationX.value = withSpring(toX, {velocity: velocity.x, damping: 50}, shouldDismissX); | ||
if (translationX.value !== 0 && velocity.x !== undefined && velocity.x !== 0) { | ||
const toX = velocity.x > 0 ? hiddenLocation.right : hiddenLocation.left; | ||
const duration = Math.abs((toX - translationX.value) / velocity.x) * 1000; | ||
translationX.value = withTiming(toX, {duration}, dismiss); | ||
} | ||
if (velocity.y !== 0) { | ||
const toY = Math.sign(translationY.value) * (Math.abs(translationY.value) + Constants.screenHeight); | ||
translationY.value = withSpring(toY, {velocity: velocity.y, damping: 50}, shouldDismissY); | ||
|
||
if (translationY.value !== 0 && velocity.y !== undefined && velocity.y !== 0) { | ||
const toY = velocity.y > 0 ? hiddenLocation.bottom : hiddenLocation.top; | ||
const duration = Math.abs((toY - translationY.value) / velocity.y) * 1000; | ||
translationY.value = withTiming(toY, {duration}, dismiss); | ||
} | ||
} else { | ||
springBackIfNeeded(); | ||
springBack(); | ||
} | ||
} else { | ||
springBackIfNeeded(); | ||
springBack(); | ||
} | ||
} | ||
}, | ||
[directions, dismissible, setTranslation, springBackIfNeeded]); | ||
[directions, dismissible, setTranslation, springBack]); | ||
|
||
return ( | ||
// TODO: delete comments once completed | ||
// <View ref={containerRef} style={containerStyle} onLayout={onLayout}> | ||
<View style={containerStyle}> | ||
<RNView ref={containerRef} style={containerStyle} onLayout={onLayout}> | ||
ethanshar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<PanGestureHandler onGestureEvent={isEmpty(directions) ? undefined : onGestureEvent}> | ||
<Animated.View | ||
// !visible.current && styles.hidden is done to fix a bug is iOS | ||
|
@@ -171,7 +167,7 @@ const PanView = (props: Props) => { | |
<View {...others}>{children}</View> | ||
</Animated.View> | ||
</PanGestureHandler> | ||
</View> | ||
</RNView> | ||
); | ||
}; | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.