Skip to content

Commit 47656bf

Browse files
author
Nicolas Gallagher
authored
[Flare] Remove longpress from press responder (#16242)
Long press will move to a separate responder.
1 parent 9914a19 commit 47656bf

3 files changed

Lines changed: 24 additions & 467 deletions

File tree

packages/react-events/docs/Press.md

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
The `Press` module responds to press events on the element it wraps. Press
44
events are dispatched for `mouse`, `pen`, `touch`, `trackpad`, and `keyboard`
55
pointer types. Press events are only dispatched for keyboards when pressing the
6-
Enter or Spacebar keys. If neither `onPress` nor `onLongPress` are called, this
7-
signifies that the press ended outside of the element hit bounds (i.e., the user
8-
aborted the press).
6+
Enter or Spacebar keys. If `onPress` is not called, this signifies that the
7+
press ended outside of the element hit bounds (i.e., the user aborted the
8+
press).
99

1010
Press events do not propagate between `Press` event responders.
1111

@@ -17,7 +17,6 @@ const Button = (props) => (
1717
<Press
1818
onPress={props.onPress}
1919
onPressChange={setPressed}
20-
onLongPress={props.onLongPress}
2120
>
2221
<div
2322
{...props}
@@ -60,8 +59,6 @@ type PressEvent = {
6059
| 'pressend'
6160
| 'presschange'
6261
| 'pressmove'
63-
| 'longpress'
64-
| 'longpresschange'
6562
| 'contextmenu',
6663
x: number,
6764
y: number
@@ -77,10 +74,6 @@ type PressOffset = {
7774

7875
## Props
7976

80-
### delayLongPress: number = 500ms
81-
82-
The duration of a press before `onLongPress` and `onLongPressChange` are called.
83-
8477
### delayPressEnd: number
8578

8679
The duration of the delay between when the press ends and when `onPressEnd` is
@@ -101,27 +94,10 @@ Disables all `Press` events.
10194
Called when the context menu is shown. When a press is active, the context menu
10295
will only be shown (and the press cancelled) if `preventDefault` is `false`.
10396

104-
### onLongPress: (e: PressEvent) => void
105-
106-
Called once the element has been pressed for the length of `delayLongPress`. If
107-
the press point moves more than 10px `onLongPress` is cancelled.
108-
109-
### onLongPressChange: boolean => void
110-
111-
Called when the element changes long-press state.
112-
113-
### longPressShouldCancelPress: () => boolean
114-
115-
Determines whether calling `onPress` should be cancelled if `onLongPress` or
116-
`onLongPressChange` have already been called. Default is `false`.
117-
11897
### onPress: (e: PressEvent) => void
11998

120-
Called immediately after a press is released, unless either 1) the press is
121-
released outside the hit bounds of the element (accounting for
122-
`pressRetentionOffset`), or 2) the press was a long press,
123-
and `onLongPress` or `onLongPressChange` props are provided, and
124-
`onLongPressCancelsPress()` is `true`.
99+
Called immediately after a press is released, unless the press is released
100+
outside the hit bounds of the element (accounting for `pressRetentionOffset`.
125101

126102
### onPressChange: boolean => void
127103

@@ -164,7 +140,6 @@ is still called.
164140

165141
Whether to `preventDefault()` native events. Native behavior is prevented by
166142
default. If an anchor is the child of `Press`, internal and external navigation
167-
should be performed in `onPress`/`onLongPress`. To rely on native behavior
168-
instead, set `preventDefault` to `false`, but be aware that native behavior will
169-
take place immediately after interaction without respect for delays or long
170-
press.
143+
should be performed in `onPress`. To rely on native behavior instead, set
144+
`preventDefault` to `false`, but be aware that native behavior will take place
145+
immediately after interaction without respect for delays or long press.

packages/react-events/src/dom/Press.js

Lines changed: 8 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ import {DiscreteEvent, UserBlockingEvent} from 'shared/ReactTypes';
1919

2020
type PressListenerProps = {|
2121
onContextMenu: (e: PressEvent) => void,
22-
onLongPress: (e: PressEvent) => void,
23-
onLongPressChange: boolean => void,
2422
onPress: (e: PressEvent) => void,
2523
onPressChange: boolean => void,
2624
onPressEnd: (e: PressEvent) => void,
@@ -30,7 +28,6 @@ type PressListenerProps = {|
3028

3129
type PressProps = {|
3230
disabled: boolean,
33-
delayLongPress: number,
3431
delayPressEnd: number,
3532
delayPressStart: number,
3633
pressRetentionOffset: {
@@ -42,8 +39,6 @@ type PressProps = {|
4239
preventContextMenu: boolean,
4340
preventDefault: boolean,
4441
stopPropagation: boolean,
45-
enableLongPress: boolean,
46-
longPressShouldCancelPress: () => boolean,
4742
|};
4843

4944
type PressState = {
@@ -54,10 +49,8 @@ type PressState = {
5449
addedRootEvents: boolean,
5550
isActivePressed: boolean,
5651
isActivePressStart: boolean,
57-
isLongPressed: boolean,
5852
isPressed: boolean,
5953
isPressWithinResponderRegion: boolean,
60-
longPressTimeout: null | number,
6154
pointerType: PointerType,
6255
pressTarget: null | Element | Document,
6356
pressEndTimeout: null | number,
@@ -86,8 +79,6 @@ type PressEventType =
8679
| 'pressstart'
8780
| 'pressend'
8881
| 'presschange'
89-
| 'longpress'
90-
| 'longpresschange'
9182
| 'contextmenu';
9283

9384
type PressEvent = {|
@@ -117,7 +108,6 @@ const isMac =
117108
: false;
118109
const DEFAULT_PRESS_END_DELAY_MS = 0;
119110
const DEFAULT_PRESS_START_DELAY_MS = 0;
120-
const DEFAULT_LONG_PRESS_DELAY_MS = 500;
121111
const DEFAULT_PRESS_RETENTION_OFFSET = {
122112
bottom: 20,
123113
top: 20,
@@ -250,14 +240,6 @@ function dispatchPressChangeEvent(
250240
context.dispatchEvent('onPressChange', bool, DiscreteEvent);
251241
}
252242

253-
function dispatchLongPressChangeEvent(
254-
context: ReactDOMResponderContext,
255-
state: PressState,
256-
): void {
257-
const bool = state.isLongPressed;
258-
context.dispatchEvent('onLongPressChange', bool, DiscreteEvent);
259-
}
260-
261243
function activate(event: ReactDOMResponderEvent, context, props, state) {
262244
const nativeEvent: any = event.nativeEvent;
263245
const {clientX: x, clientY: y} = state.touchEvent || nativeEvent;
@@ -281,15 +263,10 @@ function activate(event: ReactDOMResponderEvent, context, props, state) {
281263
}
282264

283265
function deactivate(event: ?ReactDOMResponderEvent, context, props, state) {
284-
const wasLongPressed = state.isLongPressed;
285266
state.isActivePressed = false;
286-
state.isLongPressed = false;
287267

288268
dispatchEvent('onPressEnd', event, context, state, 'pressend', DiscreteEvent);
289269
dispatchPressChangeEvent(context, state);
290-
if (wasLongPressed && props.enableLongPress) {
291-
dispatchLongPressChangeEvent(context, state);
292-
}
293270
}
294271

295272
function dispatchPressStartEvents(
@@ -308,27 +285,6 @@ function dispatchPressStartEvents(
308285
const dispatch = () => {
309286
state.isActivePressStart = true;
310287
activate(event, context, props, state);
311-
312-
if (!state.isLongPressed && props.enableLongPress) {
313-
const delayLongPress = calculateDelayMS(
314-
props.delayLongPress,
315-
10,
316-
DEFAULT_LONG_PRESS_DELAY_MS,
317-
);
318-
state.longPressTimeout = context.setTimeout(() => {
319-
state.isLongPressed = true;
320-
state.longPressTimeout = null;
321-
dispatchEvent(
322-
'onLongPress',
323-
event,
324-
context,
325-
state,
326-
'longpress',
327-
DiscreteEvent,
328-
);
329-
dispatchLongPressChangeEvent(context, state);
330-
}, delayLongPress);
331-
}
332288
};
333289

334290
if (!state.isActivePressStart) {
@@ -360,11 +316,6 @@ function dispatchPressEndEvents(
360316
state.isActivePressStart = false;
361317
state.isPressed = false;
362318

363-
if (state.longPressTimeout !== null) {
364-
context.clearTimeout(state.longPressTimeout);
365-
state.longPressTimeout = null;
366-
}
367-
368319
if (!wasActivePressStart && state.pressStartTimeout !== null) {
369320
context.clearTimeout(state.pressStartTimeout);
370321
state.pressStartTimeout = null;
@@ -596,10 +547,8 @@ const pressResponderImpl = {
596547
addedRootEvents: false,
597548
isActivePressed: false,
598549
isActivePressStart: false,
599-
isLongPressed: false,
600550
isPressed: false,
601551
isPressWithinResponderRegion: true,
602-
longPressTimeout: null,
603552
pointerType: '',
604553
pressEndTimeout: null,
605554
pressStartTimeout: null,
@@ -823,19 +772,6 @@ const pressResponderImpl = {
823772
'pressmove',
824773
UserBlockingEvent,
825774
);
826-
if (
827-
state.activationPosition != null &&
828-
state.longPressTimeout != null
829-
) {
830-
const deltaX = state.activationPosition.x - nativeEvent.clientX;
831-
const deltaY = state.activationPosition.y - nativeEvent.clientY;
832-
if (
833-
Math.hypot(deltaX, deltaY) > 10 &&
834-
state.longPressTimeout != null
835-
) {
836-
context.clearTimeout(state.longPressTimeout);
837-
}
838-
}
839775
} else {
840776
dispatchPressStartEvents(event, context, props, state);
841777
}
@@ -900,7 +836,6 @@ const pressResponderImpl = {
900836
}
901837
}
902838

903-
const wasLongPressed = state.isLongPressed;
904839
const pressTarget = state.pressTarget;
905840
dispatchPressEndEvents(event, context, props, state);
906841

@@ -928,23 +863,14 @@ const pressResponderImpl = {
928863
}
929864
}
930865
if (state.isPressWithinResponderRegion && button !== 1) {
931-
if (
932-
!(
933-
wasLongPressed &&
934-
props.enableLongPress &&
935-
props.longPressShouldCancelPress &&
936-
props.longPressShouldCancelPress()
937-
)
938-
) {
939-
dispatchEvent(
940-
'onPress',
941-
event,
942-
context,
943-
state,
944-
'press',
945-
DiscreteEvent,
946-
);
947-
}
866+
dispatchEvent(
867+
'onPress',
868+
event,
869+
context,
870+
state,
871+
'press',
872+
DiscreteEvent,
873+
);
948874
}
949875
}
950876
state.touchEvent = null;

0 commit comments

Comments
 (0)