react-native-gesture-handler
Version:
Declarative API exposing native platform touch and gesture system to React Native
44 lines (42 loc) • 1.75 kB
JavaScript
;
import { use, useCallback } from 'react';
import { JSResponderContext, updateResponderEventValue } from '../scrollViewInterop';
import { SingleGestureName } from '../types';
import { isComposedGesture, maybeUnpackValue } from './utils';
const ignoreResponderEvent = () => false;
function isSupportedGesture(gesture, countNativeAsHandled) {
if (isComposedGesture(gesture)) {
return gesture.gestures.some(child => isSupportedGesture(child, countNativeAsHandled));
}
if (maybeUnpackValue(gesture.config.enabled) === false) {
return false;
}
switch (gesture.type) {
case SingleGestureName.Tap:
case SingleGestureName.LongPress:
case SingleGestureName.Fling:
case SingleGestureName.Hover:
return true;
case SingleGestureName.Native:
return countNativeAsHandled && gesture.config.countNativeAsHandled !== false;
default:
return false;
}
}
export function useJSResponderHandler(gesture,
// Whether a tap on a Native gesture counts as handled. Scrollers and other
// non-tap-targets turn it off so touching them doesn't keep the keyboard open.
countNativeAsHandled = true) {
const jsResponderContext = use(JSResponderContext);
// `enabled` is read when the touch starts, so a SharedValue is always current
const handleStartShouldSetResponder = useCallback(() => {
if (gesture !== undefined && isSupportedGesture(gesture, countNativeAsHandled)) {
updateResponderEventValue(jsResponderContext, true);
}
return false;
}, [jsResponderContext, gesture, countNativeAsHandled]);
return {
handleStartShouldSetResponder: jsResponderContext == null ? ignoreResponderEvent : handleStartShouldSetResponder
};
}
//# sourceMappingURL=useJSResponderHandler.js.map