Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/cdk/drag-drop/directives/drag.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,19 @@ describe('CdkDrag', () => {
expect(dragElement.style.transform).toBeFalsy();
}));

it('should be able to set an alternate drag root element for ng-container', fakeAsync(() => {
const fixture = createComponent(DraggableNgContainerWithAlternateRoot);
fixture.detectChanges();

const dragRoot = fixture.componentInstance.dragRoot.nativeElement;

expect(dragRoot.style.transform).toBeFalsy();

dragElementViaMouse(fixture, dragRoot, 50, 100);

expect(dragRoot.style.transform).toBe('translate3d(50px, 100px, 0px)');
}));

it('should preserve the initial transform if the root element changes', fakeAsync(() => {
const fixture = createComponent(DraggableWithAlternateRoot);
fixture.detectChanges();
Expand Down Expand Up @@ -7150,6 +7163,21 @@ class DraggableWithRadioInputsInDropZone {
];
}


@Component({
template: `
<div #dragRoot class="alternate-root" style="width: 200px; height: 200px; background: hotpink">
<ng-container cdkDrag cdkDragRootElement=".alternate-root">
<div style="width: 100px; height: 100px; background: red;"></div>
</ng-container>
</div>
`
})
class DraggableNgContainerWithAlternateRoot {
@ViewChild('dragRoot') dragRoot: ElementRef<HTMLElement>;
@ViewChild(CdkDrag) dragInstance: CdkDrag;
}

/**
* Drags an element to a position on the page using the mouse.
* @param fixture Fixture on which to run change detection.
Expand Down
11 changes: 8 additions & 3 deletions src/cdk/drag-drop/directives/drag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,14 @@ export class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDestroy {

/** Syncs the root element with the `DragRef`. */
private _updateRootElement() {
const element = this.element.nativeElement;
const rootElement = this.rootElementSelector ?
element.closest<HTMLElement>(this.rootElementSelector) : element;
const element = this.element.nativeElement as HTMLElement;
let rootElement = element;
if (this.rootElementSelector) {
rootElement = element.closest !== undefined
? element.closest(this.rootElementSelector) as HTMLElement
// Comment tag doesn't have closest method, so use parent's one.
: element.parentElement?.closest(this.rootElementSelector) as HTMLElement;
}

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