UNPKG

@fruits-chain/react-native-xiaoshu

Version:
132 lines (128 loc) 5.14 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _react = _interopRequireWildcard(require("react")); var _reactNative = require("react-native"); var _reactNativeSafeAreaContext = require("react-native-safe-area-context"); var _useStateUpdate = _interopRequireDefault(require("../hooks/useStateUpdate.js")); var _popup = _interopRequireDefault(require("../popup/popup.js")); var _index = _interopRequireDefault(require("../theme/index.js")); var _context = require("./context.js"); var _style = require("./style.js"); var _jsxRuntime = require("react/jsx-runtime"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; } const POPUP_STYLE = { backgroundColor: 'transparent' }; const DropdownPopup = ({ zIndex, closeOnPressOutside = true, targetHeight, targetPageY, onPressShade, safeAreaInset = true, showShade = true, contentStyle, children, ...restProps }) => { const insets = (0, _reactNativeSafeAreaContext.useSafeAreaInsets)(); const { height: windowHeight } = (0, _reactNative.useWindowDimensions)(); const { theme } = (0, _context.useDropdownConfig)(); const [CV] = _index.default.useStyle({ varCreator: _style.varCreator, theme }); const [wrapperStyle, setWrapperStyle] = (0, _useStateUpdate.default)({ maxHeight: 0 }); /** 弹出层可以的最大高度 */ const onLayoutPlace = (0, _react.useCallback)(e => { setWrapperStyle({ maxHeight: e.nativeEvent.layout.height }); }, []); const [_isBottom, _shadeStyles, _boxStyles] = (0, _react.useMemo)(() => { const topHeight = targetPageY - insets.top; const bottomHeight = windowHeight - targetPageY - targetHeight - insets.bottom; const isBottom = topHeight >= bottomHeight; const shadeStyles = { position: 'absolute', left: 0, right: 0, zIndex, // backgroundColor: '#f30', // to test ui // state.shadeStyle, ...(isBottom ? { top: targetPageY, bottom: 0 } : { top: 0, height: targetPageY + targetHeight }) }; const boxStyles = { position: 'absolute', left: 0, right: 0, zIndex, overflow: 'hidden', // backgroundColor: '#000', // to test ui // state.ctxStyle, ...(isBottom ? { top: 0, height: targetPageY } : { top: targetPageY + targetHeight, bottom: 0 }) }; return [isBottom, shadeStyles, boxStyles]; }, [insets.bottom, insets.top, targetHeight, targetPageY, windowHeight, zIndex]); const placeholderHeight = _isBottom ? insets.top : insets.bottom; const placeholderJSX = /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.TouchableWithoutFeedback, { onPress: closeOnPressOutside ? onPressShade : undefined, children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, { style: { height: placeholderHeight } }) }); return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, { children: [restProps.visible && showShade ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.TouchableWithoutFeedback, { onPress: closeOnPressOutside ? onPressShade : undefined, children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, { style: _shadeStyles }) }) : null, /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, { style: _boxStyles, pointerEvents: "box-none", onLayout: onLayoutPlace, children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_popup.default, { ...restProps, style: POPUP_STYLE, position: _isBottom ? 'bottom' : 'top', children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, { style: wrapperStyle, children: [_isBottom && safeAreaInset ? placeholderJSX : null, /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, { style: [{ maxHeight: wrapperStyle.maxHeight - placeholderHeight, backgroundColor: CV.dropdown_background_color }, contentStyle], children: children }), !_isBottom && safeAreaInset ? placeholderJSX : null] }) }) })] }); }; var _default = exports.default = /*#__PURE__*/(0, _react.memo)(DropdownPopup); //# sourceMappingURL=dropdown-popup.js.map