import React from 'react' import { View, type ViewProps } from 'react-native' import { claimExternalPressOwnership, releaseExternalPressOwnership, type ExternalPressOwnershipToken, } from './gestureState' export interface PressBoundaryProps extends ViewProps { enabled?: boolean /** * Alias for enabling the boundary. The behavior is limited to Tamagui's * shared press ownership and does not patch arbitrary RN bubbling. */ stopPropagation?: boolean debugName?: string | null } function composeFirst void>(ours: T, theirs?: T) { return (...args: Parameters) => { ours(...args) theirs?.(...args) } } function composeLast void>(theirs: T | undefined, ours: T) { return (...args: Parameters) => { theirs?.(...args) ours(...args) } } export const PressBoundary: React.ForwardRefExoticComponent< PressBoundaryProps & React.RefAttributes > = React.forwardRef(function PressBoundary( { enabled, stopPropagation, debugName, onTouchStart, onTouchEnd, onTouchCancel, onResponderGrant, onResponderRelease, onResponderTerminate, ...props }, forwardedRef ) { const tokenRef = React.useRef(null) const isEnabled = enabled ?? stopPropagation ?? true const claim = React.useCallback((): void => { if (!isEnabled) return if (tokenRef.current) { releaseExternalPressOwnership(tokenRef.current, debugName) } tokenRef.current = claimExternalPressOwnership(debugName) }, [debugName, isEnabled]) const release = React.useCallback((): void => { if (!tokenRef.current) return releaseExternalPressOwnership(tokenRef.current, debugName) tokenRef.current = null }, [debugName]) React.useEffect(() => release, [release]) return ( ) })