Skip to content

Commit d782c95

Browse files
committed
handling animated events in sendEvents
1 parent 067becb commit d782c95

File tree

8 files changed

+73
-26
lines changed

8 files changed

+73
-26
lines changed

packages/react-native-gesture-handler/src/RNGestureHandlerModule.web.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export default {
4747
handlerTag: number,
4848
// eslint-disable-next-line @typescript-eslint/no-explicit-any
4949
newView: any,
50-
_actionType: ActionType,
50+
actionType: ActionType,
5151
propsRef: React.RefObject<unknown>
5252
) {
5353
if (!(newView instanceof Element || newView instanceof React.Component)) {
@@ -63,7 +63,7 @@ export default {
6363
}
6464

6565
// @ts-ignore Types should be HTMLElement or React.Component
66-
NodeManager.getHandler(handlerTag).init(newView, propsRef);
66+
NodeManager.getHandler(handlerTag).init(newView, propsRef, actionType);
6767
},
6868
detachGestureHandler(handlerTag: number) {
6969
shouldPreventDrop = false;

packages/react-native-gesture-handler/src/web/handlers/GestureHandler.ts

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/* eslint-disable @typescript-eslint/no-empty-function */
2-
import { createRef } from 'react';
32
import { State } from '../../State';
43
import {
54
Config,
@@ -19,6 +18,7 @@ import IGestureHandler from './IGestureHandler';
1918
import { MouseButton } from '../../handlers/gestureHandlerCommon';
2019
import { PointerType } from '../../PointerType';
2120
import { GestureHandlerDelegate } from '../tools/GestureHandlerDelegate';
21+
import { ActionType } from '../../ActionType';
2222

2323
export default abstract class GestureHandler implements IGestureHandler {
2424
private lastSentState: State | null = null;
@@ -29,8 +29,9 @@ export default abstract class GestureHandler implements IGestureHandler {
2929
protected hasCustomActivationCriteria = false;
3030
private _enabled = false;
3131

32-
private viewRef!: number;
33-
private propsRef!: React.RefObject<unknown>;
32+
private viewRef: number | null = null;
33+
private propsRef: React.RefObject<unknown> | null = null;
34+
private actionType: ActionType | null = null;
3435
private _handlerTag!: number;
3536
private _config: Config = { enabled: false };
3637

@@ -57,19 +58,25 @@ export default abstract class GestureHandler implements IGestureHandler {
5758
// Initializing handler
5859
//
5960

60-
protected init(viewRef: number, propsRef: React.RefObject<unknown>) {
61+
protected init(
62+
viewRef: number,
63+
propsRef: React.RefObject<unknown>,
64+
actionType: ActionType
65+
) {
6166
this.propsRef = propsRef;
6267
this.viewRef = viewRef;
63-
68+
this.actionType = actionType;
6469
this.state = State.UNDETERMINED;
6570

6671
this.delegate.init(viewRef, this);
6772
}
6873

6974
public detachHtml() {
70-
this.propsRef = createRef();
71-
this.viewRef = 0;
75+
this.propsRef = null;
76+
this.viewRef = null;
77+
this.actionType = null;
7278
this.state = State.UNDETERMINED;
79+
7380
this.delegate.detachHtml();
7481
}
7582

@@ -344,18 +351,18 @@ export default abstract class GestureHandler implements IGestureHandler {
344351
}
345352

346353
public sendTouchEvent(event: AdaptedEvent): void {
347-
if (!this.enabled) {
354+
if (!this.enabled || !this.propsRef) {
348355
return;
349356
}
350357

351-
const { onGestureHandlerEvent }: PropsRef = this.propsRef
358+
const { onGestureHandlerTouchEvent }: PropsRef = this.propsRef
352359
.current as PropsRef;
353360

354361
const touchEvent: ResultTouchEvent | undefined =
355362
this.transformTouchEvent(event);
356363

357364
if (touchEvent) {
358-
invokeNullableMethod(onGestureHandlerEvent, touchEvent);
365+
invokeNullableMethod(onGestureHandlerTouchEvent, touchEvent);
359366
}
360367
}
361368

@@ -364,9 +371,14 @@ export default abstract class GestureHandler implements IGestureHandler {
364371
//
365372

366373
public sendEvent = (newState: State, oldState: State): void => {
367-
const { onGestureHandlerEvent, onGestureHandlerStateChange }: PropsRef =
368-
this.propsRef.current as PropsRef;
369-
374+
if (!this.propsRef) {
375+
return;
376+
}
377+
const {
378+
onGestureHandlerEvent,
379+
onGestureHandlerStateChange,
380+
onGestureHandlerAnimatedEvent,
381+
}: PropsRef = this.propsRef.current as PropsRef;
370382
const resultEvent: ResultEvent = this.transformEventData(
371383
newState,
372384
oldState
@@ -383,7 +395,14 @@ export default abstract class GestureHandler implements IGestureHandler {
383395
}
384396
if (this.state === State.ACTIVE) {
385397
resultEvent.nativeEvent.oldState = undefined;
386-
invokeNullableMethod(onGestureHandlerEvent, resultEvent);
398+
if (
399+
this.actionType === ActionType.NATIVE_DETECTOR ||
400+
this.actionType === ActionType.NATIVE_DETECTOR_ANIMATED_EVENT
401+
) {
402+
invokeNullableMethod(onGestureHandlerAnimatedEvent, resultEvent);
403+
} else {
404+
invokeNullableMethod(onGestureHandlerEvent, resultEvent);
405+
}
387406
}
388407
};
389408

@@ -397,7 +416,7 @@ export default abstract class GestureHandler implements IGestureHandler {
397416
),
398417
...this.transformNativeEvent(),
399418
handlerTag: this.handlerTag,
400-
target: this.viewRef,
419+
target: this.viewRef!,
401420
oldState: newState !== oldState ? oldState : undefined,
402421
pointerType: this.pointerType,
403422
},
@@ -512,7 +531,7 @@ export default abstract class GestureHandler implements IGestureHandler {
512531

513532
const trackerData = this.tracker.trackedPointers;
514533

515-
if (trackerData.size === 0) {
534+
if (trackerData.size === 0 || !this.propsRef) {
516535
return;
517536
}
518537

packages/react-native-gesture-handler/src/web/handlers/LongPressGestureHandler.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { ActionType } from '../../ActionType';
12
import { State } from '../../State';
23
import { AdaptedEvent, Config } from '../interfaces';
34

@@ -21,12 +22,16 @@ export default class LongPressGestureHandler extends GestureHandler {
2122

2223
private activationTimeout: number | undefined;
2324

24-
public init(ref: number, propsRef: React.RefObject<unknown>) {
25+
public init(
26+
ref: number,
27+
propsRef: React.RefObject<unknown>,
28+
actionType: ActionType
29+
) {
2530
if (this.config.enableContextMenu === undefined) {
2631
this.config.enableContextMenu = false;
2732
}
2833

29-
super.init(ref, propsRef);
34+
super.init(ref, propsRef, actionType);
3035
}
3136

3237
protected transformNativeEvent() {

packages/react-native-gesture-handler/src/web/handlers/NativeViewGestureHandler.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { DEFAULT_TOUCH_SLOP } from '../constants';
44
import { AdaptedEvent, Config } from '../interfaces';
55

66
import GestureHandler from './GestureHandler';
7+
import { ActionType } from '../../ActionType';
78
export default class NativeViewGestureHandler extends GestureHandler {
89
private buttonRole!: boolean;
910

@@ -16,8 +17,12 @@ export default class NativeViewGestureHandler extends GestureHandler {
1617
private startY = 0;
1718
private minDistSq = DEFAULT_TOUCH_SLOP * DEFAULT_TOUCH_SLOP;
1819

19-
public init(ref: number, propsRef: React.RefObject<unknown>): void {
20-
super.init(ref, propsRef);
20+
public init(
21+
ref: number,
22+
propsRef: React.RefObject<unknown>,
23+
actionType: ActionType
24+
): void {
25+
super.init(ref, propsRef, actionType);
2126

2227
this.shouldCancelWhenOutside = true;
2328

packages/react-native-gesture-handler/src/web/handlers/PinchGestureHandler.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import GestureHandler from './GestureHandler';
66
import ScaleGestureDetector, {
77
ScaleGestureListener,
88
} from '../detectors/ScaleGestureDetector';
9+
import { ActionType } from '../../ActionType';
910

1011
export default class PinchGestureHandler extends GestureHandler {
1112
private scale = 1;
@@ -48,8 +49,12 @@ export default class PinchGestureHandler extends GestureHandler {
4849
this.scaleDetectorListener
4950
);
5051

51-
public init(ref: number, propsRef: React.RefObject<unknown>) {
52-
super.init(ref, propsRef);
52+
public init(
53+
ref: number,
54+
propsRef: React.RefObject<unknown>,
55+
actionType: ActionType
56+
) {
57+
super.init(ref, propsRef, actionType);
5358

5459
this.shouldCancelWhenOutside = false;
5560
}

packages/react-native-gesture-handler/src/web/handlers/RotationGestureHandler.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import GestureHandler from './GestureHandler';
55
import RotationGestureDetector, {
66
RotationGestureListener,
77
} from '../detectors/RotationGestureDetector';
8+
import { ActionType } from '../../ActionType';
89

910
const ROTATION_RECOGNITION_THRESHOLD = Math.PI / 36;
1011

@@ -44,8 +45,12 @@ export default class RotationGestureHandler extends GestureHandler {
4445
private rotationGestureDetector: RotationGestureDetector =
4546
new RotationGestureDetector(this.rotationGestureListener);
4647

47-
public init(ref: number, propsRef: React.RefObject<unknown>): void {
48-
super.init(ref, propsRef);
48+
public init(
49+
ref: number,
50+
propsRef: React.RefObject<unknown>,
51+
actionType: ActionType
52+
): void {
53+
super.init(ref, propsRef, actionType);
4954

5055
this.shouldCancelWhenOutside = false;
5156
}

packages/react-native-gesture-handler/src/web/interfaces.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ export interface ResultTouchEvent
130130

131131
export interface PropsRef {
132132
onGestureHandlerEvent: () => void;
133+
onGestureHandlerAnimatedEvent: () => void;
133134
onGestureHandlerStateChange: () => void;
135+
onGestureHandlerTouchEvent: () => void;
134136
}
135137

136138
export interface StylusData {

packages/react-native-gesture-handler/src/web/tools/GestureHandlerWebDelegate.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ export class GestureHandlerWebDelegate
6565

6666
detachHtml(): void {
6767
this.view = null as unknown as HTMLElement;
68+
this.defaultViewStyles = {
69+
userSelect: '',
70+
touchAction: '',
71+
};
72+
73+
this.eventManagers = [];
6874
}
6975

7076
isPointerInBounds({ x, y }: { x: number; y: number }): boolean {

0 commit comments

Comments
 (0)