docs/.vitepress/theme/components/settings/MenuHelp.vue

002c6123df4d38eced237a6bf103f7a140ca76d0
wotaku/docs/.vitepress/theme/components/settings/MenuHelp.vue history · blame · raw

87 lines · 1926 bytes

 1<script setup lang="ts">
 2import { useElementBounding, useElementHover, useMounted } from '@vueuse/core'
 3import { computed, ref, toRef, watch } from 'vue'
 4
 5const props = defineProps<{
 6  menuTitleElementRef?: HTMLDivElement
 7  isPoppedUp?: boolean
 8}>()
 9
10const emits = defineEmits<{
11  (e: 'update:isPoppedUp', value: boolean): void
12}>()
13
14const menuTitleElementRef = toRef(props, 'menuTitleElementRef')
15const helpElementRef = ref<HTMLSpanElement>()
16const popupElementRef = ref<HTMLDivElement>()
17
18const mounted = useMounted()
19const isHovered = useElementHover(helpElementRef)
20const bounding = useElementBounding(menuTitleElementRef)
21const popupBounding = useElementBounding(popupElementRef)
22
23const helpPopupStyle = computed(() => {
24  return {
25    top: `${bounding.top.value}px`,
26    left: `${bounding.left.value - popupBounding.width.value - 16}px`
27  }
28})
29
30watch(isHovered, (value) => {
31  emits('update:isPoppedUp', value)
32})
33
34watch(
35  isHovered,
36  () => {
37    bounding.update()
38    popupBounding.update()
39  },
40  {
41    flush: 'pre'
42  }
43)
44</script>
45
46<template>
47  <span
48    ref="helpElementRef"
49    text="$vp-nolebase-enhanced-readabilities-menu-text-color"
50    class="i-carbon:help-filled opacity-50 hover:opacity-100"
51    transition="all duration-200 ease"
52    cursor-help
53  />
54  <Teleport to="body">
55    <Transition name="fade">
56      <div
57        v-if="mounted"
58        v-show="isHovered"
59        ref="popupElementRef"
60        :style="helpPopupStyle"
61        bg="$vp-c-bg-elv"
62        text="$vp-nolebase-enhanced-readabilities-menu-text-color"
63        border="1 solid $vp-c-divider"
64        pointer-events-none
65        fixed
66        z-100
67        rounded-xl
68        p-4
69        shadow-xl
70      >
71        <slot />
72      </div>
73    </Transition>
74  </Teleport>
75</template>
76
77<style scoped>
78.fade-enter-active,
79.fade-leave-active {
80  transition: opacity 0.2s ease-in-out;
81}
82
83.fade-enter-from,
84.fade-leave-to {
85  opacity: 0;
86}
87</style>