Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

usePreventNavigation.tsx 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /* eslint-disable @typescript-eslint/ban-ts-comment */
  2. import * as React from 'react'
  3. export function usePreventNavigation(
  4. rCanvas: React.RefObject<HTMLDivElement>,
  5. width: number
  6. ): void {
  7. React.useEffect(() => {
  8. const preventGestureNavigation = (event: TouchEvent) => {
  9. event.preventDefault()
  10. }
  11. const preventNavigation = (event: TouchEvent) => {
  12. // Center point of the touch area
  13. const touchXPosition = event.touches[0].pageX
  14. // Size of the touch area
  15. const touchXRadius = event.touches[0].radiusX || 0
  16. // We set a threshold (10px) on both sizes of the screen,
  17. // if the touch area overlaps with the screen edges
  18. // it's likely to trigger the navigation. We prevent the
  19. // touchstart event in that case.
  20. if (touchXPosition - touchXRadius < 10 || touchXPosition + touchXRadius > width - 10) {
  21. event.preventDefault()
  22. }
  23. }
  24. const elm = rCanvas.current
  25. if (!elm) return () => void null
  26. elm.addEventListener('touchstart', preventGestureNavigation)
  27. // @ts-ignore
  28. elm.addEventListener('gestureend', preventGestureNavigation)
  29. // @ts-ignore
  30. elm.addEventListener('gesturechange', preventGestureNavigation)
  31. // @ts-ignore
  32. elm.addEventListener('gesturestart', preventGestureNavigation)
  33. // @ts-ignore
  34. elm.addEventListener('touchstart', preventNavigation)
  35. return () => {
  36. if (elm) {
  37. elm.removeEventListener('touchstart', preventGestureNavigation)
  38. // @ts-ignore
  39. elm.removeEventListener('gestureend', preventGestureNavigation)
  40. // @ts-ignore
  41. elm.removeEventListener('gesturechange', preventGestureNavigation)
  42. // @ts-ignore
  43. elm.removeEventListener('gesturestart', preventGestureNavigation)
  44. // @ts-ignore
  45. elm.removeEventListener('touchstart', preventNavigation)
  46. }
  47. }
  48. }, [rCanvas, width])
  49. }