docs/.vitepress/theme/components/settings/SettingsFlyout.vue
126 lines · 2890 bytes
1<script setup lang="ts">
2import { nextTick, onMounted, onUnmounted, ref } from 'vue'
3
4const open = ref(false)
5const el = ref<HTMLElement>()
6const button = ref<HTMLElement>()
7const menu = ref<HTMLElement>()
8const menuStyle = ref<Record<string, string>>({})
9
10function toggle() {
11 open.value = !open.value
12 if (open.value) void nextTick(updateMenuPosition)
13}
14
15function updateMenuPosition() {
16 if (!el.value || !button.value || !menu.value) return
17
18 const viewportMargin = 12
19 const elRect = el.value.getBoundingClientRect()
20 const buttonRect = button.value.getBoundingClientRect()
21 const menuWidth = menu.value.offsetWidth
22 const centeredLeft = buttonRect.left + buttonRect.width / 2 - menuWidth / 2
23 const maxLeft = Math.max(
24 viewportMargin,
25 window.innerWidth - viewportMargin - menuWidth
26 )
27 const viewportLeft = Math.min(Math.max(centeredLeft, viewportMargin), maxLeft)
28
29 menuStyle.value = {
30 '--settings-menu-left': `${viewportLeft - elRect.left}px`,
31 '--settings-menu-shift': '0px'
32 }
33}
34
35function onDocClick(e: MouseEvent) {
36 if (el.value && !el.value.contains(e.target as Node)) {
37 open.value = false
38 }
39}
40
41function onResize() {
42 if (open.value) updateMenuPosition()
43}
44
45onMounted(() => {
46 document.addEventListener('click', onDocClick, true)
47 window.addEventListener('resize', onResize)
48})
49
50onUnmounted(() => {
51 document.removeEventListener('click', onDocClick, true)
52 window.removeEventListener('resize', onResize)
53})
54</script>
55
56<template>
57 <div ref="el" class="VPFlyout">
58 <button
59 ref="button"
60 type="button"
61 class="button"
62 aria-haspopup="true"
63 :aria-expanded="open"
64 aria-label="Enhanced Readability"
65 @click="toggle"
66 >
67 <span class="text">
68 <span class="i-lucide:settings option-icon" />
69 </span>
70 </button>
71
72 <div v-show="open" ref="menu" class="menu" :style="menuStyle">
73 <div class="VPMenu">
74 <slot />
75 </div>
76 </div>
77 </div>
78</template>
79
80<style scoped>
81.VPFlyout {
82 position: relative;
83}
84
85.button {
86 display: flex;
87 align-items: center;
88 padding: 0 12px;
89 height: var(--vp-nav-height);
90 color: var(--vp-c-text-1);
91 transition: color 0.5s;
92}
93
94.text {
95 display: flex;
96 align-items: center;
97 line-height: var(--vp-nav-height);
98}
99
100.option-icon {
101 font-size: 16px;
102}
103
104.menu {
105 position: absolute;
106 top: calc(var(--vp-nav-height) / 2 + 20px);
107 left: var(--settings-menu-left, 50%);
108 z-index: 100;
109 max-width: calc(100vw - 24px);
110 transform: translateX(var(--settings-menu-shift, -50%));
111 transform-origin: top center;
112}
113
114.VPMenu {
115 border-radius: 12px;
116 padding: 12px;
117 width: 300px;
118 max-width: 100%;
119 border: 1px solid var(--vp-c-divider);
120 background-color: var(--vp-c-bg-elv);
121 box-shadow: var(--vp-shadow-3);
122 transition: background-color 0.5s;
123 max-height: calc(100vh - var(--vp-nav-height));
124 overflow-y: auto;
125}
126</style>