docs/.vitepress/theme/components/settings/MenuHelp.vue
149 lines · 3839 bytes
1<script setup lang="ts">
2import { onClickOutside, 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 isClicked = ref(false)
21const isVisible = computed(() => isHovered.value || isClicked.value)
22
23// Clear click state when hover ends so desktop hover-out closes the popup cleanly
24watch(isHovered, (val) => {
25 if (!val) isClicked.value = false
26})
27
28const bounding = useElementBounding(menuTitleElementRef)
29const popupBounding = useElementBounding(popupElementRef)
30
31// Enclosing flyout panel — anchor the popup to its outer left edge.
32const menuPanelRef = computed(
33 () => (menuTitleElementRef.value?.closest('.VPMenu') as HTMLElement | null) ?? undefined
34)
35const menuBounding = useElementBounding(menuPanelRef)
36
37const helpPopupStyle = computed(() => {
38 const margin = 8
39 const popupW = popupBounding.width.value
40 const popupH = popupBounding.height.value
41 const vw = window.innerWidth
42 const vh = window.innerHeight
43 const isMobile = vw < 768
44
45 let top: number
46 let left: number
47
48 if (isMobile) {
49 // Mobile: place above the title row, center horizontally, fall back to below
50 const aboveTop = bounding.top.value - popupH - 8
51 const belowTop = bounding.bottom.value + 8
52 top = aboveTop >= margin
53 ? aboveTop
54 : belowTop + popupH <= vh - margin
55 ? belowTop
56 : Math.max(margin, vh - popupH - margin)
57 left = Math.max(margin, Math.min((vw - popupW) / 2, vw - popupW - margin))
58 } else {
59 // Desktop: place to the left of the menu panel, aligned vertically
60 const anchorLeft = menuBounding.width.value
61 ? menuBounding.left.value
62 : bounding.left.value
63 left = Math.max(margin, anchorLeft - popupW - 16)
64 top = bounding.top.value
65 }
66
67 return {
68 top: `${top}px`,
69 left: `${left}px`
70 }
71})
72
73function onHelpClick() {
74 isClicked.value = !isClicked.value
75}
76
77function onTouchStart(e: TouchEvent) {
78 // preventDefault stops the browser from firing synthetic mouseenter after touch,
79 // which would interfere with the isHovered state and break the toggle.
80 e.preventDefault()
81 isClicked.value = !isClicked.value
82}
83
84onClickOutside(helpElementRef, () => {
85 isClicked.value = false
86})
87
88watch(isVisible, (value) => {
89 emits('update:isPoppedUp', value)
90})
91
92watch(
93 isVisible,
94 () => {
95 bounding.update()
96 popupBounding.update()
97 menuBounding.update()
98 },
99 {
100 flush: 'pre'
101 }
102)
103</script>
104
105<template>
106 <span
107 ref="helpElementRef"
108 text="$vp-nolebase-enhanced-readabilities-menu-text-color"
109 class="i-carbon:help-filled opacity-50 hover:opacity-100"
110 transition="all duration-200 ease"
111 cursor-help
112 @touchstart="onTouchStart"
113 @click="onHelpClick"
114 />
115 <Teleport to="body">
116 <Transition name="fade">
117 <div
118 v-if="mounted"
119 v-show="isVisible"
120 ref="popupElementRef"
121 :style="helpPopupStyle"
122 bg="$vp-c-bg-elv"
123 text="$vp-nolebase-enhanced-readabilities-menu-text-color"
124 border="1 solid $vp-c-divider"
125 pointer-events-none
126 fixed
127 z-10000
128 rounded-xl
129 p-4
130 shadow-xl
131 max-w="[calc(100vw-16px)]"
132 >
133 <slot />
134 </div>
135 </Transition>
136 </Teleport>
137</template>
138
139<style scoped>
140.fade-enter-active,
141.fade-leave-active {
142 transition: opacity 0.2s ease-in-out;
143}
144
145.fade-enter-from,
146.fade-leave-to {
147 opacity: 0;
148}
149</style>