react-native-bottom-sheet: [v4] BottomSheetBackdrop does not close BottomSheet onTouch

Bug

BottomSheetBackdrop does not close BottomSheet onTouch even with pressBehavior=“close” props. I tried several methods but I can’t get an onPress event working on the BottomSheetBackdrop component either.

Environment info

Library Version
@gorhom/bottom-sheet 4
react-native 0.69.6
react-native-reanimated ~2.9.1
react-native-gesture-handler ~2.5.0

Steps To Reproduce

See code below.

Describe what you expected to happen:

  1. Clicking on the Backdrop closes the BottomSheet

Reproducible sample code

const tailwind = useTailwind()

const bottomSheetRef = useRef<BottomSheet>(null);
const snapPoints = useMemo(() => ['15%', '30%'], []);

const handleSheetChanges = useCallback((index: number) => {
	console.log('handleSheetChanges', index);
}, []);

useEffect(() => {
	if (open) {
		bottomSheetRef.current?.expand();
	}
	else {
		bottomSheetRef.current?.close();
	}
}, [open])

const renderBackdrop = useCallback(
	props => (
		<BottomSheetBackdrop
			{...props}
			pressBehavior='close'
		/>
	),
	[]
);

console.log({ MyBottomSheet: { open } })
return (
	<BottomSheet
		ref={bottomSheetRef}
		index={-1}
		snapPoints={snapPoints}
		onChange={handleSheetChanges}
		enablePanDownToClose
		backdropComponent={renderBackdrop}
		backgroundComponent={({ style }) => <View style={[style, tailwind('bg-slate-800')]} />}
		enableOverDrag
		enableHandlePanningGesture
	>
		<View />
	</BottomSheet>
)

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 6
  • Comments: 16

Most upvoted comments

This works for me:

 const renderBackdrop = useCallback(
    (props: BottomSheetBackdropProps) => (
      <BottomSheetBackdrop
        {...props}
        style={[props.style]}
        disappearsOnIndex={-1}
        appearsOnIndex={0}
        opacity={0.5}
        pressBehavior="close"
   
      />
    ),
    []
  );

@yaaamin it still doesn’t seem to work even with GestureHandlerRootView at the root in App.js

Did you try <GestureHandlerRootView style={{ flex: 1 }}>{/* content */}</ GestureHandlerRootView> ?

@yaaamin it still doesn’t seem to work even with GestureHandlerRootView at the root in App.js

For anyone who is still looking for fixes for this issue, just wrap your App.js component with the following

export default function App() { return <GestureHandlerRootView>{/* content */}</GestureHandlerRootView>; }

_extracted from https://docs.swmansion.com/react-native-gesture-handler/docs/1.10.3/_

This works for me:

 const renderBackdrop = useCallback(
    (props: BottomSheetBackdropProps) => (
      <BottomSheetBackdrop
        {...props}
        style={[props.style]}
        disappearsOnIndex={-1}
        appearsOnIndex={0}
        opacity={0.5}
        pressBehavior="close"
   
      />
    ),
    []
  );

These should be the default values. I imagine at least 90% of people adding a backdrop just want this native-like behaviour.

Solved with update MainActivity.java with RNGestureHandlerEnabledRootView and wrapped JS code with view < GestureHandlerRootView>

package com.swmansion.gesturehandler.react.example;

import com.facebook.react.ReactActivity;
import com.swmansion.gesturehandler.react.RNGestureHandlerEnabledRootView;
public class MainActivity extends ReactActivity {

   @Override
   protected ReactActivityDelegate createReactActivityDelegate() {
     return new ReactActivityDelegate(this, getMainComponentName()) {
       @Override
       protected ReactRootView createRootView() {
        return new RNGestureHandlerEnabledRootView(MainActivity.this);
       }
     };
   }
}

See reference https://docs.swmansion.com/react-native-gesture-handler/docs/guides/migrating-off-rnghenabledroot

Is there anyway we can overcome this issue for people using Expo Managed Workflow?