PopupInner.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = void 0;
  7. var _vue = require("vue");
  8. var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
  9. var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
  10. var _useVisibleStatus = _interopRequireDefault(require("./useVisibleStatus"));
  11. var _useStretchStyle = _interopRequireDefault(require("./useStretchStyle"));
  12. var _Align = _interopRequireDefault(require("../../vc-align/Align"));
  13. var _motionUtil = require("../utils/motionUtil");
  14. var _propsUtil = require("../../_util/props-util");
  15. var _classNames = _interopRequireDefault(require("../../_util/classNames"));
  16. var _interface = require("./interface");
  17. var _transition = require("../../_util/transition");
  18. var _supportsPassive = _interopRequireDefault(require("../../_util/supportsPassive"));
  19. var _default = exports.default = (0, _vue.defineComponent)({
  20. compatConfig: {
  21. MODE: 3
  22. },
  23. name: 'PopupInner',
  24. inheritAttrs: false,
  25. props: _interface.innerProps,
  26. emits: ['mouseenter', 'mouseleave', 'mousedown', 'touchstart', 'align'],
  27. setup(props, _ref) {
  28. let {
  29. expose,
  30. attrs,
  31. slots
  32. } = _ref;
  33. const alignRef = (0, _vue.shallowRef)();
  34. const elementRef = (0, _vue.shallowRef)();
  35. const alignedClassName = (0, _vue.shallowRef)();
  36. // ======================= Measure ========================
  37. const [stretchStyle, measureStretchStyle] = (0, _useStretchStyle.default)((0, _vue.toRef)(props, 'stretch'));
  38. const doMeasure = () => {
  39. if (props.stretch) {
  40. measureStretchStyle(props.getRootDomNode());
  41. }
  42. };
  43. const visible = (0, _vue.shallowRef)(false);
  44. let timeoutId;
  45. (0, _vue.watch)(() => props.visible, val => {
  46. clearTimeout(timeoutId);
  47. if (val) {
  48. timeoutId = setTimeout(() => {
  49. visible.value = props.visible;
  50. });
  51. } else {
  52. visible.value = false;
  53. }
  54. }, {
  55. immediate: true
  56. });
  57. // ======================== Status ========================
  58. const [status, goNextStatus] = (0, _useVisibleStatus.default)(visible, doMeasure);
  59. // ======================== Aligns ========================
  60. const prepareResolveRef = (0, _vue.shallowRef)();
  61. // `target` on `rc-align` can accept as a function to get the bind element or a point.
  62. // ref: https://www.npmjs.com/package/rc-align
  63. const getAlignTarget = () => {
  64. if (props.point) {
  65. return props.point;
  66. }
  67. return props.getRootDomNode;
  68. };
  69. const forceAlign = () => {
  70. var _a;
  71. (_a = alignRef.value) === null || _a === void 0 ? void 0 : _a.forceAlign();
  72. };
  73. const onInternalAlign = (popupDomNode, matchAlign) => {
  74. var _a;
  75. const nextAlignedClassName = props.getClassNameFromAlign(matchAlign);
  76. const preAlignedClassName = alignedClassName.value;
  77. if (alignedClassName.value !== nextAlignedClassName) {
  78. alignedClassName.value = nextAlignedClassName;
  79. }
  80. if (status.value === 'align') {
  81. // Repeat until not more align needed
  82. if (preAlignedClassName !== nextAlignedClassName) {
  83. Promise.resolve().then(() => {
  84. forceAlign();
  85. });
  86. } else {
  87. goNextStatus(() => {
  88. var _a;
  89. (_a = prepareResolveRef.value) === null || _a === void 0 ? void 0 : _a.call(prepareResolveRef);
  90. });
  91. }
  92. (_a = props.onAlign) === null || _a === void 0 ? void 0 : _a.call(props, popupDomNode, matchAlign);
  93. }
  94. };
  95. // ======================== Motion ========================
  96. const motion = (0, _vue.computed)(() => {
  97. const m = typeof props.animation === 'object' ? props.animation : (0, _motionUtil.getMotion)(props);
  98. ['onAfterEnter', 'onAfterLeave'].forEach(eventName => {
  99. const originFn = m[eventName];
  100. m[eventName] = node => {
  101. goNextStatus();
  102. // 结束后,强制 stable
  103. status.value = 'stable';
  104. originFn === null || originFn === void 0 ? void 0 : originFn(node);
  105. };
  106. });
  107. return m;
  108. });
  109. const onShowPrepare = () => {
  110. return new Promise(resolve => {
  111. prepareResolveRef.value = resolve;
  112. });
  113. };
  114. (0, _vue.watch)([motion, status], () => {
  115. if (!motion.value && status.value === 'motion') {
  116. goNextStatus();
  117. }
  118. }, {
  119. immediate: true
  120. });
  121. expose({
  122. forceAlign,
  123. getElement: () => {
  124. return elementRef.value.$el || elementRef.value;
  125. }
  126. });
  127. const alignDisabled = (0, _vue.computed)(() => {
  128. var _a;
  129. if (((_a = props.align) === null || _a === void 0 ? void 0 : _a.points) && (status.value === 'align' || status.value === 'stable')) {
  130. return false;
  131. }
  132. return true;
  133. });
  134. return () => {
  135. var _a;
  136. const {
  137. zIndex,
  138. align,
  139. prefixCls,
  140. destroyPopupOnHide,
  141. onMouseenter,
  142. onMouseleave,
  143. onTouchstart = () => {},
  144. onMousedown
  145. } = props;
  146. const statusValue = status.value;
  147. // ======================== Render ========================
  148. const mergedStyle = [(0, _extends2.default)((0, _extends2.default)({}, stretchStyle.value), {
  149. zIndex,
  150. opacity: statusValue === 'motion' || statusValue === 'stable' || !visible.value ? null : 0,
  151. // pointerEvents: statusValue === 'stable' ? null : 'none',
  152. pointerEvents: !visible.value && statusValue !== 'stable' ? 'none' : null
  153. }), attrs.style];
  154. let childNode = (0, _propsUtil.flattenChildren)((_a = slots.default) === null || _a === void 0 ? void 0 : _a.call(slots, {
  155. visible: props.visible
  156. }));
  157. // Wrapper when multiple children
  158. if (childNode.length > 1) {
  159. const _childNode = function () {
  160. return childNode;
  161. }();
  162. childNode = (0, _vue.createVNode)("div", {
  163. "class": `${prefixCls}-content`
  164. }, [childNode]);
  165. }
  166. const mergedClassName = (0, _classNames.default)(prefixCls, attrs.class, alignedClassName.value, !props.arrow && `${prefixCls}-arrow-hidden`);
  167. const hasAnimate = visible.value || !props.visible;
  168. const transitionProps = hasAnimate ? (0, _transition.getTransitionProps)(motion.value.name, motion.value) : {};
  169. return (0, _vue.createVNode)(_vue.Transition, (0, _objectSpread2.default)((0, _objectSpread2.default)({
  170. "ref": elementRef
  171. }, transitionProps), {}, {
  172. "onBeforeEnter": onShowPrepare
  173. }), {
  174. default: () => {
  175. return !destroyPopupOnHide || props.visible ? (0, _vue.withDirectives)((0, _vue.createVNode)(_Align.default, {
  176. "target": getAlignTarget(),
  177. "key": "popup",
  178. "ref": alignRef,
  179. "monitorWindowResize": true,
  180. "disabled": alignDisabled.value,
  181. "align": align,
  182. "onAlign": onInternalAlign
  183. }, {
  184. default: () => (0, _vue.createVNode)("div", {
  185. "class": mergedClassName,
  186. "onMouseenter": onMouseenter,
  187. "onMouseleave": onMouseleave,
  188. "onMousedown": (0, _vue.withModifiers)(onMousedown, ['capture']),
  189. [_supportsPassive.default ? 'onTouchstartPassive' : 'onTouchstart']: (0, _vue.withModifiers)(onTouchstart, ['capture']),
  190. "style": mergedStyle
  191. }, [childNode])
  192. }), [[_vue.vShow, visible.value]]) : null;
  193. }
  194. });
  195. };
  196. }
  197. });