From 89098e9c430917ec0930f6de64b9cb18663242ab Mon Sep 17 00:00:00 2001 From: Mo Gorhom Date: Fri, 7 May 2021 20:10:13 +0100 Subject: [PATCH] refactor: converted all internal state/memoized variables to reanimated shared values. (#430) * refactor: converted all internal variables into shared values * chore: updated dev screen --- example/index.ts | 4 + example/src/Dev.tsx | 75 +- src/components/bottomSheet/BottomSheet.tsx | 766 +++++++++--------- src/components/bottomSheet/constants.ts | 20 +- src/components/bottomSheet/types.d.ts | 93 ++- .../BottomSheetContainer.tsx | 70 +- .../bottomSheetContainer/types.d.ts | 7 +- .../BottomSheetHandleContainer.tsx | 54 +- .../bottomSheetHandleContainer/types.d.ts | 6 +- .../BottomSheetModalProvider.tsx | 23 +- src/contexts/internal.ts | 1 - src/contexts/modal/internal.ts | 3 +- src/hooks/useNormalizedSnapPoints.ts | 56 +- src/hooks/useReactiveSharedValue.ts | 26 +- src/index.ts | 3 + src/types.d.ts | 4 + src/utilities/index.ts | 3 +- src/utilities/logger.ts | 42 + src/utilities/normalizeSnapPoint.ts | 20 + src/utilities/normalizeSnapPoints.ts | 16 - 20 files changed, 743 insertions(+), 549 deletions(-) create mode 100644 src/utilities/logger.ts create mode 100644 src/utilities/normalizeSnapPoint.ts delete mode 100644 src/utilities/normalizeSnapPoints.ts diff --git a/example/index.ts b/example/index.ts index d3c277a28..fc6f4f5e8 100644 --- a/example/index.ts +++ b/example/index.ts @@ -3,6 +3,10 @@ import 'react-native-gesture-handler'; import { enableScreens } from 'react-native-screens'; enableScreens(true); +// @ts-ignore +import { enableLogging } from '@gorhom/bottom-sheet'; +enableLogging(); + import { AppRegistry, LogBox } from 'react-native'; import App from './src/App'; import { name as appName } from './app.json'; diff --git a/example/src/Dev.tsx b/example/src/Dev.tsx index 635b18760..84c10f684 100644 --- a/example/src/Dev.tsx +++ b/example/src/Dev.tsx @@ -4,28 +4,34 @@ import React, { useCallback, useMemo, useRef, useState } from 'react'; import { View, StyleSheet, Dimensions, StatusBar } from 'react-native'; import { createStackNavigator } from '@react-navigation/stack'; import { useAnimatedStyle, useSharedValue } from 'react-native-reanimated'; -import { useSafeAreaInsets } from 'react-native-safe-area-context'; +import { + SafeAreaProvider, + useSafeAreaInsets, +} from 'react-native-safe-area-context'; import BottomSheet from '@gorhom/bottom-sheet'; import SearchHandle from './components/searchHandle'; import Button from './components/button'; import ContactList from './components/contactList'; -import { NavigationContainer } from '@react-navigation/native'; +import { NavigationContainer, useNavigation } from '@react-navigation/native'; const { height: windowHeight } = Dimensions.get('window'); const BasicExample = () => { //#region state + const shownHeader = useRef(true); const [dynamicSnapPoint, setDynamicSnapPoint] = useState(300); //#endregion //#region hooks + const { setOptions } = useNavigation(); const bottomSheetRef = useRef(null); const { top: topSafeArea, bottom: bottomSafeArea } = useSafeAreaInsets(); //#endregion //#region variables - const snapPoints = useMemo(() => [150, dynamicSnapPoint], [dynamicSnapPoint]); + const snapPoints = useMemo(() => [150, 400, '100%'], []); const animatedPosition = useSharedValue(0); + const animatedContainerHeight = useSharedValue(800); //#endregion //#region styles @@ -83,12 +89,21 @@ const BasicExample = () => { const handleSnapPress = useCallback(index => { bottomSheetRef.current?.snapTo(index); }, []); + const handleSnapPosition = useCallback(position => { + bottomSheetRef.current?.snapTo(position); + }, []); const handleClosePress = useCallback(() => { bottomSheetRef.current?.close(); }, []); const handleIncreaseDynamicSnapPoint = useCallback(() => { setDynamicSnapPoint(state => state + 50); }, []); + const handleHideHeaderPress = useCallback(() => { + shownHeader.current = !shownHeader.current; + setOptions({ + headerShown: shownHeader.current, + }); + }, []); //#endregion // renders @@ -98,36 +113,45 @@ const BasicExample = () => { label="Increase Dynamic Snap Point" style={styles.buttonContainer} onPress={handleIncreaseDynamicSnapPoint} - /> + /> */}