UNPKG

react-native-keyboard-controller

Version:

Keyboard manager which works in identical way on both iOS and Android

130 lines (126 loc) 6.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _react = _interopRequireWildcard(require("react")); var _reactNative = require("react-native"); var _reactNativeReanimated = _interopRequireWildcard(require("react-native-reanimated")); var _useCombinedRef = _interopRequireDefault(require("../hooks/useCombinedRef")); var _ScrollViewWithBottomPadding = _interopRequireDefault(require("../ScrollViewWithBottomPadding")); var _useChatKeyboard = require("./useChatKeyboard"); var _useEndVisible = require("./useEndVisible"); var _useExtraContentPadding = require("./useExtraContentPadding"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } 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 _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); } const ZERO_CONTENT_PADDING = (0, _reactNativeReanimated.makeMutable)(0); const ZERO_BLANK_SPACE = (0, _reactNativeReanimated.makeMutable)(0); const KeyboardChatScrollView = /*#__PURE__*/(0, _react.forwardRef)(({ children, ScrollViewComponent = _reactNativeReanimated.default.ScrollView, inverted = false, keyboardLiftBehavior = "always", freeze = false, offset = 0, extraContentPadding = ZERO_CONTENT_PADDING, blankSpace = ZERO_BLANK_SPACE, applyWorkaroundForContentInsetHitTestBug = false, onLayout: onLayoutProp, onContentSizeChange: onContentSizeChangeProp, onEndVisible, ...rest }, ref) => { const scrollViewRef = (0, _reactNativeReanimated.useAnimatedRef)(); const onRef = (0, _useCombinedRef.default)(ref, scrollViewRef); const freezeSV = (0, _reactNativeReanimated.useDerivedValue)(() => typeof freeze === "boolean" ? freeze : freeze.value); const { padding, currentHeight, contentOffsetY, scroll, layout, size, onLayout: onLayoutInternal, onContentSizeChange: onContentSizeChangeInternal } = (0, _useChatKeyboard.useChatKeyboard)(scrollViewRef, { inverted, keyboardLiftBehavior, freeze: freezeSV, offset, blankSpace, extraContentPadding }); (0, _useExtraContentPadding.useExtraContentPadding)({ scrollViewRef, extraContentPadding, keyboardPadding: padding, blankSpace, scroll, layout, size, contentOffsetY, inverted, keyboardLiftBehavior, freeze: freezeSV }); (0, _useEndVisible.useEndVisible)({ scroll, layout, size, inverted, onEndVisible }); // intentionally clamp `blankSpace` at one ScrollView viewport. The Android // `ClippingScrollView` workaround temporarily substitutes padding/range // during touch handling, and oversized blank ranges can de-sync during fast // momentum gestures. One viewport is enough for the short-content case; // larger values only allow scrolling to a fully blank screen which is not // the use case for this library. If you found this comment and it causes // a bug for you, please open an issue. const totalPadding = (0, _reactNativeReanimated.useDerivedValue)(() => Math.min(layout.value.height, Math.max(blankSpace.value, padding.value + extraContentPadding.value))); // Scroll indicator inset = keyboard + extraContentPadding (excludes blankSpace). // Apps that render into the unsafe area can supply a negative // scrollIndicatorInsets adjustment at the application layer. const indicatorPadding = (0, _reactNativeReanimated.useDerivedValue)(() => padding.value + extraContentPadding.value); const onLayout = (0, _react.useCallback)(e => { onLayoutInternal(e); onLayoutProp === null || onLayoutProp === void 0 || onLayoutProp(e); }, [onLayoutInternal, onLayoutProp]); const onContentSizeChange = (0, _react.useCallback)((w, h) => { onContentSizeChangeInternal(w, h); onContentSizeChangeProp === null || onContentSizeChangeProp === void 0 || onContentSizeChangeProp(w, h); }, [onContentSizeChangeInternal, onContentSizeChangeProp]); // Invisible view whose animated style changes every frame during keyboard // animation. On Fabric, this forces Reanimated to schedule a commit, // which flushes the scrollTo call in the same frame (fixing de-synchronization). // see https://github.com/software-mansion/react-native-reanimated/issues/9000 const commitStyle = (0, _reactNativeReanimated.useAnimatedStyle)(() => ({ transform: [{ translateY: -currentHeight.value }] }), []); const commit = (0, _react.useMemo)(() => [styles.commitView, commitStyle], [commitStyle]); return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_ScrollViewWithBottomPadding.default, _extends({ ref: onRef }, rest, { applyWorkaroundForContentInsetHitTestBug: applyWorkaroundForContentInsetHitTestBug, bottomPadding: totalPadding, contentOffsetY: contentOffsetY, inverted: inverted, scrollIndicatorPadding: indicatorPadding, ScrollViewComponent: ScrollViewComponent, onContentSizeChange: onContentSizeChange, onLayout: onLayout }), children), /*#__PURE__*/_react.default.createElement(_reactNativeReanimated.default.View, { style: commit })); }); const styles = _reactNative.StyleSheet.create({ commitView: { display: "none", position: "absolute" } }); var _default = exports.default = KeyboardChatScrollView; //# sourceMappingURL=index.js.map