From 3edb2d1f9a9a8b1ba2e04803cd12306e4353199b Mon Sep 17 00:00:00 2001 From: Mo Gorhom Date: Sun, 6 Jun 2021 17:03:19 +0100 Subject: [PATCH] feat: introduced more stable handling for dynamic snap points --- .../advanced/DynamicSnapPointExample.tsx | 34 ++++------- .../screens/modal/DynamicSnapPointExample.tsx | 36 +++++------- src/components/bottomSheet/BottomSheet.tsx | 44 +++++++++------ src/components/bottomSheet/types.d.ts | 6 +- src/components/bottomSheetDebugView/styles.ts | 2 +- .../BottomSheetHandleContainer.tsx | 42 +++++++------- .../BottomSheetView.tsx} | 21 ++++--- src/components/bottomSheetView/index.ts | 1 + .../{view => bottomSheetView}/styles.ts | 0 .../{view => bottomSheetView}/types.d.ts | 3 +- src/components/view/index.ts | 1 - src/contexts/internal.ts | 1 + src/hooks/index.ts | 1 + src/hooks/useBottomSheetDynamicSnapPoints.ts | 56 +++++++++++++++++++ src/hooks/useNormalizedSnapPoints.ts | 5 +- src/hooks/usePropsValidator.ts | 9 +-- src/hooks/useScrollableInternal.ts | 3 + src/index.ts | 3 +- 18 files changed, 166 insertions(+), 102 deletions(-) rename src/components/{view/View.tsx => bottomSheetView/BottomSheetView.tsx} (64%) create mode 100644 src/components/bottomSheetView/index.ts rename src/components/{view => bottomSheetView}/styles.ts (100%) rename src/components/{view => bottomSheetView}/types.d.ts (90%) delete mode 100644 src/components/view/index.ts create mode 100644 src/hooks/useBottomSheetDynamicSnapPoints.ts diff --git a/example/src/screens/advanced/DynamicSnapPointExample.tsx b/example/src/screens/advanced/DynamicSnapPointExample.tsx index 3104e5e0f..5ded9f776 100644 --- a/example/src/screens/advanced/DynamicSnapPointExample.tsx +++ b/example/src/screens/advanced/DynamicSnapPointExample.tsx @@ -1,21 +1,22 @@ import React, { useCallback, useMemo, useRef, useState } from 'react'; import { View, StyleSheet, Text } from 'react-native'; -import BottomSheet, { BottomSheetView } from '@gorhom/bottom-sheet'; +import BottomSheet, { + BottomSheetView, + useBottomSheetDynamicSnapPoints, +} from '@gorhom/bottom-sheet'; import { useSafeAreaInsets } from 'react-native-safe-area-context'; import Button from '../../components/button'; const DynamicSnapPointExample = () => { // state const [count, setCount] = useState(0); - const [contentHeight, setContentHeight] = useState(0); // hooks const bottomSheetRef = useRef(null); + const { animatedHandleHeight, animatedSnapPoints, contentProps } = + useBottomSheetDynamicSnapPoints(['CONTENT_HEIGHT']); const { bottom: safeBottomArea } = useSafeAreaInsets(); - // variables - const snapPoints = useMemo(() => [0, contentHeight], [contentHeight]); - // callbacks const handleIncreaseContentPress = useCallback(() => { setCount(state => state + 1); @@ -29,22 +30,12 @@ const DynamicSnapPointExample = () => { const handleClosePress = useCallback(() => { bottomSheetRef.current?.close(); }, []); - const handleOnLayout = useCallback( - ({ - nativeEvent: { - layout: { height }, - }, - }) => { - setContentHeight(height); - }, - [] - ); // styles const contentContainerStyle = useMemo( () => ({ ...styles.contentContainerStyle, - paddingBottom: safeBottomArea, + paddingBottom: safeBottomArea || 6, }), [safeBottomArea] ); @@ -63,14 +54,12 @@ const DynamicSnapPointExample = () => {