trigger2.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var vue = require('vue');
  4. var lodashUnified = require('lodash-unified');
  5. var core = require('@vueuse/core');
  6. var constants = require('./constants.js');
  7. var trigger = require('./trigger.js');
  8. var pluginVue_exportHelper = require('../../../_virtual/plugin-vue_export-helper.js');
  9. var index = require('../../../hooks/use-forward-ref/index.js');
  10. var aria = require('../../../utils/dom/aria.js');
  11. var onlyChild = require('../../slot/src/only-child.js');
  12. var types = require('../../../utils/types.js');
  13. const __default__ = vue.defineComponent({
  14. name: "ElPopperTrigger",
  15. inheritAttrs: false
  16. });
  17. const _sfc_main = /* @__PURE__ */ vue.defineComponent({
  18. ...__default__,
  19. props: trigger.popperTriggerProps,
  20. setup(__props, { expose }) {
  21. const props = __props;
  22. const { role, triggerRef } = vue.inject(constants.POPPER_INJECTION_KEY, void 0);
  23. index.useForwardRef(triggerRef);
  24. const ariaControls = vue.computed(() => {
  25. return ariaHaspopup.value ? props.id : void 0;
  26. });
  27. const ariaDescribedby = vue.computed(() => {
  28. if (role && role.value === "tooltip") {
  29. return props.open && props.id ? props.id : void 0;
  30. }
  31. return void 0;
  32. });
  33. const ariaHaspopup = vue.computed(() => {
  34. if (role && role.value !== "tooltip") {
  35. return role.value;
  36. }
  37. return void 0;
  38. });
  39. const ariaExpanded = vue.computed(() => {
  40. return ariaHaspopup.value ? `${props.open}` : void 0;
  41. });
  42. let virtualTriggerAriaStopWatch = void 0;
  43. const TRIGGER_ELE_EVENTS = [
  44. "onMouseenter",
  45. "onMouseleave",
  46. "onClick",
  47. "onKeydown",
  48. "onFocus",
  49. "onBlur",
  50. "onContextmenu"
  51. ];
  52. vue.onMounted(() => {
  53. vue.watch(() => props.virtualRef, (virtualEl) => {
  54. if (virtualEl) {
  55. triggerRef.value = core.unrefElement(virtualEl);
  56. }
  57. }, {
  58. immediate: true
  59. });
  60. vue.watch(triggerRef, (el, prevEl) => {
  61. virtualTriggerAriaStopWatch == null ? void 0 : virtualTriggerAriaStopWatch();
  62. virtualTriggerAriaStopWatch = void 0;
  63. if (types.isElement(el)) {
  64. TRIGGER_ELE_EVENTS.forEach((eventName) => {
  65. var _a;
  66. const handler = props[eventName];
  67. if (handler) {
  68. el.addEventListener(eventName.slice(2).toLowerCase(), handler);
  69. (_a = prevEl == null ? void 0 : prevEl.removeEventListener) == null ? void 0 : _a.call(prevEl, eventName.slice(2).toLowerCase(), handler);
  70. }
  71. });
  72. if (aria.isFocusable(el)) {
  73. virtualTriggerAriaStopWatch = vue.watch([ariaControls, ariaDescribedby, ariaHaspopup, ariaExpanded], (watches) => {
  74. [
  75. "aria-controls",
  76. "aria-describedby",
  77. "aria-haspopup",
  78. "aria-expanded"
  79. ].forEach((key, idx) => {
  80. lodashUnified.isNil(watches[idx]) ? el.removeAttribute(key) : el.setAttribute(key, watches[idx]);
  81. });
  82. }, { immediate: true });
  83. }
  84. }
  85. if (types.isElement(prevEl) && aria.isFocusable(prevEl)) {
  86. [
  87. "aria-controls",
  88. "aria-describedby",
  89. "aria-haspopup",
  90. "aria-expanded"
  91. ].forEach((key) => prevEl.removeAttribute(key));
  92. }
  93. }, {
  94. immediate: true
  95. });
  96. });
  97. vue.onBeforeUnmount(() => {
  98. virtualTriggerAriaStopWatch == null ? void 0 : virtualTriggerAriaStopWatch();
  99. virtualTriggerAriaStopWatch = void 0;
  100. if (triggerRef.value && types.isElement(triggerRef.value)) {
  101. const el = triggerRef.value;
  102. TRIGGER_ELE_EVENTS.forEach((eventName) => {
  103. const handler = props[eventName];
  104. if (handler) {
  105. el.removeEventListener(eventName.slice(2).toLowerCase(), handler);
  106. }
  107. });
  108. triggerRef.value = void 0;
  109. }
  110. });
  111. expose({
  112. triggerRef
  113. });
  114. return (_ctx, _cache) => {
  115. return !_ctx.virtualTriggering ? (vue.openBlock(), vue.createBlock(vue.unref(onlyChild.OnlyChild), vue.mergeProps({ key: 0 }, _ctx.$attrs, {
  116. "aria-controls": vue.unref(ariaControls),
  117. "aria-describedby": vue.unref(ariaDescribedby),
  118. "aria-expanded": vue.unref(ariaExpanded),
  119. "aria-haspopup": vue.unref(ariaHaspopup)
  120. }), {
  121. default: vue.withCtx(() => [
  122. vue.renderSlot(_ctx.$slots, "default")
  123. ]),
  124. _: 3
  125. }, 16, ["aria-controls", "aria-describedby", "aria-expanded", "aria-haspopup"])) : vue.createCommentVNode("v-if", true);
  126. };
  127. }
  128. });
  129. var ElPopperTrigger = /* @__PURE__ */ pluginVue_exportHelper["default"](_sfc_main, [["__file", "trigger.vue"]]);
  130. exports["default"] = ElPopperTrigger;
  131. //# sourceMappingURL=trigger2.js.map