diff --git a/packages/react-native-gesture-handler/src/web/handlers/GestureHandler.ts b/packages/react-native-gesture-handler/src/web/handlers/GestureHandler.ts index 7ac76a9558..462e42fa7a 100644 --- a/packages/react-native-gesture-handler/src/web/handlers/GestureHandler.ts +++ b/packages/react-native-gesture-handler/src/web/handlers/GestureHandler.ts @@ -40,7 +40,7 @@ export default abstract class GestureHandler implements IGestureHandler { private _state: State = State.UNDETERMINED; private _shouldCancelWhenOutside = false; - private _enabled = false; + private _enabled: boolean | null = null; private viewRef: number | null = null; private propsRef: React.RefObject | null = null; @@ -712,16 +712,28 @@ export default abstract class GestureHandler implements IGestureHandler { // Handling config // + // Helper function to correctly set enabled property + private updateEnabled(enabled: boolean | undefined) { + if (enabled === undefined) { + if (this._enabled) { + return; + } + + this._enabled = true; + this.delegate.onEnabledChange(); + } else if (this._enabled !== enabled) { + this._enabled = enabled; + this.delegate.onEnabledChange(); + } + } + public setGestureConfig(config: Config) { this.resetConfig(); this.updateGestureConfig(config); } public updateGestureConfig(config: Config): void { - if (config.enabled !== undefined && this.enabled !== config.enabled) { - this._enabled = config.enabled; - this.delegate.onEnabledChange(); - } + this.updateEnabled(config.enabled); if (config.hitSlop !== undefined) { this.hitSlop = config.hitSlop; @@ -911,7 +923,7 @@ export default abstract class GestureHandler implements IGestureHandler { } protected resetConfig(): void { - this._enabled = true; + this._enabled = null; this.manualActivation = false; this.shouldCancelWhenOutside = false; this.mouseButton = undefined; diff --git a/packages/react-native-gesture-handler/src/web/handlers/IGestureHandler.ts b/packages/react-native-gesture-handler/src/web/handlers/IGestureHandler.ts index b3e8359bd2..6afc8257bb 100644 --- a/packages/react-native-gesture-handler/src/web/handlers/IGestureHandler.ts +++ b/packages/react-native-gesture-handler/src/web/handlers/IGestureHandler.ts @@ -23,7 +23,7 @@ export default interface IGestureHandler { state: State; shouldCancelWhenOutside: boolean; shouldResetProgress: boolean; - readonly enabled: boolean; + readonly enabled: boolean | null; readonly pointerType: PointerType; enableContextMenu: boolean; readonly activeCursor?: ActiveCursor; diff --git a/packages/react-native-gesture-handler/src/web/tools/EventManager.ts b/packages/react-native-gesture-handler/src/web/tools/EventManager.ts index ea63906678..95c82b4e1a 100644 --- a/packages/react-native-gesture-handler/src/web/tools/EventManager.ts +++ b/packages/react-native-gesture-handler/src/web/tools/EventManager.ts @@ -94,6 +94,15 @@ export default abstract class EventManager { this.pointersInBounds.splice(index, 1); } + public setEnabled(value: boolean | null) { + if (value) { + this.registerListeners(); + } else { + this.resetManager(); + this.unregisterListeners(); + } + } + public resetManager(): void { // Reseting activePointersCounter is necessary to make gestures such as pinch work properly // There are gestures that end when there is still one active pointer (like pinch/rotation) diff --git a/packages/react-native-gesture-handler/src/web/tools/GestureHandlerWebDelegate.ts b/packages/react-native-gesture-handler/src/web/tools/GestureHandlerWebDelegate.ts index 8e46b32c9b..14476c9ad2 100644 --- a/packages/react-native-gesture-handler/src/web/tools/GestureHandlerWebDelegate.ts +++ b/packages/react-native-gesture-handler/src/web/tools/GestureHandlerWebDelegate.ts @@ -38,8 +38,6 @@ export class GestureHandlerWebDelegate ); } - this.isInitialized = true; - this.gestureHandler = handler; this.view = findNodeHandle(viewRef) as unknown as HTMLElement; @@ -48,10 +46,6 @@ export class GestureHandlerWebDelegate touchAction: this.view.style.touchAction, }; - this.setUserSelect(); - this.setTouchAction(); - this.setContextMenu(); - this.eventManagers.push(new PointerEventManager(this.view)); this.eventManagers.push(new KeyboardEventManager(this.view)); this.eventManagers.push(new WheelEventManager(this.view)); @@ -59,6 +53,8 @@ export class GestureHandlerWebDelegate this.eventManagers.forEach((manager) => this.gestureHandler.attachEventManager(manager) ); + + this.isInitialized = true; } detach(): void { @@ -68,8 +64,9 @@ export class GestureHandlerWebDelegate }; this.eventManagers.forEach((manager) => { - manager.unregisterListeners(); + manager.setEnabled(false); }); + this.removeContextMenuListeners(); this._view = null; this.eventManagers = []; @@ -198,15 +195,9 @@ export class GestureHandlerWebDelegate this.setTouchAction(); this.setContextMenu(); - if (this.gestureHandler.enabled) { - this.eventManagers.forEach((manager) => { - manager.registerListeners(); - }); - } else { - this.eventManagers.forEach((manager) => { - manager.unregisterListeners(); - }); - } + this.eventManagers.forEach((manager) => { + manager.setEnabled(this.gestureHandler.enabled); + }); } onBegin(): void {