From e9baaaa172ddb222ec9f74c24dd594474bdbd781 Mon Sep 17 00:00:00 2001 From: Mathieu Acthernoene Date: Fri, 27 Nov 2026 16:42:43 +0100 Subject: [PATCH] Delay Pressable press-in inside ScrollView # Conflicts: # packages/react-native/src/private/featureflags/ReactNativeFeatureFlags.js --- .../Components/Pressable/Pressable.js | 20 +++- .../__tests__/Pressable-scrollView-itest.js | 96 +++++++++++++++++++ .../ReactNativeFeatureFlags.config.js | 11 +++ .../featureflags/ReactNativeFeatureFlags.js | 8 +- 4 files changed, 131 insertions(+), 4 deletions(-) create mode 100644 packages/react-native/Libraries/Components/Pressable/__tests__/Pressable-scrollView-itest.js diff --git a/packages/react-native/Libraries/Components/Pressable/Pressable.js b/packages/react-native/Libraries/Components/Pressable/Pressable.js index bced2044a881..ceb0f4963266 100644 --- a/packages/react-native/Libraries/Components/Pressable/Pressable.js +++ b/packages/react-native/Libraries/Components/Pressable/Pressable.js @@ -17,16 +17,21 @@ import type { } from '../../Types/CoreEventTypes'; import type {ViewProps} from '../View/ViewPropTypes'; +import * as ReactNativeFeatureFlags from '../../../src/private/featureflags/ReactNativeFeatureFlags'; import {PressabilityDebugView} from '../../Pressability/PressabilityDebug'; import usePressability from '../../Pressability/usePressability'; import {type RectOrSize} from '../../StyleSheet/Rect'; import useMergeRefs from '../../Utilities/useMergeRefs'; +import ScrollViewContext from '../ScrollView/ScrollViewContext'; import View from '../View/View'; import useAndroidRippleForView, { type PressableAndroidRippleConfig, } from './useAndroidRippleForView'; import * as React from 'react'; -import {memo, useMemo, useRef, useState} from 'react'; +import {memo, useContext, useMemo, useRef, useState} from 'react'; + +// Gives the parent scroll view time to take the touch before feedback shows. +const SCROLL_VIEW_PRESS_DELAY_MS = 130; export type PressableInstance = HostInstance; @@ -245,6 +250,15 @@ function Pressable({ const [pressed, setPressed] = usePressState(testOnly_pressed === true); + const isInScrollView = useContext(ScrollViewContext) != null; + + const delayPressIn = + unstable_pressDelay == null && + isInScrollView && + ReactNativeFeatureFlags.delayPressInInScrollView() + ? SCROLL_VIEW_PRESS_DELAY_MS + : unstable_pressDelay; + const shouldUpdatePressed = typeof children === 'function' || typeof style === 'function'; @@ -294,7 +308,7 @@ function Pressable({ delayHoverIn, delayHoverOut, delayLongPress, - delayPressIn: unstable_pressDelay, + delayPressIn, onBlur, onFocus, onHoverIn, @@ -335,6 +349,7 @@ function Pressable({ delayHoverIn, delayHoverOut, delayLongPress, + delayPressIn, disabled, hitSlop, onBlur, @@ -349,7 +364,6 @@ function Pressable({ pressRetentionOffset, setPressed, shouldUpdatePressed, - unstable_pressDelay, ], ); diff --git a/packages/react-native/Libraries/Components/Pressable/__tests__/Pressable-scrollView-itest.js b/packages/react-native/Libraries/Components/Pressable/__tests__/Pressable-scrollView-itest.js new file mode 100644 index 000000000000..569abc3fe15e --- /dev/null +++ b/packages/react-native/Libraries/Components/Pressable/__tests__/Pressable-scrollView-itest.js @@ -0,0 +1,96 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @flow strict-local + * @format + * @oncall react_native + * @fantom_flags delayPressInInScrollView:true + */ + +import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment'; + +import type {HostInstance} from 'react-native'; + +import * as Fantom from '@react-native/fantom'; +import nullthrows from 'nullthrows'; +import * as React from 'react'; +import {createRef} from 'react'; +import {Pressable, ScrollView} from 'react-native'; + +function render(inScrollView: boolean, onPressIn: () => void): HostInstance { + const ref = createRef(); + const pressable = ( + + ); + const root = Fantom.createRoot(); + Fantom.runTask(() => { + root.render( + inScrollView ? {pressable} : pressable, + ); + }); + return nullthrows(ref.current); +} + +function touch( + element: HostInstance, + type: 'onTouchStart' | 'onTouchEnd' | 'onTouchCancel', +): void { + const point = {identifier: 0, pageX: 5, pageY: 5, timestamp: 0}; + Fantom.dispatchNativeEvent( + element, + type, + {touches: type === 'onTouchStart' ? [point] : [], changedTouches: [point]}, + {category: Fantom.NativeEventCategory.Discrete}, + ); +} + +describe(' in ', () => { + let timers: Fantom.TimerMock; + + beforeEach(() => { + timers = Fantom.installTimerMock(); + }); + + afterEach(() => { + timers.runAllTimers(); + timers.uninstall(); + }); + + it('delays onPressIn', () => { + const onPressIn = jest.fn(); + const element = render(true, onPressIn); + + touch(element, 'onTouchStart'); + timers.advanceTimersByTime(129); + expect(onPressIn).toHaveBeenCalledTimes(0); + + timers.advanceTimersByTime(1); + expect(onPressIn).toHaveBeenCalledTimes(1); + + touch(element, 'onTouchEnd'); + }); + + it('does not call onPressIn when the touch is canceled before the delay', () => { + const onPressIn = jest.fn(); + const element = render(true, onPressIn); + + touch(element, 'onTouchStart'); + touch(element, 'onTouchCancel'); + timers.runAllTimers(); + + expect(onPressIn).toHaveBeenCalledTimes(0); + }); + + it('does not delay onPressIn outside of a scroll view', () => { + const onPressIn = jest.fn(); + const element = render(false, onPressIn); + + touch(element, 'onTouchStart'); + expect(onPressIn).toHaveBeenCalledTimes(1); + + touch(element, 'onTouchEnd'); + }); +}); diff --git a/packages/react-native/scripts/featureflags/ReactNativeFeatureFlags.config.js b/packages/react-native/scripts/featureflags/ReactNativeFeatureFlags.config.js index 1aadb56c7ecd..f5a64f8e509f 100644 --- a/packages/react-native/scripts/featureflags/ReactNativeFeatureFlags.config.js +++ b/packages/react-native/scripts/featureflags/ReactNativeFeatureFlags.config.js @@ -1215,6 +1215,17 @@ const definitions: FeatureFlagDefinitions = { }, ossReleaseStage: 'none', }, + delayPressInInScrollView: { + defaultValue: false, + metadata: { + dateAdded: '2026-11-27', + description: + 'Delay `onPressIn` of `Pressable` placed inside a `ScrollView`, so press feedback does not show when the touch turns into a scroll.', + expectedReleaseValue: true, + purpose: 'experimentation', + }, + ossReleaseStage: 'none', + }, externalElementInspectionEnabled: { defaultValue: true, metadata: { diff --git a/packages/react-native/src/private/featureflags/ReactNativeFeatureFlags.js b/packages/react-native/src/private/featureflags/ReactNativeFeatureFlags.js index 7ff078b47241..f6b8c08419be 100644 --- a/packages/react-native/src/private/featureflags/ReactNativeFeatureFlags.js +++ b/packages/react-native/src/private/featureflags/ReactNativeFeatureFlags.js @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @generated SignedSource<<96fba2f12c24ed676fa6511735fc8cce>> + * @generated SignedSource<> * @flow strict * @noformat */ @@ -34,6 +34,7 @@ export type ReactNativeFeatureFlagsJsOnly = Readonly<{ animatedKeepListenersOnDetach: Getter, animatedShouldSyncValueBeforeStartCallback: Getter, deferFlatListFocusChangeRenderUpdate: Getter, + delayPressInInScrollView: Getter, externalElementInspectionEnabled: Getter, fixCrossOrientationNestedListViewability: Getter, fixVirtualizeListCollapseWindowSize: Getter, @@ -183,6 +184,11 @@ export const animatedShouldSyncValueBeforeStartCallback: Getter = creat */ export const deferFlatListFocusChangeRenderUpdate: Getter = createJavaScriptFlagGetter('deferFlatListFocusChangeRenderUpdate', false); +/** + * Delay `onPressIn` of `Pressable` placed inside a `ScrollView`, so press feedback does not show when the touch turns into a scroll. + */ +export const delayPressInInScrollView: Getter = createJavaScriptFlagGetter('delayPressInInScrollView', false); + /** * Enable the external inspection API for DevTools to communicate with the Inspector overlay. */