Skip to content

Commit dc657bc

Browse files
fix: contextmenu may be shown while dragging
1 parent 390139a commit dc657bc

File tree

3 files changed

+49
-10
lines changed

3 files changed

+49
-10
lines changed

e2e/draggable.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Draggable from '../src/main';
22
import { pointerdown, pointermove, pointerup } from './pointer-util';
3+
import { aMouseEvent } from './util';
34

45
describe('Draggable with Pointer events', () => {
56
let el;
@@ -196,5 +197,39 @@ describe('Draggable with Pointer events', () => {
196197
expect(handler).not.toHaveBeenCalled();
197198
});
198199
});
200+
201+
describe("contextmenu", () => {
202+
beforeEach(() => {
203+
draggable = new Draggable({});
204+
205+
draggable.bindTo(el);
206+
});
207+
208+
it('prevents contextmenu while dragging', () => {
209+
pointerdown(el, 100, 200);
210+
211+
const e = aMouseEvent('contextmenu');
212+
spyOn(e, 'preventDefault');
213+
214+
el.dispatchEvent(e);
215+
216+
expect(e.preventDefault).toHaveBeenCalledTimes(1);
217+
218+
219+
pointerup(el, 100, 200);
220+
});
221+
222+
it('does not prevent contextmenu after release', () => {
223+
pointerdown(el, 100, 200);
224+
pointerup(el, 100, 200);
225+
226+
const e = aMouseEvent('contextmenu');
227+
spyOn(e, 'preventDefault');
228+
229+
el.dispatchEvent(e);
230+
231+
expect(e.preventDefault).not.toHaveBeenCalled();
232+
});
233+
});
199234
});
200235

e2e/util.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
11
/* eslint max-params: [2, 5] */
22

3-
const aMouseEvent = (type, x, y, button) =>
4-
new MouseEvent(type, {
5-
bubbles: true,
6-
cancelable: true,
7-
view: window,
8-
clientX: x,
9-
clientY: y,
10-
button: button
11-
});
12-
133
const aTouch = (el, x, y, id) =>
144
new Touch({
155
identifier: id,
@@ -26,6 +16,16 @@ const aTouchEvent = (type, touches) =>
2616
changedTouches: touches
2717
});
2818

19+
export const aMouseEvent = (type, x, y, button) =>
20+
new MouseEvent(type, {
21+
bubbles: true,
22+
cancelable: true,
23+
view: window,
24+
clientX: x,
25+
clientY: y,
26+
button: button
27+
});
28+
2929
export function mousedown(element, x, y, button) {
3030
element.dispatchEvent(aMouseEvent("mousedown", x, y, button));
3131
}

src/main.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ const unbind = (el, event, callback) =>
88

99
const noop = () => { /* empty */ };
1010

11+
const preventDefault = e => e.preventDefault();
12+
1113
const touchRegExp = /touch/;
1214

1315
// 300ms is the usual mouse interval;
@@ -107,6 +109,7 @@ export class Draggable {
107109
if (e.isPrimary && e.button === 0) {
108110
bind(this._element, "pointermove", this._pointermove);
109111
bind(this._element, "pointerup", this._pointerup);
112+
bind(this._element, "contextmenu", preventDefault);
110113

111114
this._touchAction = e.target.style.touchAction;
112115
e.target.style.touchAction = "none";
@@ -126,6 +129,7 @@ export class Draggable {
126129
if (e.isPrimary) {
127130
unbind(this._element, "pointermove", this._pointermove);
128131
unbind(this._element, "pointerup", this._pointerup);
132+
unbind(this._element, "contextmenu", preventDefault);
129133

130134
e.target.style.touchAction = this._touchAction;
131135
e.target.releasePointerCapture(e.pointerId);

0 commit comments

Comments
 (0)