Skip to content

feat: add enabled prop to draggable view #2160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions example/src/screens/advanced/DraggableViewExample.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import React, { useCallback, useMemo, useRef } from 'react';
import { View, StyleSheet, Text } from 'react-native';
import BottomSheet, { BottomSheetDraggableView, BottomSheetView } from '@gorhom/bottom-sheet';
import { Button } from '../../components/button';

const DraggableViewExample = () => {
// hooks
const bottomSheetRef = useRef<BottomSheet>(null);

// variables
const snapPoints = useMemo(() => ['25%', '50%'], []);

// callbacks
const handleExpandPress = useCallback(() => {
bottomSheetRef.current?.expand();
}, []);
const handleCollapsePress = useCallback(() => {
bottomSheetRef.current?.collapse();
}, []);
const handleClosePress = useCallback(() => {
bottomSheetRef.current?.close();
}, []);

// renders
return (
<View style={styles.container}>
<Button label="Expand" onPress={handleExpandPress} />
<Button label="Collapse" onPress={handleCollapsePress} />
<Button label="Close" onPress={handleClosePress} />
<BottomSheet
ref={bottomSheetRef}
snapPoints={snapPoints}
keyboardBehavior="interactive"
keyboardBlurBehavior="restore"
enablePanDownToClose={true}
enableContentPanningGesture={false}
>
<BottomSheetView style={styles.row}>
<BottomSheetDraggableView style={styles.leftView}>
Copy link

@IslamRustamov IslamRustamov Mar 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you miss enabled prop here, no?

Edit: it's set as default as true, okie

<Text>Draggable</Text>
</BottomSheetDraggableView>
<View style={styles.rightView}>
<Text>Not draggable</Text>
</View>
</BottomSheetView>
</BottomSheet>
</View>
);
};

const styles = StyleSheet.create({
container: {
flex: 1,
padding: 24,
},
row: {
flexDirection: 'row',
height: '100%',
},
leftView: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#ccc',
},
rightView: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#ddd',
},
});

export default DraggableViewExample;
5 changes: 5 additions & 0 deletions example/src/screens/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ const advancedSection = {
slug: 'Advanced/FooterExample',
getScreen: () => require('./advanced/FooterExample').default,
},
{
name: 'DraggableView',
slug: 'Advanced/DraggableViewExample',
getScreen: () => require('./advanced/DraggableViewExample').default,
},
],
};
if (Platform.OS !== 'web') {
Expand Down
8 changes: 3 additions & 5 deletions src/components/bottomSheet/BottomSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1866,9 +1866,6 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
//#endregion

// render
const DraggableView = enableContentPanningGesture
? BottomSheetDraggableView
: Animated.View;
return (
<BottomSheetProvider value={externalContextVariables}>
<BottomSheetInternalProvider value={internalContextVariables}>
Expand Down Expand Up @@ -1906,12 +1903,13 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
accessibilityRole={_providedAccessibilityRole ?? undefined}
accessibilityLabel={_providedAccessibilityLabel ?? undefined}
>
<DraggableView
<BottomSheetDraggableView
key="BottomSheetRootDraggableView"
style={contentContainerStyle}
enabled={enableContentPanningGesture}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i dont think we need to pass it as props, as it is already read internally from useBottomSheetInternal

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is necessary so that BottomSheetDraggableView can also be used when enableContentPanningGesture of the BottomSheet is set to false, see PR description.

I've added an example DraggableViewExample which showcases the usage.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mmm i see, i think for scalability , i would move the enableContentPanningGesture to each scrollable view instead. but we could go ahead with this temporary solution

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, in my case I need to wrap an external component so the solution of this MR would work better for me...

>
{children}
</DraggableView>
</BottomSheetDraggableView>
{footerComponent && (
<BottomSheetFooterContainer
footerComponent={footerComponent}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ const BottomSheetDraggableViewComponent = ({
refreshControlGestureRef,
style,
children,
enabled = true,
...rest
}: BottomSheetDraggableViewProps) => {
//#region hooks
const {
enableContentPanningGesture,
simultaneousHandlers: _providedSimultaneousHandlers,
waitFor,
activeOffsetX,
Expand Down Expand Up @@ -56,7 +56,7 @@ const BottomSheetDraggableViewComponent = ({
]);
const draggableGesture = useMemo(() => {
let gesture = Gesture.Pan()
.enabled(enableContentPanningGesture)
.enabled(enabled)
.shouldCancelWhenOutside(false)
.runOnJS(false)
.onStart(contentPanGestureHandler.handleOnStart)
Expand Down Expand Up @@ -94,7 +94,7 @@ const BottomSheetDraggableViewComponent = ({
}, [
activeOffsetX,
activeOffsetY,
enableContentPanningGesture,
enabled,
failOffsetX,
failOffsetY,
simultaneousHandlers,
Expand Down
1 change: 1 addition & 0 deletions src/components/bottomSheetDraggableView/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export type BottomSheetDraggableViewProps = RNViewProps & {
nativeGestureRef?: Exclude<GestureRef, number>;
refreshControlGestureRef?: Exclude<GestureRef, number>;
children: ReactNode[] | ReactNode;
enabled?: boolean
};