Skip to content

Commit d010733

Browse files
csigritzmattlewis92
authored andcommitted
feat(draggable): add option to show the original element while dragging
1 parent bfe9bb4 commit d010733

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

src/draggable.directive.ts

+12-5
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ export class DraggableDirective implements OnInit, OnChanges, OnDestroy {
8484
*/
8585
@Input() ghostDragEnabled: boolean = true;
8686

87+
/**
88+
* Show the original element when ghostDragEnabled is true
89+
*/
90+
@Input() showOriginalElementWhileDragging: boolean = false;
91+
8792
/**
8893
* Allow custom behaviour to control when the element is dragged
8994
*/
@@ -264,11 +269,13 @@ export class DraggableDirective implements OnInit, OnChanges, OnDestroy {
264269
const clone = this.element.nativeElement.cloneNode(
265270
true
266271
) as HTMLElement;
267-
this.renderer.setStyle(
268-
this.element.nativeElement,
269-
'visibility',
270-
'hidden'
271-
);
272+
if (!this.showOriginalElementWhileDragging) {
273+
this.renderer.setStyle(
274+
this.element.nativeElement,
275+
'visibility',
276+
'hidden'
277+
);
278+
}
272279

273280
if (this.ghostElementAppendTo) {
274281
this.ghostElementAppendTo.appendChild(clone);

test/draggable.directive.spec.ts

+13
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ describe('draggable directive', () => {
1515
[dragAxis]="dragAxis"
1616
[dragSnapGrid]="dragSnapGrid"
1717
[ghostDragEnabled]="ghostDragEnabled"
18+
[showOriginalElementWhileDragging]="showOriginalElementWhileDragging"
1819
[validateDrag]="validateDrag"
1920
[dragCursor]="dragCursor"
2021
[dragActiveClass]="dragActiveClass"
@@ -36,6 +37,7 @@ describe('draggable directive', () => {
3637
dragAxis: any = { x: true, y: true };
3738
dragSnapGrid: any = {};
3839
ghostDragEnabled: boolean = true;
40+
showOriginalElementWhileDragging: boolean = false;
3941
validateDrag: ValidateDrag;
4042
dragCursor = 'move';
4143
dragActiveClass: string;
@@ -587,6 +589,17 @@ describe('draggable directive', () => {
587589
expect(draggableElement.style.visibility).not.to.be.ok;
588590
});
589591

592+
it('should create a clone of the element and leave old element visible', () => {
593+
fixture.componentInstance.showOriginalElementWhileDragging = true;
594+
fixture.detectChanges();
595+
const draggableElement =
596+
fixture.componentInstance.draggableElement.nativeElement;
597+
triggerDomEvent('mousedown', draggableElement, { clientX: 5, clientY: 10 });
598+
triggerDomEvent('mousemove', draggableElement, { clientX: 7, clientY: 10 });
599+
expect(draggableElement.style.visibility).not.to.be.ok;
600+
triggerDomEvent('mouseup', draggableElement, { clientX: 7, clientY: 8 });
601+
});
602+
590603
it('should add and remove the drag active class', () => {
591604
fixture.componentInstance.dragActiveClass = 'drag-active';
592605
fixture.detectChanges();

0 commit comments

Comments
 (0)