Skip to content

Commit 2bdd543

Browse files
authored
[web] fix context menu (#3939)
## Description Context menu was broken on v3, this PR fixes it. WebDelegate never handled changing contextMenu. when context had been disabled the `areContextMenuListenersAdded` was set to true, later when it was enabled it would not change the listener from disabled to enabled as the `addContextMenuListeners` returned after seeing that the listeners had already been added. ## Test plan Tested on the following example: <details> ```tsx import React from 'react'; import { StyleSheet, View } from 'react-native'; import { GestureDetector, MouseButton, usePanGesture, } from 'react-native-gesture-handler'; export default function ContextMenuExample() { const p1 = usePanGesture({ mouseButton: MouseButton.RIGHT }); const p2 = usePanGesture({}); const p3 = usePanGesture({}); return ( <View style={styles.container}> <GestureDetector gesture={p1}> <View style={[styles.box, styles.grandParent]}> <GestureDetector gesture={p2} enableContextMenu={true}> <View style={[styles.box, styles.parent]}> <GestureDetector gesture={p3} enableContextMenu={false}> <View style={[styles.box, styles.child]} /> </GestureDetector> </View> </GestureDetector> </View> </GestureDetector> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'space-around', alignItems: 'center', }, grandParent: { width: 300, height: 300, backgroundColor: "navy", }, parent: { width: 200, height: 200, backgroundColor: "purple", }, child: { width: 100, height: 100, backgroundColor: "blue", }, box: { display: 'flex', justifyContent: 'space-around', alignItems: 'center', borderRadius: 20, }, }); ``` </details>
1 parent 0c130e3 commit 2bdd543

1 file changed

Lines changed: 17 additions & 5 deletions

File tree

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export class GestureHandlerWebDelegate
3333
};
3434

3535
private areContextMenuListenersAdded = false;
36+
private wasContextMenuEnabled = false;
3637

3738
init(viewRef: number, handler: IGestureHandler): void {
3839
if (!viewRef) {
@@ -158,9 +159,11 @@ export class GestureHandlerWebDelegate
158159
}
159160

160161
if (this.shouldDisableContextMenu()) {
162+
this.wasContextMenuEnabled = false;
161163
this.view.addEventListener('contextmenu', this.disableContextMenu);
162164
this.areContextMenuListenersAdded = true;
163165
} else if (this.gestureHandler.enableContextMenu) {
166+
this.wasContextMenuEnabled = true;
164167
this.view.addEventListener('contextmenu', this.enableContextMenu);
165168
this.areContextMenuListenersAdded = true;
166169
}
@@ -173,10 +176,14 @@ export class GestureHandlerWebDelegate
173176

174177
this.ensureView(this.view);
175178

176-
if (this.shouldDisableContextMenu()) {
179+
if (!this.areContextMenuListenersAdded) {
180+
return;
181+
}
182+
183+
if (!this.wasContextMenuEnabled) {
177184
this.view.removeEventListener('contextmenu', this.disableContextMenu);
178185
this.areContextMenuListenersAdded = false;
179-
} else if (this.gestureHandler.enableContextMenu) {
186+
} else {
180187
this.view.removeEventListener('contextmenu', this.enableContextMenu);
181188
this.areContextMenuListenersAdded = false;
182189
}
@@ -220,11 +227,16 @@ export class GestureHandlerWebDelegate
220227
}
221228

222229
private setContextMenu() {
223-
if (this.gestureHandler.enabled) {
224-
this.addContextMenuListeners();
225-
} else {
230+
if (!this.gestureHandler.enabled) {
231+
this.removeContextMenuListeners();
232+
return;
233+
}
234+
235+
if (!this.wasContextMenuEnabled) {
226236
this.removeContextMenuListeners();
227237
}
238+
239+
this.addContextMenuListeners();
228240
}
229241

230242
onEnabledChange(): void {

0 commit comments

Comments
 (0)