Skip to content

fix(drag-drop): detect changes on custom preview/placeholder before measuring #18698

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
43 changes: 40 additions & 3 deletions src/cdk/drag-drop/directives/drag.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3010,6 +3010,30 @@ describe('CdkDrag', () => {
expect(placeholder.textContent!.trim()).not.toContain('Custom placeholder');
}));

it('should measure the custom placeholder after the first change detection', fakeAsync(() => {
const fixture = createComponent(DraggableInDropZoneWithCustomPlaceholder);
fixture.componentInstance.extraPlaceholderClass = 'tall-placeholder';
fixture.detectChanges();
const dragItems = fixture.componentInstance.dragItems;
const item = dragItems.toArray()[0].element.nativeElement;

startDraggingViaMouse(fixture, item);

const thirdItem = dragItems.toArray()[2].element.nativeElement;
const thirdItemRect = thirdItem.getBoundingClientRect();

dispatchMouseEvent(document, 'mousemove', thirdItemRect.left + 1, thirdItemRect.top + 1);
fixture.detectChanges();

dispatchMouseEvent(document, 'mouseup');
fixture.detectChanges();
flush();
fixture.detectChanges();

const event = fixture.componentInstance.droppedSpy.calls.mostRecent().args[0];
expect(event.currentIndex).toBe(2);
}));

it('should not throw when custom placeholder only has text', fakeAsync(() => {
const fixture = createComponent(DraggableInDropZoneWithCustomTextOnlyPlaceholder);
fixture.detectChanges();
Expand Down Expand Up @@ -5127,21 +5151,34 @@ class DraggableInDropZoneWithCustomTextOnlyPreview {

@Component({
template: `
<div cdkDropList style="width: 100px; background: pink;">
<div
cdkDropList
(cdkDropListDropped)="droppedSpy($event)"
style="width: 100px; background: pink;">
<div *ngFor="let item of items" cdkDrag
style="width: 100%; height: ${ITEM_HEIGHT}px; background: red;">
{{item}}
<ng-container *ngIf="renderPlaceholder">
<div class="custom-placeholder" *cdkDragPlaceholder>Custom placeholder</div>
<div
class="custom-placeholder"
[ngClass]="extraPlaceholderClass"
*cdkDragPlaceholder>Custom placeholder</div>
</ng-container>
</div>
</div>
`
`,
styles: [`
.tall-placeholder {
height: ${ITEM_HEIGHT * 3}px;
}
`]
})
class DraggableInDropZoneWithCustomPlaceholder {
@ViewChildren(CdkDrag) dragItems: QueryList<CdkDrag>;
items = ['Zero', 'One', 'Two', 'Three'];
renderPlaceholder = true;
extraPlaceholderClass = '';
droppedSpy = jasmine.createSpy('dropped spy');
}

@Component({
Expand Down
2 changes: 2 additions & 0 deletions src/cdk/drag-drop/drag-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,7 @@ export class DragRef<T = any> {
if (previewTemplate) {
const viewRef = previewConfig!.viewContainer.createEmbeddedView(previewTemplate,
previewConfig!.context);
viewRef.detectChanges();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than directly calling detectChanges, couldn't we instead defer measuring until Angular completes the change detection it needs to do anyway?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost none of the drag&drop events trigger change detection so we can't really depend on it since it can happen at any time for other reason. This only detects on a small chunk of the tree anyway.

preview = getRootNode(viewRef, this._document);
this._previewRef = viewRef;

Expand Down Expand Up @@ -984,6 +985,7 @@ export class DragRef<T = any> {
placeholderTemplate,
placeholderConfig!.context
);
this._placeholderRef.detectChanges();
placeholder = getRootNode(this._placeholderRef, this._document);
} else {
placeholder = deepCloneNode(this._rootElement);
Expand Down