PopupInner.js 8.1 KB

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