2023-07-31 23:55:25 +08:00
|
|
|
import CloseOutlined from '@ant-design/icons/CloseOutlined';
|
2022-06-06 23:39:00 +08:00
|
|
|
import classNames from 'classnames';
|
|
|
|
import Dialog from 'rc-dialog';
|
2023-05-06 15:49:37 +08:00
|
|
|
import * as React from 'react';
|
2023-06-16 15:36:28 +08:00
|
|
|
import useClosable from '../_util/hooks/useClosable';
|
2021-02-08 17:09:13 +08:00
|
|
|
import { getTransitionName } from '../_util/motion';
|
2022-06-06 23:39:00 +08:00
|
|
|
import { canUseDocElement } from '../_util/styleChecker';
|
2022-08-23 16:55:57 +08:00
|
|
|
import warning from '../_util/warning';
|
2023-06-05 09:57:26 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
|
|
|
import { NoFormStyle } from '../form/context';
|
|
|
|
import { NoCompactStyle } from '../space/Compact';
|
|
|
|
import type { ModalProps, MousePosition } from './interface';
|
2023-06-06 09:54:14 +08:00
|
|
|
import { Footer, renderCloseIcon } from './shared';
|
2022-04-26 22:45:39 +08:00
|
|
|
import useStyle from './style';
|
2023-08-08 16:48:26 +08:00
|
|
|
import { usePanelRef } from '../watermark/context';
|
2016-01-12 17:07:24 +08:00
|
|
|
|
2022-11-15 22:20:02 +08:00
|
|
|
let mousePosition: MousePosition;
|
2016-01-12 17:07:24 +08:00
|
|
|
|
2019-04-18 23:20:13 +08:00
|
|
|
// ref: https://github.com/ant-design/ant-design/issues/15795
|
|
|
|
const getClickPosition = (e: MouseEvent) => {
|
|
|
|
mousePosition = {
|
|
|
|
x: e.pageX,
|
|
|
|
y: e.pageY,
|
|
|
|
};
|
|
|
|
// 100ms 内发生过点击事件,则从点击位置动画展示
|
|
|
|
// 否则直接 zoom 展示
|
|
|
|
// 这样可以兼容非点击方式展开
|
2020-06-08 18:01:50 +08:00
|
|
|
setTimeout(() => {
|
|
|
|
mousePosition = null;
|
|
|
|
}, 100);
|
2019-04-18 23:20:13 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// 只有点击事件支持从鼠标位置动画展开
|
2021-01-27 14:03:05 +08:00
|
|
|
if (canUseDocElement()) {
|
2020-11-27 15:53:51 +08:00
|
|
|
document.documentElement.addEventListener('click', getClickPosition, true);
|
2019-04-18 23:20:13 +08:00
|
|
|
}
|
|
|
|
|
2022-11-17 14:04:31 +08:00
|
|
|
const Modal: React.FC<ModalProps> = (props) => {
|
2021-07-26 16:47:55 +08:00
|
|
|
const {
|
|
|
|
getPopupContainer: getContextPopupContainer,
|
|
|
|
getPrefixCls,
|
|
|
|
direction,
|
2023-06-27 17:14:08 +08:00
|
|
|
modal,
|
2021-07-26 16:47:55 +08:00
|
|
|
} = React.useContext(ConfigContext);
|
2016-03-03 17:43:38 +08:00
|
|
|
|
2020-05-19 20:15:51 +08:00
|
|
|
const handleCancel = (e: React.MouseEvent<HTMLButtonElement>) => {
|
|
|
|
const { onCancel } = props;
|
2021-02-19 18:26:53 +08:00
|
|
|
onCancel?.(e);
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2016-01-12 17:07:24 +08:00
|
|
|
|
2020-05-19 20:15:51 +08:00
|
|
|
const handleOk = (e: React.MouseEvent<HTMLButtonElement>) => {
|
|
|
|
const { onOk } = props;
|
2021-02-19 18:26:53 +08:00
|
|
|
onOk?.(e);
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2016-01-12 17:07:24 +08:00
|
|
|
|
2022-08-23 16:55:57 +08:00
|
|
|
warning(
|
|
|
|
!('visible' in props),
|
|
|
|
'Modal',
|
|
|
|
`\`visible\` will be removed in next major version, please use \`open\` instead.`,
|
|
|
|
);
|
|
|
|
|
2020-05-19 20:15:51 +08:00
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
2022-06-11 21:39:58 +08:00
|
|
|
className,
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName,
|
2022-10-11 21:33:29 +08:00
|
|
|
open,
|
2020-05-19 20:15:51 +08:00
|
|
|
wrapClassName,
|
|
|
|
centered,
|
|
|
|
getContainer,
|
|
|
|
closeIcon,
|
2023-06-16 15:36:28 +08:00
|
|
|
closable,
|
2020-11-30 16:57:33 +08:00
|
|
|
focusTriggerAfterClose = true,
|
2023-06-27 17:14:08 +08:00
|
|
|
style,
|
2022-09-06 21:46:49 +08:00
|
|
|
// Deprecated
|
|
|
|
visible,
|
|
|
|
|
2022-10-02 16:50:34 +08:00
|
|
|
width = 520,
|
2023-03-13 19:55:59 +08:00
|
|
|
footer,
|
2020-05-19 20:15:51 +08:00
|
|
|
...restProps
|
|
|
|
} = props;
|
|
|
|
|
|
|
|
const prefixCls = getPrefixCls('modal', customizePrefixCls);
|
2021-02-08 17:09:13 +08:00
|
|
|
const rootPrefixCls = getPrefixCls();
|
2022-04-26 22:45:39 +08:00
|
|
|
// Style
|
|
|
|
const [wrapSSR, hashId] = useStyle(prefixCls);
|
2021-02-08 17:09:13 +08:00
|
|
|
|
2020-05-19 20:15:51 +08:00
|
|
|
const wrapClassNameExtended = classNames(wrapClassName, {
|
|
|
|
[`${prefixCls}-centered`]: !!centered,
|
|
|
|
[`${prefixCls}-wrap-rtl`]: direction === 'rtl',
|
|
|
|
});
|
2022-08-24 11:15:25 +08:00
|
|
|
|
2022-09-06 21:46:49 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
warning(!('visible' in props), 'Modal', '`visible` is deprecated, please use `open` instead.');
|
|
|
|
}
|
2022-08-24 11:15:25 +08:00
|
|
|
|
2023-03-14 15:14:40 +08:00
|
|
|
const dialogFooter =
|
|
|
|
footer === undefined ? <Footer {...props} onOk={handleOk} onCancel={handleCancel} /> : footer;
|
|
|
|
|
2023-06-16 15:36:28 +08:00
|
|
|
const [mergedClosable, mergedCloseIcon] = useClosable(
|
|
|
|
closable,
|
|
|
|
closeIcon,
|
|
|
|
(icon) => renderCloseIcon(prefixCls, icon),
|
|
|
|
<CloseOutlined className={`${prefixCls}-close-icon`} />,
|
|
|
|
true,
|
|
|
|
);
|
|
|
|
|
2023-08-08 16:48:26 +08:00
|
|
|
// ============================ Refs ============================
|
|
|
|
// Select `ant-modal-content` by `panelRef`
|
|
|
|
const panelRef = usePanelRef(`.${prefixCls}-content`);
|
|
|
|
|
|
|
|
// =========================== Render ===========================
|
2022-04-26 22:45:39 +08:00
|
|
|
return wrapSSR(
|
2022-11-22 22:52:40 +08:00
|
|
|
<NoCompactStyle>
|
|
|
|
<NoFormStyle status override>
|
|
|
|
<Dialog
|
|
|
|
width={width}
|
|
|
|
{...restProps}
|
2022-12-15 17:48:29 +08:00
|
|
|
getContainer={getContainer === undefined ? getContextPopupContainer : getContainer}
|
2022-11-22 22:52:40 +08:00
|
|
|
prefixCls={prefixCls}
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName={classNames(hashId, rootClassName)}
|
2022-11-22 22:52:40 +08:00
|
|
|
wrapClassName={wrapClassNameExtended}
|
2023-03-14 15:14:40 +08:00
|
|
|
footer={dialogFooter}
|
2022-11-22 22:52:40 +08:00
|
|
|
visible={open ?? visible}
|
|
|
|
mousePosition={restProps.mousePosition ?? mousePosition}
|
|
|
|
onClose={handleCancel}
|
2023-06-16 15:36:28 +08:00
|
|
|
closable={mergedClosable}
|
|
|
|
closeIcon={mergedCloseIcon}
|
2022-11-22 22:52:40 +08:00
|
|
|
focusTriggerAfterClose={focusTriggerAfterClose}
|
|
|
|
transitionName={getTransitionName(rootPrefixCls, 'zoom', props.transitionName)}
|
|
|
|
maskTransitionName={getTransitionName(rootPrefixCls, 'fade', props.maskTransitionName)}
|
2023-06-27 17:14:08 +08:00
|
|
|
className={classNames(hashId, className, modal?.className)}
|
|
|
|
style={{ ...modal?.style, ...style }}
|
2023-08-08 16:48:26 +08:00
|
|
|
panelRef={panelRef}
|
2022-11-22 22:52:40 +08:00
|
|
|
/>
|
|
|
|
</NoFormStyle>
|
|
|
|
</NoCompactStyle>,
|
2020-05-19 20:15:51 +08:00
|
|
|
);
|
|
|
|
};
|
2016-08-29 20:48:47 +08:00
|
|
|
|
2020-05-19 20:15:51 +08:00
|
|
|
export default Modal;
|