-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a0d0e3d
commit aa148ec
Showing
2 changed files
with
64 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
apps/wallet-mobile/src/features/Notifications/useCases/common/SwipeOutWrapper.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import * as React from 'react' | ||
import {Animated, Dimensions, PanResponder} from 'react-native' | ||
|
||
export const SwipeOutWrapper = ({children, onSwipeOut}: {children: React.ReactNode; onSwipeOut: () => void}) => { | ||
const {pan, panResponder} = usePanAnimation({onRelease: onSwipeOut}) | ||
|
||
return ( | ||
<Animated.View | ||
style={{ | ||
transform: [{translateX: pan.x}], | ||
}} | ||
{...panResponder.panHandlers} | ||
> | ||
{children} | ||
</Animated.View> | ||
) | ||
} | ||
|
||
const usePanAnimation = ({onRelease}: {onRelease: () => void}) => { | ||
const pan = React.useRef(new Animated.ValueXY()).current | ||
const screenWidth = Dimensions.get('window').width | ||
const screenLimitInPercentAfterWhichShouldRelease = 0.3 | ||
|
||
const panResponder = React.useRef( | ||
PanResponder.create({ | ||
onMoveShouldSetPanResponder: () => true, | ||
onPanResponderMove: (e, gestureState) => { | ||
if (gestureState.dx > 0) { | ||
Animated.event([null, {dx: pan.x, dy: pan.y}], {useNativeDriver: false})(e, gestureState) | ||
} | ||
}, | ||
onPanResponderRelease: (e, gestureState) => { | ||
if (gestureState.dx > screenWidth * screenLimitInPercentAfterWhichShouldRelease) { | ||
Animated.spring(pan, { | ||
toValue: {x: screenWidth, y: 0}, | ||
useNativeDriver: false, | ||
}).start(() => onRelease()) | ||
} else { | ||
Animated.spring(pan, { | ||
toValue: {x: 0, y: 0}, | ||
useNativeDriver: false, | ||
}).start() | ||
} | ||
}, | ||
}), | ||
).current | ||
|
||
return {pan, panResponder} | ||
} |