mirror of
https://github.com/element-plus/element-plus.git
synced 2026-03-13 07:51:17 +08:00
* refactor(tokens): remove tokens * Remove tokens/breadcrumb. * refactor(tokens): remove tokens/button * refactor(tokens): remove tokens/carousel * refactor(tokens): removing tokens/checkbox * refactor(tokens): removing tokens/collapse * refactor(tokens): removing tokens/dialog * refactor(tokens): removing tokens/pagination * refactor(tokens): removing tokens/radio * refactor(tokens): removing tokens/row * refactor(tokens): removing tokens/scrollbar * refactor(tokens): removing tokens/slider * refactor(tokens): removing tokens/tabs * refactor(tokens): removing tokens/upload * refactor(tokens): removing tokens/popper * refactor(tokens): removing tokens/tooltip * refactor(tokens): removing tokens/tooltip-v2 * refactor(tokens): removing tokens/date-picker * refactor(project): removing tokens/experimentals * Remove tokens/experimentals * Remove package/tokens * Remove tokens related parts * refactor(project): removing packages/tokens completely * chore: update import statement
147 lines
3.5 KiB
Vue
147 lines
3.5 KiB
Vue
<template>
|
|
<el-only-child
|
|
v-if="!virtualTriggering"
|
|
v-bind="$attrs"
|
|
:aria-controls="ariaControls"
|
|
:aria-describedby="ariaDescribedby"
|
|
:aria-expanded="ariaExpanded"
|
|
:aria-haspopup="ariaHaspopup"
|
|
>
|
|
<slot />
|
|
</el-only-child>
|
|
</template>
|
|
|
|
<script lang="ts" setup>
|
|
import { computed, inject, onBeforeUnmount, onMounted, watch } from 'vue'
|
|
import { isNil } from 'lodash-unified'
|
|
import { unrefElement } from '@vueuse/core'
|
|
import { ElOnlyChild } from '@element-plus/components/slot'
|
|
import { useForwardRef } from '@element-plus/hooks'
|
|
import { isElement } from '@element-plus/utils'
|
|
import { POPPER_INJECTION_KEY } from './constants'
|
|
import { popperTriggerProps } from './trigger'
|
|
|
|
import type { WatchStopHandle } from 'vue'
|
|
|
|
defineOptions({
|
|
name: 'ElPopperTrigger',
|
|
inheritAttrs: false,
|
|
})
|
|
|
|
const props = defineProps(popperTriggerProps)
|
|
|
|
const { role, triggerRef } = inject(POPPER_INJECTION_KEY, undefined)!
|
|
|
|
useForwardRef(triggerRef)
|
|
|
|
const ariaControls = computed<string | undefined>(() => {
|
|
return ariaHaspopup.value ? props.id : undefined
|
|
})
|
|
|
|
const ariaDescribedby = computed<string | undefined>(() => {
|
|
if (role && role.value === 'tooltip') {
|
|
return props.open && props.id ? props.id : undefined
|
|
}
|
|
return undefined
|
|
})
|
|
|
|
const ariaHaspopup = computed<string | undefined>(() => {
|
|
if (role && role.value !== 'tooltip') {
|
|
return role.value
|
|
}
|
|
return undefined
|
|
})
|
|
|
|
const ariaExpanded = computed<string | undefined>(() => {
|
|
return ariaHaspopup.value ? `${props.open}` : undefined
|
|
})
|
|
|
|
let virtualTriggerAriaStopWatch: WatchStopHandle | undefined = undefined
|
|
|
|
onMounted(() => {
|
|
watch(
|
|
() => props.virtualRef,
|
|
(virtualEl) => {
|
|
if (virtualEl) {
|
|
triggerRef.value = unrefElement(virtualEl as HTMLElement)
|
|
}
|
|
},
|
|
{
|
|
immediate: true,
|
|
}
|
|
)
|
|
|
|
watch(
|
|
triggerRef,
|
|
(el, prevEl) => {
|
|
virtualTriggerAriaStopWatch?.()
|
|
virtualTriggerAriaStopWatch = undefined
|
|
if (isElement(el)) {
|
|
;(
|
|
[
|
|
'onMouseenter',
|
|
'onMouseleave',
|
|
'onClick',
|
|
'onKeydown',
|
|
'onFocus',
|
|
'onBlur',
|
|
'onContextmenu',
|
|
] as const
|
|
).forEach((eventName) => {
|
|
const handler = props[eventName]
|
|
if (handler) {
|
|
;(el as HTMLElement).addEventListener(
|
|
eventName.slice(2).toLowerCase(),
|
|
handler
|
|
)
|
|
;(prevEl as HTMLElement)?.removeEventListener?.(
|
|
eventName.slice(2).toLowerCase(),
|
|
handler
|
|
)
|
|
}
|
|
})
|
|
virtualTriggerAriaStopWatch = watch(
|
|
[ariaControls, ariaDescribedby, ariaHaspopup, ariaExpanded],
|
|
(watches) => {
|
|
;[
|
|
'aria-controls',
|
|
'aria-describedby',
|
|
'aria-haspopup',
|
|
'aria-expanded',
|
|
].forEach((key, idx) => {
|
|
isNil(watches[idx])
|
|
? el.removeAttribute(key)
|
|
: el.setAttribute(key, watches[idx]!)
|
|
})
|
|
},
|
|
{ immediate: true }
|
|
)
|
|
}
|
|
if (isElement(prevEl)) {
|
|
;[
|
|
'aria-controls',
|
|
'aria-describedby',
|
|
'aria-haspopup',
|
|
'aria-expanded',
|
|
].forEach((key) => prevEl.removeAttribute(key))
|
|
}
|
|
},
|
|
{
|
|
immediate: true,
|
|
}
|
|
)
|
|
})
|
|
|
|
onBeforeUnmount(() => {
|
|
virtualTriggerAriaStopWatch?.()
|
|
virtualTriggerAriaStopWatch = undefined
|
|
})
|
|
|
|
defineExpose({
|
|
/**
|
|
* @description trigger element
|
|
*/
|
|
triggerRef,
|
|
})
|
|
</script>
|