-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tooltip): remove tooltip on touch scroll
Remove tooltip on touch-devices if user scrolls more than 150 pixels.
- Loading branch information
1 parent
2842e1c
commit 4bdde64
Showing
3 changed files
with
88 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './useTouchScrollDistance' |
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,70 @@ | ||
import { useState, useEffect, useLayoutEffect } from 'react' | ||
|
||
export const useTouchScrollDistance = () => { | ||
const [origin, setOrigin] = useState<TouchList | null>(null) | ||
const [touches, setTouches] = useState<TouchList | null>(null) | ||
/** | ||
* The distance between touch origin and touch current for both | ||
* x-axis and y-axis | ||
*/ | ||
const [touchScrollDistance, setTouchScrollDistance] = useState({ x: 0, y: 0 }) | ||
|
||
useEffect(() => { | ||
const touchStartHandler = (event: TouchEvent) => { | ||
if (origin === null) { | ||
setOrigin(event.touches) | ||
} | ||
} | ||
|
||
const touchMoveHandler = (event: TouchEvent) => | ||
setTouches(event.changedTouches) | ||
|
||
const touchEndHandler = (event: TouchEvent) => { | ||
if (event.touches.length === 0) { | ||
setOrigin(null) | ||
setTouches(null) | ||
setTouchScrollDistance({ x: 0, y: 0 }) | ||
} | ||
} | ||
|
||
const touchCancelHandler = () => { | ||
setOrigin(null) | ||
setTouches(null) | ||
setTouchScrollDistance({ x: 0, y: 0 }) | ||
} | ||
|
||
document.addEventListener('touchstart', touchStartHandler) | ||
document.addEventListener('touchmove', touchMoveHandler) | ||
document.addEventListener('touchend', touchEndHandler) | ||
document.addEventListener('touchcancel', touchCancelHandler) | ||
|
||
return () => { | ||
document.removeEventListener('touchstart', touchStartHandler) | ||
document.removeEventListener('touchmove', touchMoveHandler) | ||
document.removeEventListener('touchend', touchEndHandler) | ||
document.removeEventListener('touchcancel', touchCancelHandler) | ||
} | ||
}, [origin]) | ||
|
||
/** | ||
* Calculates the distance in pixels between the origin of | ||
* a touch event and position updates to that touch event. | ||
*/ | ||
useLayoutEffect(() => { | ||
if (origin === null || touches === null) { | ||
return | ||
} | ||
|
||
// User is not scrolling | ||
if (touches.length > 1) { | ||
return | ||
} | ||
|
||
const deltaX = touches[0].clientX - origin[0].clientX | ||
const deltaY = touches[0].clientY - origin[0].clientY | ||
|
||
setTouchScrollDistance({ x: deltaX, y: deltaY }) | ||
}, [origin, touches]) | ||
|
||
return touchScrollDistance | ||
} |