Skip to content

Commit 50d1962

Browse files
author
Matt Lewis
committed
feat(draggable): auto change the cursor to the move icon on hover
Closes #9
1 parent 3445337 commit 50d1962

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

demo/demo.component.ts

-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import {Component} from '@angular/core';
2424
background-color: red;
2525
width: 200px;
2626
height: 200px;
27-
cursor: move;
2827
position: relative;
2928
z-index: 1;
3029
float: left;

src/draggable.directive.ts

+25
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ export type DragAxis = {x: boolean, y: boolean};
1414

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

17+
const MOVE_CURSOR: string = 'move';
18+
1719
@Directive({
1820
selector: '[mwlDraggable]'
1921
})
@@ -48,6 +50,7 @@ export class Draggable implements OnInit, OnDestroy {
4850
.flatMap((mouseDownEvent: MouseEvent) => {
4951

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

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

152+
/**
153+
* @private
154+
*/
155+
@HostListener('mouseenter')
156+
onMouseEnter(): void {
157+
if (this.canDrag()) {
158+
this.setCursor(MOVE_CURSOR);
159+
}
160+
}
161+
162+
/**
163+
* @private
164+
*/
165+
@HostListener('mouseleave')
166+
onMouseLeave(): void {
167+
this.setCursor(null);
168+
}
169+
149170
private setCssTransform(value: string): void {
150171
if (this.ghostDragEnabled) {
151172
this.renderer.setElementStyle(this.element.nativeElement, 'transform', value);
@@ -160,4 +181,8 @@ export class Draggable implements OnInit, OnDestroy {
160181
return this.dragAxis.x || this.dragAxis.y;
161182
}
162183

184+
private setCursor(value: string): void {
185+
this.renderer.setElementStyle(this.element.nativeElement, 'cursor', value);
186+
}
187+
163188
}

test/draggable.directive.spec.ts

+16
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,20 @@ describe('draggable directive', () => {
191191
expect(draggableElement.style.transform).not.to.ok;
192192
});
193193

194+
it('should auto set the mouse cursor to the move icon on hover', () => {
195+
const draggableElement: HTMLElement = fixture.componentInstance.draggable.element.nativeElement;
196+
triggerDomEvent('mouseenter', draggableElement);
197+
expect(draggableElement.style.cursor).to.equal('move');
198+
triggerDomEvent('mouseleave', draggableElement);
199+
expect(draggableElement.style.cursor).not.to.be.ok;
200+
});
201+
202+
it('should not set the mouse cursor when the element cant be dragged', () => {
203+
fixture.componentInstance.dragAxis = {x: false, y: false};
204+
fixture.detectChanges();
205+
const draggableElement: HTMLElement = fixture.componentInstance.draggable.element.nativeElement;
206+
triggerDomEvent('mouseenter', draggableElement);
207+
expect(draggableElement.style.cursor).not.to.be.ok;
208+
});
209+
194210
});

0 commit comments

Comments
 (0)