From 22536bb943731a1305d12a25594893ac1bcb679c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82?= Date: Mon, 13 Oct 2025 15:02:51 +0200 Subject: [PATCH 1/4] Add runOnJS --- .../gesturehandler/core/GestureHandler.kt | 6 ++--- .../apple/RNGestureHandler.mm | 2 +- .../src/v3/NativeDetector/NativeDetector.tsx | 2 +- .../v3/hooks/relations/useComposedGesture.ts | 9 ++++--- .../src/v3/hooks/useGesture.ts | 27 +++++-------------- .../src/v3/hooks/utils.ts | 16 +++++++++++ .../src/v3/hooks/utils/reanimatedUtils.ts | 11 +++++++- .../src/v3/types.ts | 17 +++++++----- 8 files changed, 53 insertions(+), 37 deletions(-) diff --git a/packages/react-native-gesture-handler/android/src/main/java/com/swmansion/gesturehandler/core/GestureHandler.kt b/packages/react-native-gesture-handler/android/src/main/java/com/swmansion/gesturehandler/core/GestureHandler.kt index 1d7175887f..63f44732a5 100644 --- a/packages/react-native-gesture-handler/android/src/main/java/com/swmansion/gesturehandler/core/GestureHandler.kt +++ b/packages/react-native-gesture-handler/android/src/main/java/com/swmansion/gesturehandler/core/GestureHandler.kt @@ -902,8 +902,8 @@ open class GestureHandler { if (config.hasKey(KEY_DISPATCHES_ANIMATED_EVENTS)) { handler.dispatchesAnimatedEvents = config.getBoolean(KEY_DISPATCHES_ANIMATED_EVENTS) } - if (config.hasKey(KEY_SHOULD_USE_REANIMATED)) { - handler.dispatchesReanimatedEvents = config.getBoolean(KEY_SHOULD_USE_REANIMATED) + if (config.hasKey(KEY_DISPATCHES_REANIMATED_EVENTS)) { + handler.dispatchesReanimatedEvents = config.getBoolean(KEY_DISPATCHES_REANIMATED_EVENTS) } if (config.hasKey(KEY_MANUAL_ACTIVATION)) { handler.manualActivation = config.getBoolean(KEY_MANUAL_ACTIVATION) @@ -920,7 +920,7 @@ open class GestureHandler { private const val KEY_ENABLED = "enabled" private const val KEY_NEEDS_POINTER_DATA = "needsPointerData" private const val KEY_DISPATCHES_ANIMATED_EVENTS = "dispatchesAnimatedEvents" - private const val KEY_SHOULD_USE_REANIMATED = "shouldUseReanimated" + private const val KEY_DISPATCHES_REANIMATED_EVENTS = "dispatchesReanimatedEvents" private const val KEY_MANUAL_ACTIVATION = "manualActivation" private const val KEY_MOUSE_BUTTON = "mouseButton" private const val KEY_HIT_SLOP = "hitSlop" diff --git a/packages/react-native-gesture-handler/apple/RNGestureHandler.mm b/packages/react-native-gesture-handler/apple/RNGestureHandler.mm index a2dd49fd93..4c27ab3032 100644 --- a/packages/react-native-gesture-handler/apple/RNGestureHandler.mm +++ b/packages/react-native-gesture-handler/apple/RNGestureHandler.mm @@ -147,7 +147,7 @@ - (void)updateConfig:(NSDictionary *)config _dispatchesAnimatedEvents = [RCTConvert BOOL:prop]; } - prop = config[@"shouldUseReanimated"]; + prop = config[@"dispatchesReanimatedEvents"]; if (prop != nil) { _dispatchesReanimatedEvents = [RCTConvert BOOL:prop]; } diff --git a/packages/react-native-gesture-handler/src/v3/NativeDetector/NativeDetector.tsx b/packages/react-native-gesture-handler/src/v3/NativeDetector/NativeDetector.tsx index 6bd6b3a120..5e923acccc 100644 --- a/packages/react-native-gesture-handler/src/v3/NativeDetector/NativeDetector.tsx +++ b/packages/react-native-gesture-handler/src/v3/NativeDetector/NativeDetector.tsx @@ -35,7 +35,7 @@ export function NativeDetector({ const NativeDetectorComponent = gesture.config.dispatchesAnimatedEvents ? AnimatedNativeDetector - : gesture.config.shouldUseReanimated + : gesture.config.shouldUseReanimatedDetector ? ReanimatedNativeDetector : HostGestureDetector; diff --git a/packages/react-native-gesture-handler/src/v3/hooks/relations/useComposedGesture.ts b/packages/react-native-gesture-handler/src/v3/hooks/relations/useComposedGesture.ts index bdb8d11f2c..d8e113e767 100644 --- a/packages/react-native-gesture-handler/src/v3/hooks/relations/useComposedGesture.ts +++ b/packages/react-native-gesture-handler/src/v3/hooks/relations/useComposedGesture.ts @@ -5,6 +5,7 @@ import { ComposedGesture, ComposedGestureName, Gesture, + ComposedGestureConfig, } from '../../types'; import { tagMessage } from '../../../utils'; import { Reanimated } from '../../../handlers/gestures/reanimatedWrapper'; @@ -27,16 +28,16 @@ export function useComposedGesture( ); } - const config = { - shouldUseReanimated: gestures.some( - (gesture) => gesture.config.shouldUseReanimated + const config: ComposedGestureConfig = { + shouldUseReanimatedDetector: gestures.some( + (gesture) => gesture.config.shouldUseReanimatedDetector ), dispatchesAnimatedEvents: gestures.some( (gesture) => gesture.config.dispatchesAnimatedEvents ), }; - if (config.shouldUseReanimated && config.dispatchesAnimatedEvents) { + if (config.shouldUseReanimatedDetector && config.dispatchesAnimatedEvents) { throw new Error( tagMessage( 'Composed gestures cannot use both Reanimated and Animated events at the same time.' diff --git a/packages/react-native-gesture-handler/src/v3/hooks/useGesture.ts b/packages/react-native-gesture-handler/src/v3/hooks/useGesture.ts index cec1f7dc18..96d066dcb9 100644 --- a/packages/react-native-gesture-handler/src/v3/hooks/useGesture.ts +++ b/packages/react-native-gesture-handler/src/v3/hooks/useGesture.ts @@ -2,19 +2,11 @@ import { useEffect, useMemo } from 'react'; import { getNextHandlerTag } from '../../handlers/getNextHandlerTag'; import RNGestureHandlerModule from '../../RNGestureHandlerModule'; import { useGestureCallbacks } from './useGestureCallbacks'; -import { Reanimated } from '../../handlers/gestures/reanimatedWrapper'; -import { - prepareConfig, - isAnimatedEvent, - shouldHandleTouchEvents, -} from './utils'; + +import { prepareConfigForNativeSide, prepareConfig } from './utils'; import { tagMessage } from '../../utils'; import { BaseGestureConfig, SingleGesture, SingleGestureName } from '../types'; -import { - bindSharedValues, - hasWorkletEventHandlers, - unbindSharedValues, -} from './utils/reanimatedUtils'; +import { bindSharedValues, unbindSharedValues } from './utils/reanimatedUtils'; import { prepareRelations } from './utils/relationUtils'; export function useGesture( @@ -33,14 +25,9 @@ export function useGesture( } // This has to be done ASAP as other hooks depend `shouldUseReanimated`. - config.shouldUseReanimated = - !config.disableReanimated && - Reanimated !== undefined && - hasWorkletEventHandlers(config); - config.needsPointerData = shouldHandleTouchEvents(config); - config.dispatchesAnimatedEvents = isAnimatedEvent(config.onUpdate); + prepareConfig(config); - if (config.dispatchesAnimatedEvents && config.shouldUseReanimated) { + if (config.dispatchesAnimatedEvents && config.shouldUseReanimatedDetector) { throw new Error( tagMessage( `${type}: You cannot use Animated.Event together with callbacks running on the UI thread. Either remove Animated.Event from onUpdate, or set runOnJS property to true on the gesture.` @@ -71,7 +58,7 @@ export function useGesture( } if ( - config.shouldUseReanimated && + config.shouldUseReanimatedDetector && (!onReanimatedStateChange || !onReanimatedUpdateEvent || !onReanimatedTouchEvent) @@ -94,7 +81,7 @@ export function useGesture( }, [type, tag]); useEffect(() => { - const preparedConfig = prepareConfig(config); + const preparedConfig = prepareConfigForNativeSide(config); RNGestureHandlerModule.setGestureHandlerConfig(tag, preparedConfig); RNGestureHandlerModule.flushOperations(); diff --git a/packages/react-native-gesture-handler/src/v3/hooks/utils.ts b/packages/react-native-gesture-handler/src/v3/hooks/utils.ts index 13285c3e07..d9485fd58d 100644 --- a/packages/react-native-gesture-handler/src/v3/hooks/utils.ts +++ b/packages/react-native-gesture-handler/src/v3/hooks/utils.ts @@ -14,6 +14,7 @@ import { import { GestureTouchEvent } from '../../handlers/gestureHandlerCommon'; import { tagMessage } from '../../utils'; import { Reanimated } from '../../handlers/gestures/reanimatedWrapper'; +import { hasWorkletEventHandlers } from './utils/reanimatedUtils'; export function isNativeEvent( event: GestureHandlerEvent @@ -81,6 +82,21 @@ export function checkMappingForChangeProperties(animatedEvent: AnimatedEvent) { export function prepareConfig( config: BaseGestureConfig +) { + const runOnJS = maybeUnpackValue(config.runOnJS); + + config.shouldUseReanimatedDetector = + !config.disableReanimated && + Reanimated !== undefined && + hasWorkletEventHandlers(config); + config.needsPointerData = shouldHandleTouchEvents(config); + config.dispatchesAnimatedEvents = isAnimatedEvent(config.onUpdate); + config.dispatchesReanimatedEvents = + config.shouldUseReanimatedDetector && runOnJS !== true; +} + +export function prepareConfigForNativeSide( + config: BaseGestureConfig ) { const copy = { ...config }; diff --git a/packages/react-native-gesture-handler/src/v3/hooks/utils/reanimatedUtils.ts b/packages/react-native-gesture-handler/src/v3/hooks/utils/reanimatedUtils.ts index 1cd7929a60..e8ab354fd9 100644 --- a/packages/react-native-gesture-handler/src/v3/hooks/utils/reanimatedUtils.ts +++ b/packages/react-native-gesture-handler/src/v3/hooks/utils/reanimatedUtils.ts @@ -36,7 +36,16 @@ export function bindSharedValues( const listenerId = baseListenerId + keyHash; sharedValue.addListener(listenerId, (value) => { - updateGestureHandlerConfig(handlerTag, { [configKey]: value }); + if (configKey === 'runOnJS') { + config.dispatchesReanimatedEvents = + config.shouldUseReanimatedDetector && !value; + + updateGestureHandlerConfig(handlerTag, { + dispatchesReanimatedEvents: config.dispatchesReanimatedEvents, + }); + } else { + updateGestureHandlerConfig(handlerTag, { [configKey]: value }); + } flushOperations(); }); }; diff --git a/packages/react-native-gesture-handler/src/v3/types.ts b/packages/react-native-gesture-handler/src/v3/types.ts index 1883b54b9a..c5197c47ce 100644 --- a/packages/react-native-gesture-handler/src/v3/types.ts +++ b/packages/react-native-gesture-handler/src/v3/types.ts @@ -116,13 +116,15 @@ export type SingleGesture = { gestureRelations: GestureRelations; }; +export type ComposedGestureConfig = { + shouldUseReanimatedDetector: boolean; + dispatchesAnimatedEvents: boolean; +}; + export type ComposedGesture = { tags: number[]; type: ComposedGestureName; - config: { - shouldUseReanimated: boolean; - dispatchesAnimatedEvents: boolean; - }; + config: ComposedGestureConfig; gestureEvents: GestureEvents; externalSimultaneousHandlers: number[]; gestures: Gesture[]; @@ -167,15 +169,16 @@ export interface GestureCallbacks { } export type InternalConfigProps = { - shouldUseReanimated?: boolean; + shouldUseReanimatedDetector?: boolean; + dispatchesReanimatedEvents?: boolean; dispatchesAnimatedEvents?: boolean; needsPointerData?: boolean; changeEventCalculator?: ChangeCalculatorType; }; -type CommonGestureConfig = WithSharedValue< +type CommonGestureConfig = { disableReanimated?: boolean } & WithSharedValue< { - disableReanimated?: boolean; + runOnJS?: boolean; enabled?: boolean; shouldCancelWhenOutside?: boolean; hitSlop?: HitSlop; From 7a4a0147359bdb577a9eaa40d7322fc39fb26a4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82?= Date: Tue, 14 Oct 2025 11:35:54 +0200 Subject: [PATCH 2/4] Update comment --- .../react-native-gesture-handler/src/v3/hooks/useGesture.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/react-native-gesture-handler/src/v3/hooks/useGesture.ts b/packages/react-native-gesture-handler/src/v3/hooks/useGesture.ts index 96d066dcb9..5035f76398 100644 --- a/packages/react-native-gesture-handler/src/v3/hooks/useGesture.ts +++ b/packages/react-native-gesture-handler/src/v3/hooks/useGesture.ts @@ -24,7 +24,7 @@ export function useGesture( ); } - // This has to be done ASAP as other hooks depend `shouldUseReanimated`. + // This has to be done ASAP as other hooks depend `shouldUseReanimatedDetector`. prepareConfig(config); if (config.dispatchesAnimatedEvents && config.shouldUseReanimatedDetector) { From b47076450a7d471c7aeb0f10b50bb2a3e23633a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82?= Date: Tue, 14 Oct 2025 11:44:33 +0200 Subject: [PATCH 3/4] Use not operator --- packages/react-native-gesture-handler/src/v3/hooks/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/react-native-gesture-handler/src/v3/hooks/utils.ts b/packages/react-native-gesture-handler/src/v3/hooks/utils.ts index d9485fd58d..67c70cd845 100644 --- a/packages/react-native-gesture-handler/src/v3/hooks/utils.ts +++ b/packages/react-native-gesture-handler/src/v3/hooks/utils.ts @@ -92,7 +92,7 @@ export function prepareConfig( config.needsPointerData = shouldHandleTouchEvents(config); config.dispatchesAnimatedEvents = isAnimatedEvent(config.onUpdate); config.dispatchesReanimatedEvents = - config.shouldUseReanimatedDetector && runOnJS !== true; + config.shouldUseReanimatedDetector && !runOnJS; } export function prepareConfigForNativeSide( From 52f3079b4af369f5d9e438265024418c2ab72351 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82?= Date: Wed, 15 Oct 2025 15:58:56 +0200 Subject: [PATCH 4/4] Remove props lists from configUtils --- .../src/v3/hooks/utils/configUtils.ts | 78 ++----------------- .../src/v3/hooks/utils/index.ts | 1 + .../src/v3/hooks/utils/propsWhiteList.ts | 76 ++++++++++++++++++ .../src/v3/hooks/utils/reanimatedUtils.ts | 2 +- 4 files changed, 84 insertions(+), 73 deletions(-) create mode 100644 packages/react-native-gesture-handler/src/v3/hooks/utils/propsWhiteList.ts diff --git a/packages/react-native-gesture-handler/src/v3/hooks/utils/configUtils.ts b/packages/react-native-gesture-handler/src/v3/hooks/utils/configUtils.ts index d3e72ffd10..e94d64a59f 100644 --- a/packages/react-native-gesture-handler/src/v3/hooks/utils/configUtils.ts +++ b/packages/react-native-gesture-handler/src/v3/hooks/utils/configUtils.ts @@ -2,21 +2,17 @@ import { Reanimated } from '../../../handlers/gestures/reanimatedWrapper'; import { tagMessage } from '../../../utils'; import { BaseGestureConfig, - CommonGestureConfig, ExcludeInternalConfigProps, - GestureCallbacks, - HandlersPropsWhiteList, - InternalConfigProps, SingleGestureName, } from '../../types'; -import { PanNativeProperties } from '../gestures/pan/PanProperties'; -import { FlingNativeProperties } from '../gestures/fling/FlingProperties'; -import { HoverNativeProperties } from '../gestures/hover/HoverProperties'; -import { LongPressNativeProperties } from '../gestures/longPress/LongPressProperties'; -import { NativeHandlerNativeProperties } from '../gestures/native/NativeProperties'; -import { TapNativeProperties } from '../gestures/tap/TapProperties'; import { hasWorkletEventHandlers, maybeUnpackValue } from './reanimatedUtils'; import { isAnimatedEvent, shouldHandleTouchEvents } from './eventUtils'; +import { + allowedNativeProps, + EMPTY_WHITE_LIST, + PropsToFilter, + PropsWhiteLists, +} from './propsWhiteList'; export function prepareConfig( config: BaseGestureConfig @@ -33,68 +29,6 @@ export function prepareConfig( config.shouldUseReanimatedDetector && !runOnJS; } -const allowedNativeProps = new Set< - keyof CommonGestureConfig | keyof InternalConfigProps ->([ - // CommonGestureConfig - 'disableReanimated', - 'enabled', - 'shouldCancelWhenOutside', - 'hitSlop', - 'userSelect', - 'activeCursor', - 'mouseButton', - 'enableContextMenu', - 'touchAction', - - // InternalConfigProps - 'dispatchesReanimatedEvents', - 'dispatchesAnimatedEvents', - 'needsPointerData', - 'changeEventCalculator', -]); - -export const HandlerCallbacks = new Set< - keyof Required> ->([ - 'onBegin', - 'onStart', - 'onUpdate', - 'onEnd', - 'onFinalize', - 'onTouchesDown', - 'onTouchesMove', - 'onTouchesUp', - 'onTouchesCancelled', -]); - -const PropsToFilter = new Set>([ - ...HandlerCallbacks, - - // Config props - 'changeEventCalculator', - 'disableReanimated', - - // Relations - 'simultaneousWithExternalGesture', - 'requireExternalGestureToFail', - 'blocksExternalGesture', -]); - -export const PropsWhiteLists = new Map< - SingleGestureName, - HandlersPropsWhiteList ->([ - [SingleGestureName.Pan, PanNativeProperties], - [SingleGestureName.Tap, TapNativeProperties], - [SingleGestureName.Native, NativeHandlerNativeProperties], - [SingleGestureName.Fling, FlingNativeProperties], - [SingleGestureName.Hover, HoverNativeProperties], - [SingleGestureName.LongPress, LongPressNativeProperties], -]); - -const EMPTY_WHITE_LIST = new Set(); - export function prepareConfigForNativeSide( handlerType: SingleGestureName, config: BaseGestureConfig diff --git a/packages/react-native-gesture-handler/src/v3/hooks/utils/index.ts b/packages/react-native-gesture-handler/src/v3/hooks/utils/index.ts index 753d4398e2..6210dbec1f 100644 --- a/packages/react-native-gesture-handler/src/v3/hooks/utils/index.ts +++ b/packages/react-native-gesture-handler/src/v3/hooks/utils/index.ts @@ -3,3 +3,4 @@ export * from './eventHandlersUtils'; export * from './eventUtils'; export * from './reanimatedUtils'; export * from './relationUtils'; +export * from './propsWhiteList'; diff --git a/packages/react-native-gesture-handler/src/v3/hooks/utils/propsWhiteList.ts b/packages/react-native-gesture-handler/src/v3/hooks/utils/propsWhiteList.ts new file mode 100644 index 0000000000..e873107bd7 --- /dev/null +++ b/packages/react-native-gesture-handler/src/v3/hooks/utils/propsWhiteList.ts @@ -0,0 +1,76 @@ +import { + BaseGestureConfig, + CommonGestureConfig, + GestureCallbacks, + HandlersPropsWhiteList, + InternalConfigProps, + SingleGestureName, +} from '../../types'; +import { FlingNativeProperties } from '../gestures/fling/FlingProperties'; +import { HoverNativeProperties } from '../gestures/hover/HoverProperties'; +import { LongPressNativeProperties } from '../gestures/longPress/LongPressProperties'; +import { NativeHandlerNativeProperties } from '../gestures/native/NativeProperties'; +import { PanNativeProperties } from '../gestures/pan/PanProperties'; +import { TapNativeProperties } from '../gestures/tap/TapProperties'; + +export const allowedNativeProps = new Set< + keyof CommonGestureConfig | keyof InternalConfigProps +>([ + // CommonGestureConfig + 'disableReanimated', + 'enabled', + 'shouldCancelWhenOutside', + 'hitSlop', + 'userSelect', + 'activeCursor', + 'mouseButton', + 'enableContextMenu', + 'touchAction', + + // InternalConfigProps + 'dispatchesReanimatedEvents', + 'dispatchesAnimatedEvents', + 'needsPointerData', + 'changeEventCalculator', +]); + +export const HandlerCallbacks = new Set< + keyof Required> +>([ + 'onBegin', + 'onStart', + 'onUpdate', + 'onEnd', + 'onFinalize', + 'onTouchesDown', + 'onTouchesMove', + 'onTouchesUp', + 'onTouchesCancelled', +]); + +export const PropsToFilter = new Set>([ + ...HandlerCallbacks, + + // Config props + 'changeEventCalculator', + 'disableReanimated', + + // Relations + 'simultaneousWithExternalGesture', + 'requireExternalGestureToFail', + 'blocksExternalGesture', +]); + +export const PropsWhiteLists = new Map< + SingleGestureName, + HandlersPropsWhiteList +>([ + [SingleGestureName.Pan, PanNativeProperties], + [SingleGestureName.Tap, TapNativeProperties], + [SingleGestureName.Native, NativeHandlerNativeProperties], + [SingleGestureName.Fling, FlingNativeProperties], + [SingleGestureName.Hover, HoverNativeProperties], + [SingleGestureName.LongPress, LongPressNativeProperties], +]); + +export const EMPTY_WHITE_LIST = new Set(); diff --git a/packages/react-native-gesture-handler/src/v3/hooks/utils/reanimatedUtils.ts b/packages/react-native-gesture-handler/src/v3/hooks/utils/reanimatedUtils.ts index d3e23f8999..300ec5f0ad 100644 --- a/packages/react-native-gesture-handler/src/v3/hooks/utils/reanimatedUtils.ts +++ b/packages/react-native-gesture-handler/src/v3/hooks/utils/reanimatedUtils.ts @@ -1,7 +1,7 @@ import RNGestureHandlerModule from '../../../RNGestureHandlerModule'; import { Reanimated } from '../../../handlers/gestures/reanimatedWrapper'; import { BaseGestureConfig, SharedValue, SharedValueOrT } from '../../types'; -import { HandlerCallbacks } from './configUtils'; +import { HandlerCallbacks } from './propsWhiteList'; // Variant of djb2 hash function. // Taken from https://gist.github.com/eplawless/52813b1d8ad9af510d85?permalink_comment_id=3367765#gistcomment-3367765