fix(mobile): remove GestureDetector on ItemPressable (#3544)

This commit is contained in:
Whitewater 2025-04-21 16:46:45 +08:00 committed by GitHub
parent 2bbb00c9fa
commit 7e2be7ba2f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 31 additions and 44 deletions

View File

@ -1,10 +1,9 @@
import { useTypeScriptHappyCallback } from "@follow/hooks"
import { cn, composeEventHandlers } from "@follow/utils"
import type { FC } from "react"
import { Fragment, memo, useEffect, useMemo, useRef, useState } from "react"
import { Fragment, memo, useEffect, useState } from "react"
import type { PressableProps } from "react-native"
import { StyleSheet } from "react-native"
import { Gesture, GestureDetector } from "react-native-gesture-handler"
import Animated, {
cancelAnimation,
interpolateColor,
@ -53,51 +52,39 @@ export const ItemPressable: FC<ItemPressableProps> = memo(
}
})
const timerRef = useRef<NodeJS.Timeout | null>(null)
const tapGesture = useMemo(() => {
return Gesture.Tap()
.numberOfTaps(1)
.maxDuration(100)
.runOnJS(true)
.onStart(() => {
setIsPressing(true)
timerRef.current && clearTimeout(timerRef.current)
timerRef.current = setTimeout(() => {
setIsPressing(false)
}, 100)
})
}, [setIsPressing])
const isUnStyled = itemStyle === ItemPressableStyle.UnStyled
return (
<GestureDetector gesture={tapGesture}>
<ReAnimatedPressable
{...props}
// This is a workaround to prevent context menu crash when release too quickly
// https://github.com/nandorojo/zeego/issues/61
onLongPress={composeEventHandlers(props.onLongPress, () => {})}
delayLongPress={props.delayLongPress ?? 100}
className={cn("relative overflow-hidden", props.className)}
style={StyleSheet.flatten([
props.style,
!isUnStyled && { backgroundColor: itemNormalColor },
])}
>
{useTypeScriptHappyCallback(
(props) => {
return (
<Fragment>
{touchHighlight && (
<Animated.View className="absolute inset-0" style={colorStyle} />
)}
{typeof children === "function" ? children(props) : children}
</Fragment>
)
},
[children, colorStyle],
)}
</ReAnimatedPressable>
</GestureDetector>
<ReAnimatedPressable
{...props}
onPressIn={composeEventHandlers(props.onPressIn, () => setIsPressing(true))}
onPressOut={composeEventHandlers(props.onPressOut, () => setIsPressing(false))}
onHoverIn={composeEventHandlers(props.onHoverIn, () => setIsPressing(true))}
onHoverOut={composeEventHandlers(props.onHoverOut, () => setIsPressing(false))}
// This is a workaround to prevent context menu crash when release too quickly
// https://github.com/nandorojo/zeego/issues/61
onLongPress={composeEventHandlers(props.onLongPress, () => {})}
delayLongPress={props.delayLongPress ?? 100}
className={cn("relative overflow-hidden", props.className)}
style={StyleSheet.flatten([
props.style,
!isUnStyled && { backgroundColor: itemNormalColor },
])}
>
{useTypeScriptHappyCallback(
(props) => {
return (
<Fragment>
{touchHighlight && (
<Animated.View className="absolute inset-0" style={colorStyle} />
)}
{typeof children === "function" ? children(props) : children}
</Fragment>
)
},
[children, colorStyle],
)}
</ReAnimatedPressable>
)
},
)