Skip to content

fix(drag-drop): preview not being rendered inside fullscreen element #15066

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 1 commit into from
Feb 22, 2019
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
1 change: 1 addition & 0 deletions src/cdk/drag-drop/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ ng_test_library(
name = "drag-drop_test_sources",
srcs = glob(["**/*.spec.ts"]),
deps = [
"@angular//packages/common",
"@rxjs",
"//src/cdk/testing",
"//src/cdk/bidi",
Expand Down
36 changes: 36 additions & 0 deletions src/cdk/drag-drop/directives/drag.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
ChangeDetectionStrategy,
} from '@angular/core';
import {TestBed, ComponentFixture, fakeAsync, flush, tick} from '@angular/core/testing';
import {DOCUMENT} from '@angular/common';
import {DragDropModule} from '../drag-drop-module';
import {
createMouseEvent,
Expand Down Expand Up @@ -1248,6 +1249,41 @@ describe('CdkDrag', () => {
expect(preview.parentNode).toBeFalsy('Expected preview to be removed from the DOM');
}));

it('should create the preview inside the fullscreen element when in fullscreen mode',
fakeAsync(() => {
// Provide a limited stub of the document since we can't trigger fullscreen
// mode in unit tests and there are some issues with doing it in e2e tests.
const fakeDocument = {
body: document.body,
fullscreenElement: document.createElement('div'),
ELEMENT_NODE: Node.ELEMENT_NODE,
querySelectorAll: function() {
return document.querySelectorAll.apply(document, arguments);
},
addEventListener: function() {
document.addEventListener.apply(document, arguments);
},
removeEventListener: function() {
document.addEventListener.apply(document, arguments);
}
};
const fixture = createComponent(DraggableInDropZone, [{
provide: DOCUMENT,
useFactory: () => fakeDocument
}]);
fixture.detectChanges();
const item = fixture.componentInstance.dragItems.toArray()[1].element.nativeElement;

document.body.appendChild(fakeDocument.fullscreenElement);
startDraggingViaMouse(fixture, item);
flush();

const preview = document.querySelector('.cdk-drag-preview')! as HTMLElement;

expect(preview.parentNode).toBe(fakeDocument.fullscreenElement);
fakeDocument.fullscreenElement.parentNode!.removeChild(fakeDocument.fullscreenElement);
}));

it('should be able to constrain the preview position', fakeAsync(() => {
const fixture = createComponent(DraggableInDropZone);
fixture.componentInstance.boundarySelector = '.cdk-drop-list';
Expand Down
14 changes: 13 additions & 1 deletion src/cdk/drag-drop/drag-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ export class DragRef<T = any> {
// from the DOM completely, because iOS will stop firing all subsequent events in the chain.
element.style.display = 'none';
this._document.body.appendChild(element.parentNode!.replaceChild(placeholder, element));
this._document.body.appendChild(preview);
getPreviewInsertionPoint(this._document).appendChild(preview);
this._dropContainer.start();
}
}
Expand Down Expand Up @@ -1016,3 +1016,15 @@ function removeElement(element: HTMLElement | null) {
function isTouchEvent(event: MouseEvent | TouchEvent): event is TouchEvent {
return event.type.startsWith('touch');
}

/** Gets the element into which the drag preview should be inserted. */
function getPreviewInsertionPoint(documentRef: any): HTMLElement {
// We can't use the body if the user is in fullscreen mode,
// because the preview will render under the fullscreen element.
// TODO(crisbeto): dedupe this with the `FullscreenOverlayContainer` eventually.
return documentRef.fullscreenElement ||
documentRef.webkitFullscreenElement ||
documentRef.mozFullScreenElement ||
documentRef.msFullscreenElement ||
Copy link
Member

Choose a reason for hiding this comment

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

Add a TODO here to dedupe this with the code in FullscreenOverlayContainer? I don't know of a good place for it now, but we'll probably have something eventually

documentRef.body;
}