Commit 4f4018c9ff
Unsigned
Layout: unified · split
docs/.vitepress/theme/components/settings/MenuHelp.vue +57 −6
| @@ -1,5 +1,5 @@ | ||
| 1 | 1 | <script setup lang="ts"> |
| 2 | import { useElementBounding, useElementHover, useMounted } from '@vueuse/core' | |
| 2 | import { onClickOutside, useElementBounding, useElementHover, useMounted } from '@vueuse/core' | |
| 3 | 3 | import { computed, ref, toRef, watch } from 'vue' |
| 4 | 4 | |
| 5 | 5 | const props = defineProps<{ |
| @@ -17,22 +17,70 @@ const popupElementRef = ref<HTMLDivElement>() | ||
| 17 | 17 | |
| 18 | 18 | const mounted = useMounted() |
| 19 | 19 | const isHovered = useElementHover(helpElementRef) |
| 20 | const isClicked = ref(false) | |
| 21 | const isVisible = computed(() => isHovered.value || isClicked.value) | |
| 22 | ||
| 23 | // Clear click state when hover ends so desktop hover-out closes the popup cleanly | |
| 24 | watch(isHovered, (val) => { | |
| 25 | if (!val) isClicked.value = false | |
| 26 | }) | |
| 27 | ||
| 20 | 28 | const bounding = useElementBounding(menuTitleElementRef) |
| 21 | 29 | const popupBounding = useElementBounding(popupElementRef) |
| 22 | 30 | |
| 23 | 31 | const 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 | ||
| 24 | 57 | 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` | |
| 27 | 60 | } |
| 28 | 61 | }) |
| 29 | 62 | |
| 30 | watch(isHovered, (value) => { | |
| 63 | function onHelpClick() { | |
| 64 | isClicked.value = !isClicked.value | |
| 65 | } | |
| 66 | ||
| 67 | function 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 | ||
| 74 | onClickOutside(helpElementRef, () => { | |
| 75 | isClicked.value = false | |
| 76 | }) | |
| 77 | ||
| 78 | watch(isVisible, (value) => { | |
| 31 | 79 | emits('update:isPoppedUp', value) |
| 32 | 80 | }) |
| 33 | 81 | |
| 34 | 82 | watch( |
| 35 | isHovered, | |
| 83 | isVisible, | |
| 36 | 84 | () => { |
| 37 | 85 | bounding.update() |
| 38 | 86 | popupBounding.update() |
| @@ -50,12 +98,14 @@ watch( | ||
| 50 | 98 | class="i-carbon:help-filled opacity-50 hover:opacity-100" |
| 51 | 99 | transition="all duration-200 ease" |
| 52 | 100 | cursor-help |
| 101 | @touchstart="onTouchStart" | |
| 102 | @click="onHelpClick" | |
| 53 | 103 | /> |
| 54 | 104 | <Teleport to="body"> |
| 55 | 105 | <Transition name="fade"> |
| 56 | 106 | <div |
| 57 | 107 | v-if="mounted" |
| 58 | v-show="isHovered" | |
| 108 | v-show="isVisible" | |
| 59 | 109 | ref="popupElementRef" |
| 60 | 110 | :style="helpPopupStyle" |
| 61 | 111 | bg="$vp-c-bg-elv" |
| @@ -67,6 +117,7 @@ watch( | ||
| 67 | 117 | rounded-xl |
| 68 | 118 | p-4 |
| 69 | 119 | shadow-xl |
| 120 | max-w="[calc(100vw-16px)]" | |
| 70 | 121 | > |
| 71 | 122 | <slot /> |
| 72 | 123 | </div> |