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

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

139 lines · 3444 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
 31const 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  const isMobile = vw < 768
 38
 39  let top: number
 40  let left: number
 41
 42  if (isMobile) {
 43    // Mobile: place above the title row, center horizontally, fall back to below
 44    const aboveTop = bounding.top.value - popupH - 8
 45    const belowTop = bounding.bottom.value + 8
 46    top = aboveTop >= margin
 47      ? aboveTop
 48      : belowTop + popupH <= vh - margin
 49        ? belowTop
 50        : Math.max(margin, vh - popupH - margin)
 51    left = Math.max(margin, Math.min((vw - popupW) / 2, vw - popupW - margin))
 52  } else {
 53    // Desktop: place to the left of the menu title, aligned vertically
 54    left = bounding.left.value - popupW - 16
 55    top = bounding.top.value
 56  }
 57
 58  return {
 59    top: `${top}px`,
 60    left: `${left}px`
 61  }
 62})
 63
 64function onHelpClick() {
 65  isClicked.value = !isClicked.value
 66}
 67
 68function onTouchStart(e: TouchEvent) {
 69  // preventDefault stops the browser from firing synthetic mouseenter after touch,
 70  // which would interfere with the isHovered state and break the toggle.
 71  e.preventDefault()
 72  isClicked.value = !isClicked.value
 73}
 74
 75onClickOutside(helpElementRef, () => {
 76  isClicked.value = false
 77})
 78
 79watch(isVisible, (value) => {
 80  emits('update:isPoppedUp', value)
 81})
 82
 83watch(
 84  isVisible,
 85  () => {
 86    bounding.update()
 87    popupBounding.update()
 88  },
 89  {
 90    flush: 'pre'
 91  }
 92)
 93</script>
 94
 95<template>
 96  <span
 97    ref="helpElementRef"
 98    text="$vp-nolebase-enhanced-readabilities-menu-text-color"
 99    class="i-carbon:help-filled opacity-50 hover:opacity-100"
100    transition="all duration-200 ease"
101    cursor-help
102    @touchstart="onTouchStart"
103    @click="onHelpClick"
104  />
105  <Teleport to="body">
106    <Transition name="fade">
107      <div
108        v-if="mounted"
109        v-show="isVisible"
110        ref="popupElementRef"
111        :style="helpPopupStyle"
112        bg="$vp-c-bg-elv"
113        text="$vp-nolebase-enhanced-readabilities-menu-text-color"
114        border="1 solid $vp-c-divider"
115        pointer-events-none
116        fixed
117        z-100
118        rounded-xl
119        p-4
120        shadow-xl
121        max-w="[calc(100vw-16px)]"
122      >
123        <slot />
124      </div>
125    </Transition>
126  </Teleport>
127</template>
128
129<style scoped>
130.fade-enter-active,
131.fade-leave-active {
132  transition: opacity 0.2s ease-in-out;
133}
134
135.fade-enter-from,
136.fade-leave-to {
137  opacity: 0;
138}
139</style>