Skip to content

Commit 46cf45f

Browse files
crisbetojelbourn
authored andcommitted
fix(drag-drop): preview position relative to cursor thrown off if item has margin (#16180)
Fixes the preview shifting sideways relative to the cursor, if the item that's being dragged has a margin. Fixes #16171.
1 parent 7bf5e4e commit 46cf45f

File tree

2 files changed

+4
-0
lines changed

2 files changed

+4
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1765,6 +1765,8 @@ describe('CdkDrag', () => {
17651765
expect(previewRect.height).toBe(itemRect.height, 'Expected preview height to match element');
17661766
expect(preview.style.pointerEvents)
17671767
.toBe('none', 'Expected pointer events to be disabled on the preview');
1768+
// Use a regex here since some browsers normalize 0 to 0px, but others don't.
1769+
expect(preview.style.margin).toMatch(/^0(px)?$/, 'Expected the preview margin to be reset.');
17681770

17691771
dispatchMouseEvent(document, 'mouseup');
17701772
fixture.detectChanges();

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,8 @@ export class DragRef<T = any> {
867867
// It's important that we disable the pointer events on the preview, because
868868
// it can throw off the `document.elementFromPoint` calls in the `CdkDropList`.
869869
pointerEvents: 'none',
870+
// We have to reset the margin, because can throw off positioning relative to the viewport.
871+
margin: '0',
870872
position: 'fixed',
871873
top: '0',
872874
left: '0',

0 commit comments

Comments
 (0)