Skip to content

Commit 8ee5fb6

Browse files
crisbetojelbourn
authored andcommitted
fix(drag-drop): text selection not disabled on safari if user drags out of the viewport (#12864)
Currently we disable text selection on all draggable elements and their handles, however that doesn't seem to be enough on Safari, because moving your cursor towards the end of the viewport causes the entire body text to be marked. These changes also disable text marking on the entire body while the user is dragging.
1 parent ec0de52 commit 8ee5fb6

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
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
@@ -155,6 +155,16 @@ describe('DragDropRegistry', () => {
155155
expect(dispatchTouchEvent(document, 'touchmove').defaultPrevented).toBe(true);
156156
});
157157

158+
it('should disable the native interactions on the body while dragging', () => {
159+
const firstItem = testComponent.dragItems.first;
160+
161+
registry.startDragging(firstItem, createMouseEvent('mousedown'));
162+
expect(document.body.classList).toContain('cdk-drag-drop-disable-native-interactions');
163+
164+
registry.stopDragging(firstItem);
165+
expect(document.body.classList).not.toContain('cdk-drag-drop-disable-native-interactions');
166+
});
167+
158168
});
159169

160170
@Component({

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ export class DragDropRegistry<I, C extends {id: string}> implements OnDestroy {
116116
const moveEvent = isTouchEvent ? 'touchmove' : 'mousemove';
117117
const upEvent = isTouchEvent ? 'touchend' : 'mouseup';
118118

119+
// We need to disable the native interactions on the entire body, because
120+
// the user can start marking text if they drag too far in Safari.
121+
this._document.body.classList.add('cdk-drag-drop-disable-native-interactions');
122+
119123
// We explicitly bind __active__ listeners here, because newer browsers will default to
120124
// passive ones for `mousemove` and `touchmove`. The events need to be active, because we
121125
// use `preventDefault` to prevent the page from scrolling while the user is dragging.
@@ -136,6 +140,7 @@ export class DragDropRegistry<I, C extends {id: string}> implements OnDestroy {
136140

137141
if (this._activeDragInstances.size === 0) {
138142
this._clearGlobalListeners();
143+
this._document.body.classList.remove('cdk-drag-drop-disable-native-interactions');
139144
}
140145
}
141146

src/cdk/drag-drop/drop.scss

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ $cdk-z-index-drag-preview: 1000;
88
}
99

1010
.cdk-drag,
11-
.cdk-drag-handle {
11+
.cdk-drag-handle,
12+
.cdk-drag-drop-disable-native-interactions {
1213
touch-action: none;
1314
-webkit-user-drag: none;
1415
-webkit-tap-highlight-color: transparent;

0 commit comments

Comments
 (0)