Skip to content

fix(drag-drop): text selection not disabled on safari if user drags out of the viewport #12864

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
10 changes: 10 additions & 0 deletions src/cdk/drag-drop/drag-drop-registry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,16 @@ describe('DragDropRegistry', () => {
expect(dispatchTouchEvent(document, 'touchmove').defaultPrevented).toBe(true);
});

it('should disable the native interactions on the body while dragging', () => {
const firstItem = testComponent.dragItems.first;

registry.startDragging(firstItem, createMouseEvent('mousedown'));
expect(document.body.classList).toContain('cdk-drag-drop-disable-native-interactions');

registry.stopDragging(firstItem);
expect(document.body.classList).not.toContain('cdk-drag-drop-disable-native-interactions');
});

});

@Component({
Expand Down
5 changes: 5 additions & 0 deletions src/cdk/drag-drop/drag-drop-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ export class DragDropRegistry<I, C extends {id: string}> implements OnDestroy {
const moveEvent = isTouchEvent ? 'touchmove' : 'mousemove';
const upEvent = isTouchEvent ? 'touchend' : 'mouseup';

// We need to disable the native interactions on the entire body, because
// the user can start marking text if they drag too far in Safari.
this._document.body.classList.add('cdk-drag-drop-disable-native-interactions');

// We explicitly bind __active__ listeners here, because newer browsers will default to
// passive ones for `mousemove` and `touchmove`. The events need to be active, because we
// use `preventDefault` to prevent the page from scrolling while the user is dragging.
Expand All @@ -133,6 +137,7 @@ export class DragDropRegistry<I, C extends {id: string}> implements OnDestroy {

if (this._activeDragInstances.size === 0) {
this._clearGlobalListeners();
this._document.body.classList.remove('cdk-drag-drop-disable-native-interactions');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this still get removed if something interrupts the drag? Something like an OS-level prompt or the OS changing window focus/mouse location.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As long as the mouseup/touchend event fires, it will.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what I meant- is there any situation where the end event wouldn't fire? I was wondering if there should be something that removed the class if neither a move event nor an end event aren't detected after a certain amount of time

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to avoid adding more timers. Even if the user's pointer leaves the browser and then comes back, the next mouseup/touchend event will reset it.

}
}

Expand Down
3 changes: 2 additions & 1 deletion src/cdk/drag-drop/drop.scss
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ $cdk-z-index-drag-preview: 1000;
}

.cdk-drag,
.cdk-drag-handle {
.cdk-drag-handle,
.cdk-drag-drop-disable-native-interactions {
touch-action: none;
-webkit-user-drag: none;
-webkit-tap-highlight-color: transparent;
Expand Down