From 690f8c2f0dd6440fe1437747bb2e35ade10308f8 Mon Sep 17 00:00:00 2001 From: damyanpetev Date: Fri, 20 Nov 2026 19:36:14 +0200 Subject: [PATCH] fix(drag-drop): keep the ghost under the pointer during window auto-scroll Co-Authored-By: Claude Opus 5.5 --- .../drag-drop/drag-drop.directive.ts | 8 ++++-- .../directives/drag-drop/drag-drop.spec.ts | 26 +++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/projects/igniteui-angular/directives/src/directives/drag-drop/drag-drop.directive.ts b/projects/igniteui-angular/directives/src/directives/drag-drop/drag-drop.directive.ts index 1d0f192cf91..680f1b2f6e4 100644 --- a/projects/igniteui-angular/directives/src/directives/drag-drop/drag-drop.directive.ts +++ b/projects/igniteui-angular/directives/src/directives/drag-drop/drag-drop.directive.ts @@ -589,7 +589,9 @@ export class IgxDragDirective implements AfterContentInit, OnDestroy { protected get ghostLeft() { if (this.ghostElement) { - return parseInt(this.ghostElement.style.left, 10) + this._ghostHostX; + // Includes the margin, like the pageX the setter takes. + const ghostMarginLeft = parseInt((this.document.defaultView!.getComputedStyle(this.ghostElement) as any)['margin-left'], 10); + return parseInt(this.ghostElement.style.left, 10) + ghostMarginLeft + this._ghostHostX; } return undefined!; } @@ -605,7 +607,9 @@ export class IgxDragDirective implements AfterContentInit, OnDestroy { protected get ghostTop() { if (this.ghostElement) { - return parseInt(this.ghostElement.style.top, 10) + this._ghostHostY; + // Includes the margin, like the pageY the setter takes. + const ghostMarginTop = parseInt((this.document.defaultView!.getComputedStyle(this.ghostElement) as any)['margin-top'], 10); + return parseInt(this.ghostElement.style.top, 10) + ghostMarginTop + this._ghostHostY; } return undefined!; } diff --git a/projects/igniteui-angular/directives/src/directives/drag-drop/drag-drop.spec.ts b/projects/igniteui-angular/directives/src/directives/drag-drop/drag-drop.spec.ts index cd09b4e0a48..ade4039cd46 100644 --- a/projects/igniteui-angular/directives/src/directives/drag-drop/drag-drop.spec.ts +++ b/projects/igniteui-angular/directives/src/directives/drag-drop/drag-drop.spec.ts @@ -3207,6 +3207,32 @@ describe('igxDrag window auto-scroll', () => { expect(drag.ghostElement).toBeTruthy(); }; + it('should scroll the window and move the ghost with it while dragging near the bottom of the viewport', async () => { + const firstDrag = fix.componentInstance.dragElems.first; + const maxScrollY = document.documentElement.scrollHeight - document.documentElement.clientHeight; + expect(maxScrollY).toBeGreaterThan(0); + + await dragToViewportBottom(firstDrag); + const ghostRect = firstDrag.ghostElement.getBoundingClientRect(); + + await vi.waitFor(() => expect(window.scrollY).toBeGreaterThan(0)); + expect(firstDrag.ghostElement.getBoundingClientRect().top).toBe(ghostRect.top); + expect(firstDrag.ghostElement.getBoundingClientRect().left).toBe(ghostRect.left); + + // The ghost extends the document while it moves down, but scrolling stops (within one 5px step) + // at the size the document had when scrolling started. + await vi.waitFor(() => expect(window.scrollY).toBeGreaterThanOrEqual(maxScrollY)); + const finalScrollY = window.scrollY; + await wait(100); + expect(window.scrollY).toBe(finalScrollY); + expect(finalScrollY).toBeLessThan(maxScrollY + 5); + expect(firstDrag.ghostElement.getBoundingClientRect().top).toBe(ghostRect.top); + expect(firstDrag.ghostElement.getBoundingClientRect().left).toBe(ghostRect.left); + + UIInteractions.simulatePointerEvent('pointerup', firstDrag.ghostElement, ghostRect.left, ghostRect.top + finalScrollY); + expect(firstDrag.ghostElement).toBeNull(); + }); + it('should stop scrolling the window when destroyed while auto-scrolling', async () => { const firstDrag = fix.componentInstance.dragElems.first; await dragToViewportBottom(firstDrag);