|
| 1 | +import React, { useRef, useState } from 'react'; |
| 2 | +import { StyleSheet, Text, View } from 'react-native'; |
| 3 | +import { Gesture, GestureDetector } from 'react-native-gesture-handler'; |
| 4 | +import { SharedValue } from 'react-native-reanimated'; |
| 5 | + |
| 6 | +import ReanimatedDrawerLayout, { |
| 7 | + DrawerType, |
| 8 | + DrawerPosition, |
| 9 | + DrawerLayoutMethods, |
| 10 | + DrawerLockMode, |
| 11 | +} from 'react-native-gesture-handler/ReanimatedDrawerLayout'; |
| 12 | +import { LoremIpsum } from '../../../src/common'; |
| 13 | + |
| 14 | +const DrawerPage = ({ progress }: { progress?: SharedValue }) => { |
| 15 | + progress && console.log('Drawer opening progress:', progress); |
| 16 | + return ( |
| 17 | + <View style={styles.drawerContainer}> |
| 18 | + <LoremIpsum /> |
| 19 | + </View> |
| 20 | + ); |
| 21 | +}; |
| 22 | + |
| 23 | +export default function ReanimatedDrawerExample() { |
| 24 | + const drawerRef = useRef<DrawerLayoutMethods>(null); |
| 25 | + const [side, setSide] = useState(DrawerPosition.LEFT); |
| 26 | + const [type, setType] = useState(DrawerType.FRONT); |
| 27 | + const [lock, setLock] = useState(DrawerLockMode.UNLOCKED); |
| 28 | + |
| 29 | + const tapGesture = Gesture.Tap() |
| 30 | + .runOnJS(true) |
| 31 | + .onStart(() => |
| 32 | + drawerRef.current?.openDrawer({ animationSpeed: 1, initialVelocity: 0 }) |
| 33 | + ); |
| 34 | + |
| 35 | + const toggleSideGesture = Gesture.Tap() |
| 36 | + .runOnJS(true) |
| 37 | + .onStart(() => |
| 38 | + setSide( |
| 39 | + side === DrawerPosition.LEFT |
| 40 | + ? DrawerPosition.RIGHT |
| 41 | + : DrawerPosition.LEFT |
| 42 | + ) |
| 43 | + ); |
| 44 | + |
| 45 | + const toggleTypeGesture = Gesture.Tap() |
| 46 | + .runOnJS(true) |
| 47 | + .onStart(() => |
| 48 | + setType( |
| 49 | + type === DrawerType.FRONT |
| 50 | + ? DrawerType.BACK |
| 51 | + : type === DrawerType.BACK |
| 52 | + ? DrawerType.SLIDE |
| 53 | + : DrawerType.FRONT |
| 54 | + ) |
| 55 | + ); |
| 56 | + |
| 57 | + const toggleLockGesture = Gesture.Tap() |
| 58 | + .runOnJS(true) |
| 59 | + .onStart(() => |
| 60 | + setLock( |
| 61 | + lock === DrawerLockMode.UNLOCKED |
| 62 | + ? DrawerLockMode.LOCKED_CLOSED |
| 63 | + : lock === DrawerLockMode.LOCKED_CLOSED |
| 64 | + ? DrawerLockMode.LOCKED_OPEN |
| 65 | + : DrawerLockMode.UNLOCKED |
| 66 | + ) |
| 67 | + ); |
| 68 | + |
| 69 | + return ( |
| 70 | + <ReanimatedDrawerLayout |
| 71 | + ref={drawerRef} |
| 72 | + renderNavigationView={() => <DrawerPage />} |
| 73 | + drawerPosition={side} |
| 74 | + drawerType={type} |
| 75 | + drawerLockMode={lock}> |
| 76 | + <View style={styles.innerContainer}> |
| 77 | + <GestureDetector gesture={tapGesture}> |
| 78 | + <View style={styles.box}> |
| 79 | + <Text>Open drawer</Text> |
| 80 | + </View> |
| 81 | + </GestureDetector> |
| 82 | + <GestureDetector gesture={toggleSideGesture}> |
| 83 | + <View style={styles.box}> |
| 84 | + <Text> |
| 85 | + Currently opening from:{' '} |
| 86 | + {side === DrawerPosition.LEFT ? 'left' : 'right'} |
| 87 | + </Text> |
| 88 | + </View> |
| 89 | + </GestureDetector> |
| 90 | + <GestureDetector gesture={toggleTypeGesture}> |
| 91 | + <View style={styles.box}> |
| 92 | + <Text> |
| 93 | + Current background type:{' '} |
| 94 | + {type === DrawerType.FRONT |
| 95 | + ? 'front' |
| 96 | + : type === DrawerType.BACK |
| 97 | + ? 'back' |
| 98 | + : 'slide'} |
| 99 | + </Text> |
| 100 | + </View> |
| 101 | + </GestureDetector> |
| 102 | + <GestureDetector gesture={toggleLockGesture}> |
| 103 | + <View style={styles.box}> |
| 104 | + <Text> |
| 105 | + Current lock mode:{' '} |
| 106 | + {lock === DrawerLockMode.UNLOCKED |
| 107 | + ? 'unlocked' |
| 108 | + : lock === DrawerLockMode.LOCKED_OPEN |
| 109 | + ? 'locked-open' |
| 110 | + : 'locked-closed'} |
| 111 | + </Text> |
| 112 | + </View> |
| 113 | + </GestureDetector> |
| 114 | + </View> |
| 115 | + </ReanimatedDrawerLayout> |
| 116 | + ); |
| 117 | +} |
| 118 | + |
| 119 | +const styles = StyleSheet.create({ |
| 120 | + drawerContainer: { |
| 121 | + flex: 1, |
| 122 | + justifyContent: 'center', |
| 123 | + alignItems: 'center', |
| 124 | + backgroundColor: 'pink', |
| 125 | + }, |
| 126 | + innerContainer: { |
| 127 | + flex: 1, |
| 128 | + backgroundColor: 'white', |
| 129 | + alignItems: 'center', |
| 130 | + justifyContent: 'center', |
| 131 | + gap: 20, |
| 132 | + }, |
| 133 | + box: { |
| 134 | + width: 150, |
| 135 | + padding: 10, |
| 136 | + paddingHorizontal: 5, |
| 137 | + backgroundColor: 'pink', |
| 138 | + }, |
| 139 | +}); |
0 commit comments