Skip to content

Commit 73c0c60

Browse files
authored
fix(cdk/drag-drop): expose pickup position in constrainPosition callback (#25341)
Exposes the pickup position within the element to the `constrainPosition` callback so that users can account for it in their calculations. Relates to #25154.
1 parent 6883100 commit 73c0c60

File tree

4 files changed

+7
-3
lines changed

4 files changed

+7
-3
lines changed

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

+2
Original file line numberDiff line numberDiff line change
@@ -1082,6 +1082,7 @@ describe('CdkDrag', () => {
10821082
jasmine.objectContaining({x: 300, y: 300}),
10831083
jasmine.any(DragRef),
10841084
jasmine.anything(),
1085+
jasmine.objectContaining({x: jasmine.any(Number), y: jasmine.any(Number)}),
10851086
);
10861087

10871088
const elementRect = dragElement.getBoundingClientRect();
@@ -3685,6 +3686,7 @@ describe('CdkDrag', () => {
36853686
jasmine.objectContaining({x: 200, y: 200}),
36863687
jasmine.any(DragRef),
36873688
jasmine.anything(),
3689+
jasmine.objectContaining({x: jasmine.any(Number), y: jasmine.any(Number)}),
36883690
);
36893691
expect(Math.floor(previewRect.top)).toBe(50);
36903692
expect(Math.floor(previewRect.left)).toBe(50);

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

+1
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ export class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDestroy {
139139
userPointerPosition: Point,
140140
dragRef: DragRef,
141141
dimensions: ClientRect,
142+
pickupPositionInElement: Point,
142143
) => Point;
143144

144145
/** Class to be added to the preview element. */

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ export class DragRef<T = any> {
365365
userPointerPosition: Point,
366366
dragRef: DragRef,
367367
dimensions: ClientRect,
368+
pickupPositionInElement: Point,
368369
) => Point;
369370

370371
constructor(
@@ -1239,7 +1240,7 @@ export class DragRef<T = any> {
12391240
private _getConstrainedPointerPosition(point: Point): Point {
12401241
const dropContainerLock = this._dropContainer ? this._dropContainer.lockAxis : null;
12411242
let {x, y} = this.constrainPosition
1242-
? this.constrainPosition(point, this, this._initialClientRect!)
1243+
? this.constrainPosition(point, this, this._initialClientRect!, this._pickupPositionInElement)
12431244
: point;
12441245

12451246
if (this.lockAxis === 'x' || dropContainerLock === 'x') {

tools/public_api_guard/cdk/drag-drop.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDestroy {
5555
dropContainer: CdkDropListInternal,
5656
_document: any, _ngZone: NgZone, _viewContainerRef: ViewContainerRef, config: DragDropConfig, _dir: Directionality, dragDrop: DragDrop, _changeDetectorRef: ChangeDetectorRef, _selfHandle?: CdkDragHandle | undefined, _parentDrag?: CdkDrag<any> | undefined);
5757
boundaryElement: string | ElementRef<HTMLElement> | HTMLElement;
58-
constrainPosition?: (userPointerPosition: Point, dragRef: DragRef, dimensions: ClientRect) => Point;
58+
constrainPosition?: (userPointerPosition: Point, dragRef: DragRef, dimensions: ClientRect, pickupPositionInElement: Point) => Point;
5959
data: T;
6060
get disabled(): boolean;
6161
set disabled(value: BooleanInput);
@@ -359,7 +359,7 @@ export class DragDropRegistry<I extends {
359359
export class DragRef<T = any> {
360360
constructor(element: ElementRef<HTMLElement> | HTMLElement, _config: DragRefConfig, _document: Document, _ngZone: NgZone, _viewportRuler: ViewportRuler, _dragDropRegistry: DragDropRegistry<DragRef, DropListRefInternal>);
361361
readonly beforeStarted: Subject<void>;
362-
constrainPosition?: (userPointerPosition: Point, dragRef: DragRef, dimensions: ClientRect) => Point;
362+
constrainPosition?: (userPointerPosition: Point, dragRef: DragRef, dimensions: ClientRect, pickupPositionInElement: Point) => Point;
363363
data: T;
364364
get disabled(): boolean;
365365
set disabled(value: boolean);

0 commit comments

Comments
 (0)