mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-11 03:24:16 +00:00
🎨 Improve useLongPress
hook
This commit is contained in:
parent
68c577079c
commit
9329826ce3
@ -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,27 +28,35 @@ export default (
|
|||||||
}
|
}
|
||||||
|
|
||||||
timeout.current = setTimeout(() => {
|
timeout.current = setTimeout(() => {
|
||||||
onLongPress(event)
|
if (typeof onLongPress === "function") {
|
||||||
|
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()
|
}
|
||||||
setLongPressTriggered(false)
|
|
||||||
|
|
||||||
if (typeof onTouchEnd === "function") {
|
if (shouldTriggerClick && !longPressTriggered && typeof onClick === "function") {
|
||||||
onTouchEnd()
|
onClick()
|
||||||
}
|
}
|
||||||
|
|
||||||
if (shouldPreventDefault && target.current) {
|
setLongPressTriggered(false)
|
||||||
target.current.removeEventListener("touchend", preventDefault)
|
|
||||||
}
|
if (typeof onTouchEnd === "function") {
|
||||||
},
|
onTouchEnd()
|
||||||
|
}
|
||||||
|
|
||||||
|
if (shouldPreventDefault && target.current) {
|
||||||
|
target.current.removeEventListener("touchend", preventDefault)
|
||||||
|
}
|
||||||
|
},
|
||||||
[shouldPreventDefault, onClick, longPressTriggered]
|
[shouldPreventDefault, onClick, longPressTriggered]
|
||||||
)
|
)
|
||||||
|
|
@ -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"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user