index.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. import _extends from "@babel/runtime/helpers/esm/extends";
  2. import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
  3. import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
  4. import classNames from 'classnames';
  5. import contains from "rc-util/es/Dom/contains";
  6. import useId from "rc-util/es/hooks/useId";
  7. import KeyCode from "rc-util/es/KeyCode";
  8. import pickAttrs from "rc-util/es/pickAttrs";
  9. import * as React from 'react';
  10. import { useEffect, useRef } from 'react';
  11. import { getMotionName } from "../util";
  12. import Content from "./Content";
  13. import Mask from "./Mask";
  14. import { warning } from "rc-util/es/warning";
  15. var Dialog = function Dialog(props) {
  16. var _props$prefixCls = props.prefixCls,
  17. prefixCls = _props$prefixCls === void 0 ? 'rc-dialog' : _props$prefixCls,
  18. zIndex = props.zIndex,
  19. _props$visible = props.visible,
  20. visible = _props$visible === void 0 ? false : _props$visible,
  21. _props$keyboard = props.keyboard,
  22. keyboard = _props$keyboard === void 0 ? true : _props$keyboard,
  23. _props$focusTriggerAf = props.focusTriggerAfterClose,
  24. focusTriggerAfterClose = _props$focusTriggerAf === void 0 ? true : _props$focusTriggerAf,
  25. wrapStyle = props.wrapStyle,
  26. wrapClassName = props.wrapClassName,
  27. wrapProps = props.wrapProps,
  28. onClose = props.onClose,
  29. afterOpenChange = props.afterOpenChange,
  30. afterClose = props.afterClose,
  31. transitionName = props.transitionName,
  32. animation = props.animation,
  33. _props$closable = props.closable,
  34. closable = _props$closable === void 0 ? true : _props$closable,
  35. _props$mask = props.mask,
  36. mask = _props$mask === void 0 ? true : _props$mask,
  37. maskTransitionName = props.maskTransitionName,
  38. maskAnimation = props.maskAnimation,
  39. _props$maskClosable = props.maskClosable,
  40. maskClosable = _props$maskClosable === void 0 ? true : _props$maskClosable,
  41. maskStyle = props.maskStyle,
  42. maskProps = props.maskProps,
  43. rootClassName = props.rootClassName,
  44. modalClassNames = props.classNames,
  45. modalStyles = props.styles;
  46. if (process.env.NODE_ENV !== 'production') {
  47. ['wrapStyle', 'bodyStyle', 'maskStyle'].forEach(function (prop) {
  48. // (prop in props) && console.error(`Warning: ${prop} is deprecated, please use styles instead.`)
  49. warning(!(prop in props), "".concat(prop, " is deprecated, please use styles instead."));
  50. });
  51. if ('wrapClassName' in props) {
  52. warning(false, "wrapClassName is deprecated, please use classNames instead.");
  53. }
  54. }
  55. var lastOutSideActiveElementRef = useRef();
  56. var wrapperRef = useRef();
  57. var contentRef = useRef();
  58. var _React$useState = React.useState(visible),
  59. _React$useState2 = _slicedToArray(_React$useState, 2),
  60. animatedVisible = _React$useState2[0],
  61. setAnimatedVisible = _React$useState2[1];
  62. // ========================== Init ==========================
  63. var ariaId = useId();
  64. function saveLastOutSideActiveElementRef() {
  65. if (!contains(wrapperRef.current, document.activeElement)) {
  66. lastOutSideActiveElementRef.current = document.activeElement;
  67. }
  68. }
  69. function focusDialogContent() {
  70. if (!contains(wrapperRef.current, document.activeElement)) {
  71. var _contentRef$current;
  72. (_contentRef$current = contentRef.current) === null || _contentRef$current === void 0 || _contentRef$current.focus();
  73. }
  74. }
  75. // ========================= Events =========================
  76. function onDialogVisibleChanged(newVisible) {
  77. // Try to focus
  78. if (newVisible) {
  79. focusDialogContent();
  80. } else {
  81. // Clean up scroll bar & focus back
  82. setAnimatedVisible(false);
  83. if (mask && lastOutSideActiveElementRef.current && focusTriggerAfterClose) {
  84. try {
  85. lastOutSideActiveElementRef.current.focus({
  86. preventScroll: true
  87. });
  88. } catch (e) {
  89. // Do nothing
  90. }
  91. lastOutSideActiveElementRef.current = null;
  92. }
  93. // Trigger afterClose only when change visible from true to false
  94. if (animatedVisible) {
  95. afterClose === null || afterClose === void 0 || afterClose();
  96. }
  97. }
  98. afterOpenChange === null || afterOpenChange === void 0 || afterOpenChange(newVisible);
  99. }
  100. function onInternalClose(e) {
  101. onClose === null || onClose === void 0 || onClose(e);
  102. }
  103. // >>> Content
  104. var contentClickRef = useRef(false);
  105. var contentTimeoutRef = useRef();
  106. // We need record content click incase content popup out of dialog
  107. var onContentMouseDown = function onContentMouseDown() {
  108. clearTimeout(contentTimeoutRef.current);
  109. contentClickRef.current = true;
  110. };
  111. var onContentMouseUp = function onContentMouseUp() {
  112. contentTimeoutRef.current = setTimeout(function () {
  113. contentClickRef.current = false;
  114. });
  115. };
  116. // >>> Wrapper
  117. // Close only when element not on dialog
  118. var onWrapperClick = null;
  119. if (maskClosable) {
  120. onWrapperClick = function onWrapperClick(e) {
  121. if (contentClickRef.current) {
  122. contentClickRef.current = false;
  123. } else if (wrapperRef.current === e.target) {
  124. onInternalClose(e);
  125. }
  126. };
  127. }
  128. function onWrapperKeyDown(e) {
  129. if (keyboard && e.keyCode === KeyCode.ESC) {
  130. e.stopPropagation();
  131. onInternalClose(e);
  132. return;
  133. }
  134. // keep focus inside dialog
  135. if (visible && e.keyCode === KeyCode.TAB) {
  136. contentRef.current.changeActive(!e.shiftKey);
  137. }
  138. }
  139. // ========================= Effect =========================
  140. useEffect(function () {
  141. if (visible) {
  142. setAnimatedVisible(true);
  143. saveLastOutSideActiveElementRef();
  144. }
  145. }, [visible]);
  146. // Remove direct should also check the scroll bar update
  147. useEffect(function () {
  148. return function () {
  149. clearTimeout(contentTimeoutRef.current);
  150. };
  151. }, []);
  152. var mergedStyle = _objectSpread(_objectSpread(_objectSpread({
  153. zIndex: zIndex
  154. }, wrapStyle), modalStyles === null || modalStyles === void 0 ? void 0 : modalStyles.wrapper), {}, {
  155. display: !animatedVisible ? 'none' : null
  156. });
  157. // ========================= Render =========================
  158. return /*#__PURE__*/React.createElement("div", _extends({
  159. className: classNames("".concat(prefixCls, "-root"), rootClassName)
  160. }, pickAttrs(props, {
  161. data: true
  162. })), /*#__PURE__*/React.createElement(Mask, {
  163. prefixCls: prefixCls,
  164. visible: mask && visible,
  165. motionName: getMotionName(prefixCls, maskTransitionName, maskAnimation),
  166. style: _objectSpread(_objectSpread({
  167. zIndex: zIndex
  168. }, maskStyle), modalStyles === null || modalStyles === void 0 ? void 0 : modalStyles.mask),
  169. maskProps: maskProps,
  170. className: modalClassNames === null || modalClassNames === void 0 ? void 0 : modalClassNames.mask
  171. }), /*#__PURE__*/React.createElement("div", _extends({
  172. tabIndex: -1,
  173. onKeyDown: onWrapperKeyDown,
  174. className: classNames("".concat(prefixCls, "-wrap"), wrapClassName, modalClassNames === null || modalClassNames === void 0 ? void 0 : modalClassNames.wrapper),
  175. ref: wrapperRef,
  176. onClick: onWrapperClick,
  177. style: mergedStyle
  178. }, wrapProps), /*#__PURE__*/React.createElement(Content, _extends({}, props, {
  179. onMouseDown: onContentMouseDown,
  180. onMouseUp: onContentMouseUp,
  181. ref: contentRef,
  182. closable: closable,
  183. ariaId: ariaId,
  184. prefixCls: prefixCls,
  185. visible: visible && animatedVisible,
  186. onClose: onInternalClose,
  187. onVisibleChanged: onDialogVisibleChanged,
  188. motionName: getMotionName(prefixCls, transitionName, animation)
  189. }))));
  190. };
  191. export default Dialog;