Portal.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 _vueTypes = _interopRequireDefault(require("./vue-types"));
  9. var _context = require("../vc-trigger/context");
  10. var _default = exports.default = (0, _vue.defineComponent)({
  11. compatConfig: {
  12. MODE: 3
  13. },
  14. name: 'Portal',
  15. inheritAttrs: false,
  16. props: {
  17. getContainer: _vueTypes.default.func.isRequired,
  18. didUpdate: Function
  19. },
  20. setup(props, _ref) {
  21. let {
  22. slots
  23. } = _ref;
  24. let isSSR = true;
  25. // getContainer 不会改变,不用响应式
  26. let container;
  27. const {
  28. shouldRender
  29. } = (0, _context.useInjectPortal)();
  30. function setContainer() {
  31. if (shouldRender.value) {
  32. container = props.getContainer();
  33. }
  34. }
  35. (0, _vue.onBeforeMount)(() => {
  36. isSSR = false;
  37. // drawer
  38. setContainer();
  39. });
  40. (0, _vue.onMounted)(() => {
  41. if (container) return;
  42. // https://github.com/vueComponent/ant-design-vue/issues/6937
  43. setContainer();
  44. });
  45. const stopWatch = (0, _vue.watch)(shouldRender, () => {
  46. if (shouldRender.value && !container) {
  47. container = props.getContainer();
  48. }
  49. if (container) {
  50. stopWatch();
  51. }
  52. });
  53. (0, _vue.onUpdated)(() => {
  54. (0, _vue.nextTick)(() => {
  55. var _a;
  56. if (shouldRender.value) {
  57. (_a = props.didUpdate) === null || _a === void 0 ? void 0 : _a.call(props, props);
  58. }
  59. });
  60. });
  61. // onBeforeUnmount(() => {
  62. // if (container && container.parentNode) {
  63. // container.parentNode.removeChild(container);
  64. // }
  65. // });
  66. return () => {
  67. var _a;
  68. if (!shouldRender.value) return null;
  69. if (isSSR) {
  70. return (_a = slots.default) === null || _a === void 0 ? void 0 : _a.call(slots);
  71. }
  72. return container ? (0, _vue.createVNode)(_vue.Teleport, {
  73. "to": container
  74. }, slots) : null;
  75. };
  76. }
  77. });