Skip to content

Commit 0df720c

Browse files
crisbetojelbourn
authored andcommitted
fix(drag-drop): add class to indicate that an item is being dragged (#12810)
Simiarly to `cdk-drop`, these changes add a class to `cdkDrag` to make it easier to indicate that an item is being dragged.
1 parent 58efca0 commit 0df720c

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,25 @@ describe('CdkDrag', () => {
303303
expect(dragElement.style.transform).toBe('translate3d(0px, 300px, 0px)');
304304
}));
305305

306+
it('should add a class while an element is being dragged', fakeAsync(() => {
307+
const fixture = createComponent(StandaloneDraggable);
308+
fixture.detectChanges();
309+
310+
const element = fixture.componentInstance.dragElement.nativeElement;
311+
312+
expect(element.classList).not.toContain('cdk-drag-dragging');
313+
314+
dispatchMouseEvent(element, 'mousedown');
315+
fixture.detectChanges();
316+
317+
expect(element.classList).toContain('cdk-drag-dragging');
318+
319+
dispatchMouseEvent(document, 'mouseup');
320+
fixture.detectChanges();
321+
322+
expect(element.classList).not.toContain('cdk-drag-dragging');
323+
}));
324+
306325
});
307326

308327
describe('draggable with a handle', () => {

src/cdk/drag-drop/drag.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import {takeUntil} from 'rxjs/operators';
5252
exportAs: 'cdkDrag',
5353
host: {
5454
'class': 'cdk-drag',
55+
'[class.cdk-drag-dragging]': '_isDragging()',
5556
'(mousedown)': '_startDragging($event)',
5657
'(touchstart)': '_startDragging($event)',
5758
}
@@ -189,7 +190,7 @@ export class CdkDrag<T = any> implements OnDestroy {
189190

190191
// Do this check before removing from the registry since it'll
191192
// stop being considered as dragged once it is removed.
192-
if (this._dragDropRegistry.isDragging(this)) {
193+
if (this._isDragging()) {
193194
// Since we move out the element to the end of the body while it's being
194195
// dragged, we have to make sure that it's removed if it gets destroyed.
195196
this._removeElement(this.element.nativeElement);
@@ -220,11 +221,16 @@ export class CdkDrag<T = any> implements OnDestroy {
220221
}
221222
}
222223

224+
/** Checks whether the element is currently being dragged. */
225+
_isDragging() {
226+
return this._dragDropRegistry.isDragging(this);
227+
}
228+
223229
/** Handler for when the pointer is pressed down on the element or the handle. */
224230
private _pointerDown = (referenceElement: ElementRef<HTMLElement>,
225231
event: MouseEvent | TouchEvent) => {
226232

227-
const isDragging = this._dragDropRegistry.isDragging(this);
233+
const isDragging = this._isDragging();
228234

229235
// Abort if the user is already dragging or is using a mouse button other than the primary one.
230236
if (isDragging || (!this._isTouchEvent(event) && event.button !== 0)) {
@@ -274,7 +280,7 @@ export class CdkDrag<T = any> implements OnDestroy {
274280
private _pointerMove = (event: MouseEvent | TouchEvent) => {
275281
// TODO(crisbeto): this should start dragging after a certain threshold,
276282
// otherwise we risk interfering with clicks on the element.
277-
if (!this._dragDropRegistry.isDragging(this)) {
283+
if (!this._isDragging()) {
278284
return;
279285
}
280286

@@ -310,7 +316,7 @@ export class CdkDrag<T = any> implements OnDestroy {
310316

311317
/** Handler that is invoked when the user lifts their pointer up, after initiating a drag. */
312318
private _pointerUp = () => {
313-
if (!this._dragDropRegistry.isDragging(this)) {
319+
if (!this._isDragging()) {
314320
return;
315321
}
316322

0 commit comments

Comments
 (0)