Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}