Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PlatformConstants no longer a member of NativeModules (breaks ForceTouchGestureHandler) #1210

Closed
drewandre opened this issue Oct 13, 2020 · 3 comments · Fixed by #1211
Closed
Assignees
Labels
Bug Platform: iOS This issue is specific to iOS

Comments

@drewandre
Copy link
Contributor

Description

ForceTouchGestureHandler was not working on my iPhone XS Max (claiming it was unavailable on the platform which is untrue) and after digging into it I can see that in PlatformConstants.js, PlatformConstants is no longer a member of react-native's NativeModules export. Because we're querying PlatformConstants.forceTouchAvailable in Gestures.js, this results in <ForceTouchFallback /> being rendered rather than the actual gesture handler.

I wasn't easily able to see which RN version broke this, but since Gestures.js is the only file that imports PlatformConstants.js, I think we could just import { Platform } from 'react-native' here and query Platform.constants.forceTouchAvailable rather than conditionally import NativeModules if on RN v0.60.x, and from Platform if on newer RN versions. That being said, I imagine for future development it's nice to have a centralized PlatformConstants file. I just don't know how the software mansion team would approach this issue while maintaining backwards compatibility.

Screenshots

Steps To Reproduce

Rather than upgrading the entire react-native-gesture-library to support the latest react-native v0.63.3, I just used the reanimated v2 playground repo.

  1. git clone https://github.com/software-mansion-labs/reanimated-2-playground
  2. npm install, pod install
  3. Paste the example code below.
  4. To fix, replace import PlatformConstants from './PlatformConstants'; with import { Platform } from 'react-native'; in node_modules/react-native-gesture-handler/Gestures.js, and change both instances of PlatformConstants && PlatformConstants.forceTouchAvailable with Platform.constants.forceTouchAvailable.

Expected behavior

ForceTouchGestureHandler works as expected on force touch-capable devices running latest react-native versions.

Actual behavior

<ForceTouchFallback /> is rendered on devices that do in fact support a force touch handler.

Snack or minimal code example

Here is a force touch gesture handler example using reanimated v2, though the issue would still be present in reanimated v1:

import Animated, {
  useSharedValue,
  useAnimatedStyle,
  useAnimatedGestureHandler,
} from 'react-native-reanimated';
import { View, StyleSheet } from 'react-native';
import React from 'react';
import { ForceTouchGestureHandler } from 'react-native-gesture-handler';

export default function AnimatedStyleUpdateExample(props) {
  const force = useSharedValue(0);

  const animatedBoxStyles = useAnimatedStyle(() => {
    return {
      transform: [
        { scale: 1 + force.value }
      ]
    };
  });

  const onForceTouchGestureEvent = useAnimatedGestureHandler({
    onStart: (evt) => {
      force.value = evt.force
    },
    onActive: (evt) => {
      force.value = evt.force
    },
    onFinish: () => {
      force.value = 0
    }
  })

  return (
    <View style={styles.container}>
      <ForceTouchGestureHandler
        minForce={0}
        feedbackOnActivation
        onGestureEvent={onForceTouchGestureEvent}>
        <Animated.View style={[styles.box, animatedBoxStyles]} />
      </ForceTouchGestureHandler>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center'
  },
  box: {
    width: 100,
    height: 100,
    backgroundColor: 'red'
  }
})

Package versions

"react": "16.13.1",
"react-native": "0.63.1",
"react-native-gesture-handler": "^1.6.1",
"react-native-reanimated": "2.0.0-alpha.7",
@jkadamczyk jkadamczyk self-assigned this Oct 13, 2020
@jkadamczyk
Copy link
Contributor

Hey! I'll try to look into this. Seems like it should be fairly easy to fix 😄

@jkadamczyk
Copy link
Contributor

But if you also know how to go around it, Pull Requests are welcome, we can always correct things when the PR is open 😄

@drewandre
Copy link
Contributor Author

@jkadamczyk thanks for the speedy response -- I've opened #1211.

@jkadamczyk jkadamczyk added Bug Platform: iOS This issue is specific to iOS labels Oct 16, 2020
jakub-gonet added a commit that referenced this issue Feb 13, 2021
## Description

Fixes #1210 by importing platform constants from react-native's `Platform` API instead of `NativeModules.PlatformConstants`. ForceTouchGestureHandler is the only handler that requires this file, so this change only affects this handler. It is also backward-compatible.

I'm a bit confused why PlatformConstants would ever be used over Platform. They have a slightly different data structure, but as far as I can tell the constants are the same.

## Test plan

I tested this on the [gesture-handler example app](https://github.com/software-mansion/react-native-gesture-handler/tree/master/Example) which is running RN v0.61.2, as well as the [reanimated v2 playground repo](https://github.com/software-mansion-labs/reanimated-2-playground) which runs RN v0.63.1. I did not test on the web, but PlatformConstants is not a file required by any web-specific files. There is a PlatformConstants.web.js file, but the getter defined in this file is the same data structure as exporting `Platform.constants` in PlatformConstants.js for mobile.


Co-authored-by: Jakub Adamczyk <[email protected]>
Co-authored-by: Jakub Gonet <[email protected]>
braincore pushed a commit to braincore/react-native-gesture-handler that referenced this issue Mar 4, 2021
## Description

Fixes software-mansion#1210 by importing platform constants from react-native's `Platform` API instead of `NativeModules.PlatformConstants`. ForceTouchGestureHandler is the only handler that requires this file, so this change only affects this handler. It is also backward-compatible.

I'm a bit confused why PlatformConstants would ever be used over Platform. They have a slightly different data structure, but as far as I can tell the constants are the same.

## Test plan

I tested this on the [gesture-handler example app](https://github.com/software-mansion/react-native-gesture-handler/tree/master/Example) which is running RN v0.61.2, as well as the [reanimated v2 playground repo](https://github.com/software-mansion-labs/reanimated-2-playground) which runs RN v0.63.1. I did not test on the web, but PlatformConstants is not a file required by any web-specific files. There is a PlatformConstants.web.js file, but the getter defined in this file is the same data structure as exporting `Platform.constants` in PlatformConstants.js for mobile.


Co-authored-by: Jakub Adamczyk <[email protected]>
Co-authored-by: Jakub Gonet <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Platform: iOS This issue is specific to iOS
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants