popover.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <template>
  2. <el-tooltip
  3. ref="tooltipRef"
  4. v-bind="$attrs"
  5. :trigger="trigger"
  6. :placement="placement"
  7. :disabled="disabled"
  8. :visible="visible"
  9. :transition="transition"
  10. :popper-options="popperOptions"
  11. :tabindex="tabindex"
  12. :content="content"
  13. :offset="offset"
  14. :show-after="showAfter"
  15. :hide-after="hideAfter"
  16. :auto-close="autoClose"
  17. :show-arrow="showArrow"
  18. :aria-label="title"
  19. :effect="effect"
  20. :enterable="enterable"
  21. :popper-class="kls"
  22. :popper-style="style"
  23. :teleported="teleported"
  24. :persistent="persistent"
  25. :gpu-acceleration="gpuAcceleration"
  26. @update:visible="onUpdateVisible"
  27. @before-show="beforeEnter"
  28. @before-hide="beforeLeave"
  29. @show="afterEnter"
  30. @hide="afterLeave"
  31. >
  32. <template v-if="$slots.reference">
  33. <slot name="reference" />
  34. </template>
  35. <template #content>
  36. <div v-if="title" :class="ns.e('title')" role="title">
  37. {{ title }}
  38. </div>
  39. <slot>
  40. {{ content }}
  41. </slot>
  42. </template>
  43. </el-tooltip>
  44. </template>
  45. <script lang="ts" setup>
  46. import { computed, ref, unref } from 'vue'
  47. import { ElTooltip } from '@element-plus/components/tooltip'
  48. import { addUnit } from '@element-plus/utils'
  49. import { useNamespace } from '@element-plus/hooks'
  50. import { popoverEmits, popoverProps } from './popover'
  51. import type { TooltipInstance } from '@element-plus/components/tooltip'
  52. defineOptions({
  53. name: 'ElPopover',
  54. })
  55. const props = defineProps(popoverProps)
  56. const emit = defineEmits(popoverEmits)
  57. const updateEventKeyRaw = `onUpdate:visible` as const
  58. const onUpdateVisible = computed(() => {
  59. return props[updateEventKeyRaw]
  60. })
  61. const ns = useNamespace('popover')
  62. const tooltipRef = ref<TooltipInstance>()
  63. const popperRef = computed(() => {
  64. return unref(tooltipRef)?.popperRef
  65. })
  66. const style = computed(() => {
  67. return [
  68. {
  69. width: addUnit(props.width),
  70. },
  71. props.popperStyle!,
  72. ]
  73. })
  74. const kls = computed(() => {
  75. return [ns.b(), props.popperClass!, { [ns.m('plain')]: !!props.content }]
  76. })
  77. const gpuAcceleration = computed(() => {
  78. return props.transition === `${ns.namespace.value}-fade-in-linear`
  79. })
  80. const hide = () => {
  81. tooltipRef.value?.hide()
  82. }
  83. const beforeEnter = () => {
  84. emit('before-enter')
  85. }
  86. const beforeLeave = () => {
  87. emit('before-leave')
  88. }
  89. const afterEnter = () => {
  90. emit('after-enter')
  91. }
  92. const afterLeave = () => {
  93. emit('update:visible', false)
  94. emit('after-leave')
  95. }
  96. defineExpose({
  97. /** @description popper ref */
  98. popperRef,
  99. /** @description hide popover */
  100. hide,
  101. })
  102. </script>