Skip to content

Conversation

@latekvo
Copy link
Member

@latekvo latekvo commented Aug 29, 2024

Description

Pan gesture allows for use of activateAfterLongPress modifier, which allows it to be activated after holding down the gesture down for a specified amount of time.

This modifier uses setTimeout underneath, which was not properly cleared when the gesture was disabled while being pressed down.

Closes: #3074

Test plan

  • run the attached code
  • when the button is clicked 2 times, nothing happens
  • without this PR, clicking the button 2 times results in onStart firing on the Pan gesture due to lack of timeout clearing

Attached code

  • make sure to have @shopify/flash-list installed
Collapsed code
import React from 'react';
import { Button, View } from 'react-native';
import { FlashList } from '@shopify/flash-list';
import { Gesture, GestureDetector } from 'react-native-gesture-handler';

export default function HomeScreen() {
  const [enabled, setEnabled] = React.useState(true);
  const data = [0];

  const panGesture = Gesture.Pan()
    .enabled(enabled)
    .activateAfterLongPress(750)
    .onStart(() => {
      console.warn('pan triggered');
    });

  return (
    <GestureDetector gesture={panGesture}>
      <FlashList
        data={data}
        ListHeaderComponent={
          <Button
            title={enabled ? 'Disable Gestures' : 'Enable Gestures'}
            onPress={() => {
              setEnabled((prev) => !prev);
            }}
          />
        }
        renderItem={() => {
          return (
            <View
              style={{
                height: 70,
                backgroundColor: 'tomato',
              }}
            />
          );
        }}
      />
    </GestureDetector>
  );
}

Results

Collapsed results recordings

Before

Screen.Recording.2024-08-29.at.11.10.22.mov

Fixed

Screen.Recording.2024-08-29.at.11.09.38.mov

Important:

As far as I see, all the other calls to this.clearActivationTimeout() can be activated individually, and none of them are redundant.

@latekvo latekvo requested a review from j-piasecki August 29, 2024 09:20
@latekvo latekvo marked this pull request as ready for review August 29, 2024 09:20
Copy link

@frankcalise frankcalise left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works perfectly! Tested a patch on 2.18.1, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[web] Gesture.Pan() fires onStart when toggling enabled

4 participants