Skip to content

Instantly share code, notes, and snippets.

@amcdnl
Last active July 10, 2018 10:09
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save amcdnl/32a5676ef5b4489ee5fbadf632642e01 to your computer and use it in GitHub Desktop.
Save amcdnl/32a5676ef5b4489ee5fbadf632642e01 to your computer and use it in GitHub Desktop.
import {
Directive,
Output,
EventEmitter,
HostBinding,
HostListener
} from 'angular2/core.js';
@Directive({
selector: '[long-press]'
})
export class LongPressButton {
@Output()
onLongPress = new EventEmitter();
@Output()
onLongPressing = new EventEmitter();
@HostBinding('class.press')
get press() { return this._pressing; }
@HostBinding('class.longpress')
get longPress() { return this._longPressing; }
@HostListener('mousedown', ['$event'])
onMouseDown(event) {
this._pressing = true;
this._longPressing = false;
this._timeout = setTimeout(() => {
this._longPressing = true;
this.onLongPress.emit(event);
this._interval = setInterval(() => {
this.onLongPressing.emit(event);
}, 50);
}, 300);
}
@HostListener('mouseup')
onMouseUp = () => this.endPress()
@HostListener('mouseleave')
onMouseLeave = () => this.endPress()
endPress() {
clearTimeout(this._timeout);
clearInterval(this._interval);
this._longPressing = false;
this._pressing = false;
}
}
<button
long-press
(onLongPressing)="myLongContinousPressFn()"
(onLongPress)="myLongPressFn()"
/>
@leoiii12
Copy link

https://gist.github.com/leoiii12/7579bbcc7d5213203f63a476e729197c

This revision capable of touching on mobile.

@nnajm
Copy link

nnajm commented Jul 10, 2018

Hello, what is the license of this gist?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment