UNPKG

@attio/react-native-bottom-sheet-toolbox

Version:
59 lines (58 loc) 2.84 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useBottomSheetScrollGesture = useBottomSheetScrollGesture; var _reactNativeGestureHandler = require("react-native-gesture-handler"); var _reactNativeReanimated = require("react-native-reanimated"); var _context = require("../context"); var _useBottomSheetDragGesture = require("../hooks/use-bottom-sheet-drag-gesture"); function useBottomSheetScrollGesture({ scrollRef, scrollOffsetYSharedValue, scrollContentHeightSharedValue, scrollLayoutHeightSharedValue, inverted, isFrozenSharedValue }) { const { normalizedSnapPointsSharedValue, currentPositionSharedValue } = (0, _context.useBottomSheetToolboxInternalContext)(); const isExpandedSharedValue = (0, _reactNativeReanimated.useDerivedValue)(() => { const maxSnap = Math.max(...(normalizedSnapPointsSharedValue.get() || [])); return currentPositionSharedValue.get() === maxSnap; }, [normalizedSnapPointsSharedValue, currentPositionSharedValue]); const panFrozenSharedValue = (0, _reactNativeReanimated.useDerivedValue)(() => { let isAtTop = false; const scrollOffsetY = scrollOffsetYSharedValue.get(); if (inverted) { const scrollLayoutHeight = scrollLayoutHeightSharedValue.get(); const scrollContentHeight = scrollContentHeightSharedValue.get(); const isReady = scrollLayoutHeight !== null && scrollContentHeight !== null; const ROUNDING_ERROR_EPSILON = 0.1; isAtTop = isReady && scrollContentHeight - scrollOffsetY - scrollLayoutHeight <= ROUNDING_ERROR_EPSILON; } else { isAtTop = scrollOffsetY <= 0; } return isFrozenSharedValue?.get() === true || isExpandedSharedValue.get() && !isAtTop; }, [scrollOffsetYSharedValue, isFrozenSharedValue, inverted, scrollContentHeightSharedValue.get, scrollLayoutHeightSharedValue.get]); const panGesture = (0, _useBottomSheetDragGesture.useBottomSheetDragGesture)({ id: "scroll", clamped: true, frozenSharedValue: panFrozenSharedValue }); const nativeGesture = _reactNativeGestureHandler.Gesture.Native(); const initialScrollOffsetYSharedValue = (0, _reactNativeReanimated.useSharedValue)(0); const scrollControllerGesture = _reactNativeGestureHandler.Gesture.Pan().onBegin(() => { initialScrollOffsetYSharedValue.set(scrollOffsetYSharedValue.get()); }).onUpdate(() => { if (!isExpandedSharedValue.get()) { (0, _reactNativeReanimated.scrollTo)(scrollRef, 0, initialScrollOffsetYSharedValue.get(), false); } else { initialScrollOffsetYSharedValue.set(scrollOffsetYSharedValue.get()); } }).failOffsetY(9999999999).failOffsetX(9999999999); return _reactNativeGestureHandler.Gesture.Simultaneous(nativeGesture, panGesture, scrollControllerGesture); } //# sourceMappingURL=use-bottom-sheet-scroll-gesture.js.map