🎨 Improve useLongPress hook

This commit is contained in:
SrGooglo 2023-05-09 21:36:11 +00:00
parent 68c577079c
commit 9329826ce3
2 changed files with 22 additions and 15 deletions

View File

@ -5,7 +5,7 @@ export default (
onClick, onClick,
{ {
shouldPreventDefault = true, shouldPreventDefault = true,
delay = 300, delay = app.cores.settings.get("longPressDelay") ?? 500,
onTouchStart, onTouchStart,
onTouchEnd, onTouchEnd,
} = {} } = {}
@ -28,17 +28,25 @@ export default (
} }
timeout.current = setTimeout(() => { timeout.current = setTimeout(() => {
if (typeof onLongPress === "function") {
onLongPress(event) onLongPress(event)
}
setLongPressTriggered(true) setLongPressTriggered(true)
}, delay) }, delay)
}, },
[onLongPress, delay, shouldPreventDefault] [onLongPress, delay, shouldPreventDefault]
) )
const clear = useCallback( const clear = useCallback((event, shouldTriggerClick = true) => {
(event, shouldTriggerClick = true) => { if (timeout.current) {
timeout.current && clearTimeout(timeout.current) clearTimeout(timeout.current)
shouldTriggerClick && !longPressTriggered && onClick() }
if (shouldTriggerClick && !longPressTriggered && typeof onClick === "function") {
onClick()
}
setLongPressTriggered(false) setLongPressTriggered(false)
if (typeof onTouchEnd === "function") { if (typeof onTouchEnd === "function") {

View File

@ -1,4 +1,3 @@
export { default as useLongPress } from "./useLongPress"
export { default as findChildById } from "./findChildById" export { default as findChildById } from "./findChildById"
export { default as cursorPosition } from "./cursorPosition" export { default as cursorPosition } from "./cursorPosition"
export { default as getBase64 } from "./getBase64" export { default as getBase64 } from "./getBase64"