Skip to content

Commit 1c31aca

Browse files
committed
feat: added enableBlurKeyboardOnGesture prop to handle blurring keyboard on gesture
1 parent 0850cb8 commit 1c31aca

File tree

8 files changed

+47
-6
lines changed

8 files changed

+47
-6
lines changed

example/src/screens/advanced/KeyboardHandlingExample.tsx

+14-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import React, { useCallback, useMemo, useRef, useState } from 'react';
2-
import { View, StyleSheet } from 'react-native';
31
import BottomSheet from '@gorhom/bottom-sheet';
2+
import React, { useCallback, useMemo, useRef, useState } from 'react';
3+
import { StyleSheet, View } from 'react-native';
4+
import { Button } from '../../components/button';
5+
import { ContactList } from '../../components/contactList';
46
import {
5-
SearchHandle,
67
SEARCH_HANDLE_HEIGHT,
8+
SearchHandle,
79
} from '../../components/searchHandle';
8-
import { Button } from '../../components/button';
9-
import { ContactList } from '../../components/contactList';
1010

1111
const KeyboardHandlingExample = () => {
1212
// state
@@ -16,6 +16,7 @@ const KeyboardHandlingExample = () => {
1616
const [keyboardBlurBehavior, setKeyboardBlurBehavior] = useState<
1717
'none' | 'restore'
1818
>('none');
19+
const [blurKeyboardOnGesture, setBlurKeyboardOnGesture] = useState(false);
1920

2021
// hooks
2122
const bottomSheetRef = useRef<BottomSheet>(null);
@@ -46,6 +47,9 @@ const KeyboardHandlingExample = () => {
4647
}
4748
});
4849
}, []);
50+
const handleToggleBlurKeyboardOnGesture = useCallback(() => {
51+
setBlurKeyboardOnGesture(state => !state);
52+
}, []);
4953
const handleExpandPress = useCallback(() => {
5054
bottomSheetRef.current?.expand();
5155
}, []);
@@ -67,6 +71,10 @@ const KeyboardHandlingExample = () => {
6771
label={`Toggle Keyboard Blur Behavior: ${keyboardBlurBehavior}`}
6872
onPress={handleToggleKeyboardBlurBehavior}
6973
/>
74+
<Button
75+
label={`Toggle Blur Keyboard On Gesture: ${blurKeyboardOnGesture}`}
76+
onPress={handleToggleBlurKeyboardOnGesture}
77+
/>
7078
<Button label="Expand" onPress={handleExpandPress} />
7179
<Button label="Collapse" onPress={handleCollapsePress} />
7280
<Button label="Close" onPress={handleClosePress} />
@@ -76,6 +84,7 @@ const KeyboardHandlingExample = () => {
7684
enableDynamicSizing={false}
7785
keyboardBehavior={keyboardBehavior}
7886
keyboardBlurBehavior={keyboardBlurBehavior}
87+
enableBlurKeyboardOnGesture={blurKeyboardOnGesture}
7988
handleComponent={SearchHandle}
8089
>
8190
<ContactList count={12} type="FlatList" />

src/components/bottomSheet/BottomSheet.tsx

+4
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ import {
6969
DEFAULT_ACCESSIBLE,
7070
DEFAULT_ANIMATE_ON_MOUNT,
7171
DEFAULT_DYNAMIC_SIZING,
72+
DEFAULT_ENABLE_BLUR_KEYBOARD_ON_GESTURE,
7273
DEFAULT_ENABLE_CONTENT_PANNING_GESTURE,
7374
DEFAULT_ENABLE_OVER_DRAG,
7475
DEFAULT_ENABLE_PAN_DOWN_TO_CLOSE,
@@ -125,6 +126,7 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
125126
keyboardBehavior = DEFAULT_KEYBOARD_BEHAVIOR,
126127
keyboardBlurBehavior = DEFAULT_KEYBOARD_BLUR_BEHAVIOR,
127128
android_keyboardInputMode = DEFAULT_KEYBOARD_INPUT_MODE,
129+
enableBlurKeyboardOnGesture = DEFAULT_ENABLE_BLUR_KEYBOARD_ON_GESTURE,
128130

129131
// layout
130132
containerHeight: _providedContainerHeight,
@@ -1393,6 +1395,7 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
13931395
activeOffsetY: _providedActiveOffsetY,
13941396
failOffsetX: _providedFailOffsetX,
13951397
failOffsetY: _providedFailOffsetY,
1398+
enableBlurKeyboardOnGesture,
13961399
animateToPosition,
13971400
stopAnimation,
13981401
setScrollableRef,
@@ -1428,6 +1431,7 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
14281431
enableOverDrag,
14291432
enablePanDownToClose,
14301433
enableDynamicSizing,
1434+
enableBlurKeyboardOnGesture,
14311435
_providedSimultaneousHandlers,
14321436
_providedWaitFor,
14331437
_providedActiveOffsetX,

src/components/bottomSheet/constants.ts

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const DEFAULT_DYNAMIC_SIZING = true;
1919
const DEFAULT_KEYBOARD_BEHAVIOR = KEYBOARD_BEHAVIOR.interactive;
2020
const DEFAULT_KEYBOARD_BLUR_BEHAVIOR = KEYBOARD_BLUR_BEHAVIOR.none;
2121
const DEFAULT_KEYBOARD_INPUT_MODE = KEYBOARD_INPUT_MODE.adjustPan;
22+
const DEFAULT_ENABLE_BLUR_KEYBOARD_ON_GESTURE = false;
2223

2324
// initial values
2425
const INITIAL_VALUE = Number.NEGATIVE_INFINITY;
@@ -51,6 +52,7 @@ export {
5152
DEFAULT_KEYBOARD_BEHAVIOR,
5253
DEFAULT_KEYBOARD_BLUR_BEHAVIOR,
5354
DEFAULT_KEYBOARD_INPUT_MODE,
55+
DEFAULT_ENABLE_BLUR_KEYBOARD_ON_GESTURE,
5456
// layout
5557
INITIAL_POSITION,
5658
INITIAL_CONTAINER_HEIGHT,

src/components/bottomSheet/types.d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,11 @@ export interface BottomSheetProps
161161
* - `restore`: restore sheet position.
162162
*/
163163
keyboardBlurBehavior?: keyof typeof KEYBOARD_BLUR_BEHAVIOR;
164+
/**
165+
* Enable blurring the keyboard when user start to drag the bottom sheet.
166+
* @default false
167+
*/
168+
enableBlurKeyboardOnGesture?: boolean;
164169
/**
165170
* Defines keyboard input mode for Android only.
166171
* @link {https://developer.android.com/guide/topics/manifest/activity-element#wsoft}

src/contexts/internal.ts

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export interface BottomSheetInternalContextType
2424
| 'enableOverDrag'
2525
| 'enablePanDownToClose'
2626
| 'enableDynamicSizing'
27+
| 'enableBlurKeyboardOnGesture'
2728
| 'overDragResistanceFactor'
2829
>
2930
> {

src/hooks/useGestureEventsHandlersDefault.tsx

+12
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export const useGestureEventsHandlersDefault: GestureEventsHandlersHookType =
5959
overDragResistanceFactor,
6060
isInTemporaryPosition,
6161
isScrollableRefreshable,
62+
enableBlurKeyboardOnGesture,
6263
animateToPosition,
6364
stopAnimation,
6465
} = useBottomSheetInternal();
@@ -74,6 +75,16 @@ export const useGestureEventsHandlersDefault: GestureEventsHandlersHookType =
7475
// cancel current animation
7576
stopAnimation();
7677

78+
let initialKeyboardState = animatedKeyboardState.value;
79+
// blur the keyboard when user start dragging the bottom sheet
80+
if (
81+
enableBlurKeyboardOnGesture &&
82+
initialKeyboardState === KEYBOARD_STATE.SHOWN
83+
) {
84+
initialKeyboardState = KEYBOARD_STATE.HIDDEN;
85+
runOnJS(dismissKeyboard)();
86+
}
87+
7788
// store current animated position
7889
context.value = {
7990
...context.value,
@@ -94,6 +105,7 @@ export const useGestureEventsHandlersDefault: GestureEventsHandlersHookType =
94105
},
95106
[
96107
stopAnimation,
108+
enableBlurKeyboardOnGesture,
97109
animatedPosition,
98110
animatedKeyboardState,
99111
animatedScrollableContentOffsetY,

website/docs/guides/keyboard-handling.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Keyboard handling is one of the main feature of `BottomSheet v4`, thanks to the
1111

1212
To handle the keyboard appearance, I have simplified the approach by creating a pre-integrated `TextInput` called [BottomSheetTextInput](./components/bottomsheettextinput), which communicate internally to react to the keyboard appearance.
1313

14-
Also I have introduce two props to allow users to customize the handling, [keyboardBehavior](./props#keyboardbehavior), [keyboardBlurBehavior](./props#keyboardblurbehavior) and [android_keyboardInputMode](./props#android_keyboardinputmode) that is only for `Android`.
14+
Also I have introduce two props to allow users to customize the handling, [keyboardBehavior](./props#keyboardbehavior), [keyboardBlurBehavior](./props#keyboardblurbehavior), [enableBlurKeyboardOnGesture](./props#enableblurkeyboardongesture) and [android_keyboardInputMode](./props#android_keyboardinputmode) that is only for `Android`.
1515

1616
:::tip
1717
To use custom `TextInput`, you will need to copy the `handleOnFocus` and `handleOnBlur` from [BottomSheetTextInput](https://github.com/gorhom/react-native-bottom-sheet/blob/master/src/components/bottomSheetTextInput/BottomSheetTextInput.tsx) into your own component.

website/docs/props.md

+8
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,14 @@ Defines the keyboard blur behavior.
238238
| ------------------- | ------- | -------- |
239239
| 'none' \| 'restore' | 'none' | NO |
240240

241+
### enableBlurKeyboardOnGesture
242+
243+
Enable blurring the keyboard when user start to drag the bottom sheet.
244+
245+
| type | default | required |
246+
| ------- | ------- | -------- |
247+
| boolean | false | NO |
248+
241249
### android_keyboardInputMode
242250

243251
Defines keyboard input mode for `Android` only, [learn more](https://developer.android.com/guide/topics/manifest/activity-element#wsoft).

0 commit comments

Comments
 (0)