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