Skip to content

Commit 35d0583

Browse files
authored
Add swipe right drawer gesture (#852)
1 parent a22932a commit 35d0583

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

ios/Podfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2043,9 +2043,9 @@ SPEC CHECKSUMS:
20432043
TcpSockets: 14306fb79f9750ea7d2ddd02d8bed182abb01797
20442044
Toast: 91b396c56ee72a5790816f40d3a94dd357abc196
20452045
Turf: a1604e74adce15c58462c9ae2acdbf049d5be35e
2046-
Yoga: 2246eea72aaf1b816a68a35e6e4b74563653ae09
2046+
Yoga: 950bbfd7e6f04790fdb51149ed51df41f329fcc8
20472047
ZXingObjC: 8898711ab495761b2dbbdec76d90164a6d7e14c5
20482048

20492049
PODFILE CHECKSUM: 51a354c5ff94b58e8c8bd1903d2326a93a17b4d0
20502050

2051-
COCOAPODS: 1.16.2
2051+
COCOAPODS: 1.16.1

src/app/services/ServiceSheet.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,18 @@ import HeliumBottomSheet from '@components/HeliumBottomSheet'
2828
import { useSafeAreaInsets } from 'react-native-safe-area-context'
2929
import { useAccountStorage } from '@config/storage/AccountStorageProvider'
3030
import changeNavigationBarColor from 'react-native-navigation-bar-color'
31-
import { Platform, StyleProp, ViewStyle } from 'react-native'
31+
import {
32+
GestureResponderEvent,
33+
Platform,
34+
StyleProp,
35+
ViewStyle,
36+
} from 'react-native'
3237
import { wh } from '@utils/layout'
3338
import { useSelector } from 'react-redux'
3439
import { RootState } from '@store/rootReducer'
3540
import StickersPage from '@features/stickers/StickersPage'
3641
import { StickerProvider } from '@features/stickers/StickerContext'
42+
import { useSwipe } from '@hooks/useSwipe'
3743
import { ServiceSheetNavigationProp } from './serviceSheetTypes'
3844

3945
type ServiceSheetProps = {
@@ -59,6 +65,12 @@ const ServiceSheet = ({
5965

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

68+
const onSwipeRight = useCallback((_: GestureResponderEvent) => {
69+
setIsExpanded(true)
70+
}, [])
71+
72+
const { onTouchStart, onTouchEnd } = useSwipe(undefined, onSwipeRight, 6)
73+
6274
const onRoute = useCallback(
6375
(value: string) => {
6476
setIsExpanded(false)
@@ -179,6 +191,7 @@ const ServiceSheet = ({
179191
),
180192
}
181193
}, [isChild, rootSheetPosition])
194+
182195
return (
183196
<Box flex={1} style={{ paddingTop: top }}>
184197
<SideDrawer
@@ -205,6 +218,7 @@ const ServiceSheet = ({
205218
backgroundStyle={backgroundStyle}
206219
// // TODO: Bring this back once we have the stickers page
207220
enablePanDownToClose={false}
221+
enableContentPanningGesture={false}
208222
>
209223
<ThemeProvider theme={lightTheme}>
210224
<Box
@@ -213,6 +227,9 @@ const ServiceSheet = ({
213227
flexDirection="column"
214228
zIndex={100}
215229
position="relative"
230+
onTouchStart={onTouchStart}
231+
onTouchEnd={onTouchEnd}
232+
onTouchEndCapture={onTouchEnd}
216233
>
217234
{children}
218235
</Box>

src/hooks/useSwipe.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { Dimensions, GestureResponderEvent } from 'react-native'
2+
3+
const windowWidth = Dimensions.get('window').width
4+
5+
export function useSwipe(
6+
onSwipeLeft?: ((event: GestureResponderEvent) => void) | undefined,
7+
onSwipeRight?: ((event: GestureResponderEvent) => void) | undefined,
8+
rangeOffset = 4,
9+
) {
10+
let firstTouch = 0
11+
12+
// set user touch start position
13+
function onTouchStart(e: GestureResponderEvent) {
14+
firstTouch = e.nativeEvent.pageX
15+
}
16+
17+
// when touch ends check for swipe directions
18+
function onTouchEnd(e: GestureResponderEvent) {
19+
// get touch position and screen size
20+
const positionX = e.nativeEvent.pageX
21+
const range = windowWidth / rangeOffset
22+
23+
// check if position is growing positively and has reached specified range
24+
if (positionX - firstTouch > range) {
25+
if (onSwipeRight) onSwipeRight(e)
26+
}
27+
// check if position is growing negatively and has reached specified range
28+
else if (firstTouch - positionX > range) {
29+
if (onSwipeLeft) onSwipeLeft(e)
30+
}
31+
}
32+
33+
return { onTouchStart, onTouchEnd }
34+
}

0 commit comments

Comments
 (0)