PortalWrapper.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
  2. import _typeof from "@babel/runtime/helpers/esm/typeof";
  3. import { createVNode as _createVNode, resolveDirective as _resolveDirective } from "vue";
  4. import PropTypes from './vue-types';
  5. import switchScrollingEffect from './switchScrollingEffect';
  6. import setStyle from './setStyle';
  7. import Portal from './Portal';
  8. import { defineComponent, ref, watch, onMounted, onBeforeUnmount, onUpdated, getCurrentInstance, nextTick } from 'vue';
  9. import canUseDom from './canUseDom';
  10. import ScrollLocker from '../vc-util/Dom/scrollLocker';
  11. import raf from './raf';
  12. var openCount = 0;
  13. var supportDom = canUseDom();
  14. /** @private Test usage only */
  15. export function getOpenCount() {
  16. return process.env.NODE_ENV === 'test' ? openCount : 0;
  17. }
  18. // https://github.com/ant-design/ant-design/issues/19340
  19. // https://github.com/ant-design/ant-design/issues/19332
  20. var cacheOverflow = {};
  21. var getParent = function getParent(getContainer) {
  22. if (!supportDom) {
  23. return null;
  24. }
  25. if (getContainer) {
  26. if (typeof getContainer === 'string') {
  27. return document.querySelectorAll(getContainer)[0];
  28. }
  29. if (typeof getContainer === 'function') {
  30. return getContainer();
  31. }
  32. if (_typeof(getContainer) === 'object' && getContainer instanceof window.HTMLElement) {
  33. return getContainer;
  34. }
  35. }
  36. return document.body;
  37. };
  38. export default defineComponent({
  39. compatConfig: {
  40. MODE: 3
  41. },
  42. name: 'PortalWrapper',
  43. inheritAttrs: false,
  44. props: {
  45. wrapperClassName: String,
  46. forceRender: {
  47. type: Boolean,
  48. default: undefined
  49. },
  50. getContainer: PropTypes.any,
  51. visible: {
  52. type: Boolean,
  53. default: undefined
  54. }
  55. },
  56. setup: function setup(props, _ref) {
  57. var slots = _ref.slots;
  58. var container = ref();
  59. var componentRef = ref();
  60. var rafId = ref();
  61. var scrollLocker = new ScrollLocker({
  62. container: getParent(props.getContainer)
  63. });
  64. var removeCurrentContainer = function removeCurrentContainer() {
  65. var _container$value, _container$value$pare;
  66. // Portal will remove from `parentNode`.
  67. // Let's handle this again to avoid refactor issue.
  68. (_container$value = container.value) === null || _container$value === void 0 ? void 0 : (_container$value$pare = _container$value.parentNode) === null || _container$value$pare === void 0 ? void 0 : _container$value$pare.removeChild(container.value);
  69. };
  70. var attachToParent = function attachToParent() {
  71. var force = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  72. if (force || container.value && !container.value.parentNode) {
  73. var parent = getParent(props.getContainer);
  74. if (parent) {
  75. parent.appendChild(container.value);
  76. return true;
  77. }
  78. return false;
  79. }
  80. return true;
  81. };
  82. // attachToParent();
  83. var getContainer = function getContainer() {
  84. if (!supportDom) {
  85. return null;
  86. }
  87. if (!container.value) {
  88. container.value = document.createElement('div');
  89. attachToParent(true);
  90. }
  91. setWrapperClassName();
  92. return container.value;
  93. };
  94. var setWrapperClassName = function setWrapperClassName() {
  95. var wrapperClassName = props.wrapperClassName;
  96. if (container.value && wrapperClassName && wrapperClassName !== container.value.className) {
  97. container.value.className = wrapperClassName;
  98. }
  99. };
  100. onUpdated(function () {
  101. setWrapperClassName();
  102. attachToParent();
  103. });
  104. /**
  105. * Enhance ./switchScrollingEffect
  106. * 1. Simulate document body scroll bar with
  107. * 2. Record body has overflow style and recover when all of PortalWrapper invisible
  108. * 3. Disable body scroll when PortalWrapper has open
  109. *
  110. * @memberof PortalWrapper
  111. */
  112. var switchScrolling = function switchScrolling() {
  113. if (openCount === 1 && !Object.keys(cacheOverflow).length) {
  114. switchScrollingEffect();
  115. // Must be set after switchScrollingEffect
  116. cacheOverflow = setStyle({
  117. overflow: 'hidden',
  118. overflowX: 'hidden',
  119. overflowY: 'hidden'
  120. });
  121. } else if (!openCount) {
  122. setStyle(cacheOverflow);
  123. cacheOverflow = {};
  124. switchScrollingEffect(true);
  125. }
  126. };
  127. var instance = getCurrentInstance();
  128. onMounted(function () {
  129. var init = false;
  130. watch([function () {
  131. return props.visible;
  132. }, function () {
  133. return props.getContainer;
  134. }], function (_ref2, _ref3) {
  135. var _ref4 = _slicedToArray(_ref2, 2),
  136. visible = _ref4[0],
  137. getContainer = _ref4[1];
  138. var _ref5 = _slicedToArray(_ref3, 2),
  139. prevVisible = _ref5[0],
  140. prevGetContainer = _ref5[1];
  141. // Update count
  142. if (supportDom && getParent(props.getContainer) === document.body) {
  143. if (visible && !prevVisible) {
  144. openCount += 1;
  145. } else if (init) {
  146. openCount -= 1;
  147. }
  148. }
  149. if (init) {
  150. // Clean up container if needed
  151. var getContainerIsFunc = typeof getContainer === 'function' && typeof prevGetContainer === 'function';
  152. if (getContainerIsFunc ? getContainer.toString() !== prevGetContainer.toString() : getContainer !== prevGetContainer) {
  153. removeCurrentContainer();
  154. }
  155. // updateScrollLocker
  156. if (visible && visible !== prevVisible && supportDom && getParent(getContainer) !== scrollLocker.getContainer()) {
  157. scrollLocker.reLock({
  158. container: getParent(getContainer)
  159. });
  160. }
  161. }
  162. init = true;
  163. }, {
  164. immediate: true,
  165. flush: 'post'
  166. });
  167. nextTick(function () {
  168. if (!attachToParent()) {
  169. rafId.value = raf(function () {
  170. instance.update();
  171. });
  172. }
  173. });
  174. });
  175. onBeforeUnmount(function () {
  176. var visible = props.visible,
  177. getContainer = props.getContainer;
  178. if (supportDom && getParent(getContainer) === document.body) {
  179. // 离开时不会 render, 导到离开时数值不变,改用 func 。。
  180. openCount = visible && openCount ? openCount - 1 : openCount;
  181. }
  182. removeCurrentContainer();
  183. raf.cancel(rafId.value);
  184. });
  185. return function () {
  186. var forceRender = props.forceRender,
  187. visible = props.visible;
  188. var portal = null;
  189. var childProps = {
  190. getOpenCount: function getOpenCount() {
  191. return openCount;
  192. },
  193. getContainer: getContainer,
  194. switchScrollingEffect: switchScrolling,
  195. scrollLocker: scrollLocker
  196. };
  197. if (forceRender || visible || componentRef.value) {
  198. portal = _createVNode(Portal, {
  199. "getContainer": getContainer,
  200. "ref": componentRef
  201. }, {
  202. default: function _default() {
  203. var _slots$default;
  204. return (_slots$default = slots.default) === null || _slots$default === void 0 ? void 0 : _slots$default.call(slots, childProps);
  205. }
  206. });
  207. }
  208. return portal;
  209. };
  210. }
  211. });