@attio/react-native-bottom-sheet-toolbox
Version:
77 lines (74 loc) • 3.71 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.useFooterBottomPositionSharedValue = useFooterBottomPositionSharedValue;
var _reactNativeReanimated = require("react-native-reanimated");
var _context = require("../context");
var _useFooterFreezeWhenExpanding = require("./use-footer-freeze-when-expanding");
var _useLastSharedValueBeforeLock = require("./use-last-shared-value-before-lock");
/**
* A shared value used as the bottom position for a footer.
*/
function useFooterBottomPositionSharedValue({
bufferSharedValue: outerBufferSharedValue,
disableKeyboardAdjust = false,
freezeWhenExpanding = true
}) {
const {
isReadySharedValue,
currentPositionSharedValue,
childrenHeightSharedValue,
wrapperHeightSharedValue,
footerHeightSharedValue
} = (0, _context.useBottomSheetToolboxInternalContext)();
const keyboardHeightSharedValue = (0, _context.useKeyboardHeightSharedValue)();
const bufferSharedValue = (0, _reactNativeReanimated.useDerivedValue)(() => {
if (outerBufferSharedValue) return outerBufferSharedValue.get();
const childrenHeight = childrenHeightSharedValue.get();
const wrapperHeight = wrapperHeightSharedValue.get();
const footerHeight = footerHeightSharedValue.get();
if (childrenHeight === null || wrapperHeight === null || footerHeight === null) {
return null;
}
return Math.min(childrenHeight, wrapperHeight - footerHeight);
}, [outerBufferSharedValue, childrenHeightSharedValue, wrapperHeightSharedValue, footerHeightSharedValue]);
const rawBottomSharedValue = (0, _reactNativeReanimated.useDerivedValue)(() => {
const footerHeight = footerHeightSharedValue.get();
const buffer = bufferSharedValue.get();
if (!isReadySharedValue.get() || buffer === null || footerHeight === null) {
return null;
}
const keyboardAdjust = disableKeyboardAdjust ? 0 : keyboardHeightSharedValue.get();
const unclampedClosingOffset = currentPositionSharedValue.get() - footerHeight - buffer - keyboardAdjust;
const closingOffset = Math.min(0, unclampedClosingOffset);
const bottom = keyboardAdjust + closingOffset;
return bottom;
}, [isReadySharedValue, currentPositionSharedValue, disableKeyboardAdjust, footerHeightSharedValue, keyboardHeightSharedValue]);
const bottomAtTimeOfLockSharedValue = (0, _useLastSharedValueBeforeLock.useLastSharedValueBeforeLock)(rawBottomSharedValue);
const freezeWhenExpandingSharedValue = (0, _useFooterFreezeWhenExpanding.useFooterFreezeWhenExpanding)({
bufferSharedValue,
bottomAtTimeOfLockSharedValue
});
/**
* 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.
*/
const frozenBottomSharedValue = (0, _reactNativeReanimated.useDerivedValue)(() => {
const bottom = rawBottomSharedValue.get();
const bottomAtTimeOfLock = bottomAtTimeOfLockSharedValue.get();
const shouldFreezeBottom = freezeWhenExpanding && freezeWhenExpandingSharedValue.get();
return shouldFreezeBottom ? bottomAtTimeOfLock ?? bottom : bottom;
}, [bottomAtTimeOfLockSharedValue, freezeWhenExpanding, freezeWhenExpandingSharedValue]);
/**
* Note: returning different SharedValue references doesn't play well with
* useAnimatedStyle. Hence we always return the derived value.
*/
return frozenBottomSharedValue;
}
//# sourceMappingURL=use-footer-bottom-position-shared-value.js.map