Skip to content

Commit

Permalink
fix(cdk/drag-drop): stop pointer events on placeholder (#24404)
Browse files Browse the repository at this point in the history
Removes the pointer events from the drag placeholder so that the user can't interact with it while the preview is animating.

Fixes #24403.

(cherry picked from commit 761f5fd)
  • Loading branch information
crisbeto authored and andrewseguin committed Feb 17, 2022
1 parent bd3f39f commit 81898ca
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/cdk/drag-drop/directives/drag.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,18 @@ describe('CdkDrag', () => {

expect(dragElement.style.transform).toBeFalsy();
}));

it('should prevent the default dragstart action', fakeAsync(() => {
const fixture = createComponent(StandaloneDraggable);
fixture.detectChanges();
const event = dispatchFakeEvent(
fixture.componentInstance.dragElement.nativeElement,
'dragstart',
);
fixture.detectChanges();

expect(event.defaultPrevented).toBe(true);
}));
});

describe('touch dragging', () => {
Expand Down Expand Up @@ -2952,6 +2964,9 @@ describe('CdkDrag', () => {
expect(placeholder.textContent!.trim())
.withContext('Expected placeholder content to match element')
.toContain('One');
expect(placeholder.style.pointerEvents)
.withContext('Expected pointer events to be disabled on placeholder')
.toBe('none');

dispatchMouseEvent(document, 'mouseup');
fixture.detectChanges();
Expand Down
12 changes: 12 additions & 0 deletions src/cdk/drag-drop/drag-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,9 @@ export class DragRef<T = any> {
this._ngZone.runOutsideAngular(() => {
element.addEventListener('mousedown', this._pointerDown, activeEventListenerOptions);
element.addEventListener('touchstart', this._pointerDown, passiveEventListenerOptions);
// Usually this isn't necessary since the we prevent the default action in `pointerDown`,
// but some cases like dragging of links can slip through (see #24403).
element.addEventListener('dragstart', preventDefault, activeEventListenerOptions);
});
this._initialTransform = undefined;
this._rootElement = element;
Expand Down Expand Up @@ -1159,6 +1162,9 @@ export class DragRef<T = any> {
placeholder = deepCloneNode(this._rootElement);
}

// Stop pointer events on the preview so the user can't
// interact with it while the preview is animating.
placeholder.style.pointerEvents = 'none';
placeholder.classList.add('cdk-drag-placeholder');
return placeholder;
}
Expand Down Expand Up @@ -1290,6 +1296,7 @@ export class DragRef<T = any> {
private _removeRootElementListeners(element: HTMLElement) {
element.removeEventListener('mousedown', this._pointerDown, activeEventListenerOptions);
element.removeEventListener('touchstart', this._pointerDown, passiveEventListenerOptions);
element.removeEventListener('dragstart', preventDefault, activeEventListenerOptions);
}

/**
Expand Down Expand Up @@ -1555,3 +1562,8 @@ function matchElementSize(target: HTMLElement, sourceRect: ClientRect): void {
target.style.height = `${sourceRect.height}px`;
target.style.transform = getTransform(sourceRect.left, sourceRect.top);
}

/** Utility to prevent the default action of an event. */
function preventDefault(event: Event): void {
event.preventDefault();
}

0 comments on commit 81898ca

Please sign in to comment.