UNPKG

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

Version:
66 lines (63 loc) 3.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useFooterFreezeWhenExpanding = useFooterFreezeWhenExpanding; var _reactNative = require("react-native"); var _reactNativeReanimated = require("react-native-reanimated"); var _context = require("../context"); var _useLastSharedValueBeforeLock = require("./use-last-shared-value-before-lock"); /** * Returns a shared value that tells the footer whether it should freeze its * position as the content is expanding. */ function useFooterFreezeWhenExpanding({ bufferSharedValue, bottomAtTimeOfLockSharedValue }) { const { currentPositionDestinationSharedValue } = (0, _context.useBottomSheetToolboxInternalContext)(); const keyboardProgressSharedValue = (0, _context.useKeyboardProgressSharedValue)(); const lastBufferSharedValue = (0, _useLastSharedValueBeforeLock.useLastSharedValueBeforeLock)(bufferSharedValue); const pixelRatio = _reactNative.PixelRatio.get(); (0, _reactNativeReanimated.useAnimatedReaction)(() => currentPositionDestinationSharedValue.get(), (destination, lastDestination) => { const lastBuffer = lastBufferSharedValue.get(); if (lastBuffer !== null) { const destinationDiff = destination - (lastDestination ?? 0); const destinationDiffRounded = Math.round(destinationDiff * pixelRatio) / pixelRatio; if (destinationDiffRounded < -1) { lastBufferSharedValue.set(bufferSharedValue.get()); } } }, [currentPositionDestinationSharedValue]); const isExpandingSharedValue = (0, _reactNativeReanimated.useDerivedValue)(() => { const buffer = bufferSharedValue.get(); const lastBuffer = lastBufferSharedValue.get(); const delta = buffer === null || lastBuffer === null ? null : buffer - lastBuffer; return delta !== null && delta > 0; }, [lastBufferSharedValue, bufferSharedValue.get]); // This happens when opening and quickly closing. const wasMovingWithSheetSharedValue = (0, _reactNativeReanimated.useDerivedValue)(() => { const bottomAtTimeOfLock = bottomAtTimeOfLockSharedValue.get(); const roundedBottom = bottomAtTimeOfLock !== null ? Math.round(bottomAtTimeOfLock * pixelRatio) / pixelRatio : null; return roundedBottom !== null && roundedBottom < 0; }, [bottomAtTimeOfLockSharedValue, pixelRatio]); const isKeyboardMovingSharedValue = (0, _reactNativeReanimated.useDerivedValue)(() => keyboardProgressSharedValue.get() !== 0 && keyboardProgressSharedValue.get() !== 1, [keyboardProgressSharedValue]); /** * If the content-size of the sheet increases, then we actually want the * footer to stay where it is (not shift down as if it's animating back in). * * * To determine this, we snapshot the sheet position at the time of the lock * and see if we're expanding up or down. We also keep a snapshot of the * bottom right before the lock was engaged. */ return (0, _reactNativeReanimated.useDerivedValue)(() => { const wasMovingWithSheet = wasMovingWithSheetSharedValue.get(); const isSettled = !wasMovingWithSheet && !isKeyboardMovingSharedValue.get(); const isExpanding = isExpandingSharedValue.get(); return isExpanding && isSettled; }, []); } //# sourceMappingURL=use-footer-freeze-when-expanding.js.map