Skip to content
Merged
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
20 changes: 17 additions & 3 deletions packages/react-native/Libraries/Components/Pressable/Pressable.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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';

Expand Down Expand Up @@ -294,7 +308,7 @@ function Pressable({
delayHoverIn,
delayHoverOut,
delayLongPress,
delayPressIn: unstable_pressDelay,
delayPressIn,
onBlur,
onFocus,
onHoverIn,
Expand Down Expand Up @@ -335,6 +349,7 @@ function Pressable({
delayHoverIn,
delayHoverOut,
delayLongPress,
delayPressIn,
disabled,
hitSlop,
onBlur,
Expand All @@ -349,7 +364,6 @@ function Pressable({
pressRetentionOffset,
setPressed,
shouldUpdatePressed,
unstable_pressDelay,
],
);

Expand Down
Original file line number Diff line number Diff line change
@@ -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<HostInstance>();
const pressable = (
<Pressable ref={ref} onPressIn={onPressIn} style={{height: 100}} />
);
const root = Fantom.createRoot();
Fantom.runTask(() => {
root.render(
inScrollView ? <ScrollView>{pressable}</ScrollView> : 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('<Pressable> in <ScrollView>', () => {
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');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<<b14918cdfbbb46686ebeb5ae2b58ad48>>
* @flow strict
* @noformat
*/
Expand Down Expand Up @@ -34,6 +34,7 @@ export type ReactNativeFeatureFlagsJsOnly = Readonly<{
animatedKeepListenersOnDetach: Getter<boolean>,
animatedShouldSyncValueBeforeStartCallback: Getter<boolean>,
deferFlatListFocusChangeRenderUpdate: Getter<boolean>,
delayPressInInScrollView: Getter<boolean>,
externalElementInspectionEnabled: Getter<boolean>,
fixCrossOrientationNestedListViewability: Getter<boolean>,
fixVirtualizeListCollapseWindowSize: Getter<boolean>,
Expand Down Expand Up @@ -183,6 +184,11 @@ export const animatedShouldSyncValueBeforeStartCallback: Getter<boolean> = creat
*/
export const deferFlatListFocusChangeRenderUpdate: Getter<boolean> = 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<boolean> = createJavaScriptFlagGetter('delayPressInInScrollView', false);

/**
* Enable the external inspection API for DevTools to communicate with the Inspector overlay.
*/
Expand Down
Loading