Skip to content

Commit 2394ffc

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 5a84e3e commit 2394ffc

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
@@ -689,7 +689,7 @@ export class DragRef<T = any> {
689689
if (this._nextSibling) {
690690
this._nextSibling.parentNode!.insertBefore(this._rootElement, this._nextSibling);
691691
} else {
692-
this._initialContainer.element.appendChild(this._rootElement);
692+
coerceElement(this._initialContainer.element).appendChild(this._rootElement);
693693
}
694694

695695
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.
@@ -209,7 +210,7 @@ export class DropListRef<T = any> {
209210
element.parentElement!.insertBefore(placeholder, element);
210211
this._activeDraggables.splice(newIndex, 0, item);
211212
} else {
212-
this.element.appendChild(placeholder);
213+
coerceElement(this.element).appendChild(placeholder);
213214
this._activeDraggables.push(item);
214215
}
215216

@@ -395,7 +396,7 @@ export class DropListRef<T = any> {
395396

396397
/** Caches the position of the drop list. */
397398
private _cacheOwnPosition() {
398-
this._clientRect = this.element.getBoundingClientRect();
399+
this._clientRect = coerceElement(this.element).getBoundingClientRect();
399400
}
400401

401402
/** Refreshes the position cache of the items and sibling containers. */
@@ -579,21 +580,23 @@ export class DropListRef<T = any> {
579580
return false;
580581
}
581582

582-
const elementFromPoint = this._document.elementFromPoint(x, y);
583+
const elementFromPoint = this._document.elementFromPoint(x, y) as HTMLElement | null;
583584

584585
// If there's no element at the pointer position, then
585586
// the client rect is probably scrolled out of the view.
586587
if (!elementFromPoint) {
587588
return false;
588589
}
589590

591+
const nativeElement = coerceElement(this.element);
592+
590593
// The `ClientRect`, that we're using to find the container over which the user is
591594
// hovering, doesn't give us any information on whether the element has been scrolled
592595
// out of the view or whether it's overlapping with other containers. This means that
593596
// we could end up transferring the item into a container that's invisible or is positioned
594597
// below another one. We use the result from `elementFromPoint` to get the top-most element
595598
// at the pointer position and to find whether it's one of the intersecting drop containers.
596-
return elementFromPoint === this.element || this.element.contains(elementFromPoint);
599+
return elementFromPoint === nativeElement || nativeElement.contains(elementFromPoint);
597600
}
598601

599602
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ export declare class DropListRef<T = any> {
276276
previousContainer: DropListRef<any>;
277277
isPointerOverContainer: boolean;
278278
}>;
279-
readonly element: HTMLElement;
279+
element: HTMLElement;
280280
enterPredicate: (drag: DragRef, drop: DropListRef) => boolean;
281281
entered: Subject<{
282282
item: DragRef;

0 commit comments

Comments
 (0)