Skip to content

Commit 4e8a701

Browse files
authored
Fix/incubator touch2 long press (#1435)
* Fix missing behaviour on long-press, active opacity was dismissed * added RN-touchable-opacity to demo for reference
1 parent 735c45a commit 4e8a701

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

demo/src/screens/incubatorScreens/TouchableOpacityScreen.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React, {Component} from 'react';
2+
import {TouchableOpacity} from 'react-native';
23
import {View, Text, Colors, Incubator} from 'react-native-ui-lib';
34

45
class TouchableOpacityScreen extends Component {
@@ -35,6 +36,24 @@ class TouchableOpacityScreen extends Component {
3536
);
3637
}
3738

39+
renderRNTouchableExample = () => {
40+
return (
41+
<View row centerV marginT-20>
42+
<Text marginR-14>RN TouchableOpacity</Text>
43+
<TouchableOpacity
44+
onPress={() => {
45+
console.warn('onPress');
46+
}}
47+
onLongPress={() => {
48+
console.warn('onLongPress');
49+
}}
50+
>
51+
<View style={{backgroundColor: Colors.green40, width: 100, height: 34, borderRadius: 22}}/>
52+
</TouchableOpacity>
53+
</View>
54+
);
55+
};
56+
3857
render() {
3958
const {counter, longPressCounter} = this.state;
4059
return (
@@ -64,6 +83,7 @@ class TouchableOpacityScreen extends Component {
6483
>
6584
<Text white>TouchableOpacity2</Text>
6685
</Incubator.TouchableOpacity2>
86+
{this.renderRNTouchableExample()}
6787
</View>
6888
);
6989
}

src/incubator/TouchableOpacity2.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import Reanimated, {
1111
} from 'react-native-reanimated';
1212
import {TapGestureHandler, LongPressGestureHandler, State} from 'react-native-gesture-handler';
1313
import {asBaseComponent, forwardRef, BaseComponentInjectedProps, ForwardRefInjectedProps} from '../commons/new';
14-
import {ViewProps} from '../components/view';
14+
import View, {ViewProps} from '../components/view';
1515

1616
export type TouchableOpacityProps = {
1717
/**
@@ -107,23 +107,24 @@ function TouchableOpacity(props: Props) {
107107
},
108108
onEnd: () => {
109109
toggleActive(0);
110-
111110
runOnJS(onPress)();
112111
},
113112
onFail: () => {
114-
toggleActive(0);
113+
if (!isLongPressed.value) {
114+
toggleActive(0);
115+
}
115116
}
116117
});
117118

118119
const longPressGestureHandler = useAnimatedGestureHandler({
119120
onActive: () => {
120121
if (!isLongPressed.value) {
121122
isLongPressed.value = true;
122-
toggleActive(0);
123123
runOnJS(onLongPress)();
124124
}
125125
},
126126
onFinish: () => {
127+
toggleActive(0);
127128
isLongPressed.value = false;
128129
}
129130
});
@@ -140,6 +141,8 @@ function TouchableOpacity(props: Props) {
140141
};
141142
}, [backgroundColor, feedbackColor]);
142143

144+
const Container = props.onLongPress ? LongPressGestureHandler : View;
145+
143146
return (
144147
<TapGestureHandler
145148
// @ts-expect-error
@@ -149,7 +152,7 @@ function TouchableOpacity(props: Props) {
149152
>
150153
<Reanimated.View>
151154
{/* @ts-expect-error */}
152-
<LongPressGestureHandler onGestureEvent={longPressGestureHandler} shouldCancelWhenOutside>
155+
<Container onGestureEvent={longPressGestureHandler} shouldCancelWhenOutside>
153156
<Reanimated.View
154157
{...others}
155158
ref={forwardedRef}
@@ -166,7 +169,7 @@ function TouchableOpacity(props: Props) {
166169
>
167170
{children}
168171
</Reanimated.View>
169-
</LongPressGestureHandler>
172+
</Container>
170173
</Reanimated.View>
171174
</TapGestureHandler>
172175
);

0 commit comments

Comments
 (0)