-
Notifications
You must be signed in to change notification settings - Fork 342
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Continous update of the counter when keep clicked * Refactor elements to span multiple lines with one attribute per line * Refactor: Moved logic to compostables * chore: eslint fixes * chore: refactor useLongPress composable * fix(counter): correctly use useLongPress composable * fix(counter): add long-press-delay prop --------- Co-authored-by: Maksim Nedoshev <m0ksem1337@gmail.com>
- Loading branch information
Showing
4 changed files
with
74 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { Ref, ShallowRef, unref } from 'vue' | ||
import { useEvent } from './useEvent' | ||
import { MaybeElement, useHTMLElement } from '.' | ||
|
||
type MaybeRef<T> = Ref<T> | T | ||
|
||
interface LongPressOptions { | ||
onStart: () => void; | ||
onEnd: () => void; | ||
onUpdate: () => void; | ||
delay?: MaybeRef<number>; | ||
interval?: number; | ||
} | ||
|
||
export function useLongPress (el: ShallowRef<HTMLElement | undefined>, options: Partial<LongPressOptions>) { | ||
let timeoutId = -1 | ||
let intervalId = -1 | ||
|
||
const handleMouseDown = () => { | ||
options.onStart?.() | ||
timeoutId = setTimeout(() => { | ||
intervalId = setInterval(() => options.onUpdate?.(), options.interval || 100) as any | ||
}, unref(options.delay) || 500) as unknown as number | ||
} | ||
|
||
const handleMouseUp = () => { | ||
clearTimeout(timeoutId) | ||
clearInterval(intervalId) | ||
options.onEnd?.() | ||
} | ||
|
||
const htmlElement = useHTMLElement(el) | ||
|
||
useEvent('mousedown', handleMouseDown, htmlElement) | ||
useEvent('mouseup', handleMouseUp, htmlElement) | ||
} |