Skip to content

Commit 9a1f5eb

Browse files
crisbetojelbourn
authored andcommitted
fix(drag-drop): don't allow dragging using right mouse button (#12729)
Currently users can drag draggable elements using any mouse button. These changes limit dragging to the left button.
1 parent d603b8b commit 9a1f5eb

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed

src/cdk-experimental/drag-drop/drag.spec.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ import {
1212
} from '@angular/core';
1313
import {TestBed, ComponentFixture, fakeAsync, flush, tick} from '@angular/core/testing';
1414
import {DragDropModule} from './drag-drop-module';
15-
import {dispatchMouseEvent, dispatchTouchEvent} from '@angular/cdk/testing';
15+
import {
16+
createMouseEvent,
17+
dispatchEvent,
18+
dispatchMouseEvent,
19+
dispatchTouchEvent,
20+
} from '@angular/cdk/testing';
1621
import {Directionality} from '@angular/cdk/bidi';
1722
import {CdkDrag} from './drag';
1823
import {CdkDragDrop} from './drag-events';
@@ -95,6 +100,26 @@ describe('CdkDrag', () => {
95100

96101
cleanup();
97102
}));
103+
104+
it('should not drag an element with the right mouse button', fakeAsync(() => {
105+
const fixture = createComponent(StandaloneDraggable);
106+
fixture.detectChanges();
107+
const dragElement = fixture.componentInstance.dragElement.nativeElement;
108+
const event = createMouseEvent('mousedown', 50, 100, 2);
109+
110+
expect(dragElement.style.transform).toBeFalsy();
111+
112+
dispatchEvent(dragElement, event);
113+
fixture.detectChanges();
114+
115+
dispatchMouseEvent(document, 'mousemove', 50, 100);
116+
fixture.detectChanges();
117+
118+
dispatchMouseEvent(document, 'mouseup');
119+
fixture.detectChanges();
120+
121+
expect(dragElement.style.transform).toBeFalsy();
122+
}));
98123
});
99124

100125
describe('touch dragging', () => {

src/cdk-experimental/drag-drop/drag.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -223,19 +223,23 @@ export class CdkDrag<T = any> implements OnDestroy {
223223
/** Handler for when the pointer is pressed down on the element or the handle. */
224224
private _pointerDown = (referenceElement: ElementRef<HTMLElement>,
225225
event: MouseEvent | TouchEvent) => {
226-
if (this._dragDropRegistry.isDragging(this)) {
226+
227+
const isDragging = this._dragDropRegistry.isDragging(this);
228+
229+
// Abort if the user is already dragging or is using a mouse button other than the primary one.
230+
if (isDragging || (!this._isTouchEvent(event) && event.button !== 0)) {
227231
return;
228232
}
229233

230234
const endedOrDestroyed = merge(this.ended, this._destroyed);
231235

232236
this._dragDropRegistry.pointerMove
233-
.pipe(takeUntil(endedOrDestroyed))
234-
.subscribe(this._pointerMove);
237+
.pipe(takeUntil(endedOrDestroyed))
238+
.subscribe(this._pointerMove);
235239

236-
this._dragDropRegistry.pointerUp
237-
.pipe(takeUntil(endedOrDestroyed))
238-
.subscribe(this._pointerUp);
240+
this._dragDropRegistry.pointerUp
241+
.pipe(takeUntil(endedOrDestroyed))
242+
.subscribe(this._pointerUp);
239243

240244
this._dragDropRegistry.startDragging(this, event);
241245
this._initialContainer = this.dropContainer;

src/cdk/testing/event-objects.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
/** Creates a browser MouseEvent with the specified options. */
10-
export function createMouseEvent(type: string, x = 0, y = 0) {
10+
export function createMouseEvent(type: string, x = 0, y = 0, button = 0) {
1111
const event = document.createEvent('MouseEvent');
1212

1313
event.initMouseEvent(type,
@@ -23,7 +23,7 @@ export function createMouseEvent(type: string, x = 0, y = 0) {
2323
false, /* altKey */
2424
false, /* shiftKey */
2525
false, /* metaKey */
26-
0, /* button */
26+
button, /* button */
2727
null /* relatedTarget */);
2828

2929
return event;

0 commit comments

Comments
 (0)