Skip to content
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

fix(cdk/drag-drop): account for scale when setting free drag position #29739

Merged
merged 1 commit into from
Sep 17, 2024
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
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
Loading