Skip to content

Commit b29308a

Browse files
crisbetommalerba
authored andcommitted
fix(column-resize): don't allow dragging using the right mouse… (#18758)
The column resize component allows any `mousedown` to start the drag sequence, including using the right button which can look weird since the context menu will be triggered as soon as they let go. These changes limit the dragging only to the left button, similarly to what we're doing in other places.
1 parent 0e40b8c commit b29308a

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/cdk-experimental/column-resize/overlay-handle.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ export abstract class ResizeOverlayHandle implements AfterViewInit, OnDestroy {
8080
}
8181

8282
private _dragStarted(mousedownEvent: MouseEvent) {
83+
// Only allow dragging using the left mouse button.
84+
if (mousedownEvent.button !== 0) {
85+
return;
86+
}
87+
8388
const mouseup = fromEvent<MouseEvent>(this.document, 'mouseup');
8489
const mousemove = fromEvent<MouseEvent>(this.document, 'mousemove');
8590
const escape = fromEvent<KeyboardEvent>(this.document, 'keyup')

src/material-experimental/column-resize/column-resize.spec.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,14 @@ abstract class BaseTestComponent {
173173
return parseInt((thumbElement.parentNode as HTMLElement).style.left!, 10);
174174
}
175175

176-
beginColumnResizeWithMouse(index: number): void {
176+
beginColumnResizeWithMouse(index: number, button = 0): void {
177177
const thumbElement = this.getOverlayThumbElement(index);
178178
this.table.nativeElement!.dispatchEvent(new MouseEvent('mouseleave',
179-
{bubbles: true, relatedTarget: thumbElement}));
179+
{bubbles: true, relatedTarget: thumbElement, button}));
180180
thumbElement.dispatchEvent(new MouseEvent('mousedown', {
181181
bubbles: true,
182182
screenX: MOUSE_START_OFFSET,
183+
button
183184
} as MouseEventInit));
184185
}
185186

@@ -391,6 +392,21 @@ describe('Material Popover Edit', () => {
391392
fixture.detectChanges();
392393
}));
393394

395+
it('should not start dragging using the right mouse button', fakeAsync(() => {
396+
const initialColumnWidth = component.getColumnWidth(1);
397+
398+
component.triggerHoverState();
399+
fixture.detectChanges();
400+
component.beginColumnResizeWithMouse(1, 2);
401+
402+
const initialPosition = component.getOverlayThumbPosition(1);
403+
404+
component.updateResizeWithMouseInProgress(5);
405+
406+
(expect(component.getOverlayThumbPosition(1)) as any).toBe(initialPosition);
407+
(expect(component.getColumnWidth(1)) as any).toBe(initialColumnWidth);
408+
}));
409+
394410
it('cancels an active mouse resize with the escape key', fakeAsync(() => {
395411
const initialColumnWidth = component.getColumnWidth(1);
396412

0 commit comments

Comments
 (0)