add press up handler

This commit is contained in:
Liam DeBeasi
2019-11-07 15:20:49 -05:00
parent 3b52074a10
commit ad747b05f1

View File

@ -4,7 +4,8 @@ export interface PressRecognizerOptions {
el: HTMLElement;
time: number;
threshold: number;
onPressHandler: () => void;
onPressHandler?: () => void;
onPressUpHandler?: () => void;
}
export const createPressRecognizer = (opts: PressRecognizerOptions) => {
@ -15,7 +16,10 @@ export const createPressRecognizer = (opts: PressRecognizerOptions) => {
clearGestureTimeout();
timeout = setTimeout(() => {
opts.onPressHandler();
if (opts.onPressHandler) {
opts.onPressHandler();
}
clearGestureTimeout();
}, opts.time || 500);
};
@ -28,6 +32,14 @@ export const createPressRecognizer = (opts: PressRecognizerOptions) => {
clearGestureTimeout();
};
const onEnd = () => {
if (opts.onPressUpHandler) {
opts.onPressUpHandler();
}
clearGestureTimeout();
};
const clearGestureTimeout = () => {
if (timeout) {
clearTimeout(timeout);
@ -41,6 +53,6 @@ export const createPressRecognizer = (opts: PressRecognizerOptions) => {
threshold: 0,
onStart,
onMove,
onEnd: () => clearGestureTimeout()
onEnd
});
};