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

Add swipe right drawer gesture #852

Merged
merged 1 commit into from
Dec 19, 2024
Merged
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
4 changes: 2 additions & 2 deletions ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2043,9 +2043,9 @@ SPEC CHECKSUMS:
TcpSockets: 14306fb79f9750ea7d2ddd02d8bed182abb01797
Toast: 91b396c56ee72a5790816f40d3a94dd357abc196
Turf: a1604e74adce15c58462c9ae2acdbf049d5be35e
Yoga: 2246eea72aaf1b816a68a35e6e4b74563653ae09
Yoga: 950bbfd7e6f04790fdb51149ed51df41f329fcc8
ZXingObjC: 8898711ab495761b2dbbdec76d90164a6d7e14c5

PODFILE CHECKSUM: 51a354c5ff94b58e8c8bd1903d2326a93a17b4d0

COCOAPODS: 1.16.2
COCOAPODS: 1.16.1
19 changes: 18 additions & 1 deletion src/app/services/ServiceSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,18 @@ import HeliumBottomSheet from '@components/HeliumBottomSheet'
import { useSafeAreaInsets } from 'react-native-safe-area-context'
import { useAccountStorage } from '@config/storage/AccountStorageProvider'
import changeNavigationBarColor from 'react-native-navigation-bar-color'
import { Platform, StyleProp, ViewStyle } from 'react-native'
import {
GestureResponderEvent,
Platform,
StyleProp,
ViewStyle,
} from 'react-native'
import { wh } from '@utils/layout'
import { useSelector } from 'react-redux'
import { RootState } from '@store/rootReducer'
import StickersPage from '@features/stickers/StickersPage'
import { StickerProvider } from '@features/stickers/StickerContext'
import { useSwipe } from '@hooks/useSwipe'
import { ServiceSheetNavigationProp } from './serviceSheetTypes'

type ServiceSheetProps = {
Expand All @@ -59,6 +65,12 @@ const ServiceSheet = ({

const { rootSheetPosition } = useSelector((state: RootState) => state.app)

const onSwipeRight = useCallback((_: GestureResponderEvent) => {
setIsExpanded(true)
}, [])

const { onTouchStart, onTouchEnd } = useSwipe(undefined, onSwipeRight, 6)

const onRoute = useCallback(
(value: string) => {
setIsExpanded(false)
Expand Down Expand Up @@ -179,6 +191,7 @@ const ServiceSheet = ({
),
}
}, [isChild, rootSheetPosition])

return (
<Box flex={1} style={{ paddingTop: top }}>
<SideDrawer
Expand All @@ -205,6 +218,7 @@ const ServiceSheet = ({
backgroundStyle={backgroundStyle}
// // TODO: Bring this back once we have the stickers page
enablePanDownToClose={false}
enableContentPanningGesture={false}
>
<ThemeProvider theme={lightTheme}>
<Box
Expand All @@ -213,6 +227,9 @@ const ServiceSheet = ({
flexDirection="column"
zIndex={100}
position="relative"
onTouchStart={onTouchStart}
onTouchEnd={onTouchEnd}
onTouchEndCapture={onTouchEnd}
>
{children}
</Box>
Expand Down
34 changes: 34 additions & 0 deletions src/hooks/useSwipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Dimensions, GestureResponderEvent } from 'react-native'

const windowWidth = Dimensions.get('window').width

export function useSwipe(
onSwipeLeft?: ((event: GestureResponderEvent) => void) | undefined,
onSwipeRight?: ((event: GestureResponderEvent) => void) | undefined,
rangeOffset = 4,
) {
let firstTouch = 0

// set user touch start position
function onTouchStart(e: GestureResponderEvent) {
firstTouch = e.nativeEvent.pageX
}

// when touch ends check for swipe directions
function onTouchEnd(e: GestureResponderEvent) {
// get touch position and screen size
const positionX = e.nativeEvent.pageX
const range = windowWidth / rangeOffset

// check if position is growing positively and has reached specified range
if (positionX - firstTouch > range) {
if (onSwipeRight) onSwipeRight(e)
}
// check if position is growing negatively and has reached specified range
else if (firstTouch - positionX > range) {
if (onSwipeLeft) onSwipeLeft(e)
}
}

return { onTouchStart, onTouchEnd }
}
Loading