Skip to content

Commit

Permalink
Update useMouseHold.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
alexburner committed Dec 18, 2022
1 parent e5220b3 commit 7e2e1f8
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/util/useMouseHold.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,13 @@ export const useMouseHold = (
const [isHolding, setIsHolding] = useState(false)

// This is in case we're on iOS,
// which fires touch THEN mouse events
// which fires touch AND mouse events
// causing double hold responses
//
// Touch is first
// So we ignore mouse
const heardTouch = useRef(false)

const onMouseDown = useCallback(() => {
if (heardTouch.current) return
setIsHolding(true)
}, [])
const onMouseUp = useCallback(() => {
if (heardTouch.current) return
setIsHolding(false)
}, [])
const onTouchStart = useCallback(() => {
heardTouch.current = true
setIsHolding(true)
Expand All @@ -35,12 +30,21 @@ export const useMouseHold = (
setIsHolding(false)
}, [])

// Leading edge callback
const onMouseDown = useCallback(() => {
if (heardTouch.current) return
setIsHolding(true)
}, [])
const onMouseUp = useCallback(() => {
if (heardTouch.current) return
setIsHolding(false)
}, [])

// Leading edge isHolding callback
useEffect(() => {
if (isHolding) callbackRef.current()
}, [isHolding])

// Repeated callbacks
// Repeated isHolding callbacks
useInterval(callbackRef.current, isHolding ? delay : null)

return {
Expand Down

0 comments on commit 7e2e1f8

Please sign in to comment.