index.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 CSSMotion from 'rc-motion';
  6. import ResizeObserver from 'rc-resize-observer';
  7. import useLayoutEffect from "rc-util/es/hooks/useLayoutEffect";
  8. import { composeRef } from "rc-util/es/ref";
  9. import * as React from 'react';
  10. import Arrow from "./Arrow";
  11. import Mask from "./Mask";
  12. import PopupContent from "./PopupContent";
  13. var Popup = /*#__PURE__*/React.forwardRef(function (props, ref) {
  14. var popup = props.popup,
  15. className = props.className,
  16. prefixCls = props.prefixCls,
  17. style = props.style,
  18. target = props.target,
  19. _onVisibleChanged = props.onVisibleChanged,
  20. open = props.open,
  21. keepDom = props.keepDom,
  22. fresh = props.fresh,
  23. onClick = props.onClick,
  24. mask = props.mask,
  25. arrow = props.arrow,
  26. arrowPos = props.arrowPos,
  27. align = props.align,
  28. motion = props.motion,
  29. maskMotion = props.maskMotion,
  30. forceRender = props.forceRender,
  31. getPopupContainer = props.getPopupContainer,
  32. autoDestroy = props.autoDestroy,
  33. Portal = props.portal,
  34. zIndex = props.zIndex,
  35. onMouseEnter = props.onMouseEnter,
  36. onMouseLeave = props.onMouseLeave,
  37. onPointerEnter = props.onPointerEnter,
  38. onPointerDownCapture = props.onPointerDownCapture,
  39. ready = props.ready,
  40. offsetX = props.offsetX,
  41. offsetY = props.offsetY,
  42. offsetR = props.offsetR,
  43. offsetB = props.offsetB,
  44. onAlign = props.onAlign,
  45. onPrepare = props.onPrepare,
  46. stretch = props.stretch,
  47. targetWidth = props.targetWidth,
  48. targetHeight = props.targetHeight;
  49. var childNode = typeof popup === 'function' ? popup() : popup;
  50. // We can not remove holder only when motion finished.
  51. var isNodeVisible = open || keepDom;
  52. // ======================= Container ========================
  53. var getPopupContainerNeedParams = (getPopupContainer === null || getPopupContainer === void 0 ? void 0 : getPopupContainer.length) > 0;
  54. var _React$useState = React.useState(!getPopupContainer || !getPopupContainerNeedParams),
  55. _React$useState2 = _slicedToArray(_React$useState, 2),
  56. show = _React$useState2[0],
  57. setShow = _React$useState2[1];
  58. // Delay to show since `getPopupContainer` need target element
  59. useLayoutEffect(function () {
  60. if (!show && getPopupContainerNeedParams && target) {
  61. setShow(true);
  62. }
  63. }, [show, getPopupContainerNeedParams, target]);
  64. // ========================= Render =========================
  65. if (!show) {
  66. return null;
  67. }
  68. // >>>>> Offset
  69. var AUTO = 'auto';
  70. var offsetStyle = {
  71. left: '-1000vw',
  72. top: '-1000vh',
  73. right: AUTO,
  74. bottom: AUTO
  75. };
  76. // Set align style
  77. if (ready || !open) {
  78. var _experimental;
  79. var points = align.points;
  80. var dynamicInset = align.dynamicInset || ((_experimental = align._experimental) === null || _experimental === void 0 ? void 0 : _experimental.dynamicInset);
  81. var alignRight = dynamicInset && points[0][1] === 'r';
  82. var alignBottom = dynamicInset && points[0][0] === 'b';
  83. if (alignRight) {
  84. offsetStyle.right = offsetR;
  85. offsetStyle.left = AUTO;
  86. } else {
  87. offsetStyle.left = offsetX;
  88. offsetStyle.right = AUTO;
  89. }
  90. if (alignBottom) {
  91. offsetStyle.bottom = offsetB;
  92. offsetStyle.top = AUTO;
  93. } else {
  94. offsetStyle.top = offsetY;
  95. offsetStyle.bottom = AUTO;
  96. }
  97. }
  98. // >>>>> Misc
  99. var miscStyle = {};
  100. if (stretch) {
  101. if (stretch.includes('height') && targetHeight) {
  102. miscStyle.height = targetHeight;
  103. } else if (stretch.includes('minHeight') && targetHeight) {
  104. miscStyle.minHeight = targetHeight;
  105. }
  106. if (stretch.includes('width') && targetWidth) {
  107. miscStyle.width = targetWidth;
  108. } else if (stretch.includes('minWidth') && targetWidth) {
  109. miscStyle.minWidth = targetWidth;
  110. }
  111. }
  112. if (!open) {
  113. miscStyle.pointerEvents = 'none';
  114. }
  115. return /*#__PURE__*/React.createElement(Portal, {
  116. open: forceRender || isNodeVisible,
  117. getContainer: getPopupContainer && function () {
  118. return getPopupContainer(target);
  119. },
  120. autoDestroy: autoDestroy
  121. }, /*#__PURE__*/React.createElement(Mask, {
  122. prefixCls: prefixCls,
  123. open: open,
  124. zIndex: zIndex,
  125. mask: mask,
  126. motion: maskMotion
  127. }), /*#__PURE__*/React.createElement(ResizeObserver, {
  128. onResize: onAlign,
  129. disabled: !open
  130. }, function (resizeObserverRef) {
  131. return /*#__PURE__*/React.createElement(CSSMotion, _extends({
  132. motionAppear: true,
  133. motionEnter: true,
  134. motionLeave: true,
  135. removeOnLeave: false,
  136. forceRender: forceRender,
  137. leavedClassName: "".concat(prefixCls, "-hidden")
  138. }, motion, {
  139. onAppearPrepare: onPrepare,
  140. onEnterPrepare: onPrepare,
  141. visible: open,
  142. onVisibleChanged: function onVisibleChanged(nextVisible) {
  143. var _motion$onVisibleChan;
  144. motion === null || motion === void 0 || (_motion$onVisibleChan = motion.onVisibleChanged) === null || _motion$onVisibleChan === void 0 || _motion$onVisibleChan.call(motion, nextVisible);
  145. _onVisibleChanged(nextVisible);
  146. }
  147. }), function (_ref, motionRef) {
  148. var motionClassName = _ref.className,
  149. motionStyle = _ref.style;
  150. var cls = classNames(prefixCls, motionClassName, className);
  151. return /*#__PURE__*/React.createElement("div", {
  152. ref: composeRef(resizeObserverRef, ref, motionRef),
  153. className: cls,
  154. style: _objectSpread(_objectSpread(_objectSpread(_objectSpread({
  155. '--arrow-x': "".concat(arrowPos.x || 0, "px"),
  156. '--arrow-y': "".concat(arrowPos.y || 0, "px")
  157. }, offsetStyle), miscStyle), motionStyle), {}, {
  158. boxSizing: 'border-box',
  159. zIndex: zIndex
  160. }, style),
  161. onMouseEnter: onMouseEnter,
  162. onMouseLeave: onMouseLeave,
  163. onPointerEnter: onPointerEnter,
  164. onClick: onClick,
  165. onPointerDownCapture: onPointerDownCapture
  166. }, arrow && /*#__PURE__*/React.createElement(Arrow, {
  167. prefixCls: prefixCls,
  168. arrow: arrow,
  169. arrowPos: arrowPos,
  170. align: align
  171. }), /*#__PURE__*/React.createElement(PopupContent, {
  172. cache: !open && !fresh
  173. }, childNode));
  174. });
  175. }));
  176. });
  177. if (process.env.NODE_ENV !== 'production') {
  178. Popup.displayName = 'Popup';
  179. }
  180. export default Popup;