-
Notifications
You must be signed in to change notification settings - Fork 50
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(wallet-mobile): Add slide in and out animations #3791
Changes from 5 commits
aa52044
dd93275
e08cb8d
3c9d4a7
0eac9e9
33d7eac
0bd44f1
612da13
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,18 +1,42 @@ | ||||||||
import * as React from 'react' | ||||||||
import {Animated, Dimensions, PanResponder} from 'react-native' | ||||||||
import {Animated, Dimensions, Easing, PanResponder} from 'react-native' | ||||||||
|
||||||||
type Props = { | ||||||||
children: React.ReactNode | ||||||||
onSwipeOut: () => void | ||||||||
onExpired: () => void | ||||||||
} | ||||||||
|
||||||||
export const SwipeOutWrapper = ({children, onSwipeOut}: Props) => { | ||||||||
const {pan, panResponder} = usePanAnimation({onRelease: onSwipeOut}) | ||||||||
const notificationDisplayTime = 20 * 1000 // 20 seconds | ||||||||
const fadeInTime = 200 | ||||||||
const fadeOutPaddingTime = 100 | ||||||||
|
||||||||
export const SwipeOutWrapper = ({children, onSwipeOut, onExpired}: Props) => { | ||||||||
const {pan, panResponder, fadeIn, opacity, fadeOut, translateY} = usePanAnimation({onRelease: onSwipeOut}) | ||||||||
const onExpiredRef = React.useRef(onExpired) | ||||||||
onExpiredRef.current = onExpired | ||||||||
|
||||||||
React.useEffect(() => { | ||||||||
const expiredTimeout = setTimeout(() => onExpiredRef.current(), notificationDisplayTime) | ||||||||
const fadeOutTimeout = setTimeout(() => fadeOut(), notificationDisplayTime - fadeInTime - fadeOutPaddingTime) | ||||||||
|
||||||||
return () => { | ||||||||
clearTimeout(expiredTimeout) | ||||||||
clearTimeout(fadeOutTimeout) | ||||||||
} | ||||||||
}, [fadeIn, fadeOut]) | ||||||||
|
||||||||
React.useEffect(() => { | ||||||||
// When executed without setTimeout, the animation does not start | ||||||||
const fadeInTimeout = setTimeout(() => fadeIn(), 1) | ||||||||
return () => clearTimeout(fadeInTimeout) | ||||||||
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.
Suggested change
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. needs testing, cuz if its not an issue on mounting, better run with InteractionManager or requestAnimationFrame. 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. maybe animations clashing |
||||||||
}, [fadeIn]) | ||||||||
|
||||||||
return ( | ||||||||
<Animated.View | ||||||||
style={{ | ||||||||
transform: [{translateX: pan.x}], | ||||||||
transform: [{translateX: pan.x}, {translateY}], | ||||||||
opacity, | ||||||||
}} | ||||||||
{...panResponder.panHandlers} | ||||||||
> | ||||||||
|
@@ -23,9 +47,45 @@ export const SwipeOutWrapper = ({children, onSwipeOut}: Props) => { | |||||||
|
||||||||
const usePanAnimation = ({onRelease}: {onRelease: () => void}) => { | ||||||||
const pan = React.useRef(new Animated.ValueXY()).current | ||||||||
const opacity = React.useRef(new Animated.Value(0)).current | ||||||||
const translateY = React.useRef(new Animated.Value(-50)).current | ||||||||
const screenWidth = Dimensions.get('window').width | ||||||||
const screenLimitInPercentAfterWhichShouldRelease = 0.3 | ||||||||
|
||||||||
const fadeIn = React.useCallback(() => { | ||||||||
Animated.parallel([ | ||||||||
Animated.timing(opacity, { | ||||||||
toValue: 1, | ||||||||
duration: fadeInTime, | ||||||||
useNativeDriver: false, | ||||||||
easing: Easing.inOut(Easing.ease), | ||||||||
}), | ||||||||
Animated.timing(translateY, { | ||||||||
toValue: 0, | ||||||||
duration: fadeInTime, | ||||||||
useNativeDriver: false, | ||||||||
easing: Easing.inOut(Easing.ease), | ||||||||
}), | ||||||||
]).start() | ||||||||
}, [opacity, translateY]) | ||||||||
|
||||||||
const fadeOut = React.useCallback(() => { | ||||||||
Animated.parallel([ | ||||||||
Animated.timing(opacity, { | ||||||||
toValue: 0, | ||||||||
duration: fadeInTime, | ||||||||
useNativeDriver: false, | ||||||||
easing: Easing.inOut(Easing.ease), | ||||||||
}), | ||||||||
Animated.timing(translateY, { | ||||||||
toValue: -50, | ||||||||
duration: fadeInTime, | ||||||||
useNativeDriver: false, | ||||||||
easing: Easing.inOut(Easing.ease), | ||||||||
}), | ||||||||
]).start() | ||||||||
}, [opacity, translateY]) | ||||||||
|
||||||||
const panResponder = React.useRef( | ||||||||
PanResponder.create({ | ||||||||
onMoveShouldSetPanResponder: () => true, | ||||||||
|
@@ -50,5 +110,5 @@ const usePanAnimation = ({onRelease}: {onRelease: () => void}) => { | |||||||
}), | ||||||||
).current | ||||||||
|
||||||||
return {pan, panResponder} | ||||||||
return {pan, panResponder, fadeIn, fadeOut, opacity, translateY} | ||||||||
} |
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.
2 guesses here, probably mounting issue, try to wrap the fadeIn within the
useLayoutEffect
it should work, otherwise you can leverage ->requestAnimationFrame
as well.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.
This works well now, thanks!