UNPKG

react-native-gesture-handler

Version:

Declarative API exposing native platform touch and gesture system to React Native

67 lines (61 loc) 2.34 kB
import type React from 'react'; import { useCallback } from 'react'; import findNodeHandle from '../../../findNodeHandle'; import type { PropsRef } from '../../../web/interfaces'; import type { GestureType } from '../gesture'; import type { ComposedGesture } from '../gestureComposition'; import { attachHandlers } from './attachHandlers'; import { dropHandlers } from './dropHandlers'; import { needsToReattach } from './needsToReattach'; import type { AttachedGestureState, GestureDetectorState } from './types'; import { updateHandlers } from './updateHandlers'; import { useForceRender } from './utils'; // Returns a function that's responsible for updating the attached gestures // If the view has changed, it will reattach the handlers to the new view // If the view remains the same, it will update the handlers with the new config export function useDetectorUpdater( state: GestureDetectorState, preparedGesture: AttachedGestureState, gesturesToAttach: GestureType[], gestureConfig: ComposedGesture | GestureType, webEventHandlersRef: React.RefObject<PropsRef> ) { const forceRender = useForceRender(); const updateAttachedGestures = useCallback( // skipConfigUpdate is used to prevent unnecessary updates when only checking if the view has changed (skipConfigUpdate?: boolean) => { // If the underlying view has changed we need to reattach handlers to the new view const viewTag = findNodeHandle(state.viewRef) as number; const didUnderlyingViewChange = viewTag !== state.previousViewTag; if ( didUnderlyingViewChange || needsToReattach(preparedGesture, gesturesToAttach) ) { dropHandlers(preparedGesture); attachHandlers({ preparedGesture, gestureConfig, gesturesToAttach, webEventHandlersRef, viewTag, }); if (didUnderlyingViewChange) { state.previousViewTag = viewTag; state.forceRebuildReanimatedEvent = true; forceRender(); } } else if (!skipConfigUpdate) { updateHandlers(preparedGesture, gestureConfig, gesturesToAttach); } }, [ forceRender, gestureConfig, gesturesToAttach, preparedGesture, state, webEventHandlersRef, ] ); return updateAttachedGestures; }