UNPKG

@fruits-chain/react-native-xiaoshu

Version:
125 lines (121 loc) 3.75 kB
"use strict"; import React, { useCallback, useMemo, memo } from 'react'; import { TouchableWithoutFeedback, View, useWindowDimensions } from 'react-native'; import { useSafeAreaInsets } from 'react-native-safe-area-context'; import useState from "../hooks/useStateUpdate.js"; import Popup from "../popup/popup.js"; import Theme from "../theme/index.js"; import { useDropdownConfig } from "./context.js"; import { varCreator } from "./style.js"; import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime"; const POPUP_STYLE = { backgroundColor: 'transparent' }; const DropdownPopup = ({ zIndex, closeOnPressOutside = true, targetHeight, targetPageY, onPressShade, safeAreaInset = true, showShade = true, contentStyle, children, ...restProps }) => { const insets = useSafeAreaInsets(); const { height: windowHeight } = useWindowDimensions(); const { theme } = useDropdownConfig(); const [CV] = Theme.useStyle({ varCreator, theme }); const [wrapperStyle, setWrapperStyle] = useState({ maxHeight: 0 }); /** 弹出层可以的最大高度 */ const onLayoutPlace = useCallback(e => { setWrapperStyle({ maxHeight: e.nativeEvent.layout.height }); }, []); const [_isBottom, _shadeStyles, _boxStyles] = 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__*/_jsx(TouchableWithoutFeedback, { onPress: closeOnPressOutside ? onPressShade : undefined, children: /*#__PURE__*/_jsx(View, { style: { height: placeholderHeight } }) }); return /*#__PURE__*/_jsxs(_Fragment, { children: [restProps.visible && showShade ? /*#__PURE__*/_jsx(TouchableWithoutFeedback, { onPress: closeOnPressOutside ? onPressShade : undefined, children: /*#__PURE__*/_jsx(View, { style: _shadeStyles }) }) : null, /*#__PURE__*/_jsx(View, { style: _boxStyles, pointerEvents: "box-none", onLayout: onLayoutPlace, children: /*#__PURE__*/_jsx(Popup, { ...restProps, style: POPUP_STYLE, position: _isBottom ? 'bottom' : 'top', children: /*#__PURE__*/_jsxs(View, { style: wrapperStyle, children: [_isBottom && safeAreaInset ? placeholderJSX : null, /*#__PURE__*/_jsx(View, { style: [{ maxHeight: wrapperStyle.maxHeight - placeholderHeight, backgroundColor: CV.dropdown_background_color }, contentStyle], children: children }), !_isBottom && safeAreaInset ? placeholderJSX : null] }) }) })] }); }; export default /*#__PURE__*/memo(DropdownPopup); //# sourceMappingURL=dropdown-popup.js.map