Modal.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. "use client";
  2. var __rest = this && this.__rest || function (s, e) {
  3. var t = {};
  4. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  5. if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
  6. if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  7. }
  8. return t;
  9. };
  10. import * as React from 'react';
  11. import CloseOutlined from "@ant-design/icons/es/icons/CloseOutlined";
  12. import classNames from 'classnames';
  13. import Dialog from 'rc-dialog';
  14. import { composeRef } from "rc-util/es/ref";
  15. import ContextIsolator from '../_util/ContextIsolator';
  16. import useClosable, { pickClosable } from '../_util/hooks/useClosable';
  17. import { useZIndex } from '../_util/hooks/useZIndex';
  18. import { getTransitionName } from '../_util/motion';
  19. import { canUseDocElement } from '../_util/styleChecker';
  20. import { devUseWarning } from '../_util/warning';
  21. import zIndexContext from '../_util/zindexContext';
  22. import { ConfigContext } from '../config-provider';
  23. import useCSSVarCls from '../config-provider/hooks/useCSSVarCls';
  24. import Skeleton from '../skeleton';
  25. import { usePanelRef } from '../watermark/context';
  26. import { Footer, renderCloseIcon } from './shared';
  27. import useStyle from './style';
  28. let mousePosition;
  29. // ref: https://github.com/ant-design/ant-design/issues/15795
  30. const getClickPosition = e => {
  31. mousePosition = {
  32. x: e.pageX,
  33. y: e.pageY
  34. };
  35. // 100ms 内发生过点击事件,则从点击位置动画展示
  36. // 否则直接 zoom 展示
  37. // 这样可以兼容非点击方式展开
  38. setTimeout(() => {
  39. mousePosition = null;
  40. }, 100);
  41. };
  42. // 只有点击事件支持从鼠标位置动画展开
  43. if (canUseDocElement()) {
  44. document.documentElement.addEventListener('click', getClickPosition, true);
  45. }
  46. const Modal = props => {
  47. const {
  48. prefixCls: customizePrefixCls,
  49. className,
  50. rootClassName,
  51. open,
  52. wrapClassName,
  53. centered,
  54. getContainer,
  55. focusTriggerAfterClose = true,
  56. style,
  57. // Deprecated
  58. visible,
  59. width = 520,
  60. footer,
  61. classNames: modalClassNames,
  62. styles: modalStyles,
  63. children,
  64. loading,
  65. confirmLoading,
  66. zIndex: customizeZIndex,
  67. mousePosition: customizeMousePosition,
  68. onOk,
  69. onCancel,
  70. destroyOnHidden,
  71. destroyOnClose,
  72. panelRef = null
  73. } = props,
  74. restProps = __rest(props, ["prefixCls", "className", "rootClassName", "open", "wrapClassName", "centered", "getContainer", "focusTriggerAfterClose", "style", "visible", "width", "footer", "classNames", "styles", "children", "loading", "confirmLoading", "zIndex", "mousePosition", "onOk", "onCancel", "destroyOnHidden", "destroyOnClose", "panelRef"]);
  75. const {
  76. getPopupContainer: getContextPopupContainer,
  77. getPrefixCls,
  78. direction,
  79. modal: modalContext
  80. } = React.useContext(ConfigContext);
  81. const handleCancel = e => {
  82. if (confirmLoading) {
  83. return;
  84. }
  85. onCancel === null || onCancel === void 0 ? void 0 : onCancel(e);
  86. };
  87. const handleOk = e => {
  88. onOk === null || onOk === void 0 ? void 0 : onOk(e);
  89. };
  90. if (process.env.NODE_ENV !== 'production') {
  91. const warning = devUseWarning('Modal');
  92. [['visible', 'open'], ['bodyStyle', 'styles.body'], ['maskStyle', 'styles.mask'], ['destroyOnClose', 'destroyOnHidden']].forEach(([deprecatedName, newName]) => {
  93. warning.deprecated(!(deprecatedName in props), deprecatedName, newName);
  94. });
  95. }
  96. const prefixCls = getPrefixCls('modal', customizePrefixCls);
  97. const rootPrefixCls = getPrefixCls();
  98. // Style
  99. const rootCls = useCSSVarCls(prefixCls);
  100. const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls, rootCls);
  101. const wrapClassNameExtended = classNames(wrapClassName, {
  102. [`${prefixCls}-centered`]: centered !== null && centered !== void 0 ? centered : modalContext === null || modalContext === void 0 ? void 0 : modalContext.centered,
  103. [`${prefixCls}-wrap-rtl`]: direction === 'rtl'
  104. });
  105. const dialogFooter = footer !== null && !loading ? (/*#__PURE__*/React.createElement(Footer, Object.assign({}, props, {
  106. onOk: handleOk,
  107. onCancel: handleCancel
  108. }))) : null;
  109. const [mergedClosable, mergedCloseIcon, closeBtnIsDisabled, ariaProps] = useClosable(pickClosable(props), pickClosable(modalContext), {
  110. closable: true,
  111. closeIcon: /*#__PURE__*/React.createElement(CloseOutlined, {
  112. className: `${prefixCls}-close-icon`
  113. }),
  114. closeIconRender: icon => renderCloseIcon(prefixCls, icon)
  115. });
  116. // ============================ Refs ============================
  117. // Select `ant-modal-content` by `panelRef`
  118. const innerPanelRef = usePanelRef(`.${prefixCls}-content`);
  119. const mergedPanelRef = composeRef(panelRef, innerPanelRef);
  120. // ============================ zIndex ============================
  121. const [zIndex, contextZIndex] = useZIndex('Modal', customizeZIndex);
  122. // =========================== Width ============================
  123. const [numWidth, responsiveWidth] = React.useMemo(() => {
  124. if (width && typeof width === 'object') {
  125. return [undefined, width];
  126. }
  127. return [width, undefined];
  128. }, [width]);
  129. const responsiveWidthVars = React.useMemo(() => {
  130. const vars = {};
  131. if (responsiveWidth) {
  132. Object.keys(responsiveWidth).forEach(breakpoint => {
  133. const breakpointWidth = responsiveWidth[breakpoint];
  134. if (breakpointWidth !== undefined) {
  135. vars[`--${prefixCls}-${breakpoint}-width`] = typeof breakpointWidth === 'number' ? `${breakpointWidth}px` : breakpointWidth;
  136. }
  137. });
  138. }
  139. return vars;
  140. }, [responsiveWidth]);
  141. // =========================== Render ===========================
  142. return wrapCSSVar(/*#__PURE__*/React.createElement(ContextIsolator, {
  143. form: true,
  144. space: true
  145. }, /*#__PURE__*/React.createElement(zIndexContext.Provider, {
  146. value: contextZIndex
  147. }, /*#__PURE__*/React.createElement(Dialog, Object.assign({
  148. width: numWidth
  149. }, restProps, {
  150. zIndex: zIndex,
  151. getContainer: getContainer === undefined ? getContextPopupContainer : getContainer,
  152. prefixCls: prefixCls,
  153. rootClassName: classNames(hashId, rootClassName, cssVarCls, rootCls),
  154. footer: dialogFooter,
  155. visible: open !== null && open !== void 0 ? open : visible,
  156. mousePosition: customizeMousePosition !== null && customizeMousePosition !== void 0 ? customizeMousePosition : mousePosition,
  157. onClose: handleCancel,
  158. closable: mergedClosable ? Object.assign({
  159. disabled: closeBtnIsDisabled,
  160. closeIcon: mergedCloseIcon
  161. }, ariaProps) : mergedClosable,
  162. closeIcon: mergedCloseIcon,
  163. focusTriggerAfterClose: focusTriggerAfterClose,
  164. transitionName: getTransitionName(rootPrefixCls, 'zoom', props.transitionName),
  165. maskTransitionName: getTransitionName(rootPrefixCls, 'fade', props.maskTransitionName),
  166. className: classNames(hashId, className, modalContext === null || modalContext === void 0 ? void 0 : modalContext.className),
  167. style: Object.assign(Object.assign(Object.assign({}, modalContext === null || modalContext === void 0 ? void 0 : modalContext.style), style), responsiveWidthVars),
  168. classNames: Object.assign(Object.assign(Object.assign({}, modalContext === null || modalContext === void 0 ? void 0 : modalContext.classNames), modalClassNames), {
  169. wrapper: classNames(wrapClassNameExtended, modalClassNames === null || modalClassNames === void 0 ? void 0 : modalClassNames.wrapper)
  170. }),
  171. styles: Object.assign(Object.assign({}, modalContext === null || modalContext === void 0 ? void 0 : modalContext.styles), modalStyles),
  172. panelRef: mergedPanelRef,
  173. // TODO: In the future, destroyOnClose in rc-dialog needs to be upgrade to destroyOnHidden
  174. destroyOnClose: destroyOnHidden !== null && destroyOnHidden !== void 0 ? destroyOnHidden : destroyOnClose
  175. }), loading ? (/*#__PURE__*/React.createElement(Skeleton, {
  176. active: true,
  177. title: false,
  178. paragraph: {
  179. rows: 4
  180. },
  181. className: `${prefixCls}-body-skeleton`
  182. })) : children))));
  183. };
  184. export default Modal;