Skip to content

Commit 95e1721

Browse files
committed
fix(drag-drop): prevent text selection while dragging on Safari
Fixes being able to select text while dragging an item on Safari. Fixes #14403.
1 parent 53cecbb commit 95e1721

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,16 @@ describe('DragDropRegistry', () => {
202202
expect(dispatchFakeEvent(document, 'wheel').defaultPrevented).toBe(true);
203203
});
204204

205+
it('should not prevent the default `selectstart` actions when nothing is being dragged', () => {
206+
expect(dispatchFakeEvent(document, 'selectstart').defaultPrevented).toBe(false);
207+
});
208+
209+
it('should prevent the default `selectstart` action when an item is being dragged', () => {
210+
registry.startDragging(testComponent.dragItems.first, createMouseEvent('mousedown'));
211+
expect(dispatchFakeEvent(document, 'selectstart').defaultPrevented).toBe(true);
212+
});
213+
214+
205215
});
206216

207217
@Component({

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

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ const activeCapturingEventOptions = normalizePassiveListenerOptions({
1717
capture: true
1818
});
1919

20-
/** Handler for a pointer event callback. */
21-
type PointerEventHandler = (event: TouchEvent | MouseEvent) => void;
22-
2320
/**
2421
* Service that keeps track of all the drag item and drop container
2522
* instances, and manages global event listeners on the `document`.
@@ -42,8 +39,8 @@ export class DragDropRegistry<I, C extends {id: string}> implements OnDestroy {
4239
private _activeDragInstances = new Set<I>();
4340

4441
/** Keeps track of the event listeners that we've bound to the `document`. */
45-
private _globalListeners = new Map<'touchmove' | 'mousemove' | 'touchend' | 'mouseup' | 'wheel', {
46-
handler: PointerEventHandler,
42+
private _globalListeners = new Map<string, {
43+
handler: (event: Event) => void,
4744
options?: AddEventListenerOptions | boolean
4845
}>();
4946

@@ -87,7 +84,7 @@ export class DragDropRegistry<I, C extends {id: string}> implements OnDestroy {
8784
this._ngZone.runOutsideAngular(() => {
8885
// The event handler has to be explicitly active,
8986
// because newer browsers make it passive by default.
90-
this._document.addEventListener('touchmove', this._preventScrollListener,
87+
this._document.addEventListener('touchmove', this._preventDefaultWhileDragging,
9188
activeCapturingEventOptions);
9289
});
9390
}
@@ -104,7 +101,7 @@ export class DragDropRegistry<I, C extends {id: string}> implements OnDestroy {
104101
this.stopDragging(drag);
105102

106103
if (this._dragInstances.size === 0) {
107-
this._document.removeEventListener('touchmove', this._preventScrollListener,
104+
this._document.removeEventListener('touchmove', this._preventDefaultWhileDragging,
108105
activeCapturingEventOptions);
109106
}
110107
}
@@ -127,19 +124,27 @@ export class DragDropRegistry<I, C extends {id: string}> implements OnDestroy {
127124
// use `preventDefault` to prevent the page from scrolling while the user is dragging.
128125
this._globalListeners
129126
.set(moveEvent, {
130-
handler: e => this.pointerMove.next(e),
127+
handler: (e: Event) => this.pointerMove.next(e as TouchEvent | MouseEvent),
131128
options: activeCapturingEventOptions
132129
})
133130
.set(upEvent, {
134-
handler: e => this.pointerUp.next(e),
131+
handler: (e: Event) => this.pointerUp.next(e as TouchEvent | MouseEvent),
135132
options: true
133+
})
134+
// Preventing the default action on `mousemove` isn't enough to disable text selection
135+
// on Safari so we need to prevent the selection event as well. Alternatively this can
136+
// be done by setting `user-select: none` on the `body`, however it has causes a style
137+
// recalculation which can be expensive on pages with a lot of elements.
138+
.set('selectstart', {
139+
handler: this._preventDefaultWhileDragging,
140+
options: activeCapturingEventOptions
136141
});
137142

138143
// TODO(crisbeto): prevent mouse wheel scrolling while
139144
// dragging until we've set up proper scroll handling.
140145
if (!isTouchEvent) {
141146
this._globalListeners.set('wheel', {
142-
handler: this._preventScrollListener,
147+
handler: this._preventDefaultWhileDragging,
143148
options: activeCapturingEventOptions
144149
});
145150
}
@@ -180,9 +185,10 @@ export class DragDropRegistry<I, C extends {id: string}> implements OnDestroy {
180185
}
181186

182187
/**
183-
* Listener used to prevent `touchmove` and `wheel` events while the element is being dragged.
188+
* Event listener that will prevent the default browser action while the user is dragging.
189+
* @param event Event whose default action should be prevented.
184190
*/
185-
private _preventScrollListener = (event: Event) => {
191+
private _preventDefaultWhileDragging = (event: Event) => {
186192
if (this._activeDragInstances.size) {
187193
event.preventDefault();
188194
}

src/cdk/drag-drop/drag.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,8 @@ export class CdkDrag<T = any> implements AfterViewInit, OnDestroy {
687687
zIndex: '1000'
688688
});
689689

690+
toggleNativeDragInteractions(preview, false);
691+
690692
preview.classList.add('cdk-drag-preview');
691693
preview.setAttribute('dir', this._dir ? this._dir.value : 'ltr');
692694

0 commit comments

Comments
 (0)