Skip to content

Commit d5a885b

Browse files
committed
feat(drag-drop): add the ability to disable dragging
Adds inputs to `cdkDrag`, `cdkDropList` and `cdkDragHandle` that allows for dragging to be disabled through those specific elements. Fixes #13651.
1 parent 76044e8 commit d5a885b

File tree

9 files changed

+184
-4
lines changed

9 files changed

+184
-4
lines changed

src/cdk/drag-drop/drag-drop.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,11 @@ the element that is moved as the user is dragging. This is useful for cases like
133133
draggable.
134134

135135
<!-- example(cdk-drag-drop-root-element) -->
136+
137+
### Disable dragging
138+
If you want to disable dragging for a particular drag item, you can do so by setting the
139+
`cdkDragDisabled` input on a `cdkDrag` item. Furthermore, you can disable an entire list
140+
using the `cdkDropListDisabled` input on a `cdkDropList` or a particular handle via
141+
`cdkDragHandleDisabled` on `cdkDragHandle`.
142+
143+
<!-- example(cdk-drag-drop-disabled) -->

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {Directive, ElementRef, Inject, Optional} from '@angular/core';
9+
import {Directive, ElementRef, Inject, Optional, Input} from '@angular/core';
10+
import {coerceBooleanProperty} from '@angular/cdk/coercion';
1011
import {CDK_DRAG_PARENT} from './drag-parent';
1112
import {toggleNativeDragInteractions} from './drag-styling';
1213

@@ -21,6 +22,14 @@ export class CdkDragHandle {
2122
/** Closest parent draggable instance. */
2223
_parentDrag: {} | undefined;
2324

25+
/** Whether starting to drag through this handle is disabled. */
26+
@Input('cdkDragHandleDisabled')
27+
get disabled(): boolean { return this._disabled; }
28+
set disabled(value: boolean) {
29+
this._disabled = coerceBooleanProperty(value);
30+
}
31+
private _disabled = false;
32+
2433
constructor(
2534
public element: ElementRef<HTMLElement>,
2635
@Inject(CDK_DRAG_PARENT) @Optional() parentDrag?: any) {

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,18 @@ describe('CdkDrag', () => {
412412
expect(dragElement.style.transform).toBeFalsy();
413413
}));
414414

415+
it('should not be able to drag the element if dragging is disabled', fakeAsync(() => {
416+
const fixture = createComponent(StandaloneDraggable);
417+
fixture.detectChanges();
418+
const dragElement = fixture.componentInstance.dragElement.nativeElement;
419+
420+
fixture.componentInstance.dragInstance.disabled = true;
421+
422+
expect(dragElement.style.transform).toBeFalsy();
423+
dragElementViaMouse(fixture, dragElement, 50, 100);
424+
expect(dragElement.style.transform).toBeFalsy();
425+
}));
426+
415427
it('should stop propagation for the drag sequence start event', fakeAsync(() => {
416428
const fixture = createComponent(StandaloneDraggable);
417429
fixture.detectChanges();
@@ -450,6 +462,32 @@ describe('CdkDrag', () => {
450462
expect(dragElement.style.transform).toBe('translate3d(50px, 100px, 0px)');
451463
}));
452464

465+
it('should not be able to drag the element if the handle is disabled', fakeAsync(() => {
466+
const fixture = createComponent(StandaloneDraggableWithHandle);
467+
fixture.detectChanges();
468+
const dragElement = fixture.componentInstance.dragElement.nativeElement;
469+
const handle = fixture.componentInstance.handleElement.nativeElement;
470+
471+
fixture.componentInstance.handleInstance.disabled = true;
472+
473+
expect(dragElement.style.transform).toBeFalsy();
474+
dragElementViaMouse(fixture, handle, 50, 100);
475+
expect(dragElement.style.transform).toBeFalsy();
476+
}));
477+
478+
it('should not be able to drag using the handle if the element is disabled', fakeAsync(() => {
479+
const fixture = createComponent(StandaloneDraggableWithHandle);
480+
fixture.detectChanges();
481+
const dragElement = fixture.componentInstance.dragElement.nativeElement;
482+
const handle = fixture.componentInstance.handleElement.nativeElement;
483+
484+
fixture.componentInstance.dragInstance.disabled = true;
485+
486+
expect(dragElement.style.transform).toBeFalsy();
487+
dragElementViaMouse(fixture, handle, 50, 100);
488+
expect(dragElement.style.transform).toBeFalsy();
489+
}));
490+
453491
it('should be able to use a handle that was added after init', fakeAsync(() => {
454492
const fixture = createComponent(StandaloneDraggableWithDelayedHandle);
455493

@@ -1513,6 +1551,30 @@ describe('CdkDrag', () => {
15131551
expect(thirdItem.style.transform).toBeFalsy();
15141552
}));
15151553

1554+
it('should not move the item if the list is disabled', fakeAsync(() => {
1555+
const fixture = createComponent(DraggableInDropZone);
1556+
fixture.detectChanges();
1557+
const dragItems = fixture.componentInstance.dragItems;
1558+
1559+
fixture.componentInstance.dropInstance.disabled = true;
1560+
1561+
expect(dragItems.map(drag => drag.element.nativeElement.textContent!.trim()))
1562+
.toEqual(['Zero', 'One', 'Two', 'Three']);
1563+
1564+
const firstItem = dragItems.first;
1565+
const thirdItemRect = dragItems.toArray()[2].element.nativeElement.getBoundingClientRect();
1566+
1567+
dragElementViaMouse(fixture, firstItem.element.nativeElement,
1568+
thirdItemRect.right + 1, thirdItemRect.top + 1);
1569+
flush();
1570+
fixture.detectChanges();
1571+
1572+
expect(fixture.componentInstance.droppedSpy).not.toHaveBeenCalled();
1573+
1574+
expect(dragItems.map(drag => drag.element.nativeElement.textContent!.trim()))
1575+
.toEqual(['Zero', 'One', 'Two', 'Three']);
1576+
}));
1577+
15161578
});
15171579

15181580
describe('in a connected drop container', () => {
@@ -1928,6 +1990,8 @@ class StandaloneDraggable {
19281990
class StandaloneDraggableWithHandle {
19291991
@ViewChild('dragElement') dragElement: ElementRef<HTMLElement>;
19301992
@ViewChild('handleElement') handleElement: ElementRef<HTMLElement>;
1993+
@ViewChild(CdkDrag) dragInstance: CdkDrag;
1994+
@ViewChild(CdkDragHandle) handleInstance: CdkDragHandle;
19311995
}
19321996

19331997
@Component({

src/cdk/drag-drop/drag.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
ViewContainerRef,
3030
} from '@angular/core';
3131
import {supportsPassiveEventListeners} from '@angular/cdk/platform';
32+
import {coerceBooleanProperty} from '@angular/cdk/coercion';
3233
import {Observable, Subject, Subscription, Observer} from 'rxjs';
3334
import {take} from 'rxjs/operators';
3435
import {DragDropRegistry} from './drag-drop-registry';
@@ -198,6 +199,16 @@ export class CdkDrag<T = any> implements AfterViewInit, OnDestroy {
198199
*/
199200
@Input('cdkDragRootElement') rootElementSelector: string;
200201

202+
/** Whether starting to drag this element is disabled. */
203+
@Input('cdkDragDisabled')
204+
get disabled(): boolean {
205+
return this._disabled || (this.dropContainer && this.dropContainer.disabled);
206+
}
207+
set disabled(value: boolean) {
208+
this._disabled = coerceBooleanProperty(value);
209+
}
210+
private _disabled = false;
211+
201212
/** Emits when the user starts dragging the item. */
202213
@Output('cdkDragStarted') started: EventEmitter<CdkDragStart> = new EventEmitter<CdkDragStart>();
203214

@@ -314,10 +325,10 @@ export class CdkDrag<T = any> implements AfterViewInit, OnDestroy {
314325
return !!target && (target === element || element.contains(target as HTMLElement));
315326
});
316327

317-
if (targetHandle) {
328+
if (targetHandle && !targetHandle.disabled && !this.disabled) {
318329
this._initializeDragSequence(targetHandle.element.nativeElement, event);
319330
}
320-
} else {
331+
} else if (!this.disabled) {
321332
this._initializeDragSequence(this._rootElement, event);
322333
}
323334
}

src/cdk/drag-drop/drop-list-container.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ export interface CdkDropListContainer<T = any> {
2626
/** Locks the position of the draggable elements inside the container along the specified axis. */
2727
lockAxis: 'x' | 'y';
2828

29+
/** Whether starting a dragging sequence from this container is disabled. */
30+
disabled: boolean;
31+
2932
/** Starts dragging an item. */
3033
start(): void;
3134

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {coerceArray} from '@angular/cdk/coercion';
9+
import {coerceArray, coerceBooleanProperty} from '@angular/cdk/coercion';
1010
import {
1111
ContentChildren,
1212
ElementRef,
@@ -114,6 +114,14 @@ export class CdkDropList<T = any> implements OnInit, OnDestroy {
114114
/** Locks the position of the draggable elements inside the container along the specified axis. */
115115
@Input('cdkDropListLockAxis') lockAxis: 'x' | 'y';
116116

117+
/** Whether starting a dragging sequence from this container is disabled. */
118+
@Input('cdkDropListDisabled')
119+
get disabled(): boolean { return this._disabled; }
120+
set disabled(value: boolean) {
121+
this._disabled = coerceBooleanProperty(value);
122+
}
123+
private _disabled = false;
124+
117125
/**
118126
* Function that is used to determine whether an item
119127
* is allowed to be moved into a drop container.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
.example-list {
2+
width: 500px;
3+
max-width: 100%;
4+
border: solid 1px #ccc;
5+
min-height: 60px;
6+
display: block;
7+
background: white;
8+
border-radius: 4px;
9+
overflow: hidden;
10+
}
11+
12+
.example-box {
13+
padding: 20px 10px;
14+
border-bottom: solid 1px #ccc;
15+
color: rgba(0, 0, 0, 0.87);
16+
display: flex;
17+
flex-direction: row;
18+
align-items: center;
19+
justify-content: space-between;
20+
box-sizing: border-box;
21+
cursor: move;
22+
background: white;
23+
font-size: 14px;
24+
}
25+
26+
.cdk-drag-preview {
27+
box-sizing: border-box;
28+
border-radius: 4px;
29+
box-shadow: 0 5px 5px -3px rgba(0, 0, 0, 0.2),
30+
0 8px 10px 1px rgba(0, 0, 0, 0.14),
31+
0 3px 14px 2px rgba(0, 0, 0, 0.12);
32+
}
33+
34+
.cdk-drag-placeholder {
35+
opacity: 0;
36+
}
37+
38+
.cdk-drag-animating {
39+
transition: transform 250ms cubic-bezier(0, 0, 0.2, 1);
40+
}
41+
42+
.example-box:last-child {
43+
border: none;
44+
}
45+
46+
.example-list.cdk-drop-list-dragging .example-box:not(.cdk-drag-placeholder) {
47+
transition: transform 250ms cubic-bezier(0, 0, 0.2, 1);
48+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<div cdkDropList class="example-list" (cdkDropListDropped)="drop($event)">
2+
<div
3+
class="example-box"
4+
*ngFor="let item of items"
5+
cdkDrag
6+
[cdkDragDisabled]="item.disabled">{{item.value}}</div>
7+
</div>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import {Component} from '@angular/core';
2+
import {CdkDragDrop, moveItemInArray} from '@angular/cdk/drag-drop';
3+
4+
/**
5+
* @title Drag&Drop disabled
6+
*/
7+
@Component({
8+
selector: 'cdk-drag-drop-disabled-example',
9+
templateUrl: 'cdk-drag-drop-disabled-example.html',
10+
styleUrls: ['cdk-drag-drop-disabled-example.css'],
11+
})
12+
export class CdkDragDropDisabledExample {
13+
items = [
14+
{value: 'I can be dragged', disabled: false},
15+
{value: 'I cannot be dragged', disabled: true},
16+
{value: 'I can also be dragged', disabled: false}
17+
];
18+
19+
drop(event: CdkDragDrop<string[]>) {
20+
moveItemInArray(this.items, event.previousIndex, event.currentIndex);
21+
}
22+
}

0 commit comments

Comments
 (0)