Skip to content

Commit 6e1f522

Browse files
authored
fix(cdk/drag-drop): allow using cdkDragRootElement w/ comment tag (#23596)
1 parent 980f6b2 commit 6e1f522

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

src/cdk/drag-drop/directives/drag.spec.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,19 @@ describe('CdkDrag', () => {
636636
expect(dragElement.style.transform).toBeFalsy();
637637
}));
638638

639+
it('should be able to set an alternate drag root element for ng-container', fakeAsync(() => {
640+
const fixture = createComponent(DraggableNgContainerWithAlternateRoot);
641+
fixture.detectChanges();
642+
643+
const dragRoot = fixture.componentInstance.dragRoot.nativeElement;
644+
645+
expect(dragRoot.style.transform).toBeFalsy();
646+
647+
dragElementViaMouse(fixture, dragRoot, 50, 100);
648+
649+
expect(dragRoot.style.transform).toBe('translate3d(50px, 100px, 0px)');
650+
}));
651+
639652
it('should preserve the initial transform if the root element changes', fakeAsync(() => {
640653
const fixture = createComponent(DraggableWithAlternateRoot);
641654
fixture.detectChanges();
@@ -7150,6 +7163,21 @@ class DraggableWithRadioInputsInDropZone {
71507163
];
71517164
}
71527165

7166+
7167+
@Component({
7168+
template: `
7169+
<div #dragRoot class="alternate-root" style="width: 200px; height: 200px; background: hotpink">
7170+
<ng-container cdkDrag cdkDragRootElement=".alternate-root">
7171+
<div style="width: 100px; height: 100px; background: red;"></div>
7172+
</ng-container>
7173+
</div>
7174+
`
7175+
})
7176+
class DraggableNgContainerWithAlternateRoot {
7177+
@ViewChild('dragRoot') dragRoot: ElementRef<HTMLElement>;
7178+
@ViewChild(CdkDrag) dragInstance: CdkDrag;
7179+
}
7180+
71537181
/**
71547182
* Drags an element to a position on the page using the mouse.
71557183
* @param fixture Fixture on which to run change detection.

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,9 +330,14 @@ export class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDestroy {
330330

331331
/** Syncs the root element with the `DragRef`. */
332332
private _updateRootElement() {
333-
const element = this.element.nativeElement;
334-
const rootElement = this.rootElementSelector ?
335-
element.closest<HTMLElement>(this.rootElementSelector) : element;
333+
const element = this.element.nativeElement as HTMLElement;
334+
let rootElement = element;
335+
if (this.rootElementSelector) {
336+
rootElement = element.closest !== undefined
337+
? element.closest(this.rootElementSelector) as HTMLElement
338+
// Comment tag doesn't have closest method, so use parent's one.
339+
: element.parentElement?.closest(this.rootElementSelector) as HTMLElement;
340+
}
336341

337342
if (rootElement && (typeof ngDevMode === 'undefined' || ngDevMode)) {
338343
assertElementNode(rootElement, 'cdkDrag');

0 commit comments

Comments
 (0)