Commit 4f4018c9ff

4f4018c9ff5ac25f0b93c21002af376389b2bbe1

parent: fcccb9fc54

Unsigned

Duck <57977673+woducku@users.noreply.github.com> · 2026-06-15 04:59 UTC

Update MenuHelp.vue

Layout: unified · split

docs/.vitepress/theme/components/settings/MenuHelp.vue +57 −6
@@ -1,5 +1,5 @@
11<script setup lang="ts">
2import { useElementBounding, useElementHover, useMounted } from '@vueuse/core'
2import { onClickOutside, useElementBounding, useElementHover, useMounted } from '@vueuse/core'
33import { computed, ref, toRef, watch } from 'vue'
44
55const props = defineProps<{
@@ -17,22 +17,70 @@ const popupElementRef = ref<HTMLDivElement>()
1717
1818const mounted = useMounted()
1919const 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
2028const bounding = useElementBounding(menuTitleElementRef)
2129const popupBounding = useElementBounding(popupElementRef)
2230
2331const helpPopupStyle = computed(() => {
32 const margin = 8
33 const popupW = popupBounding.width.value
34 const popupH = popupBounding.height.value
35 const vw = window.innerWidth
36 const vh = window.innerHeight
37
38 // Horizontal: prefer left of element, then right, then center in viewport
39 let left = bounding.left.value - popupW - 16
40 if (left < margin) {
41 const rightPos = bounding.right.value + 8
42 left = rightPos + popupW <= vw - margin
43 ? rightPos
44 : (vw - popupW) / 2
45 }
46 left = Math.max(margin, Math.min(left, vw - popupW - margin))
47
48 // Vertical: prefer above the title row, fall back to below
49 const aboveTop = bounding.top.value - popupH - 8
50 const belowTop = bounding.bottom.value + 8
51 const top = aboveTop >= margin
52 ? aboveTop
53 : belowTop + popupH <= vh - margin
54 ? belowTop
55 : Math.max(margin, vh - popupH - margin)
56
2457 return {
25 top: `${bounding.top.value}px`,
26 left: `${bounding.left.value - popupBounding.width.value - 16}px`
58 top: `${top}px`,
59 left: `${left}px`
2760 }
2861})
2962
30watch(isHovered, (value) => {
63function onHelpClick() {
64 isClicked.value = !isClicked.value
65}
66
67function onTouchStart(e: TouchEvent) {
68 // preventDefault stops the browser from firing synthetic mouseenter after touch,
69 // which would interfere with the isHovered state and break the toggle.
70 e.preventDefault()
71 isClicked.value = !isClicked.value
72}
73
74onClickOutside(helpElementRef, () => {
75 isClicked.value = false
76})
77
78watch(isVisible, (value) => {
3179 emits('update:isPoppedUp', value)
3280})
3381
3482watch(
35 isHovered,
83 isVisible,
3684 () => {
3785 bounding.update()
3886 popupBounding.update()
@@ -50,12 +98,14 @@ watch(
5098 class="i-carbon:help-filled opacity-50 hover:opacity-100"
5199 transition="all duration-200 ease"
52100 cursor-help
101 @touchstart="onTouchStart"
102 @click="onHelpClick"
53103 />
54104 <Teleport to="body">
55105 <Transition name="fade">
56106 <div
57107 v-if="mounted"
58 v-show="isHovered"
108 v-show="isVisible"
59109 ref="popupElementRef"
60110 :style="helpPopupStyle"
61111 bg="$vp-c-bg-elv"
@@ -67,6 +117,7 @@ watch(
67117 rounded-xl
68118 p-4
69119 shadow-xl
120 max-w="[calc(100vw-16px)]"
70121 >
71122 <slot />
72123 </div>