Skip to content

Commit

Permalink
Fix: onPressIn and onPressOut are triggered at the same time on Andro…
Browse files Browse the repository at this point in the history
…id (#3286)

## Description

<!--
Description and motivation for this PR.

Include 'Fixes #<number>' if this is fixing some issue.
-->

Fixes #3280 

The `pressInHandler` method
[returns](https://github.com/software-mansion/react-native-gesture-handler/blob/main/src/components/Pressable/Pressable.tsx#L122-L124)
early if the `isTouchPropagationAllowed` is `false`. On Android
`pressInHandler` is called initially before the
`isTouchPropagationAllowed` is set to `true` in
[buttonGesture](https://github.com/software-mansion/react-native-gesture-handler/blob/main/src/components/Pressable/Pressable.tsx#L305-L307).
Changing the order of gestures seems to fix it.

## Test plan

<!--
Describe how did you test this change here.
-->

<details>

<summary>Code</summary>

```ts
import * as React from 'react';
import {
  View,
  Text,
  Pressable as RNPressable,
  SafeAreaView,
  StyleSheet,
} from 'react-native';
import {
  GestureHandlerRootView,
  Pressable as RNGHPressable,
} from 'react-native-gesture-handler';

export default function HomeScreen() {
  return (
    <GestureHandlerRootView>
      <SafeAreaView style={{ flex: 1 }}>
        <View style={styles.container}>
          <RNGHPressable
            style={styles.pressableStyles}
            onPressIn={() => console.log(JSON.stringify('RNGH: onPressIn'))}
            onPressOut={() => console.log(JSON.stringify('RNGH: onPressOut'))}
            onPress={() => console.log(JSON.stringify('RNGH: onPress'))}>
            <Text style={styles.pressableText}>RNGH Pressable</Text>
          </RNGHPressable>
          <RNPressable
            style={styles.pressableStyles}
            onPressIn={() => console.log(JSON.stringify('RN: onPressIn'))}
            onPressOut={() => console.log(JSON.stringify('RN: onPressOut'))}
            onPress={() => console.log(JSON.stringify('RN: onPress'))}>
            <Text style={styles.pressableText}>RN Pressable</Text>
          </RNPressable>
        </View>
      </SafeAreaView>
    </GestureHandlerRootView>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    flexDirection: 'row',
    gap: 10,
    alignItems: 'center',
    justifyContent: 'center',
  },
  pressableStyles: {
    backgroundColor: '#3BE7BB',
    paddingVertical: 10,
    paddingHorizontal: 15,
    height: 50,
    alignItems: 'center',
    justifyContent: 'center',
  },
  pressableText: {
    color: '#000',
    fontSize: 16,
    fontWeight: 'bold',
  },
});

```

</details>
  • Loading branch information
coado authored Dec 13, 2024
1 parent 705419f commit 1665fc9
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/components/Pressable/Pressable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ export default function Pressable(props: PressableProps) {

const isPressableEnabled = disabled !== true;

const gestures = [pressAndTouchGesture, hoverGesture, buttonGesture];
const gestures = [buttonGesture, pressAndTouchGesture, hoverGesture];

for (const gesture of gestures) {
gesture.enabled(isPressableEnabled);
Expand Down

0 comments on commit 1665fc9

Please sign in to comment.