Skip to content

Commit eeddd24

Browse files
committed
detaching handlers
1 parent 4aaf700 commit eeddd24

File tree

6 files changed

+33
-33
lines changed

6 files changed

+33
-33
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ export default {
6565
// @ts-ignore Types should be HTMLElement or React.Component
6666
NodeManager.getHandler(handlerTag).init(newView, propsRef);
6767
},
68+
detachGestureHandler(handlerTag: number) {
69+
shouldPreventDrop = false;
70+
NodeManager.getHandler(handlerTag).detachHtml();
71+
},
6872
updateGestureHandler(handlerTag: number, newConfig: Config) {
6973
NodeManager.getHandler(handlerTag).updateGestureConfig(newConfig);
7074

@@ -80,7 +84,7 @@ export default {
8084
if (shouldPreventDrop) {
8185
return;
8286
}
83-
87+
console.log();
8488
NodeManager.dropGestureHandler(handlerTag);
8589
},
8690
// eslint-disable-next-line @typescript-eslint/no-empty-function

packages/react-native-gesture-handler/src/v3/HostGestureDetector.web.tsx

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,21 @@
11
import { useEffect, useRef } from 'react';
2-
import {
3-
GestureHandlerStateChangeEvent,
4-
GestureHandlerTouchEvent,
5-
GestureHandlerEvent,
6-
} from '../web/interfaces';
72
import { View } from 'react-native';
83
import RNGestureHandlerModuleWeb from '../RNGestureHandlerModule.web';
94
import { ActionType } from '../ActionType';
105
export interface GestureHandlerDetectorProps {
11-
onGestureHandlerEvent?: (e: GestureHandlerEvent) => void;
12-
onGestureHandlerAnimatedEvent?: (e: GestureHandlerEvent) => void;
13-
onGestureHandlerStateChange?: (e: GestureHandlerStateChangeEvent) => void;
14-
onGestureHandlerTouchEvent?: (e: GestureHandlerTouchEvent) => void;
15-
166
handlerTags: number[];
177
dispatchesAnimatedEvents: boolean;
188
moduleId: number;
199
children?: React.ReactNode;
2010
}
2111

2212
const HostGestureDetector = (props: GestureHandlerDetectorProps) => {
23-
const {
24-
onGestureHandlerEvent,
25-
onGestureHandlerAnimatedEvent,
26-
onGestureHandlerStateChange,
27-
onGestureHandlerTouchEvent,
28-
handlerTags,
29-
dispatchesAnimatedEvents,
30-
moduleId,
31-
children,
32-
} = props;
13+
const { handlerTags, children } = props;
3314

3415
const viewRef = useRef(null);
3516
const propsRef = useRef<GestureHandlerDetectorProps>(props);
3617
const oldHandlerTags = useRef<Set<number>>(new Set<number>());
3718

38-
useEffect(() => {
39-
attachHandlers();
40-
}, [
41-
onGestureHandlerEvent,
42-
onGestureHandlerAnimatedEvent,
43-
onGestureHandlerStateChange,
44-
onGestureHandlerTouchEvent,
45-
handlerTags,
46-
dispatchesAnimatedEvents,
47-
moduleId,
48-
]);
49-
5019
const attachHandlers = (): void => {
5120
const curHandlerTags = new Set(handlerTags);
5221
const newHandlerTags: Set<number> = curHandlerTags.difference(
@@ -62,6 +31,19 @@ const HostGestureDetector = (props: GestureHandlerDetectorProps) => {
6231
});
6332
};
6433

34+
const detachHandlers = (): void => {
35+
handlerTags.forEach((tag) => {
36+
RNGestureHandlerModuleWeb.detachGestureHandler(tag);
37+
});
38+
};
39+
40+
useEffect(() => {
41+
attachHandlers();
42+
return () => {
43+
detachHandlers();
44+
};
45+
}, [attachHandlers, detachHandlers]);
46+
6547
return <View ref={viewRef}>{children}</View>;
6648
};
6749

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* eslint-disable @typescript-eslint/no-empty-function */
2+
import { createRef } from 'react';
23
import { State } from '../../State';
34
import {
45
Config,
@@ -65,6 +66,13 @@ export default abstract class GestureHandler implements IGestureHandler {
6566
this.delegate.init(viewRef, this);
6667
}
6768

69+
public detachHtml() {
70+
this.propsRef = createRef();
71+
this.viewRef = 0;
72+
this.state = State.UNDETERMINED;
73+
this.delegate.detachHtml();
74+
}
75+
6876
public attachEventManager(manager: EventManager<unknown>): void {
6977
manager.setOnPointerDown(this.onPointerDown.bind(this));
7078
manager.setOnPointerAdd(this.onPointerAdd.bind(this));

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export default interface IGestureHandler {
3535
cancel: () => void;
3636

3737
reset: () => void;
38+
detachHtml: () => void;
3839

3940
shouldWaitForHandlerFailure: (handler: IGestureHandler) => boolean;
4041
shouldRequireToWaitForFailure: (handler: IGestureHandler) => boolean;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export interface GestureHandlerDelegate<TComponent, THandler> {
1111
view: TComponent;
1212

1313
init(viewRef: number, handler: THandler): void;
14+
detachHtml(): void;
1415
isPointerInBounds({ x, y }: { x: number; y: number }): boolean;
1516
measureView(): MeasureResult;
1617
reset(): void;

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ export class GestureHandlerWebDelegate
6363
);
6464
}
6565

66+
detachHtml(): void {
67+
this.view = null as unknown as HTMLElement;
68+
}
69+
6670
isPointerInBounds({ x, y }: { x: number; y: number }): boolean {
6771
return isPointerInBounds(this.view, { x, y });
6872
}

0 commit comments

Comments
 (0)