|
| 1 | +import React, {PropsWithChildren, useCallback, useMemo} from 'react'; |
| 2 | +import {LayoutChangeEvent} from 'react-native'; |
| 3 | +import Reanimated, { |
| 4 | + useAnimatedGestureHandler, |
| 5 | + useAnimatedStyle, |
| 6 | + useSharedValue, |
| 7 | + withTiming, |
| 8 | + interpolate, |
| 9 | + interpolateColor, |
| 10 | + runOnJS |
| 11 | +} from 'react-native-reanimated'; |
| 12 | +import {TapGestureHandler, LongPressGestureHandler, State} from 'react-native-gesture-handler'; |
| 13 | +import {asBaseComponent, forwardRef, BaseComponentInjectedProps, ForwardRefInjectedProps} from '../commons/new'; |
| 14 | +import {ViewProps} from '../components/view'; |
| 15 | + |
| 16 | +export type TouchableOpacityProps = { |
| 17 | + /** |
| 18 | + * Background color |
| 19 | + */ |
| 20 | + backgroundColor?: string; |
| 21 | + /** |
| 22 | + * Background color when actively pressing the touchable |
| 23 | + */ |
| 24 | + feedbackColor?: string; |
| 25 | + /** |
| 26 | + * Opacity value when actively pressing the touchable |
| 27 | + */ |
| 28 | + activeOpacity?: number; |
| 29 | + /** |
| 30 | + * Scale value when actively pressing the touchable |
| 31 | + */ |
| 32 | + activeScale?: number; |
| 33 | + /** |
| 34 | + * Callback for when tapping the touchable |
| 35 | + */ |
| 36 | + onPress?: (props: any) => void; |
| 37 | + /** |
| 38 | + * Callback for when long pressing the touchable |
| 39 | + */ |
| 40 | + onLongPress?: (props: any) => void; |
| 41 | + /** |
| 42 | + * Pass controlled pressState to track gesture state changes |
| 43 | + */ |
| 44 | + pressState?: State; |
| 45 | + /** |
| 46 | + * If true, disable all interactions for this component. |
| 47 | + */ |
| 48 | + disabled?: boolean; |
| 49 | + /** |
| 50 | + * Pass custom style |
| 51 | + */ |
| 52 | + style?: ViewProps['style']; |
| 53 | + /** |
| 54 | + * Custom value of any type to pass on to TouchableOpacity and receive back in onPress callback |
| 55 | + */ |
| 56 | + customValue?: any; |
| 57 | + onLayout?: (event: LayoutChangeEvent) => void; |
| 58 | + testID?: string; |
| 59 | +}; |
| 60 | + |
| 61 | +type Props = PropsWithChildren<TouchableOpacityProps & BaseComponentInjectedProps & ForwardRefInjectedProps>; |
| 62 | + |
| 63 | +/** |
| 64 | + * @description: a Better, enhanced TouchableOpacity component |
| 65 | + * @modifiers: flex, margin, padding, background |
| 66 | + * @example: https://github.com/wix/react-native-ui-lib/blob/master/demo/src/screens/incubatorScreens/TouchableOpacityScreen.js |
| 67 | + */ |
| 68 | +function TouchableOpacity(props: Props) { |
| 69 | + const { |
| 70 | + children, |
| 71 | + modifiers, |
| 72 | + style, |
| 73 | + disabled, |
| 74 | + forwardedRef, |
| 75 | + feedbackColor, |
| 76 | + activeOpacity = 0.2, |
| 77 | + activeScale = 1, |
| 78 | + ...others |
| 79 | + } = props; |
| 80 | + const {borderRadius, paddings, margins, alignments, flexStyle} = modifiers; |
| 81 | + |
| 82 | + const isActive = useSharedValue(0); |
| 83 | + /* This flag is for fixing an issue with long press triggering twice |
| 84 | + TODO: Consider revisiting this issue to see if it still occurs */ |
| 85 | + const isLongPressed = useSharedValue(false); |
| 86 | + |
| 87 | + const backgroundColor = useMemo(() => { |
| 88 | + return props.backgroundColor || modifiers.backgroundColor; |
| 89 | + }, [props.backgroundColor, modifiers.backgroundColor]); |
| 90 | + |
| 91 | + const onPress = useCallback(() => { |
| 92 | + props.onPress?.(props); |
| 93 | + }, [props.onPress, props.customValue]); |
| 94 | + |
| 95 | + const onLongPress = useCallback(() => { |
| 96 | + props.onLongPress?.(props); |
| 97 | + }, [props.onLongPress, props.customValue]); |
| 98 | + |
| 99 | + const toggleActive = (value: number) => { |
| 100 | + 'worklet'; |
| 101 | + isActive.value = withTiming(value, {duration: 200}); |
| 102 | + }; |
| 103 | + |
| 104 | + const tapGestureHandler = useAnimatedGestureHandler({ |
| 105 | + onStart: () => { |
| 106 | + toggleActive(1); |
| 107 | + }, |
| 108 | + onEnd: () => { |
| 109 | + toggleActive(0); |
| 110 | + |
| 111 | + runOnJS(onPress)(); |
| 112 | + }, |
| 113 | + onFail: () => { |
| 114 | + toggleActive(0); |
| 115 | + } |
| 116 | + }); |
| 117 | + |
| 118 | + const longPressGestureHandler = useAnimatedGestureHandler({ |
| 119 | + onActive: () => { |
| 120 | + if (!isLongPressed.value) { |
| 121 | + isLongPressed.value = true; |
| 122 | + toggleActive(0); |
| 123 | + runOnJS(onLongPress)(); |
| 124 | + } |
| 125 | + }, |
| 126 | + onFinish: () => { |
| 127 | + isLongPressed.value = false; |
| 128 | + } |
| 129 | + }); |
| 130 | + |
| 131 | + const animatedStyle = useAnimatedStyle(() => { |
| 132 | + const activeColor = feedbackColor || backgroundColor; |
| 133 | + const opacity = interpolate(isActive.value, [0, 1], [1, activeOpacity]); |
| 134 | + const scale = interpolate(isActive.value, [0, 1], [1, activeScale]); |
| 135 | + |
| 136 | + return { |
| 137 | + backgroundColor: interpolateColor(isActive.value, [0, 1], [backgroundColor, activeColor]), |
| 138 | + opacity, |
| 139 | + transform: [{scale}] |
| 140 | + }; |
| 141 | + }, [backgroundColor, feedbackColor]); |
| 142 | + |
| 143 | + return ( |
| 144 | + <TapGestureHandler |
| 145 | + // @ts-expect-error |
| 146 | + onGestureEvent={tapGestureHandler} |
| 147 | + shouldCancelWhenOutside |
| 148 | + enabled={!disabled} |
| 149 | + > |
| 150 | + <Reanimated.View> |
| 151 | + {/* @ts-expect-error */} |
| 152 | + <LongPressGestureHandler onGestureEvent={longPressGestureHandler} shouldCancelWhenOutside> |
| 153 | + <Reanimated.View |
| 154 | + {...others} |
| 155 | + ref={forwardedRef} |
| 156 | + style={[ |
| 157 | + borderRadius && {borderRadius}, |
| 158 | + flexStyle, |
| 159 | + paddings, |
| 160 | + margins, |
| 161 | + alignments, |
| 162 | + backgroundColor && {backgroundColor}, |
| 163 | + style, |
| 164 | + animatedStyle |
| 165 | + ]} |
| 166 | + > |
| 167 | + {children} |
| 168 | + </Reanimated.View> |
| 169 | + </LongPressGestureHandler> |
| 170 | + </Reanimated.View> |
| 171 | + </TapGestureHandler> |
| 172 | + ); |
| 173 | +} |
| 174 | + |
| 175 | +TouchableOpacity.displayName = 'Incubator.TouchableOpacity'; |
| 176 | + |
| 177 | +export default asBaseComponent<TouchableOpacityProps>(forwardRef<Props>(TouchableOpacity)); |
0 commit comments