Skip to content

Commit

Permalink
fix(cdk/drag-drop): account for scale when setting free drag position (
Browse files Browse the repository at this point in the history
…#29739)

Fixes that the scale was only being synced when the user drags, but we need it also when setting the free drag position.

Fixes #29737.
  • Loading branch information
crisbeto committed Sep 17, 2024
1 parent 234e5e0 commit 4cd2152
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/cdk/drag-drop/directives/drag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ export class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDestroy {
() => {
this._updateRootElement();
this._setupHandlesListener();
this._dragRef.scale = this.scale;

if (this.freeDragPosition) {
this._dragRef.setFreeDragPosition(this.freeDragPosition);
Expand All @@ -333,6 +334,9 @@ export class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDestroy {
this._updateRootElement();
}

// Scale affects the free drag position so we need to sync it up here.
this._dragRef.scale = this.scale;

// Skip the first change since it's being handled in the `afterNextRender` queued up in the
// constructor.
if (positionChange && !positionChange.firstChange && this.freeDragPosition) {
Expand Down
14 changes: 14 additions & 0 deletions src/cdk/drag-drop/directives/standalone-drag.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1371,6 +1371,20 @@ describe('Standalone CdkDrag', () => {
expect(dragElement.style.transform).toBe('translate3d(150px, 300px, 0px)');
}));

it('should account for the scale when setting the free drag position', fakeAsync(() => {
const fixture = createComponent(StandaloneDraggable);
fixture.componentInstance.scale = 0.5;
fixture.componentInstance.freeDragPosition = {x: 50, y: 100};
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();

const dragElement = fixture.componentInstance.dragElement.nativeElement;
const dragInstance = fixture.componentInstance.dragInstance;

expect(dragElement.style.transform).toBe('translate3d(100px, 200px, 0px)');
expect(dragInstance.getFreeDragPosition()).toEqual({x: 50, y: 100});
}));

it('should include the dragged distance as the user is dragging', fakeAsync(() => {
const fixture = createComponent(StandaloneDraggable);
fixture.detectChanges();
Expand Down

0 comments on commit 4cd2152

Please sign in to comment.