@fruits-chain/react-native-xiaoshu
Version:
🌈 React Native UI library
98 lines (94 loc) • 3.39 kB
JavaScript
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
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';
import Popup from '../popup/popup';
const POPUP_STYLE = {
backgroundColor: 'transparent'
};
const DropdownPopup = _ref => {
let {
zIndex,
closeOnPressOutside = true,
targetHeight,
targetPageY,
onPressShade,
safeAreaInset = true,
children,
...restProps
} = _ref;
const insets = useSafeAreaInsets();
const {
height: windowHeight
} = useWindowDimensions();
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 placeholderJSX = /*#__PURE__*/React.createElement(TouchableWithoutFeedback, {
onPress: closeOnPressOutside ? onPressShade : undefined
}, /*#__PURE__*/React.createElement(View, {
style: {
height: _isBottom ? insets.top : insets.bottom
}
}));
return /*#__PURE__*/React.createElement(React.Fragment, null, restProps.visible ? /*#__PURE__*/React.createElement(TouchableWithoutFeedback, {
onPress: closeOnPressOutside ? onPressShade : undefined
}, /*#__PURE__*/React.createElement(View, {
style: _shadeStyles
})) : null, /*#__PURE__*/React.createElement(View, {
style: _boxStyles,
pointerEvents: "box-none",
onLayout: onLayoutPlace
}, /*#__PURE__*/React.createElement(Popup, _extends({}, restProps, {
style: POPUP_STYLE,
position: _isBottom ? 'bottom' : 'top'
}), /*#__PURE__*/React.createElement(View, {
style: wrapperStyle
}, _isBottom && safeAreaInset ? placeholderJSX : null, children, !_isBottom && safeAreaInset ? placeholderJSX : null))));
};
export default /*#__PURE__*/memo(DropdownPopup);
//# sourceMappingURL=dropdown-popup.js.map