Skip to content

Commit e9baaaa

Browse files
committed
Delay Pressable press-in inside ScrollView
# Conflicts: # packages/react-native/src/private/featureflags/ReactNativeFeatureFlags.js
1 parent e1be1bf commit e9baaaa

4 files changed

Lines changed: 131 additions & 4 deletions

File tree

‎packages/react-native/Libraries/Components/Pressable/Pressable.js‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,21 @@ import type {
1717
} from '../../Types/CoreEventTypes';
1818
import type {ViewProps} from '../View/ViewPropTypes';
1919

20+
import * as ReactNativeFeatureFlags from '../../../src/private/featureflags/ReactNativeFeatureFlags';
2021
import {PressabilityDebugView} from '../../Pressability/PressabilityDebug';
2122
import usePressability from '../../Pressability/usePressability';
2223
import {type RectOrSize} from '../../StyleSheet/Rect';
2324
import useMergeRefs from '../../Utilities/useMergeRefs';
25+
import ScrollViewContext from '../ScrollView/ScrollViewContext';
2426
import View from '../View/View';
2527
import useAndroidRippleForView, {
2628
type PressableAndroidRippleConfig,
2729
} from './useAndroidRippleForView';
2830
import * as React from 'react';
29-
import {memo, useMemo, useRef, useState} from 'react';
31+
import {memo, useContext, useMemo, useRef, useState} from 'react';
32+
33+
// Gives the parent scroll view time to take the touch before feedback shows.
34+
const SCROLL_VIEW_PRESS_DELAY_MS = 130;
3035

3136
export type PressableInstance = HostInstance;
3237

@@ -245,6 +250,15 @@ function Pressable({
245250

246251
const [pressed, setPressed] = usePressState(testOnly_pressed === true);
247252

253+
const isInScrollView = useContext(ScrollViewContext) != null;
254+
255+
const delayPressIn =
256+
unstable_pressDelay == null &&
257+
isInScrollView &&
258+
ReactNativeFeatureFlags.delayPressInInScrollView()
259+
? SCROLL_VIEW_PRESS_DELAY_MS
260+
: unstable_pressDelay;
261+
248262
const shouldUpdatePressed =
249263
typeof children === 'function' || typeof style === 'function';
250264

@@ -294,7 +308,7 @@ function Pressable({
294308
delayHoverIn,
295309
delayHoverOut,
296310
delayLongPress,
297-
delayPressIn: unstable_pressDelay,
311+
delayPressIn,
298312
onBlur,
299313
onFocus,
300314
onHoverIn,
@@ -335,6 +349,7 @@ function Pressable({
335349
delayHoverIn,
336350
delayHoverOut,
337351
delayLongPress,
352+
delayPressIn,
338353
disabled,
339354
hitSlop,
340355
onBlur,
@@ -349,7 +364,6 @@ function Pressable({
349364
pressRetentionOffset,
350365
setPressed,
351366
shouldUpdatePressed,
352-
unstable_pressDelay,
353367
],
354368
);
355369

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict-local
8+
* @format
9+
* @oncall react_native
10+
* @fantom_flags delayPressInInScrollView:true
11+
*/
12+
13+
import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment';
14+
15+
import type {HostInstance} from 'react-native';
16+
17+
import * as Fantom from '@react-native/fantom';
18+
import nullthrows from 'nullthrows';
19+
import * as React from 'react';
20+
import {createRef} from 'react';
21+
import {Pressable, ScrollView} from 'react-native';
22+
23+
function render(inScrollView: boolean, onPressIn: () => void): HostInstance {
24+
const ref = createRef<HostInstance>();
25+
const pressable = (
26+
<Pressable ref={ref} onPressIn={onPressIn} style={{height: 100}} />
27+
);
28+
const root = Fantom.createRoot();
29+
Fantom.runTask(() => {
30+
root.render(
31+
inScrollView ? <ScrollView>{pressable}</ScrollView> : pressable,
32+
);
33+
});
34+
return nullthrows(ref.current);
35+
}
36+
37+
function touch(
38+
element: HostInstance,
39+
type: 'onTouchStart' | 'onTouchEnd' | 'onTouchCancel',
40+
): void {
41+
const point = {identifier: 0, pageX: 5, pageY: 5, timestamp: 0};
42+
Fantom.dispatchNativeEvent(
43+
element,
44+
type,
45+
{touches: type === 'onTouchStart' ? [point] : [], changedTouches: [point]},
46+
{category: Fantom.NativeEventCategory.Discrete},
47+
);
48+
}
49+
50+
describe('<Pressable> in <ScrollView>', () => {
51+
let timers: Fantom.TimerMock;
52+
53+
beforeEach(() => {
54+
timers = Fantom.installTimerMock();
55+
});
56+
57+
afterEach(() => {
58+
timers.runAllTimers();
59+
timers.uninstall();
60+
});
61+
62+
it('delays onPressIn', () => {
63+
const onPressIn = jest.fn();
64+
const element = render(true, onPressIn);
65+
66+
touch(element, 'onTouchStart');
67+
timers.advanceTimersByTime(129);
68+
expect(onPressIn).toHaveBeenCalledTimes(0);
69+
70+
timers.advanceTimersByTime(1);
71+
expect(onPressIn).toHaveBeenCalledTimes(1);
72+
73+
touch(element, 'onTouchEnd');
74+
});
75+
76+
it('does not call onPressIn when the touch is canceled before the delay', () => {
77+
const onPressIn = jest.fn();
78+
const element = render(true, onPressIn);
79+
80+
touch(element, 'onTouchStart');
81+
touch(element, 'onTouchCancel');
82+
timers.runAllTimers();
83+
84+
expect(onPressIn).toHaveBeenCalledTimes(0);
85+
});
86+
87+
it('does not delay onPressIn outside of a scroll view', () => {
88+
const onPressIn = jest.fn();
89+
const element = render(false, onPressIn);
90+
91+
touch(element, 'onTouchStart');
92+
expect(onPressIn).toHaveBeenCalledTimes(1);
93+
94+
touch(element, 'onTouchEnd');
95+
});
96+
});

‎packages/react-native/scripts/featureflags/ReactNativeFeatureFlags.config.js‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,6 +1215,17 @@ const definitions: FeatureFlagDefinitions = {
12151215
},
12161216
ossReleaseStage: 'none',
12171217
},
1218+
delayPressInInScrollView: {
1219+
defaultValue: false,
1220+
metadata: {
1221+
dateAdded: '2026-11-27',
1222+
description:
1223+
'Delay `onPressIn` of `Pressable` placed inside a `ScrollView`, so press feedback does not show when the touch turns into a scroll.',
1224+
expectedReleaseValue: true,
1225+
purpose: 'experimentation',
1226+
},
1227+
ossReleaseStage: 'none',
1228+
},
12181229
externalElementInspectionEnabled: {
12191230
defaultValue: true,
12201231
metadata: {

‎packages/react-native/src/private/featureflags/ReactNativeFeatureFlags.js‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<96fba2f12c24ed676fa6511735fc8cce>>
7+
* @generated SignedSource<<b14918cdfbbb46686ebeb5ae2b58ad48>>
88
* @flow strict
99
* @noformat
1010
*/
@@ -34,6 +34,7 @@ export type ReactNativeFeatureFlagsJsOnly = Readonly<{
3434
animatedKeepListenersOnDetach: Getter<boolean>,
3535
animatedShouldSyncValueBeforeStartCallback: Getter<boolean>,
3636
deferFlatListFocusChangeRenderUpdate: Getter<boolean>,
37+
delayPressInInScrollView: Getter<boolean>,
3738
externalElementInspectionEnabled: Getter<boolean>,
3839
fixCrossOrientationNestedListViewability: Getter<boolean>,
3940
fixVirtualizeListCollapseWindowSize: Getter<boolean>,
@@ -183,6 +184,11 @@ export const animatedShouldSyncValueBeforeStartCallback: Getter<boolean> = creat
183184
*/
184185
export const deferFlatListFocusChangeRenderUpdate: Getter<boolean> = createJavaScriptFlagGetter('deferFlatListFocusChangeRenderUpdate', false);
185186

187+
/**
188+
* Delay `onPressIn` of `Pressable` placed inside a `ScrollView`, so press feedback does not show when the touch turns into a scroll.
189+
*/
190+
export const delayPressInInScrollView: Getter<boolean> = createJavaScriptFlagGetter('delayPressInInScrollView', false);
191+
186192
/**
187193
* Enable the external inspection API for DevTools to communicate with the Inspector overlay.
188194
*/

0 commit comments

Comments
 (0)