Skip to content

Commit f968ef6

Browse files
lekhmanrusRuslan Lekhman
authored andcommitted
fix(cdk/drag-drop): allow using cdkDragRootElement w/ comment tag
1 parent d70faa0 commit f968ef6

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,22 @@ 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+
const dragElement = fixture.componentInstance.dragElement.nativeElement;
645+
646+
expect(dragRoot.style.transform).toBeFalsy();
647+
expect(dragElement.style.transform).toBeFalsy();
648+
649+
dragElementViaMouse(fixture, dragRoot, 50, 100);
650+
651+
expect(dragRoot.style.transform).toBe('translate3d(50px, 100px, 0px)');
652+
expect(dragElement.style.transform).toBeFalsy();
653+
}));
654+
639655
it('should preserve the initial transform if the root element changes', fakeAsync(() => {
640656
const fixture = createComponent(DraggableWithAlternateRoot);
641657
fixture.detectChanges();
@@ -7150,6 +7166,22 @@ class DraggableWithRadioInputsInDropZone {
71507166
];
71517167
}
71527168

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

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,9 +330,11 @@ 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;
333+
const element = this.element.nativeElement as HTMLElement;
334+
// Comment tag doesn't have closest method, so use parent's one.
335+
const closestFn = element.closest || element.parentElement?.closest;
334336
const rootElement = this.rootElementSelector ?
335-
element.closest<HTMLElement>(this.rootElementSelector) : element;
337+
closestFn?.call(this, this.rootElementSelector) as HTMLElement : element;
336338

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

0 commit comments

Comments
 (0)