context.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import { computed, inject, provide } from 'vue';
  2. const PortalContextKey = Symbol('PortalContextKey');
  3. export const useProvidePortal = function (instance) {
  4. let config = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {
  5. inTriggerContext: true
  6. };
  7. provide(PortalContextKey, {
  8. inTriggerContext: config.inTriggerContext,
  9. shouldRender: computed(() => {
  10. const {
  11. sPopupVisible,
  12. popupRef,
  13. forceRender,
  14. autoDestroy
  15. } = instance || {};
  16. // if (popPortal) return true;
  17. let shouldRender = false;
  18. if (sPopupVisible || popupRef || forceRender) {
  19. shouldRender = true;
  20. }
  21. if (!sPopupVisible && autoDestroy) {
  22. shouldRender = false;
  23. }
  24. return shouldRender;
  25. })
  26. });
  27. };
  28. export const useInjectPortal = () => {
  29. useProvidePortal({}, {
  30. inTriggerContext: false
  31. });
  32. const portalContext = inject(PortalContextKey, {
  33. shouldRender: computed(() => false),
  34. inTriggerContext: false
  35. });
  36. return {
  37. shouldRender: computed(() => portalContext.shouldRender.value || portalContext.inTriggerContext === false)
  38. };
  39. };