@attio/react-native-bottom-sheet-toolbox
Version:
33 lines (25 loc) • 1.41 kB
text/typescript
import {type SharedValue, useDerivedValue} from "react-native-reanimated"
import {useBottomSheetToolboxInternalContext, useKeyboardHeightSharedValue} from "../context"
import {CLOSED_SNAP_POINT} from "../utils"
/**
* Performs some adjustments to a normalized array of snap-points to account for the keyboard.
*
* TODO: right now, we just expand proportionally with the keyboard. However, it would be nice
* to offer a wider selection strategies such as expanding to largest snap-point etc.
*/
export function useKeyboardAvoidingSnapPoints(
normalizedSnapPointsSharedValue: SharedValue<number[] | null>
) {
const keyboardHeightSharedValue = useKeyboardHeightSharedValue()
const {wrapperHeightSharedValue} = useBottomSheetToolboxInternalContext()
return useDerivedValue(() => {
const wrapperHeight = wrapperHeightSharedValue.get()
const normalizedSnapPoints = normalizedSnapPointsSharedValue.get()
if (normalizedSnapPoints === null || wrapperHeight === null) return null
const keyboardAdjustedSnapPoints = normalizedSnapPoints.map((sp) =>
sp === CLOSED_SNAP_POINT ? sp : sp + keyboardHeightSharedValue.get()
)
const wrapperClamped = keyboardAdjustedSnapPoints.map((sp) => Math.min(sp, wrapperHeight))
return wrapperClamped
}, [keyboardHeightSharedValue, normalizedSnapPointsSharedValue, wrapperHeightSharedValue])
}