Skip to content

Commit e630bd6

Browse files
crisbetojelbourn
authored andcommitted
fix(drag-drop): allow for element in DropListRef to be changed (#15091)
It doesn't look like there's a good reason to have the `element` be readonly, aside from minimizing the coercion logic, since we only use the element in a handful of places and we don't bind any events to it so we don't need any cleanup logic. These changes allow for the element to be swapped out by removing the `readonly`. Fixes #15086.
1 parent f7c53d6 commit e630bd6

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,7 @@ export class DragRef<T = any> {
706706
if (this._nextSibling) {
707707
this._nextSibling.parentNode!.insertBefore(this._rootElement, this._nextSibling);
708708
} else {
709-
this._initialContainer.element.appendChild(this._rootElement);
709+
coerceElement(this._initialContainer.element).appendChild(this._rootElement);
710710
}
711711

712712
this._destroyPreview();

src/cdk/drag-drop/drop-list-ref.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import {ElementRef} from '@angular/core';
1010
import {DragDropRegistry} from './drag-drop-registry';
1111
import {Direction} from '@angular/cdk/bidi';
12+
import {coerceElement} from '@angular/cdk/coercion';
1213
import {Subject} from 'rxjs';
1314
import {moveItemInArray} from './drag-utils';
1415
import {DragRefInternal as DragRef} from './drag-ref';
@@ -51,7 +52,7 @@ export class DropListRef<T = any> {
5152
private _document: Document;
5253

5354
/** Element that the drop list is attached to. */
54-
readonly element: HTMLElement;
55+
element: HTMLElement | ElementRef<HTMLElement>;
5556

5657
/**
5758
* Unique ID for the drop list.
@@ -224,7 +225,7 @@ export class DropListRef<T = any> {
224225
element.parentElement!.insertBefore(placeholder, element);
225226
activeDraggables.splice(newIndex, 0, item);
226227
} else {
227-
this.element.appendChild(placeholder);
228+
coerceElement(this.element).appendChild(placeholder);
228229
activeDraggables.push(item);
229230
}
230231

@@ -416,7 +417,7 @@ export class DropListRef<T = any> {
416417

417418
/** Caches the position of the drop list. */
418419
private _cacheOwnPosition() {
419-
this._clientRect = this.element.getBoundingClientRect();
420+
this._clientRect = coerceElement(this.element).getBoundingClientRect();
420421
}
421422

422423
/** Refreshes the position cache of the items and sibling containers. */
@@ -607,21 +608,23 @@ export class DropListRef<T = any> {
607608
return false;
608609
}
609610

610-
const elementFromPoint = this._document.elementFromPoint(x, y);
611+
const elementFromPoint = this._document.elementFromPoint(x, y) as HTMLElement | null;
611612

612613
// If there's no element at the pointer position, then
613614
// the client rect is probably scrolled out of the view.
614615
if (!elementFromPoint) {
615616
return false;
616617
}
617618

619+
const nativeElement = coerceElement(this.element);
620+
618621
// The `ClientRect`, that we're using to find the container over which the user is
619622
// hovering, doesn't give us any information on whether the element has been scrolled
620623
// out of the view or whether it's overlapping with other containers. This means that
621624
// we could end up transferring the item into a container that's invisible or is positioned
622625
// below another one. We use the result from `elementFromPoint` to get the top-most element
623626
// at the pointer position and to find whether it's one of the intersecting drop containers.
624-
return elementFromPoint === this.element || this.element.contains(elementFromPoint);
627+
return elementFromPoint === nativeElement || nativeElement.contains(elementFromPoint);
625628
}
626629

627630
/**

tools/public_api_guard/cdk/drag-drop.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ export declare class DropListRef<T = any> {
292292
previousContainer: DropListRef<any>;
293293
isPointerOverContainer: boolean;
294294
}>;
295-
readonly element: HTMLElement;
295+
element: HTMLElement | ElementRef<HTMLElement>;
296296
enterPredicate: (drag: DragRef, drop: DropListRef) => boolean;
297297
entered: Subject<{
298298
item: DragRef;

0 commit comments

Comments
 (0)