Skip to content

Commit 49c1921

Browse files
authored
feat(cdk/drag-drop): adding method to set drag position (#24769)
Adds method `setFreeDragPosition` in Cdk `DragDrop` directive to set position in pixel on a drop container. Also corrects some inaccurate types on a couple of freeDragPosition methods of the `DragDrop` directive. Fixes #18530
1 parent ce67406 commit 49c1921

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,6 +1258,19 @@ describe('CdkDrag', () => {
12581258
expect(dragInstance.getFreeDragPosition()).toEqual({x: 150, y: 300});
12591259
}));
12601260

1261+
it('should be able to set the current position programmatically', fakeAsync(() => {
1262+
const fixture = createComponent(StandaloneDraggable);
1263+
fixture.detectChanges();
1264+
1265+
const dragElement = fixture.componentInstance.dragElement.nativeElement;
1266+
const dragInstance = fixture.componentInstance.dragInstance;
1267+
1268+
dragInstance.setFreeDragPosition({x: 50, y: 100});
1269+
1270+
expect(dragElement.style.transform).toBe('translate3d(50px, 100px, 0px)');
1271+
expect(dragInstance.getFreeDragPosition()).toEqual({x: 50, y: 100});
1272+
}));
1273+
12611274
it('should be able to set the current position', fakeAsync(() => {
12621275
const fixture = createComponent(StandaloneDraggable);
12631276
fixture.componentInstance.freeDragPosition = {x: 50, y: 100};

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ export class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDestroy {
116116
* Sets the position of a `CdkDrag` that is outside of a drop container.
117117
* Can be used to restore the element's position for a returning user.
118118
*/
119-
@Input('cdkDragFreeDragPosition') freeDragPosition: {x: number; y: number};
119+
@Input('cdkDragFreeDragPosition') freeDragPosition: Point;
120120

121121
/** Whether starting to drag this element is disabled. */
122122
@Input('cdkDragDisabled')
@@ -282,10 +282,18 @@ export class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDestroy {
282282
/**
283283
* Gets the pixel coordinates of the draggable outside of a drop container.
284284
*/
285-
getFreeDragPosition(): {readonly x: number; readonly y: number} {
285+
getFreeDragPosition(): Readonly<Point> {
286286
return this._dragRef.getFreeDragPosition();
287287
}
288288

289+
/**
290+
* Sets the current position in pixels the draggable outside of a drop container.
291+
* @param value New position to be set.
292+
*/
293+
setFreeDragPosition(value: Point): void {
294+
this._dragRef.setFreeDragPosition(value);
295+
}
296+
289297
ngAfterViewInit() {
290298
// Normally this isn't in the zone, but it can cause major performance regressions for apps
291299
// using `zone-patch-rxjs` because it'll trigger a change detection when it unsubscribes.

tools/public_api_guard/cdk/drag-drop.md

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,8 @@ export class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDestroy {
6767
readonly ended: EventEmitter<CdkDragEnd>;
6868
readonly entered: EventEmitter<CdkDragEnter<any>>;
6969
readonly exited: EventEmitter<CdkDragExit<any>>;
70-
freeDragPosition: {
71-
x: number;
72-
y: number;
73-
};
74-
getFreeDragPosition(): {
75-
readonly x: number;
76-
readonly y: number;
77-
};
70+
freeDragPosition: Point;
71+
getFreeDragPosition(): Readonly<Point>;
7872
getPlaceholderElement(): HTMLElement;
7973
getRootElement(): HTMLElement;
8074
_handles: QueryList<CdkDragHandle>;
@@ -93,6 +87,7 @@ export class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDestroy {
9387
readonly released: EventEmitter<CdkDragRelease>;
9488
reset(): void;
9589
rootElementSelector: string;
90+
setFreeDragPosition(value: Point): void;
9691
readonly started: EventEmitter<CdkDragStart>;
9792
// (undocumented)
9893
static ɵdir: i0.ɵɵDirectiveDeclaration<CdkDrag<any>, "[cdkDrag]", ["cdkDrag"], { "data": "cdkDragData"; "lockAxis": "cdkDragLockAxis"; "rootElementSelector": "cdkDragRootElement"; "boundaryElement": "cdkDragBoundary"; "dragStartDelay": "cdkDragStartDelay"; "freeDragPosition": "cdkDragFreeDragPosition"; "disabled": "cdkDragDisabled"; "constrainPosition": "cdkDragConstrainPosition"; "previewClass": "cdkDragPreviewClass"; "previewContainer": "cdkDragPreviewContainer"; }, { "started": "cdkDragStarted"; "released": "cdkDragReleased"; "ended": "cdkDragEnded"; "entered": "cdkDragEntered"; "exited": "cdkDragExited"; "dropped": "cdkDragDropped"; "moved": "cdkDragMoved"; }, ["_previewTemplate", "_placeholderTemplate", "_handles"]>;

0 commit comments

Comments
 (0)