From 1575217a0fda032baf8294786cf52e370735ecac Mon Sep 17 00:00:00 2001 From: Mo Gorhom Date: Sun, 20 Dec 2020 15:07:34 +0100 Subject: [PATCH 1/2] chore: added gesture control props --- src/components/bottomSheet/BottomSheet.tsx | 7 +++++++ src/components/bottomSheet/constants.ts | 4 ++++ src/components/bottomSheet/types.d.ts | 19 ++++++++++++++++--- .../BottomSheetDraggableView.tsx | 2 ++ .../BottomSheetHandleContainer.tsx | 2 ++ .../bottomSheetHandleContainer/types.d.ts | 2 +- src/components/flatList/FlatList.tsx | 6 +++++- src/components/scrollView/ScrollView.tsx | 6 +++++- src/components/sectionList/SectionList.tsx | 6 +++++- src/contexts/internal.ts | 1 + 10 files changed, 48 insertions(+), 7 deletions(-) diff --git a/src/components/bottomSheet/BottomSheet.tsx b/src/components/bottomSheet/BottomSheet.tsx index 36987a825..c23e4ab36 100644 --- a/src/components/bottomSheet/BottomSheet.tsx +++ b/src/components/bottomSheet/BottomSheet.tsx @@ -48,6 +48,8 @@ import { DEFAULT_HANDLE_HEIGHT, DEFAULT_ANIMATE_ON_MOUNT, DECELERATION_RATE, + DEFAULT_ENABLE_CONTENT_PANNING_GESTURE, + DEFAULT_ENABLE_HANDLE_PANNING_GESTURE, } from './constants'; import type { ScrollableRef, BottomSheetMethods } from '../../types'; import type { BottomSheetProps } from './types'; @@ -74,6 +76,8 @@ const BottomSheetComponent = forwardRef( index: _providedIndex = 0, snapPoints: _providedSnapPoints, animateOnMount = DEFAULT_ANIMATE_ON_MOUNT, + enableContentPanningGesture = DEFAULT_ENABLE_CONTENT_PANNING_GESTURE, + enableHandlePanningGesture = DEFAULT_ENABLE_HANDLE_PANNING_GESTURE, // layout handleHeight: _providedHandleHeight, containerHeight: _providedContainerHeight, @@ -341,6 +345,7 @@ const BottomSheetComponent = forwardRef( //#region contexts variables const internalContextVariables = useMemo( () => ({ + enableContentPanningGesture, snapPointsCount: snapPoints.length, animatedIndex, animatedPosition, @@ -362,6 +367,7 @@ const BottomSheetComponent = forwardRef( removeScrollableRef, scrollableContentOffsetY, scrollableDecelerationRate, + enableContentPanningGesture, ] ); const externalContextVariables = useMemo( @@ -504,6 +510,7 @@ const BottomSheetComponent = forwardRef( simultaneousHandlers={contentWrapperGestureRef} shouldMeasureHeight={shouldMeasureHandleHeight} snapPoints={snapPoints} + enableHandlePanningGesture={enableHandlePanningGesture} animateToPoint={animateToPoint} handleComponent={handleComponent} onMeasureHeight={handleOnHandleMeasureHeight} diff --git a/src/components/bottomSheet/constants.ts b/src/components/bottomSheet/constants.ts index 3824f6140..9772441d8 100644 --- a/src/components/bottomSheet/constants.ts +++ b/src/components/bottomSheet/constants.ts @@ -6,6 +6,8 @@ const DEFAULT_ANIMATION_EASING: Animated.EasingFunction = Easing.out( ); const DEFAULT_ANIMATION_DURATION = 500; const DEFAULT_HANDLE_HEIGHT = 24; +const DEFAULT_ENABLE_CONTENT_PANNING_GESTURE = true; +const DEFAULT_ENABLE_HANDLE_PANNING_GESTURE = true; const DEFAULT_ANIMATE_ON_MOUNT = false; const DECELERATION_RATE = Platform.select({ @@ -19,5 +21,7 @@ export { DEFAULT_ANIMATION_DURATION, DEFAULT_HANDLE_HEIGHT, DEFAULT_ANIMATE_ON_MOUNT, + DEFAULT_ENABLE_CONTENT_PANNING_GESTURE, + DEFAULT_ENABLE_HANDLE_PANNING_GESTURE, DECELERATION_RATE, }; diff --git a/src/components/bottomSheet/types.d.ts b/src/components/bottomSheet/types.d.ts index 13cf35241..db330ecf9 100644 --- a/src/components/bottomSheet/types.d.ts +++ b/src/components/bottomSheet/types.d.ts @@ -37,19 +37,32 @@ export interface BottomSheetProps extends BottomSheetAnimationConfigs { containerHeight?: number; /** * Top inset value helps to calculate percentage snap points values, - * usually comes from `@react-navigation/stack` hook `useHeaderHeight` or from `react-native-safe-area-context` hook `useSafeArea`. + * usually comes from `@react-navigation/stack` hook `useHeaderHeight` or + * from `react-native-safe-area-context` hook `useSafeArea`. * @type number * @default 0 */ topInset?: number; - - // animated nodes + /** + * Enable content panning gesture interaction. + * @type boolean + * @default true + */ + enableContentPanningGesture?: boolean; + /** + * Enable handle panning gesture interaction. + * @type boolean + * @default true + */ + enableHandlePanningGesture?: boolean; /** * To start the sheet closed and snap to initial index when it's mounted. * @type boolean * @default false */ animateOnMount?: boolean; + + // animated nodes /** * Animated value to be used as a callback of the position node internally. * @type Animated.Value diff --git a/src/components/bottomSheetDraggableView/BottomSheetDraggableView.tsx b/src/components/bottomSheetDraggableView/BottomSheetDraggableView.tsx index 2610dfec2..e99b9e769 100644 --- a/src/components/bottomSheetDraggableView/BottomSheetDraggableView.tsx +++ b/src/components/bottomSheetDraggableView/BottomSheetDraggableView.tsx @@ -17,6 +17,7 @@ const BottomSheetDraggableViewComponent = ({ // hooks const { + enableContentPanningGesture, contentWrapperGestureRef, contentPanGestureHandler, } = useBottomSheetInternal(); @@ -39,6 +40,7 @@ const BottomSheetDraggableViewComponent = ({ return ( , - Pick, + Pick, BottomSheetHandleProps { shouldMeasureHeight: boolean; snapPoints: Array; diff --git a/src/components/flatList/FlatList.tsx b/src/components/flatList/FlatList.tsx index 6218e2e44..0857a39d9 100644 --- a/src/components/flatList/FlatList.tsx +++ b/src/components/flatList/FlatList.tsx @@ -46,7 +46,10 @@ const BottomSheetFlatListComponent = forwardRef( handleScrollEvent, handleSettingScrollable, } = useScrollableInternal(BottomSheetFlatListName); - const { contentWrapperGestureRef } = useBottomSheetInternal(); + const { + contentWrapperGestureRef, + enableContentPanningGesture, + } = useBottomSheetInternal(); // effects // @ts-ignore @@ -61,6 +64,7 @@ const BottomSheetFlatListComponent = forwardRef( > ; animatedIndex: Animated.SharedValue; From 6761d29787c2a14e480f7568ddcdc98332eac867 Mon Sep 17 00:00:00 2001 From: Mo Gorhom Date: Sun, 20 Dec 2020 15:08:00 +0100 Subject: [PATCH 2/2] chore: updated examples --- example/src/screens/BasicExamples.tsx | 54 +++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 4 deletions(-) diff --git a/example/src/screens/BasicExamples.tsx b/example/src/screens/BasicExamples.tsx index ffdfb5aa6..7e030e46a 100644 --- a/example/src/screens/BasicExamples.tsx +++ b/example/src/screens/BasicExamples.tsx @@ -1,4 +1,4 @@ -import React, { useCallback, memo, useRef, useMemo } from 'react'; +import React, { useCallback, memo, useRef, useMemo, useState } from 'react'; import { StyleSheet, View } from 'react-native'; import BottomSheet from '@gorhom/bottom-sheet'; import ContactList from '../components/contactList'; @@ -12,13 +12,40 @@ interface ExampleScreenProps { const createExampleScreen = ({ type, count = 25 }: ExampleScreenProps) => memo(() => { - // hooks + //#region state + const [ + enableContentPanningGesture, + setEnableContentPanningGesture, + ] = useState(true); + const [ + enableHandlePanningGesture, + setEnableHandlePanningGesture, + ] = useState(true); + //#endregion + + //#region refs const bottomSheetRef = useRef(null); + //#endregion - // variables + //#region variables const snapPoints = useMemo(() => ['25%', '50%', '90%'], []); + const enableContentPanningGestureButtonText = useMemo( + () => + enableContentPanningGesture + ? 'Disable Content Panning Gesture' + : 'Enable Content Panning Gesture', + [enableContentPanningGesture] + ); + const enableHandlePanningGestureButtonText = useMemo( + () => + enableHandlePanningGesture + ? 'Disable Handle Panning Gesture' + : 'Enable Handle Panning Gesture', + [enableHandlePanningGesture] + ); + //#endregion - // callbacks + //#region callbacks const handleSheetChange = useCallback(index => { console.log('handleSheetChange', index); }, []); @@ -34,6 +61,13 @@ const createExampleScreen = ({ type, count = 25 }: ExampleScreenProps) => const handleClosePress = useCallback(() => { bottomSheetRef.current?.close(); }, []); + const handleEnableContentPanningGesturePress = useCallback(() => { + setEnableContentPanningGesture(state => !state); + }, []); + const handleEnableHandlePanningGesturePress = useCallback(() => { + setEnableHandlePanningGesture(state => !state); + }, []); + //#endregion return ( @@ -67,11 +101,23 @@ const createExampleScreen = ({ type, count = 25 }: ExampleScreenProps) => style={styles.buttonContainer} onPress={() => handleClosePress()} /> +