Do not call onPress on buttons while dismissing keyboard#4281
Conversation
There was a problem hiding this comment.
Pull request overview
This PR aims to align RNGH v3 Pressable/Touchable behavior with React Native when used inside a ScrollView with keyboardShouldPersistTaps="never", swallowing the tap that dismisses the soft keyboard so it doesn’t also trigger onPress-family callbacks.
Changes:
- Added shared keyboard-visibility tracking in
ScrollViewResponderInterceptorand exposedisKeyboardDismissingTap(...)for components to consult. - Updated v3
PressableandTouchableto detect “keyboard dismiss” taps and suppressonPress*callbacks for that interaction. - Adjusted v3 API tests and expanded the example app’s
keyboardShouldPersistTapsdemo to includeTouchable.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| packages/react-native-gesture-handler/src/v3/components/Touchable/Touchable.tsx | Detects keyboard-dismiss taps via JS responder context and suppresses press callbacks for that gesture. |
| packages/react-native-gesture-handler/src/v3/components/ScrollViewResponderInterceptor.tsx | Adds reference-counted keyboard visibility listeners and exposes isKeyboardDismissingTap based on keyboardShouldPersistTaps and keyboard visibility. |
| packages/react-native-gesture-handler/src/v3/components/Pressable.tsx | Suppresses v3 press handling when the initiating touch is used to dismiss the keyboard. |
| packages/react-native-gesture-handler/src/tests/api_v3.test.tsx | Updates responder-handling assertions to reflect the responder marker flow after Pressable changes. |
| apps/common-app/src/new_api/tests/keyboardShouldPersistTaps/index.tsx | Adds a Touchable variant to the interactive keyboardShouldPersistTaps example. |
Comments suppressed due to low confidence (1)
packages/react-native-gesture-handler/src/v3/components/Touchable/Touchable.tsx:196
- When the initial touch is identified as the keyboard-dismissing tap (dropKeyboardTapRef.current === true), onUpdate can still call onPressIn/onPressOut as the finger moves because it doesn't check dropKeyboardTapRef. This means a swallowed tap can still trigger press-in/out side effects, which contradicts the goal of fully swallowing the keyboard-dismiss interaction.
const onUpdate = useCallback(
(e: CallbackEventType) => {
if (pointerState.current === PointerState.UNKNOWN) {
return;
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| } | ||
|
|
||
| const setVisible = () => { | ||
| isSoftKeyboardVisible = true; |
There was a problem hiding this comment.
Why is that called Soft keyboard?
There was a problem hiding this comment.
Software 😉 But I agree that it is not obvious, removed.
| Keyboard.addListener('keyboardDidShow', setVisible), | ||
| Keyboard.addListener('keyboardWillShow', setVisible), |
There was a problem hiding this comment.
There are also other things that are considered by RN to determine if the touch should be dropped:
There was a problem hiding this comment.
Let my agent speak 😆:
Good call, dug into RN's _handleStartShouldSetResponderCapture.
Took the parts that apply:
Detached keyboard — only treat it as open when endCoordinates.height > 0 (height-0/floating → let the tap through), like _softKeyboardIsDetached.
Seed from Keyboard.metrics() on mount, for a keyboard that's already open.
isKeyboardVisible rides the same keyboardDidShow/Hide events RN uses for _keyboardMetrics, so it's basically softKeyboardMayBeOpen.
Skipped the rest on purpose:
currentlyFocusedInput() — can't use it at gesture time; the ScrollView blurs the input synchronously when it grabs the responder, so it's already null by the time our recognizer fires (that race is why we track visibility instead).
target !isTextInput — moot, a Pressable/Touchable is never a TextInput.
_keyboardEventsAreUnreliable() (Android <30) — RN only uses it alongside hasFocusedTextInput; alone it'd drop taps with the keyboard closed.
Description
Currently, when buttons are inside
ScrolViewwithkeyboardShouldPersistTaps={never}they still callonPresseven though press was meant to dismiss keyboard.This PR fixes that behavior. It attaches listeners for keyboard state and based on that it blocks
onPress*callbacks on JS side.Fixes #992
Test plan
Tested on Keyboard Should Persist Taps example