|
| 1 | +import { useEffect, useRef, useState } from 'react'; |
| 2 | +import { |
| 3 | + Animated, |
| 4 | + Dimensions, |
| 5 | + Modal, |
| 6 | + PanResponder, |
| 7 | + Pressable, |
| 8 | + StyleSheet, |
| 9 | + View, |
| 10 | +} from 'react-native'; |
| 11 | + |
| 12 | +import { bottomSheetStyle } from '@/styles'; |
| 13 | + |
| 14 | +type BottomSheetProps = { |
| 15 | + visible: boolean; |
| 16 | + onClose: () => void; |
| 17 | + children: React.ReactNode; |
| 18 | + snapHeight?: number; |
| 19 | + testID: string; |
| 20 | +}; |
| 21 | + |
| 22 | +const SCREEN_HEIGHT = Dimensions.get('window').height; |
| 23 | +const DEFAULT_SNAP_HEIGHT = 0.9; |
| 24 | +const CLOSE_THRESHOLD = 120; |
| 25 | +const DRAG_ACTIVATION_THRESHOLD = 5; |
| 26 | + |
| 27 | +const BottomSheet = ({ |
| 28 | + visible, |
| 29 | + onClose, |
| 30 | + children, |
| 31 | + snapHeight = DEFAULT_SNAP_HEIGHT, |
| 32 | +}: BottomSheetProps) => { |
| 33 | + const [internalVisible, setInternalVisible] = useState(false); |
| 34 | + const translateY = useRef(new Animated.Value(SCREEN_HEIGHT)).current; |
| 35 | + const SHEET_MAX_HEIGHT = SCREEN_HEIGHT * snapHeight; |
| 36 | + |
| 37 | + const sheetMaxHeightRef = useRef(SHEET_MAX_HEIGHT); |
| 38 | + |
| 39 | + useEffect(() => { |
| 40 | + sheetMaxHeightRef.current = SCREEN_HEIGHT * snapHeight; |
| 41 | + }, [snapHeight]); |
| 42 | + |
| 43 | + const panResponder = useRef( |
| 44 | + PanResponder.create({ |
| 45 | + onStartShouldSetPanResponder: () => true, |
| 46 | + onMoveShouldSetPanResponder: (_, gestureState) => |
| 47 | + Math.abs(gestureState.dy) > DRAG_ACTIVATION_THRESHOLD, |
| 48 | + onPanResponderMove: (_, gestureState) => { |
| 49 | + const sheetMaxHeight = sheetMaxHeightRef.current; |
| 50 | + const newY = SCREEN_HEIGHT - sheetMaxHeight + gestureState.dy; |
| 51 | + if (newY >= SCREEN_HEIGHT - sheetMaxHeight) { |
| 52 | + translateY.setValue(newY); |
| 53 | + } |
| 54 | + }, |
| 55 | + onPanResponderRelease: (_, gestureState) => { |
| 56 | + if (gestureState.dy > CLOSE_THRESHOLD) { |
| 57 | + onClose(); |
| 58 | + } else { |
| 59 | + Animated.spring(translateY, { |
| 60 | + toValue: SCREEN_HEIGHT - sheetMaxHeightRef.current, |
| 61 | + useNativeDriver: true, |
| 62 | + stiffness: 100, |
| 63 | + damping: 20, |
| 64 | + }).start(); |
| 65 | + } |
| 66 | + }, |
| 67 | + }), |
| 68 | + ).current; |
| 69 | + |
| 70 | + useEffect(() => { |
| 71 | + if (visible) { |
| 72 | + setInternalVisible(true); |
| 73 | + Animated.spring(translateY, { |
| 74 | + toValue: SCREEN_HEIGHT - sheetMaxHeightRef.current, |
| 75 | + useNativeDriver: true, |
| 76 | + stiffness: 250, |
| 77 | + damping: 28, |
| 78 | + mass: 1, |
| 79 | + }).start(); |
| 80 | + } else { |
| 81 | + Animated.timing(translateY, { |
| 82 | + toValue: SCREEN_HEIGHT, |
| 83 | + duration: 220, |
| 84 | + useNativeDriver: true, |
| 85 | + }).start(() => setInternalVisible(false)); |
| 86 | + } |
| 87 | + }, [visible, translateY]); |
| 88 | + |
| 89 | + if (!internalVisible) { |
| 90 | + return null; |
| 91 | + } |
| 92 | + |
| 93 | + return ( |
| 94 | + <Modal transparent animationType="none" visible={internalVisible}> |
| 95 | + <Pressable style={styles.backdrop} onPress={onClose} /> |
| 96 | + <Animated.View |
| 97 | + style={[styles.sheet, { height: SHEET_MAX_HEIGHT, transform: [{ translateY }] }]} |
| 98 | + > |
| 99 | + <View style={styles.grabber} {...panResponder.panHandlers} /> |
| 100 | + {children} |
| 101 | + </Animated.View> |
| 102 | + </Modal> |
| 103 | + ); |
| 104 | +}; |
| 105 | + |
| 106 | +const styles = StyleSheet.create({ |
| 107 | + backdrop: bottomSheetStyle.backdrop, |
| 108 | + sheet: bottomSheetStyle.sheet, |
| 109 | + grabber: bottomSheetStyle.grabber, |
| 110 | +}); |
| 111 | + |
| 112 | +export default BottomSheet; |
0 commit comments