{"version":3,"file":"trigger2.mjs","names":[],"sources":["../../../../../../packages/components/popper/src/trigger.vue"],"sourcesContent":["<template>\n  <el-only-child\n    v-if=\"!virtualTriggering\"\n    v-bind=\"$attrs\"\n    :aria-controls=\"ariaControls\"\n    :aria-describedby=\"ariaDescribedby\"\n    :aria-expanded=\"ariaExpanded\"\n    :aria-haspopup=\"ariaHaspopup\"\n  >\n    <slot />\n  </el-only-child>\n</template>\n\n<script lang=\"ts\" setup>\nimport { computed, inject, onBeforeUnmount, onMounted, watch } from 'vue'\nimport { isNil } from 'lodash-unified'\nimport { unrefElement } from '@vueuse/core'\nimport { ElOnlyChild } from '@element-plus/components/slot'\nimport { useForwardRef } from '@element-plus/hooks'\nimport { isElement, isFocusable } from '@element-plus/utils'\nimport { POPPER_INJECTION_KEY } from './constants'\n\nimport type { WatchStopHandle } from 'vue'\nimport type { PopperTriggerProps } from './trigger'\n\ndefineOptions({\n  name: 'ElPopperTrigger',\n  inheritAttrs: false,\n})\n\nconst props = withDefaults(defineProps<PopperTriggerProps>(), {})\n\nconst { role, triggerRef } = inject(POPPER_INJECTION_KEY, undefined)!\n\nuseForwardRef(triggerRef)\n\nconst ariaControls = computed<string | undefined>(() => {\n  return ariaHaspopup.value ? props.id : undefined\n})\n\nconst ariaDescribedby = computed<string | undefined>(() => {\n  if (role && role.value === 'tooltip') {\n    return props.open && props.id ? props.id : undefined\n  }\n  return undefined\n})\n\nconst ariaHaspopup = computed<string | undefined>(() => {\n  if (role && role.value !== 'tooltip') {\n    return role.value\n  }\n  return undefined\n})\n\nconst ariaExpanded = computed<string | undefined>(() => {\n  return ariaHaspopup.value ? `${props.open}` : undefined\n})\n\nlet virtualTriggerAriaStopWatch: WatchStopHandle | undefined = undefined\n\nconst TRIGGER_ELE_EVENTS = [\n  'onMouseenter',\n  'onMouseleave',\n  'onClick',\n  'onKeydown',\n  'onFocus',\n  'onBlur',\n  'onContextmenu',\n] as const\n\nonMounted(() => {\n  watch(\n    () => props.virtualRef,\n    (virtualEl) => {\n      if (virtualEl) {\n        triggerRef.value = unrefElement(virtualEl as HTMLElement)\n      }\n    },\n    {\n      immediate: true,\n    }\n  )\n\n  watch(\n    triggerRef,\n    (el, prevEl) => {\n      virtualTriggerAriaStopWatch?.()\n      virtualTriggerAriaStopWatch = undefined\n\n      if (isElement(prevEl)) {\n        TRIGGER_ELE_EVENTS.forEach((eventName) => {\n          const handler = props[eventName]\n          if (handler) {\n            // @ts-ignore\n            ;(prevEl as HTMLElement).removeEventListener(\n              eventName.slice(2).toLowerCase(),\n              handler,\n              ['onFocus', 'onBlur'].includes(eventName)\n            )\n          }\n        })\n      }\n      if (isElement(el)) {\n        TRIGGER_ELE_EVENTS.forEach((eventName) => {\n          const handler = props[eventName]\n          if (handler) {\n            // It's not worth doing type gymnastics here\n            // @ts-ignore\n            ;(el as HTMLElement).addEventListener(\n              eventName.slice(2).toLowerCase(),\n              handler,\n              ['onFocus', 'onBlur'].includes(eventName)\n            )\n          }\n        })\n        if (isFocusable(el as HTMLElement)) {\n          virtualTriggerAriaStopWatch = watch(\n            [ariaControls, ariaDescribedby, ariaHaspopup, ariaExpanded],\n            (watches) => {\n              ;[\n                'aria-controls',\n                'aria-describedby',\n                'aria-haspopup',\n                'aria-expanded',\n              ].forEach((key, idx) => {\n                isNil(watches[idx])\n                  ? el.removeAttribute(key)\n                  : el.setAttribute(key, watches[idx]!)\n              })\n            },\n            { immediate: true }\n          )\n        }\n      }\n      if (isElement(prevEl) && isFocusable(prevEl as HTMLElement)) {\n        ;[\n          'aria-controls',\n          'aria-describedby',\n          'aria-haspopup',\n          'aria-expanded',\n        ].forEach((key) => prevEl.removeAttribute(key))\n      }\n    },\n    {\n      immediate: true,\n    }\n  )\n})\n\nonBeforeUnmount(() => {\n  virtualTriggerAriaStopWatch?.()\n  virtualTriggerAriaStopWatch = undefined\n  if (triggerRef.value && isElement(triggerRef.value)) {\n    const el = triggerRef.value as HTMLElement\n    TRIGGER_ELE_EVENTS.forEach((eventName) => {\n      const handler = props[eventName]\n      if (handler) {\n        // @ts-ignore\n        el.removeEventListener(\n          eventName.slice(2).toLowerCase(),\n          handler,\n          ['onFocus', 'onBlur'].includes(eventName)\n        )\n      }\n    })\n    triggerRef.value = undefined\n  }\n})\n\ndefineExpose({\n  /**\n   * @description trigger element\n   */\n  triggerRef,\n})\n</script>\n"],"mappings":""}