Skip to content

Commit bd56a49

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 8b2dc82 commit bd56a49

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
@@ -142,3 +142,11 @@ new container. Depending on whether the predicate returns `true` or `false`, the
142142
be allowed into the new container.
143143

144144
<!-- example(cdk-drag-drop-enter-predicate) -->
145+
146+
### Disable dragging
147+
If you want to disable dragging for a particular drag item, you can do so by setting the
148+
`cdkDragDisabled` input on a `cdkDrag` item. Furthermore, you can disable an entire list
149+
using the `cdkDropListDisabled` input on a `cdkDropList` or a particular handle via
150+
`cdkDragHandleDisabled` on `cdkDragHandle`.
151+
152+
<!-- 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
@@ -413,6 +413,18 @@ describe('CdkDrag', () => {
413413
expect(dragElement.style.transform).toBeFalsy();
414414
}));
415415

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

482+
it('should not be able to drag the element if the handle is disabled', fakeAsync(() => {
483+
const fixture = createComponent(StandaloneDraggableWithHandle);
484+
fixture.detectChanges();
485+
const dragElement = fixture.componentInstance.dragElement.nativeElement;
486+
const handle = fixture.componentInstance.handleElement.nativeElement;
487+
488+
fixture.componentInstance.handleInstance.disabled = true;
489+
490+
expect(dragElement.style.transform).toBeFalsy();
491+
dragElementViaMouse(fixture, handle, 50, 100);
492+
expect(dragElement.style.transform).toBeFalsy();
493+
}));
494+
495+
it('should not be able to drag using the handle if the element is disabled', fakeAsync(() => {
496+
const fixture = createComponent(StandaloneDraggableWithHandle);
497+
fixture.detectChanges();
498+
const dragElement = fixture.componentInstance.dragElement.nativeElement;
499+
const handle = fixture.componentInstance.handleElement.nativeElement;
500+
501+
fixture.componentInstance.dragInstance.disabled = true;
502+
503+
expect(dragElement.style.transform).toBeFalsy();
504+
dragElementViaMouse(fixture, handle, 50, 100);
505+
expect(dragElement.style.transform).toBeFalsy();
506+
}));
507+
470508
it('should be able to use a handle that was added after init', fakeAsync(() => {
471509
const fixture = createComponent(StandaloneDraggableWithDelayedHandle);
472510

@@ -1551,6 +1589,30 @@ describe('CdkDrag', () => {
15511589
expect(thirdItem.style.transform).toBeFalsy();
15521590
}));
15531591

1592+
it('should not move the item if the list is disabled', fakeAsync(() => {
1593+
const fixture = createComponent(DraggableInDropZone);
1594+
fixture.detectChanges();
1595+
const dragItems = fixture.componentInstance.dragItems;
1596+
1597+
fixture.componentInstance.dropInstance.disabled = true;
1598+
1599+
expect(dragItems.map(drag => drag.element.nativeElement.textContent!.trim()))
1600+
.toEqual(['Zero', 'One', 'Two', 'Three']);
1601+
1602+
const firstItem = dragItems.first;
1603+
const thirdItemRect = dragItems.toArray()[2].element.nativeElement.getBoundingClientRect();
1604+
1605+
dragElementViaMouse(fixture, firstItem.element.nativeElement,
1606+
thirdItemRect.right + 1, thirdItemRect.top + 1);
1607+
flush();
1608+
fixture.detectChanges();
1609+
1610+
expect(fixture.componentInstance.droppedSpy).not.toHaveBeenCalled();
1611+
1612+
expect(dragItems.map(drag => drag.element.nativeElement.textContent!.trim()))
1613+
.toEqual(['Zero', 'One', 'Two', 'Three']);
1614+
}));
1615+
15541616
});
15551617

15561618
describe('in a connected drop container', () => {
@@ -2006,6 +2068,8 @@ class StandaloneDraggable {
20062068
class StandaloneDraggableWithHandle {
20072069
@ViewChild('dragElement') dragElement: ElementRef<HTMLElement>;
20082070
@ViewChild('handleElement') handleElement: ElementRef<HTMLElement>;
2071+
@ViewChild(CdkDrag) dragInstance: CdkDrag;
2072+
@ViewChild(CdkDragHandle) handleInstance: CdkDragHandle;
20092073
}
20102074

20112075
@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 {normalizePassiveListenerOptions} from '@angular/cdk/platform';
32+
import {coerceBooleanProperty} from '@angular/cdk/coercion';
3233
import {Observable, Subject, Subscription, Observer} from 'rxjs';
3334
import {startWith, take} from 'rxjs/operators';
3435
import {DragDropRegistry} from './drag-drop-registry';
@@ -200,6 +201,16 @@ export class CdkDrag<T = any> implements AfterViewInit, OnDestroy {
200201
*/
201202
@Input('cdkDragRootElement') rootElementSelector: string;
202203

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

@@ -327,10 +338,10 @@ export class CdkDrag<T = any> implements AfterViewInit, OnDestroy {
327338
return !!target && (target === element || element.contains(target as HTMLElement));
328339
});
329340

330-
if (targetHandle) {
341+
if (targetHandle && !targetHandle.disabled && !this.disabled) {
331342
this._initializeDragSequence(targetHandle.element.nativeElement, event);
332343
}
333-
} else {
344+
} else if (!this.disabled) {
334345
this._initializeDragSequence(this._rootElement, event);
335346
}
336347
}

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,
@@ -115,6 +115,14 @@ export class CdkDropList<T = any> implements OnInit, OnDestroy {
115115
/** Locks the position of the draggable elements inside the container along the specified axis. */
116116
@Input('cdkDropListLockAxis') lockAxis: 'x' | 'y';
117117

118+
/** Whether starting a dragging sequence from this container is disabled. */
119+
@Input('cdkDropListDisabled')
120+
get disabled(): boolean { return this._disabled; }
121+
set disabled(value: boolean) {
122+
this._disabled = coerceBooleanProperty(value);
123+
}
124+
private _disabled = false;
125+
118126
/**
119127
* Function that is used to determine whether an item
120128
* 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)