Skip to content

Commit

Permalink
feat(draggable): auto change the cursor to the move icon on hover
Browse files Browse the repository at this point in the history
Closes #9
  • Loading branch information
Matt Lewis committed Dec 10, 2016
1 parent 3445337 commit 50d1962
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
1 change: 0 additions & 1 deletion demo/demo.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {Component} from '@angular/core';
background-color: red;
width: 200px;
height: 200px;
cursor: move;
position: relative;
z-index: 1;
float: left;
Expand Down
25 changes: 25 additions & 0 deletions src/draggable.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export type DragAxis = {x: boolean, y: boolean};

export type SnapGrid = {x?: number, y?: number};

const MOVE_CURSOR: string = 'move';

@Directive({
selector: '[mwlDraggable]'
})
Expand Down Expand Up @@ -48,6 +50,7 @@ export class Draggable implements OnInit, OnDestroy {
.flatMap((mouseDownEvent: MouseEvent) => {

this.dragStart.next({x: 0, y: 0});
this.setCursor(MOVE_CURSOR);

if (this.ghostDragEnabled) {
this.renderer.setElementStyle(this.element.nativeElement, 'pointerEvents', 'none');
Expand Down Expand Up @@ -146,6 +149,24 @@ export class Draggable implements OnInit, OnDestroy {
this.mouseUp.next(event);
}

/**
* @private
*/
@HostListener('mouseenter')
onMouseEnter(): void {
if (this.canDrag()) {
this.setCursor(MOVE_CURSOR);
}
}

/**
* @private
*/
@HostListener('mouseleave')
onMouseLeave(): void {
this.setCursor(null);
}

private setCssTransform(value: string): void {
if (this.ghostDragEnabled) {
this.renderer.setElementStyle(this.element.nativeElement, 'transform', value);
Expand All @@ -160,4 +181,8 @@ export class Draggable implements OnInit, OnDestroy {
return this.dragAxis.x || this.dragAxis.y;
}

private setCursor(value: string): void {
this.renderer.setElementStyle(this.element.nativeElement, 'cursor', value);
}

}
16 changes: 16 additions & 0 deletions test/draggable.directive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,20 @@ describe('draggable directive', () => {
expect(draggableElement.style.transform).not.to.ok;
});

it('should auto set the mouse cursor to the move icon on hover', () => {
const draggableElement: HTMLElement = fixture.componentInstance.draggable.element.nativeElement;
triggerDomEvent('mouseenter', draggableElement);
expect(draggableElement.style.cursor).to.equal('move');
triggerDomEvent('mouseleave', draggableElement);
expect(draggableElement.style.cursor).not.to.be.ok;
});

it('should not set the mouse cursor when the element cant be dragged', () => {
fixture.componentInstance.dragAxis = {x: false, y: false};
fixture.detectChanges();
const draggableElement: HTMLElement = fixture.componentInstance.draggable.element.nativeElement;
triggerDomEvent('mouseenter', draggableElement);
expect(draggableElement.style.cursor).not.to.be.ok;
});

});

0 comments on commit 50d1962

Please sign in to comment.