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): constrainPosition now working well with boundary #27730

Merged
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
76 changes: 76 additions & 0 deletions src/cdk/drag-drop/directives/drag.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,32 @@ describe('CdkDrag', () => {
expect(dragElement.style.transform).toBe('translate3d(150px, 0px, 0px)');
}));

it('should be able to lock dragging along the x axis while using constrainPosition', fakeAsync(() => {
const fixture = createComponent(StandaloneDraggable);
fixture.detectChanges();
fixture.componentInstance.dragInstance.lockAxis = 'x';
fixture.componentInstance.dragInstance.constrainPosition = (
{x, y}: Point,
_dragRef: DragRef,
_dimensions: ClientRect,
pickup: Point,
) => {
x -= pickup.x;
y -= pickup.y;
return {x, y};
};

const dragElement = fixture.componentInstance.dragElement.nativeElement;

expect(dragElement.style.transform).toBeFalsy();

dragElementViaMouse(fixture, dragElement, 50, 100);
expect(dragElement.style.transform).toBe('translate3d(50px, 0px, 0px)');

dragElementViaMouse(fixture, dragElement, 100, 200);
expect(dragElement.style.transform).toBe('translate3d(150px, 0px, 0px)');
}));

it('should be able to lock dragging along the y axis', fakeAsync(() => {
const fixture = createComponent(StandaloneDraggable);
fixture.detectChanges();
Expand All @@ -576,6 +602,33 @@ describe('CdkDrag', () => {
expect(dragElement.style.transform).toBe('translate3d(0px, 300px, 0px)');
}));

it('should be able to lock dragging along the y axis while using constrainPosition', fakeAsync(() => {
const fixture = createComponent(StandaloneDraggable);
fixture.detectChanges();

fixture.componentInstance.dragInstance.lockAxis = 'y';
fixture.componentInstance.dragInstance.constrainPosition = (
{x, y}: Point,
_dragRef: DragRef,
_dimensions: ClientRect,
pickup: Point,
) => {
x -= pickup.x;
y -= pickup.y;
return {x, y};
};

const dragElement = fixture.componentInstance.dragElement.nativeElement;

expect(dragElement.style.transform).toBeFalsy();

dragElementViaMouse(fixture, dragElement, 50, 100);
expect(dragElement.style.transform).toBe('translate3d(0px, 100px, 0px)');

dragElementViaMouse(fixture, dragElement, 100, 200);
expect(dragElement.style.transform).toBe('translate3d(0px, 300px, 0px)');
}));

it('should add a class while an element is being dragged', fakeAsync(() => {
const fixture = createComponent(StandaloneDraggable);
fixture.detectChanges();
Expand Down Expand Up @@ -946,6 +999,29 @@ describe('CdkDrag', () => {
expect(dragElement.style.transform).toBe('translate3d(100px, 100px, 0px)');
}));

it('should allow for dragging to be constrained to an element while using constrainPosition', fakeAsync(() => {
const fixture = createComponent(StandaloneDraggable);
fixture.componentInstance.boundary = '.wrapper';
fixture.detectChanges();

fixture.componentInstance.dragInstance.constrainPosition = (
{x, y}: Point,
_dragRef: DragRef,
_dimensions: ClientRect,
pickup: Point,
) => {
x -= pickup.x;
y -= pickup.y;
return {x, y};
};

const dragElement = fixture.componentInstance.dragElement.nativeElement;

expect(dragElement.style.transform).toBeFalsy();
dragElementViaMouse(fixture, dragElement, 300, 300);
expect(dragElement.style.transform).toBe('translate3d(100px, 100px, 0px)');
}));

it('should be able to pass in a DOM node as the boundary', fakeAsync(() => {
const fixture = createComponent(StandaloneDraggable);
fixture.componentInstance.boundary = fixture.nativeElement.querySelector('.wrapper');
Expand Down
15 changes: 12 additions & 3 deletions src/cdk/drag-drop/drag-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1238,13 +1238,22 @@ export class DragRef<T = any> {
: point;

if (this.lockAxis === 'x' || dropContainerLock === 'x') {
mj3052 marked this conversation as resolved.
Show resolved Hide resolved
y = this._pickupPositionOnPage.y;
y =
this._pickupPositionOnPage.y -
(this.constrainPosition ? this._pickupPositionInElement.y : 0);
} else if (this.lockAxis === 'y' || dropContainerLock === 'y') {
x = this._pickupPositionOnPage.x;
x =
this._pickupPositionOnPage.x -
(this.constrainPosition ? this._pickupPositionInElement.x : 0);
}

if (this._boundaryRect) {
const {x: pickupX, y: pickupY} = this._pickupPositionInElement;
// If not using a custom constrain we need to account for the pickup position in the element
mj3052 marked this conversation as resolved.
Show resolved Hide resolved
// otherwise we do not need to do this, as it has already been accounted for
const {x: pickupX, y: pickupY} = !this.constrainPosition
? this._pickupPositionInElement
: {x: 0, y: 0};

const boundaryRect = this._boundaryRect;
const {width: previewWidth, height: previewHeight} = this._getPreviewRect();
const minY = boundaryRect.top + pickupY;
Expand Down
55 changes: 48 additions & 7 deletions src/dev-app/drag-drop/drag-drop-demo.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<div cdkDropListGroup>
<div class="demo-list">

<h2>To do</h2>
<div
cdkDropList
(cdkDropListDropped)="drop($event)"
[cdkDropListLockAxis]="axisLock"
[cdkDropListData]="todo">
[cdkDropListData]="todo"
>
<div *ngFor="let item of todo" cdkDrag>
{{item}}
<mat-icon cdkDragHandle svgIcon="dnd-move"></mat-icon>
Expand All @@ -20,7 +20,8 @@ <h2>Done</h2>
cdkDropList
(cdkDropListDropped)="drop($event)"
[cdkDropListLockAxis]="axisLock"
[cdkDropListData]="done">
[cdkDropListData]="done"
>
<div *ngFor="let item of done" cdkDrag>
{{item}}
<mat-icon cdkDragHandle svgIcon="dnd-move"></mat-icon>
Expand All @@ -37,7 +38,8 @@ <h2>Ages</h2>
cdkDropListOrientation="horizontal"
(cdkDropListDropped)="drop($event)"
[cdkDropListLockAxis]="axisLock"
[cdkDropListData]="ages">
[cdkDropListData]="ages"
>
<div *ngFor="let item of ages" cdkDrag>
{{item}}
<mat-icon cdkDragHandle svgIcon="dnd-move"></mat-icon>
Expand All @@ -52,7 +54,8 @@ <h2>Preferred Ages</h2>
cdkDropListOrientation="horizontal"
(cdkDropListDropped)="drop($event)"
[cdkDropListLockAxis]="axisLock"
[cdkDropListData]="preferredAges">
[cdkDropListData]="preferredAges"
>
<div *ngFor="let item of preferredAges" cdkDrag>
{{item}}
<mat-icon cdkDragHandle svgIcon="dnd-move"></mat-icon>
Expand All @@ -63,7 +66,45 @@ <h2>Preferred Ages</h2>

<div class="demo-list">
<h2>Free dragging</h2>
<div cdkDrag class="demo-free-draggable" [cdkDragLockAxis]="axisLock" [cdkDragStartDelay]="dragStartDelay">Drag me around</div>
<div
cdkDrag
class="demo-free-draggable"
[cdkDragLockAxis]="axisLock"
[cdkDragStartDelay]="dragStartDelay"
>
Drag me around
</div>
</div>

<div>
<h2>Drag with box boundary</h2>
<div class="demo-constrain-box" id="constrain-box-1">
<div
cdkDrag
class="demo-free-draggable"
[cdkDragLockAxis]="axisLock"
[cdkDragStartDelay]="dragStartDelay"
cdkDragBoundary="#constrain-box-1"
>
Drag me around
</div>
</div>
</div>

<div>
<h2>Drag with box boundary and custom constrain</h2>
<div class="demo-constrain-box" id="constrain-box-2">
<div
cdkDrag
class="demo-free-draggable"
[cdkDragLockAxis]="axisLock"
[cdkDragStartDelay]="dragStartDelay"
cdkDragBoundary="#constrain-box-2"
[cdkDragConstrainPosition]="constrainPosition"
>
Drag me around
</div>
</div>
</div>

<div>
Expand All @@ -90,6 +131,6 @@ <h2>Axis locking</h2>
<h2>Drag start delay</h2>

<mat-form-field>
<input matInput placeholder="Drag start delay" value="0" [(ngModel)]="dragStartDelay">
<input matInput placeholder="Drag start delay" value="0" [(ngModel)]="dragStartDelay" />
</mat-form-field>
</div>
6 changes: 6 additions & 0 deletions src/dev-app/drag-drop/drag-drop-demo.scss
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,9 @@ pre {
justify-content: center;
align-items: center;
}

.demo-constrain-box {
width: 600px;
height: 400px;
border: 1px solid black;
}
15 changes: 15 additions & 0 deletions src/dev-app/drag-drop/drag-drop-demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import {
DragDropModule,
moveItemInArray,
transferArrayItem,
Point,
DragRef,
CdkDrag,
} from '@angular/cdk/drag-drop';
import {CommonModule} from '@angular/common';
import {FormsModule} from '@angular/forms';
Expand Down Expand Up @@ -74,4 +77,16 @@ export class DragAndDropDemo {
);
}
}

constrainPosition(
{x, y}: Point,
_dragRef: DragRef,
_dimensions: ClientRect,
pickup: Point,
): Point {
// Just returning the original top left corner to not modify position
x -= pickup.x;
y -= pickup.y;
return {x, y};
}
}