about summary refs log tree commit diff
path: root/src/view/com/pager/FixedTouchableHighlight.tsx
blob: d071969757a30e2821eca016f67aca9a221a133f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// FixedTouchableHighlight.tsx
import React, {ComponentProps, useRef} from 'react'
import {GestureResponderEvent, TouchableHighlight} from 'react-native'

type Position = {pageX: number; pageY: number}

export default function FixedTouchableHighlight({
  onPress,
  onPressIn,
  ...props
}: ComponentProps<typeof TouchableHighlight>) {
  const _touchActivatePositionRef = useRef<Position | null>(null)

  function _onPressIn(e: GestureResponderEvent) {
    const {pageX, pageY} = e.nativeEvent

    _touchActivatePositionRef.current = {
      pageX,
      pageY,
    }

    onPressIn?.(e)
  }

  function _onPress(e: GestureResponderEvent) {
    const {pageX, pageY} = e.nativeEvent

    const absX = Math.abs(_touchActivatePositionRef.current?.pageX! - pageX)
    const absY = Math.abs(_touchActivatePositionRef.current?.pageY! - pageY)

    const dragged = absX > 2 || absY > 2
    if (!dragged) {
      onPress?.(e)
    }
  }

  return (
    <TouchableHighlight onPressIn={_onPressIn} onPress={_onPress} {...props}>
      {props.children}
    </TouchableHighlight>
  )
}