React Native shares an experimental cross-platform Pointer Events API
The React Native team has shared an experimental cross-platform pointer API, modeled on the W3C Pointer Events specification, and is asking the community for feedback.
Background
The React Native touch event system was not designed for mouse, keyboard or pen input, which matter on Desktop, VR, touch-screen laptops and increasingly on mobile. According to the post, users of out-of-tree platforms have forked React Native or built custom native components to support features such as hover detection or left-click, producing redundant, platform-specific event props. The team chose to follow the W3C Pointer Events specification, which defines a single set of events for cross-device pointer input. The approach to the spec is described as best effort, with documented deviations.
Testing and implementation
To verify behavior, the team built a manual testing framework for porting Web Platform Tests, tentatively called the RNTester Platform Tests, which runs through RNTester. The implementation builds on the existing touch event infrastructure, using MotionEvent on Android and UITouch on iOS. On Android, events are mapped to pointer events and dispatched through JSI to the dispatchEvent method in react-native-renderer.
Implemented events: onPointerOver, onPointerEnter, onPointerDown, onPointerMove, onPointerUp, onPointerOut, onPointerLeave. onPointerCancel is in progress, while onClick, onContextMenu, onGotPointerCapture, onLostPointerCapture and onPointerRawUpdate are yet to be implemented. Most PointerEvent properties are available via event.nativeEvent, with relatedTarget a notable exception.
Planned future work includes the Pointer Capture API (setPointerCapture(), releasePointerCapture(), hasPointerCapture()), a touch-action style property, and click, contextmenu and auxclick. The team is also exploring an EventTarget implementation for host components, and the native implementation will allow it to revisit gesture handling in the Responder, Pressability and PanResponder APIs.
Trying it out
Pointer Events only work with the New Architecture (Fabric) on React Native 0.71+, which was a release candidate at the time of the post. The team cautions that overriding the native feature flags reaches into internals and may break as the flags are phased out. In JavaScript:
import ReactNativeFeatureFlags from 'react-native/Libraries/ReactNative/ReactNativeFeatureFlags';
ReactNativeFeatureFlags.shouldEmitW3CPointerEvents = () => true;
ReactNativeFeatureFlags.shouldPressibilityUseW3CPointerEventsForHover =
() => true;
On iOS, call RCTSetDispatchW3CPointerEvents(YES); in the app's initialization code (typically AppDelegate.mm) and add UIApplicationSupportsIndirectInputEvents to info.plist. On Android, set ReactFeatureFlags.dispatchPointerEvents = true; during initialization.
Pointer Events are already used by the team's VR platform and power the Oculus Store. Feedback is being collected in a dedicated discussion on Pointer Events.
Links
Source: reactnative.dev