react-native-screen-transitions
Version:
Easy screen transitions for React Native and Expo
84 lines (83 loc) • 2.53 kB
JavaScript
import { forwardRef, memo } from "react";
import { GestureDetector } from "react-native-gesture-handler";
import Animated, { runOnUI, useAnimatedRef } from "react-native-reanimated";
import { useAssociatedStyles } from "../hooks/animation/use-associated-style";
import { useBoundsRegistry } from "../hooks/bounds/use-bound-registry";
import { useScrollRegistry } from "../hooks/gestures/use-scroll-registry";
import { useGestureContext } from "../providers/gestures";
import { jsx as _jsx } from "react/jsx-runtime";
export function createTransitionAwareComponent(Wrapped, options = {}) {
const {
isScrollable = false
} = options;
const AnimatedComponent = Animated.createAnimatedComponent(Wrapped);
const ScrollableInner = /*#__PURE__*/forwardRef((props, ref) => {
const {
nativeGesture
} = useGestureContext();
const {
scrollHandler,
onContentSizeChange,
onLayout
} = useScrollRegistry({
onScroll: props.onScroll,
onContentSizeChange: props.onContentSizeChange,
onLayout: props.onLayout
});
return /*#__PURE__*/_jsx(GestureDetector, {
gesture: nativeGesture,
children: /*#__PURE__*/_jsx(AnimatedComponent, {
...props,
ref: ref,
onScroll: scrollHandler,
onContentSizeChange: onContentSizeChange,
onLayout: onLayout,
scrollEventThrottle: props.scrollEventThrottle || 16
})
});
});
const Inner = /*#__PURE__*/forwardRef((props, _) => {
const {
children,
style,
sharedBoundTag,
styleId,
onPress,
...rest
} = props;
const animatedRef = useAnimatedRef();
const {
associatedStyles
} = useAssociatedStyles({
id: sharedBoundTag || styleId,
style
});
const {
handleInitialLayout,
captureActiveOnPress,
MeasurementSyncProvider
} = useBoundsRegistry({
sharedBoundTag,
animatedRef,
style,
onPress
});
return /*#__PURE__*/_jsx(MeasurementSyncProvider, {
children: /*#__PURE__*/_jsx(AnimatedComponent, {
...rest,
ref: animatedRef,
style: [style, associatedStyles],
onPress: captureActiveOnPress,
onLayout: runOnUI(handleInitialLayout),
collapsable: !sharedBoundTag,
children: children
})
});
});
if (isScrollable) {
return /*#__PURE__*/memo(ScrollableInner);
}
return /*#__PURE__*/memo(Inner);
}
//# sourceMappingURL=create-transition-aware-component.js.map
;