UNPKG

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

Version:
56 lines (54 loc) 3.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useMinOverlayHeightSharedValue = useMinOverlayHeightSharedValue; var React = _interopRequireWildcard(require("react")); var _reactNative = require("react-native"); var _reactNativeReanimated = require("react-native-reanimated"); var _reactNativeBottomSheetToolbox = require("@attio/react-native-bottom-sheet-toolbox"); var _getVerticalBorderWidth = require("../../utils/get-vertical-border-width"); var _getVerticalPadding = require("../../utils/get-vertical-padding"); function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); } function useMinOverlayHeightSharedValue({ textInputStyle, topInsetSharedValue, bottomInsetSharedValue, minTextInputHeightSharedValue, fallbackLines }) { const { wrapperHeightSharedValue } = (0, _reactNativeBottomSheetToolbox.useBottomSheetToolboxInternalContext)(); const flatTextInputStyle = React.useMemo(() => _reactNative.StyleSheet.flatten(textInputStyle), [textInputStyle]); const borderWidth = (0, _getVerticalBorderWidth.getVerticalBorderWidth)(flatTextInputStyle); const verticalPadding = (0, _getVerticalPadding.getVerticalPadding)(flatTextInputStyle); /** * There is a bug inside React Native which prevents this from being called * when it should be: https://github.com/facebook/react-native/issues/47186 * * Hence we try to calculate some meaningful default. */ const lineHeight = Number(flatTextInputStyle.lineHeight ?? 16) * fallbackLines; const initialContentHeight = typeof flatTextInputStyle.minHeight === "number" ? Math.max(flatTextInputStyle.minHeight, lineHeight) : lineHeight; const contentHeightSharedValue = (0, _reactNativeReanimated.useSharedValue)(initialContentHeight); const onContentSizeChange = React.useCallback(evt => { contentHeightSharedValue.set(evt.nativeEvent.contentSize.height); }, []); const keyboardHeightSharedValue = (0, _reactNativeBottomSheetToolbox.useKeyboardHeightSharedValue)(); (0, _reactNativeReanimated.useAnimatedReaction)(() => { const contentHeight = contentHeightSharedValue.get(); const wrapperHeight = wrapperHeightSharedValue.get(); const topInset = topInsetSharedValue.get(); if (contentHeight === 0 || wrapperHeight === null || topInset === null) { return null; } const maxOverlayHeight = wrapperHeight - keyboardHeightSharedValue.get() - topInset - bottomInsetSharedValue.get(); const layoutMargins = verticalPadding + borderWidth; return Math.min(contentHeight === 0 ? 0 : contentHeight + layoutMargins || 0, maxOverlayHeight); }, result => { minTextInputHeightSharedValue.set(result); }, [wrapperHeightSharedValue, keyboardHeightSharedValue, topInsetSharedValue, bottomInsetSharedValue, verticalPadding, borderWidth]); return onContentSizeChange; } //# sourceMappingURL=use-min-overlay-height-shared-value.js.map