Skip to content

Commit

Permalink
fix(cdk/drag-drop): expose pickup position in constrainPosition callb…
Browse files Browse the repository at this point in the history
…ack (#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.

(cherry picked from commit 73c0c60)
  • Loading branch information
crisbeto committed Jul 26, 2022
1 parent 3fc9d16 commit 636dd60
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/cdk/drag-drop/directives/drag.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,7 @@ describe('CdkDrag', () => {
jasmine.objectContaining({x: 300, y: 300}),
jasmine.any(DragRef),
jasmine.anything(),
jasmine.objectContaining({x: jasmine.any(Number), y: jasmine.any(Number)}),
);

const elementRect = dragElement.getBoundingClientRect();
Expand Down Expand Up @@ -3685,6 +3686,7 @@ describe('CdkDrag', () => {
jasmine.objectContaining({x: 200, y: 200}),
jasmine.any(DragRef),
jasmine.anything(),
jasmine.objectContaining({x: jasmine.any(Number), y: jasmine.any(Number)}),
);
expect(Math.floor(previewRect.top)).toBe(50);
expect(Math.floor(previewRect.left)).toBe(50);
Expand Down
1 change: 1 addition & 0 deletions src/cdk/drag-drop/directives/drag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ export class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDestroy {
userPointerPosition: Point,
dragRef: DragRef,
dimensions: ClientRect,
pickupPositionInElement: Point,
) => Point;

/** Class to be added to the preview element. */
Expand Down
3 changes: 2 additions & 1 deletion src/cdk/drag-drop/drag-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ export class DragRef<T = any> {
userPointerPosition: Point,
dragRef: DragRef,
dimensions: ClientRect,
pickupPositionInElement: Point,
) => Point;

constructor(
Expand Down Expand Up @@ -1239,7 +1240,7 @@ export class DragRef<T = any> {
private _getConstrainedPointerPosition(point: Point): Point {
const dropContainerLock = this._dropContainer ? this._dropContainer.lockAxis : null;
let {x, y} = this.constrainPosition
? this.constrainPosition(point, this, this._initialClientRect!)
? this.constrainPosition(point, this, this._initialClientRect!, this._pickupPositionInElement)
: point;

if (this.lockAxis === 'x' || dropContainerLock === 'x') {
Expand Down
4 changes: 2 additions & 2 deletions tools/public_api_guard/cdk/drag-drop.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDestroy {
dropContainer: CdkDropListInternal,
_document: any, _ngZone: NgZone, _viewContainerRef: ViewContainerRef, config: DragDropConfig, _dir: Directionality, dragDrop: DragDrop, _changeDetectorRef: ChangeDetectorRef, _selfHandle?: CdkDragHandle | undefined, _parentDrag?: CdkDrag<any> | undefined);
boundaryElement: string | ElementRef<HTMLElement> | HTMLElement;
constrainPosition?: (userPointerPosition: Point, dragRef: DragRef, dimensions: ClientRect) => Point;
constrainPosition?: (userPointerPosition: Point, dragRef: DragRef, dimensions: ClientRect, pickupPositionInElement: Point) => Point;
data: T;
get disabled(): boolean;
set disabled(value: BooleanInput);
Expand Down Expand Up @@ -359,7 +359,7 @@ export class DragDropRegistry<I extends {
export class DragRef<T = any> {
constructor(element: ElementRef<HTMLElement> | HTMLElement, _config: DragRefConfig, _document: Document, _ngZone: NgZone, _viewportRuler: ViewportRuler, _dragDropRegistry: DragDropRegistry<DragRef, DropListRefInternal>);
readonly beforeStarted: Subject<void>;
constrainPosition?: (userPointerPosition: Point, dragRef: DragRef, dimensions: ClientRect) => Point;
constrainPosition?: (userPointerPosition: Point, dragRef: DragRef, dimensions: ClientRect, pickupPositionInElement: Point) => Point;
data: T;
get disabled(): boolean;
set disabled(value: boolean);
Expand Down

0 comments on commit 636dd60

Please sign in to comment.