Skip to content

Commit 87aa831

Browse files
committed
fix(drag-drop): allow for element in DropListRef to be changed
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 2a086ce commit 87aa831

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
@@ -700,7 +700,7 @@ export class DragRef<T = any> {
700700
if (this._nextSibling) {
701701
this._nextSibling.parentNode!.insertBefore(this._rootElement, this._nextSibling);
702702
} else {
703-
this._initialContainer.element.appendChild(this._rootElement);
703+
coerceElement(this._initialContainer.element).appendChild(this._rootElement);
704704
}
705705

706706
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.
@@ -227,7 +228,7 @@ export class DropListRef<T = any> {
227228
element.parentElement!.insertBefore(placeholder, element);
228229
activeDraggables.splice(newIndex, 0, item);
229230
} else {
230-
this.element.appendChild(placeholder);
231+
coerceElement(this.element).appendChild(placeholder);
231232
activeDraggables.push(item);
232233
}
233234

@@ -413,7 +414,7 @@ export class DropListRef<T = any> {
413414

414415
/** Caches the position of the drop list. */
415416
private _cacheOwnPosition() {
416-
this._clientRect = this.element.getBoundingClientRect();
417+
this._clientRect = coerceElement(this.element).getBoundingClientRect();
417418
}
418419

419420
/** Refreshes the position cache of the items and sibling containers. */
@@ -597,21 +598,23 @@ export class DropListRef<T = any> {
597598
return false;
598599
}
599600

600-
const elementFromPoint = this._document.elementFromPoint(x, y);
601+
const elementFromPoint = this._document.elementFromPoint(x, y) as HTMLElement | null;
601602

602603
// If there's no element at the pointer position, then
603604
// the client rect is probably scrolled out of the view.
604605
if (!elementFromPoint) {
605606
return false;
606607
}
607608

609+
const nativeElement = coerceElement(this.element);
610+
608611
// The `ClientRect`, that we're using to find the container over which the user is
609612
// hovering, doesn't give us any information on whether the element has been scrolled
610613
// out of the view or whether it's overlapping with other containers. This means that
611614
// we could end up transferring the item into a container that's invisible or is positioned
612615
// below another one. We use the result from `elementFromPoint` to get the top-most element
613616
// at the pointer position and to find whether it's one of the intersecting drop containers.
614-
return elementFromPoint === this.element || this.element.contains(elementFromPoint);
617+
return elementFromPoint === nativeElement || nativeElement.contains(elementFromPoint);
615618
}
616619

617620
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ export declare class DropListRef<T = any> {
279279
previousContainer: DropListRef<any>;
280280
isPointerOverContainer: boolean;
281281
}>;
282-
readonly element: HTMLElement;
282+
element: HTMLElement | ElementRef<HTMLElement>;
283283
enterPredicate: (drag: DragRef, drop: DropListRef) => boolean;
284284
entered: Subject<{
285285
item: DragRef;

0 commit comments

Comments
 (0)