antd-mobile-taro-ui
Version:
以antd-mobile为设计标准,基于taro框架的微信小程序组件库
77 lines (74 loc) • 3.08 kB
JavaScript
import React, { useRef, useState } from 'react';
import { renderToContainer } from 'antd-mobile/es/utils/render-to-container';
import { withStopPropagation } from 'antd-mobile/es/utils/with-stop-propagation';
import { mergeProps } from 'antd-mobile/es/utils/with-default-props';
import { useIsomorphicLayoutEffect } from 'ahooks';
import { useInnerVisible } from 'antd-mobile/es/utils/use-inner-visible';
import classNames from 'classnames';
import { withNativeProps } from 'antd-mobile/es/utils/native-props';
import { ShouldRender } from 'antd-mobile/es/utils/should-render';
import { useLockScroll } from 'antd-mobile/es/utils/use-lock-scroll';
import { CloseIcon } from 'antd-mobile-taro-icons';
import { View } from '@tarojs/components';
import Mask from '../mask';
import { defaultPopupBaseProps } from '../popup/popup-base-props';
const defaultProps = Object.assign(Object.assign({}, defaultPopupBaseProps), {
getContainer: null
});
export const CenterPopup = p => {
const props = mergeProps(defaultProps, p);
const [active, setActive] = useState(props.visible);
useIsomorphicLayoutEffect(() => {
if (props.visible) {
setActive(true);
}
}, [props.visible]);
const ref = useRef(null);
useLockScroll(ref, props.disableBodyScroll && active);
const maskVisible = useInnerVisible(active && props.visible);
const body = React.createElement(View, {
className: classNames('adm-center-popup-body', props.bodyClassName),
style: props.bodyStyle
}, props.children);
const node = withStopPropagation(props.stopPropagation, withNativeProps(props, React.createElement(View, {
className: 'adm-center-popup',
style: {
display: active ? undefined : 'none',
pointerEvents: active ? undefined : 'none'
}
}, props.mask && React.createElement(Mask, {
visible: maskVisible,
forceRender: props.forceRender,
destroyOnClose: props.destroyOnClose,
onMaskClick: e => {
var _a, _b;
(_a = props.onMaskClick) === null || _a === void 0 ? void 0 : _a.call(props, e);
if (props.closeOnMaskClick) {
(_b = props.onClose) === null || _b === void 0 ? void 0 : _b.call(props);
}
},
style: props.maskStyle,
className: classNames('adm-center-popup-mask', props.maskClassName),
disableBodyScroll: false,
stopPropagation: props.stopPropagation
}), React.createElement(View, {
className: 'adm-center-popup-wrap',
"aria-label": props['aria-label']
}, React.createElement(View, {
style: {
display: maskVisible ? undefined : 'none'
},
ref: ref
}, props.showCloseButton && React.createElement(View, {
className: classNames('adm-center-popup-close', 'adm-plain-anchor'),
onClick: () => {
var _a;
(_a = props.onClose) === null || _a === void 0 ? void 0 : _a.call(props);
}
}, React.createElement(CloseIcon, null)), body)))));
return React.createElement(ShouldRender, {
active: active,
forceRender: props.forceRender,
destroyOnClose: props.destroyOnClose
}, renderToContainer(props.getContainer, node));
};